[英]Scale Y-Axis of a Chart depending on the Values within a section of X-Values for several Series
我有一个这样的应用: 使用图表下方的文本框,用户可以设置图表的X轴的最小值和最大值。 这是它的代码:
private void textBoxXaxisMin_TextChanged(object sender, EventArgs e)
{
double x;
//checks if the input is a double and smaller than the max value
//if (Double.TryParse(this.textBoxXaxisMin.Text, out x) && x < chart1.ChartAreas[0].AxisX.Maximum)
if (Double.TryParse(this.textBoxXaxisMin.Text, out x))
{
this.textBoxXaxisMin.BackColor = Color.White;
chart1.ChartAreas[0].AxisX.Minimum = Convert.ToDouble(this.textBoxXaxisMin.Text);
//changeYScalaMin(chartCharacteristicCurvesThermoelemts, Convert.ToDouble(this.textBoxCharacteristicCurvesThermoelementXmin.Text), Convert.ToDouble(this.textBoxCharacteristicCurvesThermoelementXmax.Text));
//method to scale y axis
}
else
//if the textbox is not highlighted
this.textBoxXaxisMin.BackColor = Color.Orange;
//calls the Max Function to update the chart if the Max-value is now valid
double y;
//checks if the input is a double and greater than the min value
if (Double.TryParse(this.textBoxXaxisMax.Text, out y) && y > chart1.ChartAreas[0].AxisX.Minimum)
{
this.textBoxXaxisMax.BackColor = Color.White;
chart1.ChartAreas[0].AxisX.Maximum = Convert.ToDouble(this.textBoxXaxisMax.Text);
//method to scale y axis
}
else
//if the textbox is not highlighted
this.textBoxXaxisMax.BackColor = Color.Orange;
}
现在我想让Y轴自动缩放。 应将Y-min计算为(X-min和X-max)部分中所有系列的最小值,并将Y-max计算为所选部分中所有系列的最大值。 我的问题是实施。
在此示例中,Y-min应更改为约50。
对于从0到1的所有系列,这会将Y轴缩放到X轴的最小值和最大值[0]之间的最小值和最大值:
double max = Double.MinValue;
double min = Double.MaxValue;
double leftLimit = chart1.ChartAreas[0].AxisX.Minimum;
double rightLimit = chart1.ChartAreas[0].AxisX.Maximum;
for (int s = 0; s <= 1; s++)
{
foreach (DataPoint dp in chart1.Series[s].Points)
{
if (dp.XValue >= leftLimit && dp.XValue <= rightLimit)
{
min = Math.Min(min, dp.YValues[0]);
max = Math.Max(max, dp.YValues[0]);
}
}
}
chart1.ChartAreas[0].AxisY.Maximum = max;
chart1.ChartAreas[0].AxisY.Minimum = min;
编辑:测试时我注意到重置最小值和最大值并不是很明显。 方法如下:
chart1.ChartAreas[0].AxisY.Minimum = Double.NaN;
chart1.ChartAreas[0].AxisY.Maximum = Double.NaN;
chart1.ChartAreas[0].AxisX.Minimum = Double.NaN;
chart1.ChartAreas[0].AxisX.Maximum = Double.NaN;
轴最小值自动设置为0,只需使用IsStartesFromZero属性:
chart.ChartAreas[0].AxisY.IsStartedFromZero = false;
我用于我的项目的代码是:(基于@ TaW的回答)
private void changeYScala(object chart)
{
double max = Double.MinValue;
double min = Double.MaxValue;
Chart tmpChart = (Chart)chart;
double leftLimit = tmpChart.ChartAreas[0].AxisX.Minimum;
double rightLimit = tmpChart.ChartAreas[0].AxisX.Maximum;
for (int s = 0; s < tmpChart.Series.Count(); s++)
{
foreach (DataPoint dp in tmpChart.Series[s].Points)
{
if (dp.XValue >= leftLimit && dp.XValue <= rightLimit)
{
min = Math.Min(min, dp.YValues[0]);
max = Math.Max(max, dp.YValues[0]);
}
}
}
//tmpChart.ChartAreas[0].AxisY.Maximum = max;
tmpChart.ChartAreas[0].AxisY.Maximum = (Math.Ceiling((max / 10)) * 10);
tmpChart.ChartAreas[0].AxisY.Minimum = (Math.Floor((min / 10)) * 10);
//tmpChart.ChartAreas[0].AxisY.Minimum = min;
}
在上面的问题代码中调用此方法:
changeYScala(chart1);
别忘了包括:
using System.Windows.Forms.DataVisualization.Charting;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.