WPF图表工具包-Multiple Scatter系列-Data Binding

WPF charting tool kit - Multiple Scatter series - Data Binding

我能够通过 WPF 图表工具包显示一个名为 FinalResult 的散点序列。

 public List<KeyValuePair<double,double>> FinalResult
    {
        get
        {
            return _finalResult;
        }
        set
        {
            if (_finalResult == value) return;
            _finalResult = value;
            OnPropertyChanged("FinalResult");
        }
    }
    public List<KeyValuePair<double, double>> _finalResult;

在 Xaml 中,我只是将此 属性 称为:

<chartingToolkit:Chart Name="lineChart" Background = "White" Title="CDF - Charting Kit">
            <chartingToolkit:ScatterSeries DependentValuePath="Value" IndependentValuePath="Key"  IsSelectionEnabled="True" 
                                               ItemsSource="{Binding FinalResult}" />

            <chartingToolkit:Chart.Axes>
                <chartingToolkit:LinearAxis Orientation="Y" ShowGridLines="True" />

                <chartingToolkit:LinearAxis Orientation="X" ShowGridLines="True" />

            </chartingToolkit:Chart.Axes>
        </chartingToolkit:Chart>

现在下一步是显示多个散点序列。我以为我可以像这样使用观察收集并在 XAML 中使用那个 属性 但现在我看不到任何图表。这是相关代码:

public class CDFPlot
    {
       public List<KeyValuePair<double, double>> FinalResult1 { get; set; }
    }
    ObservableCollection<CDFPlot> CDFresults = new ObservableCollection<CDFPlot>();
    public ObservableCollection<CDFPlot> CDFResults
    {
        get { return CDFresults; }
        set
        {
            CDFresults = value;
            base.OnPropertyChanged("StringList");
        }
    }

然后在 xaml 端,我绑定到 "FinalResult1"。

 <chartingToolkit:Chart Name="lineChart" Background = "White" Title="CDF - Charting Kit">
            <chartingToolkit:ScatterSeries DependentValuePath="Value" IndependentValuePath="Key"  IsSelectionEnabled="True" 
                                               ItemsSource="{Binding FinalResult1}" />

            <chartingToolkit:Chart.Axes>
                <chartingToolkit:LinearAxis Orientation="Y" ShowGridLines="True" />

                <chartingToolkit:LinearAxis Orientation="X" ShowGridLines="True" />

            </chartingToolkit:Chart.Axes>
        </chartingToolkit:Chart>

当我 运行 调试时,我可以看到填充了 CDFPLOT.FinalResult1。 我是否正确绑定它?请告知我该如何解决?

到目前为止我尝试过的另一种解决方法是在代码中构建图表,而不是通过 xaml..

 private void showChart()
    {
        CDF_PAnalysisPluginModel model = DataContext as CDF_PAnalysisPluginModel;

        LineSeries lineSeries1 = new LineSeries();

        lineSeries1.Title = "Title";
        lineSeries1.DependentValuePath = "Value";
        lineSeries1.IndependentValuePath = "Key";

            foreach (var x in model.CDFResults)
            {

                lineSeries1.ItemsSource = x.FinalResult1;

             lineChart.Series.Add(lineSeries1);  // error on this line

            }

    } 

和xaml是:

<chartingToolkit:Chart Name="lineChart" Background = "White" Title="CDF - Charting Kit">
</chartingToolkit:Chart>

但如前所述,我在该行出现错误。

它有效,假设 DataContext 已正确分配,如下所示:

<Window.DataContext>
    <local:CDFPlot/>
</Window.DataContext>
<Grid>
    <chartingToolkit:Chart Name="lineChart" Background = "White" Title="CDF - Charting Kit">
        <chartingToolkit:ScatterSeries DependentValuePath="Value" IndependentValuePath="Key"  IsSelectionEnabled="True" 
                                           ItemsSource="{Binding FinalResult1}" />

        <chartingToolkit:Chart.Axes>
            <chartingToolkit:LinearAxis Orientation="Y" ShowGridLines="True" />

            <chartingToolkit:LinearAxis Orientation="X" ShowGridLines="True" />

        </chartingToolkit:Chart.Axes>
    </chartingToolkit:Chart>
</Grid>

现在,请注意 FinalResult1 只能保存一个 ScatterSeries 的数据,而不是您想要的多个。

编辑:这就是我使用您的初始代码添加多个系列的方式:

XAML:

<Grid>
    <Grid.DataContext>
        <local:CDFViewModel/>
    </Grid.DataContext>
    <chartingToolkit:Chart x:Name="chart1" Margin="0" Title="Chart Title">
        <chartingToolkit:ScatterSeries DependentValuePath="Y" IndependentValuePath="X" ItemsSource="{Binding CDFPlotCollection[0]}">
        </chartingToolkit:ScatterSeries>
        <chartingToolkit:ScatterSeries DependentValuePath="Y" IndependentValuePath="X" ItemsSource="{Binding CDFPlotCollection[1]}">
        </chartingToolkit:ScatterSeries>
    </chartingToolkit:Chart>
</Grid>

视图模型:

public class CDFViewModel
{
    public ObservableCollection<CDFPlot> CDFPlotCollection { get; set; }

    public CDFViewModel()
    {
        Random r = new Random();
        CDFPlotCollection = new ObservableCollection<CDFPlot>();

        CDFPlotCollection.Add(new CDFPlot(r, 0));
        CDFPlotCollection.Add(new CDFPlot(r, 2));
    }
}

型号:

public class CDFPlot : ObservableCollection<Point>
{
    public CDFPlot(Random r, double delta)
    {
        for (int i = 0; i < 50; i++)
            Add(new Point { X = i, Y = delta+r.NextDouble() });
    }
}

编辑 2:或者您可以使用后面的代码获得相同的结果:

XAML:

<chartingToolkit:Chart x:Name="chart1" Margin="0" Title="Chart Title">
    </chartingToolkit:Chart>

Window.cs:

     private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        CDFViewModel model = new CDFViewModel();
        ScatterSeries series;

        for (int i = 0; i < model.CDFPlotCollection.Count; i++)
        {
            series = new ScatterSeries();
            series.DependentValuePath = "Y";
            series.IndependentValuePath = "X";
            series.ItemsSource = model.CDFPlotCollection[i];
            chart1.Series.Add(series);
        }
    }