C#移动垂直线注释在图表控件上移动16000点太慢

时间:2016-11-17 03:30:58

标签: c# winforms charts

我有列表XY点包括16000点,我将它部署到2图表类型波形图和弧图。我使用类名为VerticalLineAnnotation来设计波形图上的垂直线,并在弧图上添加系列图表类型线以反映弧图上的任意最后X点。当将垂直线向左右移动时,它可以很好地工作但是当我将鼠标移出线条时,图表(波形和弧形图形)会重绘并需要很长时间。如何防止它(重绘事件)或加速?。

这是源代码

 ChartArea CA;
 Series S1;
 VerticalLineAnnotation VA;
 double degree = 0;
 int test = 0;
 Dictionary<int, PointXY> xyMap = new Dictionary<int, PointXY>();

 private void DrawGraph(DataTable dtExcel)
        {
            Series series1 = new Series("Series2");
            series1.ChartArea = "ChartArea1";
            series1.ChartType = SeriesChartType.Spline;
            series1.Color = Color.White;
            chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 1000;
            chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 4500;
            chart1.ChartAreas["ChartArea1"].AxisY.Interval = 500;

            chart1.ChartAreas["ChartArea1"].AxisX.Minimum = 0;
            chart1.ChartAreas["ChartArea1"].AxisX.Maximum = 16000;
            chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1000;

            Series series2 = new Series("Series2");
            series2.ChartArea = "ChartArea1";
            series2.ChartType = SeriesChartType.Spline;
            series2.Color = Color.White;
            chart2.ChartAreas["ChartArea1"].AxisY.Minimum = -100;
            chart2.ChartAreas["ChartArea1"].AxisY.Maximum = 100;
            chart2.ChartAreas["ChartArea1"].AxisY.Interval = 10;
            chart2.ChartAreas["ChartArea1"].AxisX.Minimum = -100;
            chart2.ChartAreas["ChartArea1"].AxisX.Maximum = 100;
            chart2.ChartAreas["ChartArea1"].AxisX.Interval = 10;

            foreach (DataRow item in dtExcel.Rows)
            {
                series1.Points.AddXY(int.Parse(item[0].ToString()), item[1].ToString());
                CaculatePointXY(item, series2);
            }


            chart1.Series.Add(series1);



            chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.Green;
            chart1.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.Green;
            chart1.ChartAreas["ChartArea1"].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont;

            chart2.Series.Add(series2);
            chart2.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor = Color.Green;
            chart2.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.Green;
            chart2.ChartAreas["ChartArea1"].AxisX.LabelAutoFitStyle = LabelAutoFitStyles.DecreaseFont;


            //Draw vertical line
            CA = chart1.ChartAreas["ChartArea1"];

            VA = new VerticalLineAnnotation();
            VA.AxisX = CA.AxisX;
            VA.AllowMoving = true;
            VA.IsInfinitive = true;
            VA.ClipToChartArea = CA.Name;
            VA.Name = "myLine";
            VA.LineColor = Color.Red;
            VA.LineWidth = 2;         // use your numbers!
            VA.X = 8000;
            chart1.Annotations.Add(VA);


            chart2.Series.Add("Line");
            chart2.Series["Line"].Points.Add(new DataPoint(0, 0));
            chart2.Series["Line"].Color = Color.Red;
            chart2.Series["Line"].BorderWidth = 2;
            chart2.Series["Line"].Points.Add(new DataPoint(0, 100));
            chart2.Series["Line"].ChartType = SeriesChartType.Line;

        }

        private void CaculatePointXY(DataRow item, Series series2)
        {
            double distance = (int.Parse(item[1].ToString()) - 819) * 0.0030525;

            distance += 95;

            double x = distance * Math.Cos((degree * Math.PI / 180));
            double y = distance * Math.Sin((degree * Math.PI / 180));
            series2.Points.AddXY(x, y);
            xyMap.Add(int.Parse(item[0].ToString()), new PointXY()
            {
                X = x,
                Y = y
            });
            degree += 0.0225;


        }
        private void chart1_Customize(object sender, EventArgs e)
        {
            foreach (var label in chart1.ChartAreas[0].AxisY.CustomLabels)
            {
                label.Text = ((int)double.Parse(label.Text) / 100).ToString();
            }
            foreach (var label in chart1.ChartAreas[0].AxisX.CustomLabels)
            {
                label.Text = ((int)double.Parse(label.Text) / 100).ToString();
            }
        }


        private void chart1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

            var xVal = chart1.ChartAreas[0].AxisX.PixelPositionToValue(e.X);
            var yVal = chart1.ChartAreas[0].AxisY.PixelPositionToValue(e.Y);
            ZoomWaveFormGraph(xVal, yVal);

        }

        private void ZoomWaveFormGraph(double xVal, double yVal)
        {
            ZoomArcGraph((int)xVal);


            var YMin = Math.Round(yVal - 1000, 0);
            var YMax = Math.Round(yVal + 1000, 0);
            chart1.ChartAreas["ChartArea1"].AxisY.Minimum = YMin;
            chart1.ChartAreas["ChartArea1"].AxisY.Maximum = YMax;
            chart1.ChartAreas["ChartArea1"].AxisY.Interval = 100;


            var XMin = Math.Round(xVal - 1000, 0);
            var XMax = Math.Round(xVal + 1000, 0);
            chart1.ChartAreas["ChartArea1"].AxisX.Minimum = XMin;
            chart1.ChartAreas["ChartArea1"].AxisX.Maximum = XMax;
            chart1.ChartAreas["ChartArea1"].AxisX.Interval = 200;


        }

        private void ZoomArcGraph(double xVal)
        {
            int a = (int)xVal;
            var b = xyMap[a];
            chart2.ChartAreas["ChartArea1"].AxisY.Minimum = Math.Round(b.Y - 20, 0);
            chart2.ChartAreas["ChartArea1"].AxisY.Maximum = Math.Round(b.Y + 20, 0);
            chart2.ChartAreas["ChartArea1"].AxisY.Interval = 2;
            chart2.ChartAreas["ChartArea1"].AxisX.Minimum = Math.Round(b.X - 20, 0);
            chart2.ChartAreas["ChartArea1"].AxisX.Maximum = Math.Round(b.X + 20, 0);
            chart2.ChartAreas["ChartArea1"].AxisX.Interval = 2;
        }



        private double RadianToDegree(double angle)
        {
            return angle * (180.0 / Math.PI);
        }
        private void chart1_AnnotationPositionChanging(object sender, AnnotationPositionChangingEventArgs e)
        {

            test = (int)Math.Round(e.NewLocationX, 0);
            Console.WriteLine("X : " + e.NewLocationX.ToString());
        }

        private void chart1_AnnotationPositionChanged(object sender, EventArgs e)
        {
            var a = xyMap[test];
            chart2.Series["Line"].Points.ElementAt(1).SetValueXY(a.X, a.Y);
            chart2.Refresh();
        }

0 个答案:

没有答案