0
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms.DataVisualization.Charting;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Threading;
    namespace testC
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }`


        Random rand = new Random();
        Random random = new Random();

        private void Form1_Load(object sender, EventArgs e)
        {

            Chart crt = this.chart1;

            //первый график ==================================================

            ChartArea chartArea = new ChartArea();

            chartArea.Name = "Main";
            ///
            chartArea.AxisY.IsStartedFromZero = false; 
            ///// join other charts ////
            chartArea.InnerPlotPosition.Auto = true;
            /////  Scroll  ///// 
            chartArea.CursorX.IsUserEnabled = true;
            chartArea.CursorX.IsUserSelectionEnabled = true;

            chartArea.AxisX.ScaleView.Zoomable = true;
            chartArea.AxisX.ScrollBar.IsPositionedInside = true;
            ////  Style  ////
            chartArea.AxisX.LineColor = Color.Gray;
            chartArea.AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
            chartArea.AxisX.MajorGrid.LineColor = Color.Gray;
            chartArea.AxisY.LineColor = Color.Gray;
            chartArea.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;
            chartArea.AxisY.MajorGrid.LineColor = Color.Gray;


             crt.ChartAreas.Add(chartArea);


             Legend L = new Legend("Main");
             L.DockedToChartArea = "Main";
             L.Alignment = StringAlignment.Near;
             L.Docking = Docking.Top;
             L.IsDockedInsideChartArea = true;

             crt.Legends.Add(L);

             Series S = new Series("Main");

            S.IsXValueIndexed = true;
            S.ChartArea = "Main";
            S.Legend = "Main";
            S.ChartType = SeriesChartType.Line;
            S.Color = Color.Red;
            S.XValueType = ChartValueType.DateTime;

            crt.Series.Add(S);


            //// Line1 график ========================

            /**/ String SeriesName = "Line1";

            Series v = new Series(SeriesName);

            //v.IsXValueIndexed = true;
            v.ChartArea = "Main";
            v.Legend = "Main";
            v.ChartType = SeriesChartType.Line;
            v.Color = Color.Blue;
            v.LegendToolTip = SeriesName;
            v.LegendText = SeriesName;
            v.XValueType = ChartValueType.DateTime;

            crt.Series.Add(v);

            Thread myThread = new Thread(new ThreadStart(FillData));
            myThread.Name = "TestChartin in Thread";
            myThread.Start(); 


        }





        private void FillData()
        {

            for (int day = 1; day <= 90; day++)
            {

                    AddPoint("Line1", day, random.Next(8000, 8200));
                    AddPoint("Main", day, random.Next(8000, 8200));
            }

        }


        public bool AddPoint(string series, double time, double P)
        {
            chart1.InvokeIfNeeded(() =>
            { 

                chart1.Series[series].Points.AddXY(time, P);

            });

            return true;

        }

    }
}

Who knows how to build a multiseries chart on one chartaraes? The above code works only if I ran with one serias (the Main) but when I run with two series (the main and the Line1) this program crashes.

addendum about zoom: when I make zoom in or a value climbs above the upper chart border, Y scale of chart doesn't change automatically.

Question: How do set up automatical Y scale of chart?

Roman
  • 1
  • 1

1 Answers1

0

Your code crashes because you are tying to set the 2nd Series' Legend to a non-exisiting Legend.

Compare your code here:

 String SeriesName = "Line1";
 ..
 v.Legend = SeriesName;

to what you wrote for the 1st series:

 Legend L = new Legend("Main");
 ..
 S.Legend = "Main";

You have chosen "Main" as the Name of your Legend so you can use it.

All you need to do is set it to the same Legend, as it should be:

 v.Legend = L.Name;

Also note that you didn't clear the default Legend and ChartArea in code. Maybe you did it in the designer? Thsese are the two lines the code would need to do it, they belong before all others:

crt.Legends.Clear(L);
crt.ChartAread.Clear(L);
TaW
  • 53,122
  • 8
  • 69
  • 111
  • I see next notice: Indexed series (XValueIndexed = true) can not be displayed on the same axis , if they do not not aligned. series "Main" and "Line1" must be aligned for this operation . At the moment, these series contain a different number of data points. What this's means? – Roman Jul 18 '16 at 02:14
  • And when I set v.IsXValueIndexed = false; it all worked but he shows empty value on the chart. – Roman Jul 18 '16 at 05:11
  • may be it's probler because of a dela between: – Roman Jul 18 '16 at 05:16
  • May be it's problem because of a delay between: AddXY(main) and AddXY(line1). This code don't have the delay but it is at my code. – Roman Jul 18 '16 at 05:24
  • If you uncomment the 2nd addxy you should be able to keep isxvalueindexed on as you are adding the two series in synch. But doing so makes no sense, probably, so keep it off! - What do you mean by 'empty value on the Chart'?? The top half? __Did you read my final comment?__ A delay makes no difference, I'd say, but as I don*'t see it, how can I know? – TaW Jul 18 '16 at 05:25
  • yes I read! And if when adding second series they aren't synced? See this code, unsync. 'empty value' when on add 12.06.2016,13.06.2016,14.06.2016,17.06.2016,18.06.2016 - 15.06.2016,18.06.2016 you can see nothing, empty cell. – Roman Jul 18 '16 at 05:44
  • Indeed, when your x-values have gaps they will show unless you set IsXValueIndexed = true. And then your sereis all need to have the same number of datapoints. – TaW Jul 18 '16 at 06:25
  • TaW: if I set IsXValueIndexed = true I see the notice: "Indexed series (XValueIndexed = true) can not be displayed on the same axis , if they do not not aligned. series "Main" and "Line1" must be aligned for this operation . At the moment, these series contain a different number of data points". If I set IsXValueIndexed = false all working but chart contain gaps! It's the fact! – Roman Jul 18 '16 at 20:08
  • Yes, that is how it is. Now: Where is your problem? Most likely that you don't understand those rules. But as long as you don't post the real code and what your chart is supposed to do we can't help you. – TaW Jul 18 '16 at 20:12
  • ok TaW now I changed old code on the real code and you can see! I try work with chart from a thethed – Roman Jul 18 '16 at 22:45