Oxyplot,从数组中添加数据
Oxyplot, adding data from an array
我有一个包含 42 个双精度值的数组 (double[] data = new double[42];
)。现在我想用 oxyplot 图表可视化这些数据,但我不知道我该怎么做,因为在网站上给出了以下示例:
public class MainViewModel
{
public MainViewModel()
{
this.Title = "Example 2";
this.Points = new List<DataPoint>
{
new DataPoint(0, 4),
new DataPoint(10, 13),
new DataPoint(20, 15),
new DataPoint(30, 16),
new DataPoint(40, 12),
new DataPoint(50, 12)
};
}
public string Title { get; private set; }
public IList<DataPoint> Points { get; private set; }
}
我还必须想象另一个具有 16'064 个双精度值的数组。我想你可以想象我正在寻找一种尽可能简单的方法来做到这一点,例如下一个使用 Sinus 函数的例子:
正弦波示例
// Adding data (FunctionSeries) to the Chart
chart1.Series.Add(new FunctionSeries(Math.Sin, 0, 30, 0.1, "sin(x)"));
您必须将数组作为 ObservableCollection 并将其作为 itemsSource 绑定到 Series 图表。
例如,如果您想绘制 LineSeries,则:
<OxyPlot:PlotView Name="lineChart" Background="Transparent" IsHitTestVisible="False" DisconnectCanvasWhileUpdating="True" PlotMargins="75 2 2 25">
<OxyPlot:PlotView.Axes>
<OxyPlot:DateTimeAxis Name="xAxis" Position="Bottom" StringFormat="mm:ss" MajorGridlineStyle="Solid" IsZoomEnabled="False" IsPanEnabled="False" />
<OxyPlot:LinearAxis Name="yAxis" Position="Left" MajorGridlineStyle="Solid" IsZoomEnabled="False" IntervalLength="15" IsPanEnabled="False" />
</OxyPlot:PlotView.Axes>
<OxyPlot:PlotView.Series>
<OxyPlot:LineSeries DataFieldX="XValue" DataFieldY="YValue" ItemsSource="{Binding lineSeries1ItemsSource}" MarkerType="Circle" MarkerSize="2.2" Background="#FFEBEBEB" />
<OxyPlot:LineSeries DataFieldX="XValue" DataFieldY="YValue"
</OxyPlot:PlotView.Series>
</OxyPlot:PlotView>
在后面的代码中:
// Observable Collection of type Chart Data for binding to First Line Series in the Chart
public ObservableCollection<ChartData> lineSeries1ItemsSource { get; set; }
// List for adding all the Observable Collections bound as ItemsSource for Line Series
public ObservableCollection<ObservableCollection<ChartData>> lstItemsSource;
public ucLineSeriesChart()
{
InitializeComponent();
// Set the Data Context to current instance
this.DataContext = this;
// Instanciate List object
lstItemsSource = new ObservableCollection<ObservableCollection<ChartData>>();
// Instanciate Observable Collections
lineSeries1ItemsSource = new ObservableCollection<ChartData>();
// Add the Observable Collections to the List
lstItemsSource.Add(lineSeries1ItemsSource);
}
图表数据class:
public class ChartData : INotifyPropertyChanged
{
//Event that is raised when the value of some property changes
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Property for XValue of Line Series
/// </summary>
public DateTime XValue
{
get
{
return _xValue;
}
set
{
_xValue = value;
OnPropertyChanged("XValue");
}
}
/// <summary>
/// Property for YValue of Line Series
/// </summary>
public double YValue
{
get
{
return _yValue;
}
set
{
_yValue = value;
OnPropertyChanged("YValue");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
我有一个包含 42 个双精度值的数组 (double[] data = new double[42];
)。现在我想用 oxyplot 图表可视化这些数据,但我不知道我该怎么做,因为在网站上给出了以下示例:
public class MainViewModel
{
public MainViewModel()
{
this.Title = "Example 2";
this.Points = new List<DataPoint>
{
new DataPoint(0, 4),
new DataPoint(10, 13),
new DataPoint(20, 15),
new DataPoint(30, 16),
new DataPoint(40, 12),
new DataPoint(50, 12)
};
}
public string Title { get; private set; }
public IList<DataPoint> Points { get; private set; }
}
我还必须想象另一个具有 16'064 个双精度值的数组。我想你可以想象我正在寻找一种尽可能简单的方法来做到这一点,例如下一个使用 Sinus 函数的例子:
正弦波示例
// Adding data (FunctionSeries) to the Chart
chart1.Series.Add(new FunctionSeries(Math.Sin, 0, 30, 0.1, "sin(x)"));
您必须将数组作为 ObservableCollection 并将其作为 itemsSource 绑定到 Series 图表。
例如,如果您想绘制 LineSeries,则:
<OxyPlot:PlotView Name="lineChart" Background="Transparent" IsHitTestVisible="False" DisconnectCanvasWhileUpdating="True" PlotMargins="75 2 2 25">
<OxyPlot:PlotView.Axes>
<OxyPlot:DateTimeAxis Name="xAxis" Position="Bottom" StringFormat="mm:ss" MajorGridlineStyle="Solid" IsZoomEnabled="False" IsPanEnabled="False" />
<OxyPlot:LinearAxis Name="yAxis" Position="Left" MajorGridlineStyle="Solid" IsZoomEnabled="False" IntervalLength="15" IsPanEnabled="False" />
</OxyPlot:PlotView.Axes>
<OxyPlot:PlotView.Series>
<OxyPlot:LineSeries DataFieldX="XValue" DataFieldY="YValue" ItemsSource="{Binding lineSeries1ItemsSource}" MarkerType="Circle" MarkerSize="2.2" Background="#FFEBEBEB" />
<OxyPlot:LineSeries DataFieldX="XValue" DataFieldY="YValue"
</OxyPlot:PlotView.Series>
</OxyPlot:PlotView>
在后面的代码中:
// Observable Collection of type Chart Data for binding to First Line Series in the Chart
public ObservableCollection<ChartData> lineSeries1ItemsSource { get; set; }
// List for adding all the Observable Collections bound as ItemsSource for Line Series
public ObservableCollection<ObservableCollection<ChartData>> lstItemsSource;
public ucLineSeriesChart()
{
InitializeComponent();
// Set the Data Context to current instance
this.DataContext = this;
// Instanciate List object
lstItemsSource = new ObservableCollection<ObservableCollection<ChartData>>();
// Instanciate Observable Collections
lineSeries1ItemsSource = new ObservableCollection<ChartData>();
// Add the Observable Collections to the List
lstItemsSource.Add(lineSeries1ItemsSource);
}
图表数据class:
public class ChartData : INotifyPropertyChanged
{
//Event that is raised when the value of some property changes
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Property for XValue of Line Series
/// </summary>
public DateTime XValue
{
get
{
return _xValue;
}
set
{
_xValue = value;
OnPropertyChanged("XValue");
}
}
/// <summary>
/// Property for YValue of Line Series
/// </summary>
public double YValue
{
get
{
return _yValue;
}
set
{
_yValue = value;
OnPropertyChanged("YValue");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}