How to give custom labels to x axis of chart contr

2020-04-17 03:46发布

问题:

I am creating a windows project in which there is a requirement to plot a graph, for that i am using chart control.

The X-Axis of chart control has label from 0 to 100 with following code.

chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 100;

chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 200;

chart1.ChartAreas[0].AxisX.Interval = 25;
chart1.ChartAreas[0].AxisY.Interval = 25;

But i want to customize the label of X-Axis from 100 to 0.

I tried out following things.

chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Maximum = 100;

chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 200;

chart1.ChartAreas[0].AxisX.Interval = 25;
chart1.ChartAreas[0].AxisY.Interval = 25;



string[] xval = { "100", "75", "50", "25", "0" };
for (int i = 0; i < xval.Length; i++)
{
  chart1.ChartAreas[0].AxisX.CustomLabels.Add(i + 0.5, i + 1.5, xval[i]);
  //chart1.ChartAreas[0].AxisX.CustomLabels.Add(xval[i]);
}

Series S1 = new Series();
S1.Points.AddXY(184,10);
S1.Points.AddXY(100,10);
S1.ChartType = SeriesChartType.Line;
S1.Color = Color.Red;
S1.Name = "Steam Inlet Saturation Temp";
chart1.Series.Add(S1);

Series S2 = new Series();
S2.Points.AddXY(100, 10);
S2.Points.AddXY(0, 10);
S2.ChartType = SeriesChartType.Line;
S2.Color = Color.Blue;
S2.Name = "Back Pressure Temp";
chart1.Series.Add(S2);

But this thing are not working for me.

Is there any one who did this before? Your prompt reply will be really appreciated thanks.

回答1:

Following is the solution of my question.

string[] monthNames = { "100", "75" , "50" , "25" ,"0"};
int startOffset = -2;
int endOffset = 2;
foreach (string monthName in monthNames)
{
 CustomLabel monthLabel = new CustomLabel(startOffset, endOffset, monthName, 0, LabelMarkStyle.None);                        
 chart1.ChartAreas[0].AxisX.CustomLabels.Add(monthLabel);
 startOffset = startOffset + 25;
 endOffset = endOffset + 25;
}