this.dChart.Diagram.Series.Clear();//清空图的内容
var groups = result.GroupBy(itm => itm["flag"]);//将result根据flag属性分组 result是要显示为散点图的数据内容
foreach (var group in groups)
{
var t = group.First();
PointSeries2D series = new PointSeries2D() //新建散点图组
{
Name = t["flag"].ToString(),
Brush = new SolidColorBrush(Colors.Blue),//颜色
DisplayName = t["flagname"].ToString(),//显示内容
AnimationAutoStartMode = AnimationAutoStartMode.PlayOnce,
MarkerSize = 8 /*显示点的大小*/,
MarkerModel = new RingMarker2DModel(),
CrosshairEnabled = true,
CrosshairLabelVisibility = true,
CrosshairLabelPattern = "{S}
时间:{A}
水位:{V}m",//鼠标滑过显示的内容
};
int flag = Convert.ToInt32(group.Key);//group.Key就是上边根据分组的属性
switch (flag)//自定义颜色
{
case 0: series.Brush = new SolidColorBrush(Colors.Blue); break;
case 1: series.Brush = new SolidColorBrush(Colors.Green); break;
case 2: series.Brush = new SolidColorBrush(Colors.Yellow); break;
case 3: series.Brush = new SolidColorBrush(Colors.Red); break;
default: break;
}
var source = group.ToList();
foreach (var s in source)//将数据内容赋值到点上
{
if (s["a"] == null || string.IsNullOrEmpty(s["a"].ToString()))//判断空值
continue;
double z = Convert.ToDouble(s["a"]);
DateTime tm = Convert.ToDateTime(s["tm"]);
series.Points.Add(new SeriesPoint(tm, z));//将点加入
}
dChart.Diagram.Series.Add(series);//将散点组赋值给图的数据源
}