接上回话题:这次使用的是动态创建图形,即不知道要创建多少个,也不知道创建的是什么样的曲线。
新建一个类,作为曲线的数据源
public class ASCAXIModels : RingArray<ValuePoint2>
{
private const int TOTAL_POINTS = 300000;
public ASCAXIModels()
: base(TOTAL_POINTS) // here i set how much values to show
{
}
}
public class ValuePoint2
{
public double MilTime { get; set; }
public double Value { get; set; }
public ValuePoint2(double value, double time)
{
this.MilTime = time;
this.Value = value;
}
}
然后在应该动态创建图形的地方,一步步的创建所需资源,然后放入SPmap中:
Header header = new Header();
VerticalAxisTitle VAT = new VerticalAxisTitle();
HorizontalAxisTitle HAT = new HorizontalAxisTitle();
HorizontalIntegerAxis hIA = new HorizontalIntegerAxis();
header.Content = LDIC[j].Name;
VAT.Content = "数值";
HAT.Content = "时间";
ChartPlotter CP = new ChartPlotter();
CP.Name = "CP" + CPcount.ToString();
CP.HorizontalAlignment = HorizontalAlignment.Left;
CP.VerticalAlignment = VerticalAlignment.Top;
CP.Margin = new Thickness(1);
CP.Children.Add(header);
CP.Children.Add(VAT);
CP.Children.Add(HAT);
ASCAXIModels cr = new ASCAXIModels();
ChartPlotter cp = new ChartPlotter();
CursorCoordinateGraph CCG = new CursorCoordinateGraph();
CCG.LineStroke = Brushes.Brown;
CCG.UseLayoutRounding = true;
CCG.XTextMapping = value =>
{
return value.ToString("0.0000");
};
// create a new LineGraph
var lineGraph = new LineGraph();
lineGraph.Name = "line" + CPcount.ToString();
// create a new DataSource for the LineGraph
var dataSource = new EnumerableDataSource<ValuePoint2>(cr);
// map the data to x and y coordinates
dataSource.SetXMapping(x => hIA.ConvertFromDouble(x.MilTime));
dataSource.SetYMapping(y => y.Value);
// add the DataSource to the LineGraph
lineGraph.DataSource = dataSource;
lineGraph.Description = new PenDescription(LDIC[j].Name);
lineGraph.StrokeThickness = 1;
lineGraph.Stroke = Brushes.Blue;
CCG.Name = "CCG" + CPcount.ToString();
// add the LineGraph to the plotter
CP.Viewport.AutoFitToView = true;
CP.Children.Add(lineGraph);
CP.Children.Add(CCG);
CP.Legend.HorizontalAlignment = HorizontalAlignment.Left;
SPmap.Children.Add(CP);
updateCollectionTimer.Start();
其中 CCG.XTextMapping = value =>
{
return value.ToString("0.0000");
};
这个是设置X轴的鼠标显示位数,如下图中红色显示。