WPF 工具包折线图没有点,具有不同的折线颜色

本文关键字:颜色 折线 工具包 折线图 WPF | 更新日期: 2023-09-27 17:56:17

我有一些图表,我想动态添加没有数据点的线系列,只是带有一些自定义颜色的线。我发现隐藏数据点的唯一方法是:

Style style = new Style(typeof(LineDataPoint));
style.Setters.Add(new Setter(LineDataPoint.TemplateProperty, null));
var series = new LineSeries()
{
      Title = name,
      DependentValuePath = "Y",
      IndependentValuePath = "X",
      ItemsSource = new ObservableCollection<FloatingPoint>(),
      DataPointStyle = style,
        };

不幸的是,当我这样做时,所有线条都会变黄,我无法更改它们的颜色。我试图这样做:

Style style = new Style(typeof(LineDataPoint));
        style.Setters.Add(new Setter(LineDataPoint.TemplateProperty, null));
        SolidColorBrush brush = new SolidColorBrush(Colors.Red);
        var series = new LineSeries()
        {
            Title = name,
            DependentValuePath = "Y",
            IndependentValuePath = "X",
            ItemsSource = new ObservableCollection<FloatingPoint>(),
            DataPointStyle = style,
            Background = brush,
        };

但这无济于事 - 我无法更改线条颜色...即使我写

series.Background = brush;

WPF 工具包折线图没有点,具有不同的折线颜色

试试这个。

                    series = new LineSeries();
                    Style dataPointStyle = GetNewDataPointStyle();
                    series.DataPointStyle = dataPointStyle;


    /// <summary>
    /// Gets the new data point style.
    /// </summary>
    /// <returns></returns>
    private static Style GetNewDataPointStyle()
    {
        Color background = Color.FromRgb((byte)random.Next(255), 
                                         (byte)random.Next(255), 
                                         (byte)random.Next(255));
        Style style = new Style(typeof(DataPoint));
        Setter st1 = new Setter(DataPoint.BackgroundProperty, 
                                    new SolidColorBrush(background));
        Setter st2 = new Setter(DataPoint.BorderBrushProperty, 
                                    new SolidColorBrush(Colors.White));
        Setter st3 = new Setter(DataPoint.BorderThicknessProperty, new Thickness(0.1));
        Setter st4 = new Setter(DataPoint.TemplateProperty, null);
        style.Setters.Add(st1);
        style.Setters.Add(st2);
        style.Setters.Add(st3);
        style.Setters.Add(st4);
        return style;
    }