其他分享
首页 > 其他分享> > dev linechart动态加载数据

dev linechart动态加载数据

作者:互联网

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

接下来是封装的代码

#region 动态折线图
        public LineChartHelper(ChartControl chart)
        {
            LineSeriesView lineSeriesView = new LineSeriesView();
            chart.SeriesTemplate.View = lineSeriesView;

            chart.Legend.AlignmentHorizontal = DevExpress.XtraCharts.LegendAlignmentHorizontal.Left;
            chart.Legend.AlignmentVertical = DevExpress.XtraCharts.LegendAlignmentVertical.TopOutside;
            chart.Legend.Direction = DevExpress.XtraCharts.LegendDirection.RightToLeft;

            XYDiagram diagram = (XYDiagram)chart.Diagram;
            diagram.AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Millisecond;
            //diagram.AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Minute;//设置大刻度的间隔
            diagram.AxisX.DateTimeScaleOptions.ScaleMode = ScaleMode.Continuous;
            //diagram.AxisX.GridLines.MinorVisible = true;
            //diagram.AxisX.GridLines.Visible = true;
            //diagram.AxisX.Label.TextPattern = @"{A:HH:mm:ss}";
            //diagram.AxisX.Title.Text = @"时间";
            //diagram.AxisX.Title.Visible = true;
            //diagram.AxisX.VisibleInPanesSerializable = "-1";
            //diagram.AxisY.Title.Visible = true;
            //diagram.AxisY.Title.Text = @"件数";
            //diagram.AxisY.VisibleInPanesSerializable = "-1";
        }

        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.Qualitative;
            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, string 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, object argument)
        {
            XYDiagram diagram = (XYDiagram)chart.Diagram;
            if (diagram != null)
            {
                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.ToString(), 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,选择折线图,效果如下:

未解决问题:

x轴的时间格式问题:diagram.AxisX.Label.TextPattern = "{A:HH:mm:ss}";不知道为啥这样设置不起作用,

 

标签:linechart,series,AxisX,chart,dev,diagram,Series,new,加载
来源: https://www.cnblogs.com/shuaimeng/p/10948655.html