wpf toolkit line chart without points and with different line colors

user606521 picture user606521 · May 10, 2011 · Viewed 10.4k times · Source

I have some chart and I want to add dynamicly LineSeries without DataPoints, just lines with some custom colors. The only way I found to hide data points is:

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,

        };

Unfortunately when I do this all lines become yellow and I can't change their colors. I tried to do this:

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,

        };

But it doesn't help - I can't change line color... Even if I write

series.Background = brush;

Answer

Sangjin Yun picture Sangjin Yun · May 17, 2011

Try this.

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