MS Chart - обновление значений тиков по оси X во время выполнения на линейном графике - PullRequest
0 голосов
/ 16 мая 2019

Мое требование в моем линейном графике (разработанном с помощью c # MS Chart), мне

всегда нужно отображать 10 точек (выборок) одновременно.Xaxis имеет значение интервала 1.

Первоначально значения тиков Xaxis равны (1,2,3,4,5,6,7,8,9,10), через 1 секунду интервала времени I

должен построить 10 точек (выборок), начиная со 2-й точки (т.е. я должен пропустить 1-ю

точку). Теперь мне нужно обновить также значения тиков 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>>();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...