c# - 如何动态添加任意数量的线系列到WPF工具包图表中?

标签 c# wpf xaml mvvm charts

是否可以在单个工具包图表中绘制多条线,其中线号在运行时确定?我更喜欢使用 MVVM 方式将行绑定(bind)到集合。例如下面的例子,只显示一个“LineSeries”,但如果我想显示多行怎么办?谢谢!

            <ch:Chart.Series>
            <ch:LineSeries Title="{Binding Title}"
                           ItemsSource="{Binding  DataPoints}"
            IndependentValueBinding="{Binding Path=X}"
            DependentValueBinding="{Binding Path=Y}">
            </ch:LineSeries>
        </ch:Chart.Series>

最佳答案

编辑 3 - 添加测试按钮:

XAML:

<Grid>
    <chartingToolkit:Chart x:Name="chart1" HorizontalAlignment="Left" Margin="10,10,0,0" Title="Chart Title" VerticalAlignment="Top" Width="498" Height="277">
    </chartingToolkit:Chart>
    <Button x:Name="button1" Content="ADD" HorizontalAlignment="Center" Margin="226,292,217.4,0" VerticalAlignment="Top" Width="75" Click="button1_Click"/>
</Grid>

窗口:

    int index;
    MyViewModel2 viewModel;

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        viewModel = new MyViewModel2();
        DataContext = viewModel;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        PointCollection pc = new PointCollection();

        for (int i = 1; i <= 10; i++)
            pc.Add(new Point { X = i, Y = i * (2 + index) });

        LineSeries series1 = new LineSeries();
        series1.DependentValuePath = "Y";
        series1.IndependentValuePath = "X";
        series1.ItemsSource = pc;
        chart1.Series.Add(series1);

        viewModel.MyList.Add(pc);

        index++;
    }

enter image description here

编辑 2 - 动态添加系列:

XAML:

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

View 模型:

public class MyViewModel2
{
    public List<PointCollection> MyList { get; set; }

    public MyViewModel2()
    {
        MyList = new List<PointCollection>();
    }
}

窗口:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        viewModel = new MyViewModel2();
        DataContext = viewModel;

        PointCollection pc1 = new PointCollection();
        PointCollection pc2 = new PointCollection();

        for (int i = 1; i <= 10; i++)
        {
            pc1.Add(new Point { X = i, Y = i * 2 });
            pc2.Add(new Point { X = i, Y = i * 3 });
        }

        LineSeries series1 = new LineSeries();
        series1.DependentValuePath = "Y";
        series1.IndependentValuePath = "X";
        series1.ItemsSource = pc1;
        chart1.Series.Add(series1);

        viewModel.MyList.Add(pc1);

        LineSeries series2 = new LineSeries();
        series2.DependentValuePath = "Y";
        series2.IndependentValuePath = "X";
        series2.ItemsSource = pc2;
        chart1.Series.Add(series2);

        viewModel.MyList.Add(pc2);
    }

编辑 1 - 这应该可以让你继续:

XAML:

<Grid>
    <chartingToolkit:Chart Margin="0" Title="Chart Title">
        <chartingToolkit:LineSeries DependentValuePath="Y" IndependentValuePath="X" ItemsSource="{Binding MyPointCollection1}"/>
        <chartingToolkit:LineSeries DependentValuePath="Y" IndependentValuePath="X" ItemsSource="{Binding MyPointCollection2}"/>
    </chartingToolkit:Chart>
</Grid>

View 模型:

public class MyViewModel
{
    public PointCollection MyPointCollection1 { get; set; }
    public PointCollection MyPointCollection2 { get; set; }

    public MyViewModel()
    {
        MyPointCollection1 = new PointCollection();
        MyPointCollection2 = new PointCollection();
    }
}

窗口:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        viewModel = new MyViewModel();
        DataContext = viewModel;

        for (int i = 1; i <= 10; i++)
        {
            viewModel.MyPointCollection1.Add(new Point { X = i, Y = i * 2 });
            viewModel.MyPointCollection2.Add(new Point { X = i, Y = i * 3 });
        }
    }

enter image description here

关于c# - 如何动态添加任意数量的线系列到WPF工具包图表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33743559/

相关文章:

c# - 如何在 WPF 中将绿色 IDataErrorInfo 标记为 true

c# - OpenSSL.Net 创建证书 509x

wpf - WPF TabControl内存问题

xaml - 如何在 UWP XAML NavigationView MenuItems 中混合动态和静态项?

c# - 如何以wpf形式捕捉窗口关闭按钮(窗口右上角的红色X按钮)事件?

WPF 数据网格样式

xaml - 如何在具有 MVVM 体系结构的 WinRT 应用程序中重用 XAML 中的交互行为

c# - 如何重命名图像

c# - 在 web.config 中配置的 UnityContainer

c# - Json.net:如何从 [WebMethod] 返回 JObject?