Microsoft图表控件:MultiSeries和缩放

时间:2016-07-16 21:34:10

标签: c# microsoft-chart-controls

    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;

        }

    }
}

谁知道如何在一个图表上构建多重图表?上面的代码只有在我运行一个serias(Main)时运行,但是当我运行两个系列(main和Line1)时,这个程序崩溃了。

关于缩放的附录:当我进行放大或者某个值超过上方图表边框时,图表的Y比例不会自动更改。

问题:如何设置图表的自动Y比例?

1 个答案:

答案 0 :(得分:0)

你的代码崩溃是因为你想要设置第二个系列&#39;一个不存在的传说的传说。

在此处比较您的代码:

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

你为第一个系列写的内容:

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

您选择了&#34; Main&#34;作为Legend的名称,以便您可以使用它。

您需要做的就是将其设置为相同的Legend

 v.Legend = L.Name;

另请注意,您并未清除代码中的默认LegendChartArea。也许你是在设计师那里做过的?这是代码需要执行的两行,它们属于所有其他行:

crt.Legends.Clear(L);
crt.ChartAread.Clear(L);
相关问题