DynamicDataDisplay 学习心得
DynamicDataDisplay是微软提供一个画图库,在项目用到,自己走过许多弯路,现在记录下来方便学习和交流。
1.添加引用库
这点大家到网上找吧。
2.画自定义方程曲线(一元三次方程)(包括绘制曲线,获取鼠标在曲线移动的坐标,设置XY轴刻度范围等)
XML代码:
<d3:ChartPlotter Name="plotter" Grid.Row="3" Margin="1,0,0,0" MouseMove="plotter_MouseMove">
<d3:ChartPlotter.HorizontalAxis>
<d3:HorizontalAxis Name="dateAxis" FontSize="10"/>
</d3:ChartPlotter.HorizontalAxis>
<d3:ChartPlotter.VerticalAxis>
<d3:VerticalAxis Name="countAxis" FontSize="10" Width="20" Margin="0,0,0,0"/>
</d3:ChartPlotter.VerticalAxis>
<!--<d3:Header Name="Headinof" FontFamily="Arial" Content="Bug Information" />-->
<d3:VerticalAxisTitle Name="YAxis" Content="Count" Height="20"/>
<d3:HorizontalAxisTitle Name="XAxis" Content="SPEED" Height="20"/>
</d3:ChartPlotter>
CS代码:
/// <summary>
/// 设置XY轴刻度范围
/// </summary>
public class DisplayRange
{
public double Start { get; set; }
public double End { get; set; }
public DisplayRange(double start, double end)
{
Start = start;
End = end;
}
}
public class ViewportAxesRangeRestriction : IViewportRestriction
{
public DisplayRange XRange = null;
public DisplayRange YRange = null;
public Rect Apply(Rect oldVisible, Rect newVisible, Viewport2D viewport)
{
if (XRange != null)
{
newVisible.X = XRange.Start;
newVisible.Width = XRange.End - XRange.Start;
}
if (YRange != null)
{
newVisible.Y = YRange.Start;
newVisible.Height = YRange.End - YRange.Start;
}
return newVisible;
}
public event EventHandler Changed;
}
public void DrawBaseLine(int nType, int nDraftType, List<double> xAxis)//根据要求传入参数
{
if (plotter.Children != null)//主要是删除一些自带的事件
{
plotter.Children.Remove(plotter.MouseNavigation);//鼠标滚动改变比例尺
plotter.Children.Remove(plotter.KeyboardNavigation);//键盘改变比例尺
//plotter.Children.Remove(FULLgraphDraw);
//plotter.Children.Remove(HALFgraphDraw);
//plotter.Children.Remove(NOgraphDraw);
//plotter.Children.Remove(plotter.legend);//删除右上角的点描述
plotter.Children.Remove(graphDraw);//已有曲线
//plotter.Children.Clear();
}
//设置XY轴刻度范围
plotter.Viewport.AutoFitToView = true;
ViewportAxesRangeRestriction restr = new ViewportAxesRangeRestriction();
restr.XRange = new DisplayRange(-0.5, 32);
restr.YRange = new DisplayRange(-0.5, 25);
plotter.Viewport.Restrictions.Add(restr);
//List<AxisValueInfo> full_bugInfoList = LoadAxisValueInfo(nType, 0, xAxis);
List<AxisValueInfo> full_bugInfoList = LoadAxisValueInfo(nType, nDraftType, xAxis);//通过一元三次方程得到的XY点
double[] x_Axis = new double[full_bugInfoList.Count];
double[] y_Axis = new double[full_bugInfoList.Count];
for (int i = 0; i < full_bugInfoList.Count; ++i)
{
x_Axis[i] = full_bugInfoList[i].xAxis;
y_Axis[i] = full_bugInfoList[i].yAxis;
}
//获取XY点集的最大最小点
g_minX = x_Axis.Min();
g_maxX = x_Axis.Max();
g_minY = y_Axis.Min();
g_maxY = y_Axis.Max();
var x_AxisDataSource = new EnumerableDataSource<double>(x_Axis);
x_AxisDataSource.SetXMapping(x => dateAxis.ConvertToDouble(x));
x_AxisDataSource.AddMapping(ShapeElementPointMarker.ToolTipTextProperty, x => String.Format("Value is {0}", x));
var y_AxisDataSource = new EnumerableDataSource<double>(y_Axis);
y_AxisDataSource.SetYMapping(y => countAxis.ConvertToDouble(y));
y_AxisDataSource.AddMapping(ShapeElementPointMarker.ToolTipTextProperty, Y => String.Format("Value is {0}", Y));
CompositeDataSource compositeDataSource1 = new CompositeDataSource(x_AxisDataSource, y_AxisDataSource);
//PenDescription des = new PenDescription(" ");
graphDraw = plotter.AddLineGraph(compositeDataSource1, m_linecolor[0], 2, this.YAxis.Content.ToString());//绘制曲线
plotter.Viewport.FitToView();
}
//绘制鼠标在曲线移动时获取鼠标点的动态坐标
private void DrawBaseLine_Point(double[] x_Axis, double[] y_Axis, string strInfo)
{
if (plotter.Children != null)
{
plotter.Children.Remove(plotter.MouseNavigation);
plotter.Children.Remove(plotter.KeyboardNavigation);
plotter.Children.Remove(graphDraw_Point.MarkerGraph);
plotter.Children.Remove(graphDraw_Point.LineGraph);
}
double[] x_Point = x_Axis;
double[] y_Point = y_Axis;
var datesDataSource = new EnumerableDataSource<double>(x_Axis);
datesDataSource.SetXMapping(x => dateAxis.ConvertToDouble(x));
var numberOpenDataSource = new EnumerableDataSource<double>(y_Axis);
numberOpenDataSource.SetYMapping(y => countAxis.ConvertToDouble(y));
CompositeDataSource compositeDataSource1 = new
CompositeDataSource(datesDataSource, numberOpenDataSource);
graphDraw_Point = plotter.AddLineGraph(compositeDataSource1,
new Pen(Brushes.Transparent, 2),
new CirclePointMarker { Size = 8, Fill = Brushes.Red },
new PenDescription(strInfo));
plotter.Viewport.FitToView();
}
以上是我在项目用到的方法,欢迎大家补充。
浙公网安备 33010602011771号