更改LineSeries的颜色

本文关键字:颜色 LineSeries 更改 | 更新日期: 2023-09-27 18:08:16

希望有人能帮忙。到目前为止,我花了大约4个小时尝试我在论坛上找到的例子,但无济于事。下面是我的XAML代码;我想会有一个简单的方法来插入一个参数来设置颜色,但我还没有找到一个工作。

我也试过code behind;我找到的所有例子都没有改变什么。

<Grid>
    <charting:Chart Name="lineChart"
                                   Title="Stock" 
                                   VerticalAlignment="Top" 
                                   Margin="0,10,10,0" 
                                   Height="550">
        <charting:LineSeries Name="Price"
                                            Title="Price"  
                                            DependentValuePath="Value" 
                                            IndependentValuePath="Key"
                                            ItemsSource="{Binding [0]}"
                                            IsSelectionEnabled="True"/>
        <charting:LineSeries Name="SMA50" 
                                            Title="50 SMA"  
                                            DependentValuePath="Value" 
                                            IndependentValuePath="Key"
                                            ItemsSource="{Binding [1]}"
                                            IsSelectionEnabled="True"/>
        <charting:LineSeries Name="SMA200" 
                                            Title="200 SMA"  
                                            DependentValuePath="Value" 
                                            IndependentValuePath="Key"
                                            ItemsSource="{Binding [2]}"
                                            IsSelectionEnabled="True"/>
    </charting:Chart>
</Grid>

下面是我调用窗口

的代码
private void bGraph_Click(object sender, RoutedEventArgs e)
{
    Graph g = new Graph();
    g.Show();
    List<KeyValuePair<DateTime, int>> listPrice = new List<KeyValuePair<DateTime, int>>();
    List<KeyValuePair<DateTime, int>> listSMA50 = new List<KeyValuePair<DateTime, int>>();
    List<KeyValuePair<DateTime, int>> listSMA200 = new List<KeyValuePair<DateTime, int>>();
    DateTime d = new DateTime(2000,1,1);
    for (int i = 1; i < 10; i++)
    {
        listPrice.Add(new KeyValuePair<DateTime, int>(d, i));
        listSMA50.Add(new KeyValuePair<DateTime, int>(d, i*2));
        listSMA200.Add(new KeyValuePair<DateTime, int>(d, i * 3));
        d = d.AddDays(1.0);
    }

    var dataSourceList = new List<List<KeyValuePair<DateTime, int>>>();
    dataSourceList.Add(listPrice);
    dataSourceList.Add(listSMA50);
    dataSourceList.Add(listSMA200);
    g.lineChart.DataContext = dataSourceList;

}

任何帮助都会很棒。在我看来,windows窗体版本的图表要比WPF版本简单得多。

更改LineSeries的颜色

您可以使用DataPointStyle属性指定它:

<charting:LineSeries Name="Price"
                    Title="Price"  
                    DependentValuePath="Value" 
                    IndependentValuePath="Key"
                    ItemsSource="{Binding [0]}"
                    IsSelectionEnabled="True"
                    DataPointStyle="{StaticResource myDataPointStyle}" />

在这种情况下,我假设是一个静态资源:

    <Style x:Key="myDataPointStyle" TargetType="{x:Type charting:LineDataPoint}">
        <Setter Property="Background" Value="Blue"/>
    </Style>

如果你需要动态地做到这一点,而不想使用数据绑定,那么你当然也可以通过编程来做到:

var style = new Style();
style.TargetType = typeof(LineDataPoint);
style.Setters.Add(new Setter(BackgroundProperty, Brushes.Blue));
this.Price.DataPointStyle = style;

我没有制作图表的经验,但我愿意帮忙。

有几件事看起来很奇怪。

  1. 您绑定到网格DataContext{binding[0]}中的元素,但它从未设置。

  2. 你正在新建一个图形类,并试图显示它,但它没有以任何方式连接到视图

  3. 这应该在构造函数中工作:(设置窗口的DataContext,因为你的视图与之绑定)

    public MainWindow()
    {
        List<KeyValuePair<DateTime, int>> listPrice = new List<KeyValuePair<DateTime, int>>();
        List<KeyValuePair<DateTime, int>> listSMA50 = new List<KeyValuePair<DateTime, int>>();
        List<KeyValuePair<DateTime, int>> listSMA200 = new List<KeyValuePair<DateTime, int>>();
        DateTime d = new DateTime(2000, 1, 1);
        for (int i = 1; i < 10; i++)
        {
            listPrice.Add(new KeyValuePair<DateTime, int>(d, i));
            listSMA50.Add(new KeyValuePair<DateTime, int>(d, i * 2));
            listSMA200.Add(new KeyValuePair<DateTime, int>(d, i * 3));
            d = d.AddDays(1.0);
        }
    
        var dataSourceList = new List<List<KeyValuePair<DateTime, int>>>();
        dataSourceList.Add(listPrice);
        dataSourceList.Add(listSMA50);
        dataSourceList.Add(listSMA200);
        this.DataContext = dataSourceList;
        InitializeComponent();
    }
    

更新

这改变了描边的宽度,但由于某些原因,颜色没有改变。

    private void bGraph_Click(object sender, RoutedEventArgs e)
    {
        var style = new Style(typeof(Polyline));
        style.Setters.Add(new Setter(Polyline.StrokeProperty, Brushes.Red));
        style.Setters.Add(new Setter(Polyline.StrokeThicknessProperty, 5.0));
        ((LineSeries) lineChart.Series[0]).PolylineStyle = style;
        ((LineSeries) lineChart.Series[1]).PolylineStyle = style;
        ((LineSeries) lineChart.Series[2]).PolylineStyle = style;
    }