How to set values in x axis MSChart using C#

后端 未结 2 1037
一向
一向 2020-12-09 22:14

I have these XY values:

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


        
相关标签:
2条回答
  • 2020-12-09 22:39

    Here is the answer thanks to 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++;
            }                        
        }
    

    Graph

    0 讨论(0)
  • 2020-12-09 22:46

    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. e.g.

    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
    */
    
    0 讨论(0)
提交回复
热议问题