2011-05-10 63 views
8

我有一些图表,我想动态添加LineSeries没有数据点,只是线条有一些自定义颜色。我发现隐藏的数据点的唯一方法是:wpf工具包线条图没有点和不同的线条颜色

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; 

回答

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

+1太棒了!非常感谢! – Legend

+0

这应该是被接受的答案。 +1帮助我解决问题。这也适用于XAML。 –

相关问题