简体   繁体   English

Microsoft Chart Control:MultiSeries和缩放

[英]Microsoft Chart Control: MultiSeries and zoom

    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. 上面的代码仅在我使用一个系列(Main)运行时有效,但是当我使用两个系列(Main和Line1)运行时,此程序崩溃。

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. 有关缩放的附录:当我进行放大或某个值爬到图表的上限上方时,图表的Y比例不会自动更改。

Question: How do set up automatical Y scale of chart? 问题:如何设置图表自动Y刻度?

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. 您已选择“主要”作为Legend的名称,以便可以使用它。

All you need to do is set it to the same Legend , as it should be: 您需要将其设置为相同的Legend ,因为它应该是:

 v.Legend = L.Name;

Also note that you didn't clear the default Legend and ChartArea in code. 另请注意,您没有在代码中清除默认的LegendChartArea 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM