Reputation: 116
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.
Upvotes: 2
Views: 3316
Reputation: 4224
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();
Upvotes: 4