none
WinForm中如何设置Chart控件的X轴标签

    问题

  • 我用Chart控件画了一个时间序列图,1秒一个数据,但是我的X轴单位是小时,因此X轴上标签应该是{1,2,3,4,5...}(小时),每隔3600个数据有一个,请问该如何实现?

    谢谢!


    2016年6月1日 1:13

答案

  • 参考 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++;
            }                       
        }


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年6月1日 3:29

全部回复

  • 我可以用

    Chart1.ChartAreas[i].AxisX.MajorGrid.Interval = 3600;

    来设置格网的间隔,但是X轴标签并不会随着格网重新标。

    2016年6月1日 1:28
  • 参考 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++;
            }                       
        }


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年6月1日 3:29
  • 不行啊
    2016年6月1日 4:24
  • chart1_Customize是一个事件,有没有绑定

    chart1.Customize+=chart1_Customize;


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年6月1日 5:43