none
MS Chart - updating X axis tick values at run time in line chart RRS feed

  • Question

  • Hi,

    My requirement is in my line graph( which is developed with c# MS Chart), I

    always need to display 10 points(samples) at a time. The xaxis has interval value 1.

    Initially the Xaxis tick values are (1,2,3,4,5,6,7,8,9,10), After 1 second of time interval, I

    have to plot 10 points(samples) starts from 2nd point(i.e. I have to skip 1st

    point).Now I need to update the xaxis tick values also , it should be starts from

    2,now the xaxis tick values should be like 2,3,4,5,6,7,8,9,10,11). Likewise

    After every second the starting value of x axis

    tick needs to be increased by 1.

    How to update Xaxis tick value in the chart dynamically ?

    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>>();
    c#


    • Edited by sgrm123 Thursday, May 16, 2019 7:19 AM
    Thursday, May 16, 2019 7:14 AM

All replies

  • Hi sgrm123,

    Thank you for posting here.

    Based on your description, you want to update Xaxis tick value in the chart dynamically.

    You could try the following code.

    Chart chart;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                loadCsvFile("D:\\2.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");
                chart.ChartAreas["draw"].AxisX.Minimum = 1;
                chart.ChartAreas["draw"].AxisX.Maximum = 19;
                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();
    
            }
            List<string> listA = new List<string>();
            public void loadCsvFile(string filePath)
            {
                var reader = new StreamReader(File.OpenRead(filePath));
                while (!reader.EndOfStream)
                {
    
                    string line = reader.ReadLine();
                    listA.Add(line);
    
                }
                mList.Add(listA);
            }
            int i = 0;
            int n = 0;
    
            List<List<string>> mList = new List<List<string>>();
    
            private void Timer1_Tick(object sender, EventArgs e)
            {
                int j = 0;
                if (n > 20)
                {
                    n = 0;
                }
    
                chart.Series["Tags"].Points.Clear();
                for (i = n; i < listA.Count; i++)
                {
                    string l = listA[i];
    
                    chart.Series["Tags"].Points.AddXY(i + 1, l);
    
                    j++;
                    if (j == 10)
                    {
                        break;
                    }
    
                }
                n++;
                chart.Update();
    
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 16, 2019 9:42 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 24, 2019 7:00 AM
    Moderator