WPF编程,曲线控件DynamicDataDisplay的使用方法(五)
程序员文章站
2022-06-07 18:07:34
...
以下代码基于0.3版本DLL
此处主要写如何在后台代码中,给曲线设置数据源。其中plotter是控件的名字
方法一:Point
ObservableDataSource<Point> currentDataFrame = new ObservableDataSource<Point>();
Point point = new Point(0, 0);
for (int i = 1; i <= 512; i++)
{
point.X = i;
point.Y = i + 100;
currentDataFrame.AppendAsync(base.Dispatcher, point);
}
方法二:CompositeDataSource
const int N = 1000;
double[] x = new double[N];
double[] y = new double[N];
for (int i = 0; i < N; i++)
{
x[i] = i;
y[i] = i;
}
var xDataSource = x.AsXDataSource();
var yDataSource = y.AsYDataSource();
CompositeDataSource compositeDataSource = xDataSource.Join(yDataSource);
plotter.AddLineGraph(compositeDataSource, Colors.Goldenrod, 3, "Sine");
方法三:EnumerableDataSource
const int N = 100;
Point[] pts = new Point[N];
for (int i = 0; i < N; i++)
{
double x = i;
pts[i] = new Point(x, x);
}
var ds = new EnumerableDataSource<Point>(pts);
ds.SetXYMapping(pt => pt);
plotter.AddLineGraph(ds);
如果是自定义的类
var ds = new EnumerableDataSource<自定义类>(自定义类列表);
ds.SetXMapping(x => x.类中的字段);
ds.SetYMapping(y => y.类中的字段);
plotter.AddLineGraph(ds, Colors.Green, 2, "Volts");