Display Y-Values on Y-Axis without rounding [closed]

橙三吉。 提交于 2019-12-25 19:04:21

问题


How to remove rounding axis OY in Chart ?

I pass a value in the format 1,3546 and the graph shows 1,35.


回答1:


For a given Axis you can set the LabelStyle.Format.

For example you can use this :

  chart1.ChartAreas["area1"].AxisX.LabelStyle.Format = "000.000\\%";     

or this:

  chart1.ChartAreas[0].AxisY.LabelStyle.Format = "###,##0.00000";

Note: This formats the Label of the Axis Grid, hence what you perceive as rounding. In fact it is the value for the GridLines not the Values of the DataPoints!

To display the Y-Values of the DataPoints you have three options I know of and I'll show you each:

  1. You can display them inside the graph with each point

  2. You can display them as tooltips when the mouse is over a point

  3. or you can display them in CustomLabels along the Y-Axis, which may be what you want. Please note that this will only be a useful option if

    • There is a very limited number of data points
    • Those points are sped reasonably far apart

If one or both conditions are not met, the CustomLabels will overlap.

Setting CustomLabels is somewhat tricky. Here is a piece of code that sets one per each data point. As noted you may need to insert checks to prevent the overlapping..

// propare a few short names
ChartArea CA = chart1.ChartAreas[0];
Series S1 = chart1.Series[0];

// this would be option one:
S1.IsValueShownAsLabel = true;

// we clear any previous CustomLabels
CA.AxisY.CustomLabels.Clear();
// we create a version of our points collection which sorted  by Y-Values:
List<DataPoint> ptS = S1.Points.OrderBy(x => x.YValues[0]).ToList();

// now, for option three we add the custom labels:
for (int p = 0; p < ptS.Count; p++)
{
    CustomLabel L = new CustomLabel(ptS[p].YValues[0] - 0.5, 
                                    ptS[p].YValues[0] + 0.5,  
                                    ptS[p].YValues[0].ToString("##0.0000"), 
                                    0, LabelMarkStyle.None);
    CA.AxisY.CustomLabels.Add(L);

    // this is option two: tooltips for each point
    ptS[p].ToolTip = ptS[p].YValues[0].ToString("##0.0000");
}

The first two parameters of a CustomLabel are about the Y-position or actually the Y-Value range they are supposed to label. Next the value, formatted to show the decimal digits you wanted. Finally first row of labels and no tickmarks.

Here is a screenshot that shows all three options at work:



来源:https://stackoverflow.com/questions/27173680/display-y-values-on-y-axis-without-rounding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!