dev linechart动态加载数据(像股票一样的波动)

图片地址:https://blog.csdn.net/qq_33459369/article/details/80060196;(盗图)

接下来是封装的代码

#region 动态折线图
        public LineChartHelper(ChartControl chart)
        {
            LineSeriesView lineSeriesView = new LineSeriesView();
            chart.SeriesTemplate.View = lineSeriesView;
            //Legend的位置
            chart.Legend.AlignmentHorizontal = DevExpress.XtraCharts.LegendAlignmentHorizontal.Left;
            chart.Legend.AlignmentVertical = DevExpress.XtraCharts.LegendAlignmentVertical.TopOutside;
            chart.Legend.Direction = DevExpress.XtraCharts.LegendDirection.RightToLeft;
            //x轴为时间轴
            XYDiagram diagram = (XYDiagram)chart.Diagram;
            diagram.AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Millisecond;
            diagram.AxisX.DateTimeScaleOptions.ScaleMode = ScaleMode.Continuous;
        }

        public Series[] CreateSeries(ChartControl chart, List<string> names)
        {
            chart.Series.Clear();//清除Series
            int num = names.Count();
            Series[] series = new Series[num];
            for (int i = 0; i < num; i++)
            {
                series[i] = CreateSeries(names[i], ViewType.Line);
            }
            return series;
        }
        /// <summary>
        /// 创建折线
        /// </summary>
        private Series CreateSeries(string caption, ViewType viewType)
        {
            Series series = new Series(caption, viewType);
            //必须设置ArgumentScaleType的类型,否则显示会转换为日期格式,导致不是希望的格式显示
            //也就是说,显示字符串的参数,必须设置类型为ScaleType.Qualitative
            series.ArgumentScaleType = ScaleType.Auto;
            series.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;//显示标注标签
            //回归线
            LineSeriesView lineSeriesView = new LineSeriesView();
            RegressionLine regressionLine = new RegressionLine();
            regressionLine.Name = caption + "1";
            lineSeriesView.Indicators.AddRange(new DevExpress.XtraCharts.Indicator[] { regressionLine });
            series.View = lineSeriesView;
            return series;
        }
        /// <summary>
        /// 移除不在视图显示的Points数据
        /// </summary>
        /// <param name="series">曲线</param>
        /// <param name="minDate">时间线</param>
        public void RemovePoint(DateTime minDate, Series[] series)
        {
            int pointsToRemoveCount = 0;//曲线节点数据
            foreach (SeriesPoint point in series[0].Points)
            {
                if (point.DateTimeArgument < minDate)
                    pointsToRemoveCount++;
            }
            if (pointsToRemoveCount < series[0].Points.Count)
            {
                pointsToRemoveCount--;
            }
            if (pointsToRemoveCount > 0)
            {
                for (int i = 0; i < series.Count(); i++)
                {
                    series[i].Points.RemoveRange(0, pointsToRemoveCount);
                }
            }
        }
        /// <summary>
        /// 添加数据到曲线节点
        /// </summary>
        public void AddPoints(Series series, DateTime argument, double value)
        {
            if (series.View is LineSeriesView)
            {
                series.Points.Add(new SeriesPoint(argument, value));
            }
        }
        /// <summary>
        /// 设置x轴数据
        /// </summary>
        public void SetX(ChartControl chart, DateTime minDate, DateTime argument)
        {
            XYDiagram diagram = (XYDiagram)chart.Diagram;
            if (diagram != null && (diagram.AxisX.DateTimeScaleOptions.MeasureUnit == DateTimeMeasureUnit.Millisecond || diagram.AxisX.DateTimeScaleOptions.ScaleMode == ScaleMode.Continuous))
            {
                diagram.AxisX.WholeRange.SetMinMaxValues(minDate, argument);
            }
        }
        /// <summary>
        /// 获取曲线的回归线
        /// </summary>
        public RegressionLine GetRegressionLine(Series series)
        {
            if (series != null)
            {
                SwiftPlotSeriesView swiftPlotView = series.View as SwiftPlotSeriesView;
                if (swiftPlotView != null)
                {
                    Console.Write(swiftPlotView.Indicators);
                    foreach (Indicator indicator in swiftPlotView.Indicators)
                    {
                        RegressionLine regressionLine = indicator as RegressionLine;
                        if (regressionLine != null)
                            return regressionLine;
                    }
                }
            }
            return null;
        }
        #endregion

调用代码

        private readonly Random random = new Random();
        private Series[] _series;
        private LineChartHelper _lineChartHelper;
        private int _num = 0;

        public frmRealtimeLineChart()
        {
            InitializeComponent();

        }

        private void frmRealtimeLineChart_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void Init()
        {
            _lineChartHelper = new LineChartHelper(chart);
            List<string> names = new List<string>() { "曲线一", "折线二", "数字三" };
            _series = _lineChartHelper.CreateSeries(chart, names);
            _num = names.Count();
            chart.SeriesSerializable = _series;

            XYDiagram diagram = (XYDiagram)chart.Diagram;
            diagram.AxisX.GridLines.MinorVisible = true;
            diagram.AxisX.GridLines.Visible = true;
            diagram.AxisX.Label.TextPattern = "@{A:HH:mm:ss}";
            diagram.AxisX.Title.Font = new System.Drawing.Font("Tahoma", 9F);
            diagram.AxisX.Title.Text = @"时间(分)";
            diagram.AxisX.Title.Visible = true;
            diagram.AxisX.VisibleInPanesSerializable = "-1";

            diagram.AxisX.WholeRange.Auto = true;
            diagram.AxisX.WholeRange.SideMarginsValue = 1;
            diagram.AxisX.Interlaced = true;
            diagram.AxisY.WholeRange.AlwaysShowZeroLevel = false;

            diagram.AxisY.Title.Font = new System.Drawing.Font("Tahoma", 9F);
            diagram.AxisY.Title.Text = @"随机数";
            diagram.AxisY.Title.Visible = true;
            diagram.AxisY.VisibleInPanesSerializable = "-1";
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            RealtimeChart();
        }

        private void RealtimeChart()
        {
            try
            {
                DateTime argument = DateTime.Now;//x轴
                for (int i = 0; i < _num; i++)
                {
                    _lineChartHelper.AddPoints(_series[i], argument, CalculateNextValue());
                }
                DateTime minDate = argument.AddSeconds(-TimeInterval);//x轴时间曲度
                _lineChartHelper.RemovePoint(minDate, _series);
                _lineChartHelper.SetX(chart, minDate, argument);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
        }

        /// <summary>
        /// 随机数
        /// </summary>
        private double CalculateNextValue()
        {
            return Math.Round(random.NextDouble() * 100.0);
        }

        /// <summary>
        /// 间隔时间
        /// </summary>
        private int TimeInterval { get { return Convert.ToInt32(spnTimeInterval.EditValue); } }

直接拖入chart,选择折线图,效果如下:

 

posted @ 2019-05-30 11:35  世人皆萌  阅读(1404)  评论(0编辑  收藏  举报