[英]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.