wpf диаграмма с разными цветами? - PullRequest
3 голосов
/ 13 января 2011

Я работаю над графиками инструментария MS и не могу понять, как изменить цвет областей. Мне нужно заполнить диаграмму динамически, а это значит, что я не знаю заранее, сколько разделов будет в диаграмме области.

Вот код, который у меня есть.

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>

Как изменить цвет области a и области b. Прямо сейчас они имеют любой цвет по умолчанию, хотя я установил фон и передний план.

Спасибо.

1 Ответ

8 голосов
/ 13 января 2011

Вы можете использовать свойство 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>
  • Здесь - дополнительная информация.
...