MS图表-在运行时在折线图中更新X轴刻度值

时间:2019-05-16 07:11:25

标签: c# winforms

我的要求是在我的线图中(使用c#MS Chart开发),

总是需要一次显示10个点(样本)。 x轴的间隔值为1。

最初,X轴刻度值是(1,2,3,4,5,6,7,8,9,10),在1秒的时间间隔后,我

必须从第二个点开始绘制10个点(样本)(即,我必须跳过第一个点

point)。现在我还需要更新xaxis刻度值,它应该从

开始

2,现在xaxis刻度值应该像2,3,4,5,6,7,8,9,10,11。同样

每秒x轴的起始值

滴答需要增加1。

如何动态更新图表中的Xaxis刻度值?

image

我正在使用以下代码

private void Form1_Load(object sender, EventArgs e)
    {
        loadCsvFile("C:\\mydata.csv");
        this.components = new System.ComponentModel.Container();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        chart = new System.Windows.Forms.DataVisualization.Charting.Chart();
        chart.Location = new System.Drawing.Point(1, 1);
        chart.Size = new System.Drawing.Size(700, 700);
        // Add a chartarea called "draw", add axes to it and color the area black
        chart.ChartAreas.Add("draw");
        numofSamples = 10;
        chart.ChartAreas["draw"].AxisX.Minimum = 1;
        chart.ChartAreas["draw"].AxisX.Maximum = 10;
        chart.ChartAreas["draw"].AxisX.Interval = 1;
        chart.ChartAreas["draw"].AxisX.Title = "X Axis";
        chart.ChartAreas["draw"].AxisX.MajorGrid.LineColor = System.Drawing.Color.Black;
        chart.ChartAreas["draw"].AxisX.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
        chart.ChartAreas["draw"].AxisY.Minimum = 0;
        chart.ChartAreas["draw"].AxisY.Maximum = 1000;
        chart.ChartAreas["draw"].AxisY.Interval = 250;
        chart.ChartAreas["draw"].AxisY.Title = "Y Axis";
        chart.ChartAreas["draw"].AxisY.MajorGrid.LineColor = Color.Black;
        chart.ChartAreas["draw"].AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;

        chart.ChartAreas["draw"].BackColor = Color.White;

        // Create a new function series
        chart.Series.Add("Tags");
        // Set the type to line      
        chart.Series["Tags"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
        // Color the line of the graph light green and give it a thickness of 3
        chart.Series["Tags"].Color = Color.LightGreen;
        chart.Series["Tags"].BorderWidth = 3;
        chart.Series["Tags"].MarkerStyle = MarkerStyle.Circle;
        chart.Series["Tags"].MarkerSize = 10;
        chart.Legends.Add("MyLegend");
        chart.Legends["MyLegend"].BorderColor = Color.Tomato; // I like tomato juice!
        Controls.Add(this.chart);

        // hook up timer event 
        this.timer1.Interval = 1000;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        timer1.Start();


    }

    public void loadCsvFile(string filePath)
    {
        var reader = new StreamReader(File.OpenRead(filePath));
        while (!reader.EndOfStream)
        {
            List<string> listA = new List<string>();
            string line = reader.ReadLine();
            mList.Add(line );
        }
    }
    int i = 0;
    int n = 0;

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (n > 20)
            n = 0;
       int j=0;
       chart.Series["Tags"].Points.Clear();

        for (i=n; i < mList.Count; i++) 
        {
           string l  =mList[i];

            chart.Series["Tags"].Points.AddY(l);

            j++;
            if (j == 10)
                break;
        }
        n++;
        chart.Update();
    }


    List<List<string>> mList = new List<List<string>>();

0 个答案:

没有答案