如何将图表Y轴设置为动态,最大值设置为125?

时间:2015-11-19 03:04:24

标签: c# asp.net charts

我在asp.net C#中填充了一个图表。我有多个图表,每个图表的范围可以从0到125.

如果我没有设置其中一个的最大值,那么如果我的系列范围最大值为125,则图表最大值将变为140: enter image description here enter image description here

因此,我想将Y轴的最大值设置为125,但不是为所有图表静态设置它。这意味着我想使其动态仍然像第二个图表图像,最大设置为125.

请指教谢谢。

1 个答案:

答案 0 :(得分:0)

// Maximum value in Series, you may have to change YValues[0] to the appropriate 
// index for your type of chart (so that it measures the top of your markers)
double maxValue = chart.Series[0].Points.Max(x => x.YValues[0])

// yAxisMax calculated as max value in series + 10 then rounded up to nearest 10
// but capped at 125 below. You could change this to perhaps 
// maxValue * 1.1 to always get a 10 % extra above, anything goes
double yAxisMax = Math.Ceiling((maxValue + 10) / 10) * 10;

if (yAxisMax > 125)
    yAxisMax = 125;

chart.ChartAreas[0].AxisY.Minimum = 0;
chart.ChartAreas[0].AxisY.Maximum = yAxisMax;
chart.ChartAreas[0].AxisY.Interval = 5;