我在 WPF 用户控件上有一个饼图,其数据定期更改,但是我不是每次都实例化一个新的图表控件,只是清除 ItemsSource 中的数据,然后插入新值。
每次刷新值时,色觉都会继续滚动其颜色选择。
图表颜色选择总是以相同的颜色选择开始(首先是红色,然后是蓝色等),我希望能够在每次重置数据源时告诉图表从头开始重新启动它的颜色选择,而不是获取每次我清除和重置数据项时都会有不同的颜色。
我每次都尝试创建 ObservableCollection 的新实例,但这没有任何区别。
最佳答案
我遇到了同样的问题,我找到了另一个解决方案。也许它不是更好的,但它有效。
我向 PieSeries 上的对象绑定(bind)添加了一个 int 属性,它表示 ObservableCollection 中元素的索引。
然后我创建了一个 ResourceDictionaryCollection,其中包含 Palette 的所有默认颜色:
<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>
我希望这个答案可以帮助你。
关于wpf - 重置数据时如何重置 WPF Toolkit 图表调色板,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8570850/