简体   繁体   中英

Can't read maximum Y axis value on MS chart

图表

I'm using MS Chart generated image on a MVC3 view.

The chart works but the maximum value is so high on the top of the chart that I can't read the values.

Shouldn't the chart have a margin from the max value?

I don't really know if this is a real problem but I can't make this look nice unless I define a AxisYMaximum value that I think should not be used on dynamic values.

Yes, the chart control should calculate the margin necessary to clearly display the data, but in my experience, it doesn't.

Since your y-values are dynamic, you can dynamically set the AxisYMaximum to a value just above the greatest displayed y-value. Something like this could set it:

double greatestYValue = double.MinValue;

foreach (var pt in Chart1.Series[0].Points) 
{
    if (greatestYValue < pt.YValues[0]) greatestYValue = pt.YValues[0];
}

Chart1.ChartAreas[0].AxisY.Maximum = greatestYValue * 1.2;
// or
Chart1.ChartAreas[0].AxisY.Maximum = greatestYValue + 20;

I just looped through all the points in the first series to find the greatest y-value, then set the y-axis maximum to 120% of that value, or some absolute amount above that value, or whatever you need.

You could also get the greatest y-value in a one-liner using LINQ:

double greatestYValue = Chart1.Series[0].Points.Select(p => p.YValues[0]).Max();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM