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?
Asked
Active
Viewed 7,017 times
1

John Smith
- 33
- 1
- 6
2 Answers
4
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).

endofzero
- 1,830
- 1
- 22
- 33
0
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";
}

Recipe
- 1,398
- 3
- 16
- 29