Devexpress中ChartControl绘制实时曲线

  1. 引用
using System.Drawing;
using DevExpress.XtraCharts;
using DevExpress.Utils;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrintingLinks;
  1. 创建图表
Series series1 = CreateSeries("变量1", ViewType.Line, Color.Brown);
Series series2 = CreateSeries("变量2", ViewType.Line, Color.DarkGray);
Series series3 = CreateSeries("变量3", ViewType.Line, Color.Red);
Series series4 = CreateSeries("变量4", ViewType.Line, Color.Blue);
Series series5 = CreateSeries("变量5", ViewType.Line, Color.DarkGreen);
Series series6 = CreateSeries("变量6", ViewType.Line, Color.Pink);
List list = new List { series1, series2, series3, series4, series5, series6 };
chart.Series.AddRange(list.ToArray());
chart.Legend.Visibility = DefaultBoolean.True; 
chart.SeriesTemplate.LabelsVisibility = DefaultBoolean.True;
for (int i = 0; i < list.Count; i++)
    CreateAxisY(this.chart, list[i]);
/// 
/// 根据数据创建一个图形展现
/// 
/// 图形标题
/// 图形类型
/// 图形数据的行序号
/// 
public Series CreateSeries(string caption, ViewType viewType, Color _color)
{
    Series series = new Series(caption, viewType);
    series.CrosshairEnabled = DevExpress.Utils.DefaultBoolean.True;
    series.View.Color = _color;
    //必须设置ArgumentScaleType的类型,否则显示会转换为日期格式,导致不是希望的格式显示
    //也就是说,显示字符串的参数,必须设置类型为ScaleType.Qualitative
    series.ArgumentScaleType = ScaleType.Qualitative;
    //series.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;//显示标注标签
    return series;
}
/// 
/// 创建图表的第二坐标系
/// 
/// Series对象
/// 
public SecondaryAxisY CreateAxisY(DevExpress.XtraCharts.ChartControl _chartCtrl, Series series)
{
    SecondaryAxisY myAxis = new SecondaryAxisY(series.Name);
    ((XYDiagram)_chartCtrl.Diagram).SecondaryAxesY.Add(myAxis);
    ((LineSeriesView)series.View).AxisY = myAxis;
    myAxis.Title.Text = series.Name;
    myAxis.Title.Alignment = StringAlignment.Far; //顶部对齐
    myAxis.Title.Visible = true; //显示标题
    myAxis.Title.Font = new Font("宋体", 9.0f);

    Color color = series.View.Color;//设置坐标的颜色和图表线条颜色一致

    myAxis.Title.TextColor = color;
    myAxis.Label.TextColor = color;
    myAxis.Color = color;

    return myAxis;
}
  1. 图表更新
int DataRowIndex;
private void UpdateChart()
{
    DataRowIndex = DataRowIndex + 1;
    //曲线点数超过10后水平左移
    if (chart.Series["变量1"].Points.Count >= 10)
    {
        for (int i = 0; i < chart.Series.Count; i++)
        {
            if (chart.Series[i].Points.Count > 0)
                chart.Series[i].Points.RemoveAt(0);
        }
    }
    for (int i=0;i<6;i++)
    {
        chart.Series["变量" + (i + 1).ToString()].Points.Add(new SeriesPoint(DataRowIndex, value[i]));
    }
    //第二坐标系归一化
    for (int i = 0; i < chart.Series.Count; i++)
    {
        if (chart.Series[i].Points.Count > 0)
        {
            double Min = chart.Series[i].Points[0].Values[0];
            double Max = chart.Series[i].Points[0].Values[0];
            for (int j = 1; j < chart.Series[i].Points.Count; j++)
            {
                double Value = chart.Series[i].Points[j].Values[0];
                if (Value < Min)
                    Min = Value;
                if (Value > Max)
                    Max = Value;
            }
            XYDiagram RealDiag = (XYDiagram)chart.Diagram;
            if (RealDiag != null)
            {
                RealDiag.SecondaryAxesY[i].WholeRange.MinValue = Min - 1;
                RealDiag.SecondaryAxesY[i].WholeRange.MaxValue = Max + 1;
            }
        }
    }
}

你可能感兴趣的:(Devexpress中ChartControl绘制实时曲线)