Picture Address: https: //blog.csdn.net/qq_33459369/article/details/80060196; (Pirates)
The next code is packaged
#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; // set large scale intervals diagram.AxisX.DateTimeScaleOptions.ScaleMode = ScaleMode.Continuous; // diagram.AxisX = to true .GridLines.MinorVisible; // diagram.AxisX.GridLines.Visible = to true; // diagram.AxisX.Label.TextPattern = @ "{A: HH: mm: SS}"; // diagram.AxisX.Title. Text = @ "time"; // diagram.AxisX.Title.Visible = to 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> /// Create polyline /// </ Summary> Private Series CreateSeries ( String Caption, ViewType viewType ) { Series Series = new new Series (Caption, viewType); // be set ArgumentScaleType type, or converted to display the date format which is not a desirable format // That is, the character string display parameters must be set type is 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> /// remove a Points not view the display data /// </ Summary> /// <param name = "Series"> curve </ param> /// <param name = "the minDate"> Timeline </ param> public void RemovePoint (the DateTime the minDate, Series [] Series) { int pointsToRemoveCount = 0 ; // graph node data the foreach (SeriesPoint Point in Series [ 0 ] .Points) { IF (point.DateTimeArgument < the 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
Calling code
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-axis curvature time _lineChartHelper.RemovePoint (the minDate, _series); _lineChartHelper.SetX (Chart, the minDate, argument); } the catch (Exception EX) { Console.Write (EX); } } /// <Summary> /// random number /// </ Summary> Private Double CalculateNextValue () { return Math.Round (random.NextDouble () * 100.0 ); } /// <Summary> /// Intervals /// </summary> private int TimeInterval { get { return Convert.ToInt32(spnTimeInterval.EditValue); } }
Chart drag directly, select line chart, the following effects:
Unresolved issues:
Problems x-axis time format: diagram.AxisX.Label.TextPattern = "{A: HH: mm: ss}"; do not know why such setting does not work,