How to give custom labels to x axis of chart control?

試著忘記壹切 提交于 2019-12-20 03:15:08

问题


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;
}


来源:https://stackoverflow.com/questions/28691679/how-to-give-custom-labels-to-x-axis-of-chart-control

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