简体   繁体   English

如何使用 C# 在 x 轴 MSChart 中设置值

[英]How to set values in x axis MSChart using C#

I have these XY values:我有这些 XY 值:

Series S1 = new Series()
S1.Points.AddXY(9, 25);
S1.Points.AddXY(10, 35);
S1.Points.AddXY(11, 15);
chart1.Series.Add(S1);

but I need to show the X values in the graph like this:但我需要在图中显示 X 值,如下所示:

X="9-10" X="9-10"

X="10-11" X="10-11"

X="11-12" X="11-12"

How can I achieve that?我怎样才能做到这一点?


So far this is what I've found:到目前为止,这是我发现的:

图表

and here is the code:这是代码:

private void Form1_Shown(object sender, EventArgs e)
    {
        chart1.ChartAreas[0].AxisX.Minimum = 7;
        chart1.ChartAreas[0].AxisX.Maximum = 15;

        Series S1 = new Series();
        S1.Points.AddXY(9, 25);
        S1.Points.AddXY(10, 35);
        S1.Points.AddXY(11, 15);
        chart1.Series.Add(S1);

        chart1.Series[0].Points[0].AxisLabel = "9-10";
        chart1.Series[0].Points[1].AxisLabel = "10-11";
        chart1.Series[0].Points[2].AxisLabel = "11-12";

as you can see I work with numbers, and set texts for the X axis labels, but I can do that just for the DataPoints values, I need it for the whole range of values.如您所见,我使用数字并为 X 轴标签设置文本,但我可以仅针对 DataPoints 值执行此操作,我需要它用于整个值范围。

Any ideas please?请问有什么想法吗?

Here is the answer thanks to sipla:感谢sipla,这是答案:

working with Custom labels and the Customize event:使用自定义标签和自定义事件:

string[] range = new string[10];

    private void Form1_Shown(object sender, EventArgs e)
    {
        chart1.ChartAreas[0].AxisX.Minimum = 7;
        chart1.ChartAreas[0].AxisX.Maximum = 16;

        range[0] = "";
        range[1] = "7-8";
        range[2] = "8-9";
        range[3] = "9-10";
        range[4] = "10-11";
        range[5] = "11-12";
        range[6] = "12-1";
        range[7] = "1-2";
        range[8] = "2-3";
        range[9] = "";

        Series S1 = new Series();            
        S1.Points.AddXY(9, 25);
        S1.Points.AddXY(10, 35);
        S1.Points.AddXY(11, 15);
        chart1.Series.Add(S1);            

    }

    int count;
    private void chart1_Customize(object sender, EventArgs e)
    {
        count = 0;
        foreach (CustomLabel lbl in chart1.ChartAreas[0].AxisX.CustomLabels)
        {
            lbl.Text = range[count];
            count++;
        }                        
    }

图形

Curious as to why your range array was sprawled out like that.很好奇为什么你的范围数组会这样散开。 It would have been cleaner to put your array in brackets as it was defined and also initialized.在定义和初始化时将数组放在括号中会更干净。 eg例如

string[] range = new string[10] {"","7-8","8-9","9-10","10-11","11-12","12-1","1-2","2-3",""};
/*
  The tenth element is also likely unnecessary 
  as it simply repeats the first     
  element of the array
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM