Dev的Chart控件数据绑定winform
1、折线图
图示效果:
代码:
public void Init() { ChartControl.BeginInit(); ChartControl.SeriesDataMember = "SensorKey"; ChartControl.SeriesTemplate.SetDataMembers("SensorIndex", "SensorValue"); ChartControl.SeriesTemplate.CrosshairLabelPattern = "{SensorKey}\n Index:{A:0} 值:{V:0.000 mm} 时间:{FullDate}"; LineSeriesView view = new LineSeriesView(); view.LineStyle.Thickness = 2; view.LineStyle.LineJoin = LineJoin.Round; ChartControl.SeriesTemplate.View = view; ChartControl.DataSource = ReadDataFromFile(); XYDiagram diagram = new XYDiagram(); diagram.EnableAxisXScrolling = true; diagram.EnableAxisXZooming = true; diagram.EnableAxisYScrolling = true; diagram.EnableAxisYZooming = true; diagram.AxisX.WholeRange.AlwaysShowZeroLevel = false; diagram.AxisX.Title.Text = "MySensor"; diagram.AxisX.Title.Visibility = DefaultBoolean.True; diagram.AxisX.WholeRange.SideMarginsValue = 1; diagram.AxisX.WholeRange.SetMinMaxValues(1, i); //diagram.AxisX.Label.TextPattern = " {A:0.}";//x轴文字格式 ChartControl.Diagram = diagram; ChartControl.Legend.MarkerMode = LegendMarkerMode.CheckBoxAndMarker; ChartControl.Legend.Border.Visibility = DefaultBoolean.False;//能见度 ChartControl.Legend.BackColor = Color.Transparent; ChartControl.Legend.Direction = LegendDirection.TopToBottom;//CheckBox按钮顺序 ChartControl.CrosshairOptions.CrosshairLabelMode = CrosshairLabelMode.ShowCommonForAllSeries;//ShowForNearestSeries(显示最近)//ShowCommonForAllSeries(显示全部)//ShowForEachSeries(分开显示每个,不好用) ChartControl.CrosshairOptions.LinesMode = CrosshairLinesMode.Auto; ChartControl.CrosshairOptions.ShowArgumentLabels = false; //ChartControl.LegendItemChecked += ChartControl_LegendItemChecked; ChartControl.EndInit(); }
//该方法主要是填充数据,需要自行添加数据,方式如下
public List<SensorDataPoint> ReadDataFromFile()
{
List<SensorDataPoint> dataSource = new List<SensorDataPoint>();
FileStream fs = null;
try
{
dataSource.Add(new SensorDataPoint("Sensor1",
Convert.ToDouble(cells[sensor1]), i, Convert.ToDateTime(cells[time])));
dataSource.Add(new SensorDataPoint("Sensor2",
Convert.ToDouble(cells[sensor2]), i, Convert.ToDateTime(cells[time])));
dataSource.Add(new SensorDataPoint("Sensor3",
Convert.ToDouble(cells[sensor3]), i, Convert.ToDateTime(cells[time])));
dataSource.Add(new SensorDataPoint("Sensor4",
Convert.ToDouble(cells[sensor4]), i, Convert.ToDateTime(cells[time])));
dataSource.Add(new SensorDataPoint("Sensor5",
Convert.ToDouble(cells[sensor5]), i, Convert.ToDateTime(cells[time])));
dataSource.Add(new SensorDataPoint("Sensor6",
Convert.ToDouble(cells[sensor6]), i, Convert.ToDateTime(cells[time])));
dataSource.Add(new SensorDataPoint("Sensor7",
Convert.ToDouble(cells[sensor7]), i, Convert.ToDateTime(cells[time])));
dataSource.Add(new SensorDataPoint("Sensor8",
Convert.ToDouble(cells[sensor8]), i, Convert.ToDateTime(cells[time])));
dataSource.Add(new SensorDataPoint("Sensor9",
Convert.ToDouble(cells[sensor9]), i, Convert.ToDateTime(cells[time])));
}
catch
{
throw new Exception("It's impossible to load ");
}
finally
{
reader?.Close();
fs?.Close();
}
return dataSource;
}
//modle public class SensorDataPoint { public string SensorKey { get; private set; } public double SensorValue { get; private set; } public double SensorIndex { get; private set; } public DateTime FullDate { get; private set; } internal SensorDataPoint(string sensorKey, double sensorValue, int sensorIndex, DateTime fullDate) { SensorKey = sensorKey; SensorValue = sensorValue; SensorIndex = sensorIndex; FullDate = fullDate; } }
关键的地方在于,创建的modle类对应折线图的x,y轴,然后填充数据
ChartControl.SeriesDataMember = "SensorKey";
ChartControl.SeriesTemplate.SetDataMembers("SensorIndex", "SensorValue");