【问题标题】:How to set values in x axis MSChart using C#如何使用 C# 在 x 轴 MSChart 中设置值
【发布时间】:2011-07-08 11:56:16
【问题描述】:

我有这些 XY 值:

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

但我需要像这样在图表中显示 X 值:

X="9-10"

X="10-11"

X="11-12"

我怎样才能做到这一点?


到目前为止,这是我发现的:

这里是代码:

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

如您所见,我使用数字,并为 X 轴标签设置文本,但我可以只为 DataPoints 值这样做,我需要它用于整个值范围。

有什么想法吗?

【问题讨论】:

    标签: c# .net winforms data-visualization mschart


    【解决方案1】:

    感谢 sipla,这是答案:

    使用自定义标签和自定义事件:

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

    【讨论】:

      【解决方案2】:

      很好奇为什么你的范围数组会这样展开。在定义和初始化时将数组放在括号中会更干净。例如

      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
      */
      

      【讨论】:

      • 是的,这是有道理的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多