Reputation: 33
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?
Upvotes: 1
Views: 7413
Reputation: 1406
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";
}
Upvotes: 0
Reputation: 1819
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).
Upvotes: 4