2011-01-13 48 views
2

我一直在思考MS工具包图表,无法弄清楚如何改变区域的颜色。我需要动态填充图表,这意味着我不会提前知道面积图的多少部分。wpf区域图具有不同的颜色?

这是我的代码。

var a = new AreaSeries 
{ 
    Title = "a", 
    IndependentValuePath = "Key", 
    DependentValuePath = "Value", 
    Background = Brushes.Plum 
}; 

我试图改变前景和背景,没有骰子。

mcChart.Series.Add(a); 

a = new AreaSeries 
{ 
    Title = "b", 
    IndependentValuePath = "Key", 
    DependentValuePath = "Value", 
    Background = Brushes.Peru 
}; 

mcChart.Series.Add(a); 

填写图表。

((AreaSeries)mcChart.Series[0]).ItemsSource = new[] 
{ 
    new KeyValuePair<string, int>("1", 100), 
    new KeyValuePair<string, int>("2", 180), 
    new KeyValuePair<string, int>("3", 110), 
    new KeyValuePair<string, int>("4", 95), 
    new KeyValuePair<string, int>("5", 40), 
    new KeyValuePair<string, int>("6", 95) 
}; 

((AreaSeries)mcChart.Series[1]).ItemsSource = new[] 
{ 
    new KeyValuePair<string, int>("1", 150), 
    new KeyValuePair<string, int>("2", 280), 
    new KeyValuePair<string, int>("3", 310), 
    new KeyValuePair<string, int>("4", 195), 
    new KeyValuePair<string, int>("5", 340), 
    new KeyValuePair<string, int>("6", 195) 
}; 

我是新来的wpf,我无法弄清楚这有什么问题。

这里是XAML

<chartingToolkit:Chart 
    Width="600" Height="450" 
    Name="mcChart" 
    Background="LightBlue" 
    Foreground="DarkBlue" 
    Title="Area Chart">     
</chartingToolkit:Chart> 

如何更改区域和区域B的颜色。现在,即使我设置了背景和前景,它们仍然是默认颜色。

谢谢。

回答

7

可以使用Chart.Palette属性是这样的:

<Grid> 
    <charting:Chart> 
     <charting:Chart.Palette> 
      <visualizationToolkit:ResourceDictionaryCollection> 
       <ResourceDictionary> 
        <Style x:Key="DataPointStyle" TargetType="Control"> 
         <Setter Property="Background" Value="MistyRose"/> 
        </Style> 
       </ResourceDictionary> 
       <ResourceDictionary> 
        <Style x:Key="DataPointStyle" TargetType="Control"> 
         <Setter Property="Background" Value="AliceBlue"/> 
        </Style> 
       </ResourceDictionary> 
      </visualizationToolkit:ResourceDictionaryCollection> 
     </charting:Chart.Palette> 
     <charting:AreaSeries Title="Series 1"/> 
     <charting:AreaSeries Title="Series 2"/> 
    </charting:Chart> 
</Grid> 
  • Here是一些更多的信息。
+0

谢谢你的链接,但你如何通过代码做到这一点?直到我从数据库中提取数据之前,我不知道有多少个系列。我必须动态地做到这一点。有任何想法吗?再次感谢你。 – nitefrog 2011-01-13 07:09:40