locked
Problem to add another line series to a chart RRS feed

  • Question

  • Hello

    I have a chart control that contains 2 series in one chartarea:

    1. Series1 is of ChartType: Stock

    2. Series2 is of ChartType: Line

    Adding the Series1 works fine and also Series2. The only problem is that when Series2 (the line) that I want to plot out on the chart is added. The whole "Stock" chart is moved to the right and I don't understand why. 

    The problem is this function:

    void addline(....)

    The below code is complete and should work to duplicate the problem by just copy and paste

    Thank you!

            private void button5_Click(object sender, EventArgs e)
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                double highest = -1; double lowest = 999999999; double high = 0; double low = 0; int periods = 0; Random random = new Random();
                for (int i = 0; i < 20; i++)
                {
                    double value = random.Next(160,200);
    
                    high = value + 3;
                    low = value - 3;
                    chart1.Series["Series1"].Points.AddXY(i.ToString(), low, high, (value -1), (value + 1));
                    if (high > highest) { highest = high; }
                    if (low < lowest) { lowest = low; }
                    periods++;
                }
                chart1.ChartAreas[0].AxisY.Minimum = lowest;
                chart1.ChartAreas[0].AxisY.Maximum = highest;
                chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
                chart1.ChartAreas[0].AxisY.MajorGrid.LineWidth = 0;
    
                //Add line to chart
                new Thread(() => addline(chart1, lowest, highest, periods)).Start();
            }
          
            void addline(Chart chart, double lowest, double highest, int periods)
            {
                Thread.Sleep(3000);
                Invoke((System.Windows.Forms.MethodInvoker)delegate
                {
                    DataPoint ptstart = new DataPoint(0, 185);
                    DataPoint ptend = new DataPoint(5, 190);
                    chart.Series["Series2"].Points.Add(ptstart);
                    chart.Series["Series2"].Points.Add(ptend);
                });
            }



    • Edited by Silvers11 Saturday, May 9, 2020 2:23 PM
    Saturday, May 9, 2020 2:20 PM

Answers

  • I managed to do it by using the XY method in 2 different loops like this:

    Thank you!

                for (int i = 0; i < value2dLIST.Count; i++)
                {
                    allparams = new List<String>(value2dLIST[i].Split('|'));
                    if (allparams.Count == 5)
                    {
                        chart1.Series["Series1"].Points.AddXY(i.ToString(), Convert.ToDouble(allparams[3]), Convert.ToDouble(allparams[2]), Convert.ToDouble(allparams[1]), Convert.ToDouble(allparams[4]));
                    }
                }
                for (int i = 0; i < value2dLIST.Count; i++)
                {
                    if (i < 2)
                    {
                        chart1.Series["Series2"].Points.AddXY(i.ToString(), highest);
                    }
                }


    • Marked as answer by Silvers11 Saturday, May 9, 2020 6:11 PM
    • Edited by Silvers11 Saturday, May 9, 2020 6:11 PM
    Saturday, May 9, 2020 6:11 PM