Change axis values on Chart Winforms

こ雲淡風輕ζ 提交于 2020-01-03 03:24:06

问题


I have a chart in WinForms. X-axis is a time line and Y-axis is values are either 0 or 1.
How can make the chart display Success/Failure instead of 0 and 1 on Y-axis?


回答1:


You can set the Y-axis to use custom labels instead of numbers.

chart1.ChartAreas[0].AxisY.CustomLabels.Add(-0.5, 0.5, "Success");
chart1.ChartAreas[0].AxisY.CustomLabels.Add(0.5, 1.5, "Failure");

You have to set a range in which the label will appear. That's why I chose a range from -0.5 to 0.5 for "Success" (it is centered around zero).




回答2:


Assuming that you are using a string on the X-Axis ("1" or "0"):

//Build up
chart1.Series.Clear();
chart1.ChartAreas.Clear();

chart1.Series.Add("S");
chart1.ChartAreas.Add("A");

chart1.Series["S"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;

//Creating test data
chart1.Series["S"].Points.AddXY("1", 5);
chart1.Series["S"].Points.AddXY("0", 3);
chart1.Series["S"].Points.AddXY("1", 6);
chart1.Series["S"].Points.AddXY("0", 4);
chart1.Series["S"].Points.AddXY("1", 1);

//Changing labels
foreach (var p in chart1.Series["S"].Points)
{
    p.AxisLabel = (p.AxisLabel == "1") ? "Success" : "Failure";
}


来源:https://stackoverflow.com/questions/17831875/change-axis-values-on-chart-winforms

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