2011-12-20 35 views
3

我有一個餅圖在WPF用戶控件的數據定期更改,但我沒有實例化一個新的圖表控件,每次只是清除ItemsSource中的數據然後插入新的值。如何重置WPF工具包圖表調色板時重置數據

每次值得到刷新時,顏色口感繼續滾動通過其顏色選擇。

圖表顏色選擇總是以相同的顏色選擇開始(首先是紅色,然後是藍色等),我希望能夠告訴圖表重新從頭開始重新選擇顏色從每次我重置數據源,而不是每次清除和重置數據項時都會獲得不同的顏色。

我試着每次創建一個ObservableCollection的新實例,但是沒有任何區別。

回答

4

我有同樣的問題,我找到了另一種解決方案。也許這不是更好的,但它的工作原理。

我在PieSeries上的對象綁定中添加了一個int屬性,它們表示ObservableCollection中元素的索引。 然後,我創建了包含面板的所有默認顏色ResourceDictionaryCollection:

<datavis:ResourceDictionaryCollection x:Key="CouleursGraphique"> 
    <!-- Blue --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FFB9D6F7" /> 
     <GradientStop Color="#FF284B70" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Red --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FFFBB7B5" /> 
     <GradientStop Color="#FF702828" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Light Green --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FFB8C0AC" /> 
     <GradientStop Color="#FF5F7143" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Yellow --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FFFDE79C" /> 
     <GradientStop Color="#FFF6BC0C" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Indigo --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FFA9A3BD" /> 
     <GradientStop Color="#FF382C6C" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Magenta --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FFB1A1B1" /> 
     <GradientStop Color="#FF50224F" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Dark Green --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FF9DC2B3" /> 
     <GradientStop Color="#FF1D7554" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Gray Shade --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FFB5B5B5" /> 
     <GradientStop Color="#FF4C4C4C" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Blue --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FF98C1DC" /> 
     <GradientStop Color="#FF0271AE" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Brown --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FFC1C0AE" /> 
     <GradientStop Color="#FF706E41" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Cyan --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FFADBDC0" /> 
     <GradientStop Color="#FF446A73" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Special Blue --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FF2F8CE2" /> 
     <GradientStop Color="#FF0C3E69" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Gray Shade 2 --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FFDCDCDC" /> 
     <GradientStop Color="#FF757575" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Gray Shade 3 --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FFF4F4F4" /> 
     <GradientStop Color="#FFB7B7B7" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    <!-- Gray Shade 4 --> 
    <ResourceDictionary> 
     <RadialGradientBrush x:Key="Background" GradientOrigin="-0.1,-0.1" Center="0.075,0.015" RadiusX="1.05" RadiusY="0.9"> 
     <GradientStop Color="#FFF4F4F4" /> 
     <GradientStop Color="#FFA3A3A3" Offset="1" /> 
     </RadialGradientBrush> 
    </ResourceDictionary> 
    </datavis:ResourceDictionaryCollection> 

我補充說,返回ResourceDictionaryCollection的指定索引處的顏色轉換器。

/// <summary> 
    /// Convertit une valeur. 
    /// </summary> 
    /// <param name="value">Valeur produite par la source de liaison.</param> 
    /// <param name="targetType">Type de la propriété de cible de liaison.</param> 
    /// <param name="parameter">Paramètre de convertisseur à utiliser.</param> 
    /// <param name="culture">Culture à utiliser dans le convertisseur.</param> 
    /// <returns> 
    /// Une valeur convertie. Si la méthode retourne null, la valeur Null valide est utilisée. 
    /// </returns> 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     ResourceDictionaryCollection listeCouleurs = parameter as ResourceDictionaryCollection; 
     int indice = (int)value; 

     return listeCouleurs[indice % listeCouleurs.Count]["Background"]; 
    } 

,我用在XAML所有這些元素是這樣的:

<charting:Chart.Series> 
<charting:PieSeries ItemsSource="{Binding Path=Donnees}" 
        DependentValuePath="Valeur" 
        IndependentValuePath="Libelle"> 
    <charting:PieSeries.Palette> 
    <datavis:ResourceDictionaryCollection> 
     <ResourceDictionary> 
     <Style x:Key="DataPointStyle" TargetType="Control"> 
      <Setter Property="Background" Value="{Binding Path=Index, Converter={StaticResource convCouleur}, ConverterParameter={StaticResource CouleursGraphique}}"/> 
     </Style> 
     </ResourceDictionary> 
    </datavis:ResourceDictionaryCollection> 
    </charting:PieSeries.Palette> 
</charting:PieSeries> 

我希望回答可以幫助你。

+0

你能解釋一下如何將Index附加到對象上嗎?我正在使用一個字典,並不理解如何以有意義的方式訪問索引。 – markokstate 2016-12-07 22:31:11

1

如果您不想向數據和轉換器添加其他屬性,則可以使用反射。

爲了便於數據表示,wpftoolkit數據系列有一個可繼承的接口,每次調用時都要提供一組不同的資源字典。每個DataPointSeries中的ResourceDictionaryDispenser都從Chart對象中獲得它自己的枚舉器。

太糟糕了ResourceDictionaryDispenser屬性Chart是私有的,它的Reset()方法也是私有的。您可以使用下面的代碼,雖然:

Type t = typeof(Chart); 

PropertyInfo fResDispenser = t.GetProperty("ResourceDictionaryDispenser", 
    BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty); 

resDispenser = fResDispenser.GetValue(chart, null); 

ResDispenserReset = resDispenser.GetType().GetMethod("Reset", 
    BindingFlags.NonPublic | BindingFlags.Instance); 

ResDispenserReset.Invoke(resDispenser, null); 

一旦你存儲的方法和資源分配的參考,你應該避免這個系列的ItemsSource直接綁定您的數據源。攔截,而不是你的數據更新(可能與INotifyPropertyChanged.PropertyChanged事件),像這樣:

pieDataSeries.ItemsSource = null; 
ResDispenserReset.Invoke(resDispenser, null); 
pieDataSeries.ItemsSource = [new data set]; 

測試,100%的工作。

的XAML是這樣的:

<UserControl x:Class="Datamanager.Widgets.SubunitsPieChart" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:gra="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"> 
    <gra:Chart Margin="5" 
       x:Name="chart" 
       HorizontalAlignment="Stretch" 
       VerticalAlignment="Stretch" 
       BorderThickness="0"> 
     <gra:PieSeries x:Name="chartSeries" 
         DependentValuePath="Value" 
         IndependentValuePath="Key" /> 
    </gra:Chart> 
</UserControl> 

你應該添加這些using太遲了。

using System.Reflection; 
using System.Windows.Controls.DataVisualization; 
using System.Windows.Controls.DataVisualization.Charting;