gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:40:04 24 4
gpt4 key购买 nike

是否可以在运行时确定行号的单个工具包图表中绘制多条线?我更喜欢将行绑定(bind)到集合的 MVVM 方式。例如下面,只有一个“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 - 动态添加 Series:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com