3

我正在寻找使用实时图表构建具有最佳拟合线的散点图。

现在我一直在这样做的方式是SeriesCollection在主视图模型上像这样,在这个接口的类中,我手动为图表添加BubbleSeries和:LineSeries

public interface IPointAnalysisViewModel : IAnalysisViewModel
{
    SeriesCollection Series
    {
        get;
    }
}

点分析视图模型:

foreach (var pointSliceViewModel in this.slices)
{
    this.series.Add(pointSliceViewModel.Series);
}
this.bestFitLineSeries = this.BuildBestFitLine(pointAnalysisModel.BestFitValues);
this.series.Add(this.bestFitLineSeries);

然后这个系列在 xaml 中绑定到Series如下CartesianChart

<wpf:CartesianChart Grid.Column="1"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch"
                        Series="{Binding Path=Series}" >
... UI Fluff
</wpf:CartesianChart>

正如您所期望的那样非常基本,但这意味着在我的系列视图模型中,我知道该视图将为我显示一个气泡系列,如下所示:

public interface IPointSliceViewModel : IViewModel
{
    IBubbleSeriesView Series
    {
        get;
    }

    bool Show
    {
        get;
        set;
    }
}

对我来说,这看起来很像一些代码“气味”,因为我需要知道视图模型中的特定对象。

我能想到的唯一方法是让我能够将 View 和 ViewModel 的关注点分开,如果我能够为 提供CartesianChart数据模板Series并将其绑定Series到一个集合,IPointSliceViewModels如下所示:

<wpf:CartesianChart Grid.Column="1"
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Stretch"
                    SeriesSource="{Binding Path=Series}">
    <wpf:CartesianChart.SeriesTemplates>
        <DataTemplate DataType={x:Type local:PointSliceViewModel}>
            <wpf:BubbleSeries .../>
        </DataTemplate>
    </wpf:CartesianChart.SeriesTemplates>
... UI Fluff
</wpf:CartesianChart>

这只会让我拥有:

public interface IPointAnalysisViewModel : IAnalysisViewModel
{
    ObservableCollection<IPointSliceViewModel> Series
    {
        get;
    }
}

和:

public interface IPointSliceViewModel : IViewModel
{
    ChartValues<ObservablePoint> Values
    {
        get;
    }

    bool Show
    {
        get;
        set;
    }
}

目前是否可以提供DataTemplatesfor theSeries或者我现在必须在后面的代码中手动执行此操作?

或者有没有一种不同的方法可以在图表中拥有无限数量的系列,而不必在视图中定义它们中的每一个,也不必跟踪 VM 中的视图特定细节?

4

0 回答 0