重置数据时如何重置WPF工具包图表调色板

发布于 2024-12-22 04:27:55 字数 265 浏览 0 评论 0原文

我在 WPF 用户控件上有一个饼图,其数据定期更改,但是我并不是每次都实例化新的图表控件,只是清除 ItemsSource 中的数据,然后插入新值。

每次刷新值时,调色板都会继续滚动其颜色选择。

图表颜色选择总是以相同的颜色选择开始(首先是红色,然后是蓝色等),我希望能够告诉图表在每次重置数据源时从头开始重新启动其颜色选择,而不是获取每次我清除并重置数据项时都会有不同的颜色。

我每次都尝试创建 ObservableCollection 的新实例,但这没有任何区别。

I have a pie chart on a WPF user control whose data changes periodically, however I am not instantiating a new chart control each time, just clearing the data in the ItemsSource and then inserting new values.

Each time the values get refreshed, the colour palate continues rolling through its colour selections.

The chart colour selection always starts off with the same colour selections (first is red, then blue etc), I would like to be able to tell the chart to restart its colour selection from scratch each time I reset the data source, instead of getting different colours everytime I clear the and reset the data items.

I tried creating a new instance of the ObservableCollection each time but that did not make any difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月隐月明月朦胧 2024-12-29 04:27:55

我遇到了同样的问题,我找到了另一个解决方案。也许这不是更好的方法,但它确实有效。

我向 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>

我希望这个答案可以帮助你。

I've got the same problem, and I found another solution. Maybe it's not the better one, but it works.

I added an int property to my object binding on the PieSeries, who represent the element's index in the ObservableCollection.
Then I created a ResourceDictionaryCollection which contains all the default colors of the 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>

I added a converter that returns the color at the specified index of 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"];
    }

And i used all this elements in the Xaml like this:

  <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>

I hope that answer can help you.

原野 2024-12-29 04:27:55

如果您不想向数据和转换器添加其他属性,则可以使用反射。

为了帮助轻松地呈现数据,wpftoolkit 数据系列有一个可继承的 ResourceDictionaryDispenser ,负责在每次调用时提供一组不同的资源字典。每个DataPointSeries 中的ResourceDictionaryDispenserChart 对象中获取自己的枚举器。

不幸的是,Chart 中的 ResourceDictionaryDispenser 属性是私有的,它的 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;

If you don't want to add another property to your data and a converter, you can use reflection.

To aid in easy data presentation, wpftoolkit data series have an inheritable ResourceDictionaryDispenser in charge with providing a different set of resource dictionaries every time it's called. The ResourceDictionaryDispenser in each DataPointSeries gets its own enumerators from the one in the Chart object.

Too bad the ResourceDictionaryDispenser property in Chart is private, as is its Reset() method. You can use the following code, though:

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);

Once you stored a reference to the method and the resource dispenser, you should avoid direct binding your data source with the series' ItemsSource. Intercept instead your data update (maybe with the INotifyPropertyChanged.PropertyChanged event) and act like this:

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

tested and 100% working.

The XAML is like this:

<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>

you should add these usings too.

using System.Reflection;
using System.Windows.Controls.DataVisualization;
using System.Windows.Controls.DataVisualization.Charting;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文