none
Charting: How to preserve previous series while creating new series ? RRS feed

  • Question

  • Hi,

    I'm trying this code to plot multiple graphs on the same chartarea.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Windows.Forms.DataVisualization.Charting;
    
    namespace ListTest
    {
        public partial class Form1 : Form
        {
             List<rdgPair> testRdgs = new List<rdgPair>();
            testData AllTestsDta = new testData();
    
            int curSeries=-1;
    
            public Form1()
            {
                InitializeComponent();
    
                chart1.Series.Clear();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Series newSeries = new Series("TEST " + curSeries)
                {
                    ChartType = SeriesChartType.Line,
                    BorderWidth = 2
                };
    
                chart1.Series.Add(newSeries);
                curSeries++;
    
                rdgList tstRdgList = new rdgList();
    
                for (int i = 0; i < 50; i++)
                {
                    rdgPair rdgs = new rdgPair();
                    rdgs.elongRdg = curSeries * 100 + i * 100; //fake values.
                    rdgs.loadRdg = curSeries * 200 + i * 1000;
                    tstRdgList.Add(rdgs);
                }
    
                AllTestsDta.Add(tstRdgList);
    
                chart1.DataSource = AllTestsDta[curSeries];
    
                chart1.Series[curSeries].XValueMember = "elongRdg";
                chart1.Series[curSeries].YValueMembers = "loadRdg";
                chart1.DataBind();
            }
        }
        //==========================================
        public class rdgPair
        {
            public float loadRdg { get; set; }
            public float elongRdg { get; set; }
        }
        //===========================================
        public class rdgList : List<rdgPair> { }
        //===========================================
        public class testData : List<rdgList> { } 
    
    
    }
    

    I find that when I create a new series, the previous plotted series disappears.

    I know this does not happen while manually adding or binding to a simple list, and there is a mistake I'm making somewhere.

    I need to preserve all the series' (plots/graphs) until I do a Series.Clear() ;

    Please help me find the error. Thank you!

    Monday, November 25, 2019 10:56 AM

All replies

  • Correction:

    The previous series is not getting deleted

    It seems all the previous Series are taking on the points of the newly created series.

    I still need help to prevent this occurrance.

    Monday, November 25, 2019 11:26 AM
  • Hi Monostable,

    Thank you for posting here.

    For your question, you want to show some series in a chart.

    In your code, the following line caused the problem.

     chart1.DataSource = AllTestsDta[curSeries];

    AllTestsDta[curSeries] is the last series you created, not a collection.

    In fact, I think rdgPair is unnecessary for this code, it makes simple code very complicated.

    Here is a code example:

                Series series2 = new Series();
                series2.ChartType = SeriesChartType.Line;
                series2.Name = "TEST " + curSeries;
                chart1.Series.Add(series2);
                for (int i = 0; i < 50; i++)
                {
                    chart1.Series["TEST " + curSeries].Points.AddXY(curSeries * 100 + i * 100, curSeries * 200 + i * 1000);
                }
                curSeries++;

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    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.


    Tuesday, November 26, 2019 6:20 AM
  • Hi Timon,

    Thank you for the reply!

    This was a shortened 'charting test' version of my final project :-).

    In the final version, I am reading 2 float values from the serial port.

    These will be stored in rdgPair.LoadRdg and rdgPair.ElongRdg.

    This new rdgPair object will then be appended to the list 'testRdgList'.

    As it gets appended, it will reflect on the chart.

    When the final reading pair arrives, the list testRdgList will be appended to another master list 'AllTestsDta'.

    'AllTestsDta' list is saved in the user-settings.

    So each element of list 'AllTestsDta' is actually a list of point-pairs.

    In other words, 'AllTestsDta' is a list of chart series'.

    Yes AllTestDta[curSeries] is the newest list of point-pairs which need to be plotted, but while preserving the plot made from AllTestDta[curSeries-1].

    I hope all this is clear :-)

    So can you help me achieve something exactly like this ?


    • Edited by Monostable Tuesday, November 26, 2019 7:01 AM
    Tuesday, November 26, 2019 6:57 AM
  • Hi Monostable,

    Thanks for your feedback.

    I made some changes to the code using the class you provided.

    Here is the code:

            private void Button1_Click_1(object sender, EventArgs e)
            {
    
                chart1.Series.Clear();
    
                curSeries++;
    
                rdgList tstRdgList = new rdgList();
    
                for (int i = 0; i < 50; i++)
                {
                    rdgPair rdgs = new rdgPair();
                    rdgs.elongRdg = curSeries * 100 + i * 100; //fake values.
                    rdgs.loadRdg = curSeries * 200 + i * 1000;
                    tstRdgList.Add(rdgs);
                }
    
                AllTestsDta.Add(tstRdgList);
    
                for (int i = 0; i < AllTestsDta.Count; i++)
                {
    
                    rdgList list = AllTestsDta[i];
                    Series newSeries = new Series("TEST " + i)
                    {
                        ChartType = SeriesChartType.Line,
                        BorderWidth = 2
                    };
                    for (int j = 0; j < list.Count; j++)
                    {
                        newSeries.Points.AddXY(list[j].elongRdg, list[j].loadRdg);
                    }
                    chart1.Series.Add(newSeries);
                }
    
                chart1.DataBind();
            }
    

    Hope this could be helpful.

    Best Regards,

    Timon


    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.

    Wednesday, November 27, 2019 5:44 AM