简体   繁体   English

无法读取MS图表上的最大Y轴值

[英]Can't read maximum Y axis value on MS chart

图表

I'm using MS Chart generated image on a MVC3 view. 我在MVC3视图上使用MS Chart生成的图像。

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. 我真的不知道这是否是一个真正的问题,但除非我定义一个我认为不应该用于动态值的AxisYMaximum值,否则我不能让它看起来很好看。

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. 由于y值是动态的,因此可以将AxisYMaximum动态设置为略高于最大显示y值的值。 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. 我只是通过第一个系列中的所有点来找到最大的y值,然后将y轴最大值设置为该值的120%,或者将某个绝对值设置为高于该值,或者您需要的任何值。

You could also get the greatest y-value in a one-liner using LINQ: 您还可以使用LINQ在单行中获得最大的y值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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