2011-05-10 59 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。 –

相關問題