kim*_*liv 6 c# charts scaling axis winforms
我有一个这样的应用:
使用图表下方的文本框,用户可以设置图表的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;
}
Run Code Online (Sandbox Code Playgroud)
现在我想让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;
Run Code Online (Sandbox Code Playgroud)
编辑:测试时我注意到重置最小值和最大值并不是很明显.方法如下:
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;
Run Code Online (Sandbox Code Playgroud)
轴最小值自动设置为0,只需使用IsStartesFromZero属性:
chart.ChartAreas[0].AxisY.IsStartedFromZero = false;
Run Code Online (Sandbox Code Playgroud)