wpf toolkit折线图,没有点和不同的线条颜色

时间:2011-05-10 21:16:10

标签: c# wpf wpftoolkit charts

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

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;

1 个答案:

答案 0 :(得分:15)

试试这个。

                    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;
    }