Chartingtoolkit:饼图系列更新后变色
Chartingtoolkit: Pie series Changes Colors After Updating
我使用饼图来显示我添加到 ObervableCollection 的四个值的分配情况。一切正常,但是当我更新 ObervableCollection 时,颜色的分配发生变化(更新意味着我清除集合并添加新值,这可能会导致问题。但不存在 OCs 的替换方法)。我还为饼系列定义了标准颜色,什么有效,但是分配发生了变化。
这就是我将第一个值填充到绑定到图表的 ObservableCollection 时我的饼图系列的样子:
更新 ObservableCollection 后,它看起来像这样:
如您所见,值位于同一位置。只有分配给值的颜色改变了它们的位置。
这是我填充 ObservableCollection 的地方:
私有 ObservableCollection> _allocationList;
public ObservableCollection<KeyValuePair<string, int>> AllocationList
{
get { return _allocationList; }
set
{
_allocationList = value;
OnPropertyChanged("AllocationList");
}
}
if (AllocationList != null)
{
ObservableCollection<KeyValuePair<string, int>> TempAllocationList = CheckAllocation.GetAllocation(Value1List, Value2List, Value3List, Value4List);
int temp1Value = TempAllocationList[0].Value + AllocationList[0].Value;
int temp2Value = TempAllocationList[1].Value + AllocationList[1].Value;
int temp3Value = TempAllocationList[2].Value + AllocationList[2].Value;
int temp4Value = TempAllocationList[3].Value + AllocationList[3].Value;
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
AllocationList.Clear();
AllocationList.Add(new KeyValuePair<string, int>("Value1", temp1Value));
AllocationList.Add(new KeyValuePair<string, int>("Value2", temp2Value));
AllocationList.Add(new KeyValuePair<string, int>("Value3", temp3Value));
AllocationList.Add(new KeyValuePair<string, int>("Value4", temp4Value));
}));
}
else
{
AllocationList = CheckAllocation.GetAllocation(Value1List, Value2List, Value3List, Value4List);
}
这是我写的XAML:
<chartingToolkit:Chart>
<chartingToolkit:PieSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding AllocationList, UpdateSourceTrigger=PropertyChanged}">
<chartingToolkit:PieSeries.Palette>
<visualizationToolkit:ResourceDictionaryCollection>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="Green"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF045704"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF0AB60A"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF246E24"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF064006"/>
</Style>
</ResourceDictionary>
</visualizationToolkit:ResourceDictionaryCollection>
</chartingToolkit:PieSeries.Palette>
</chartingToolkit:PieSeries >
</chartingToolkit:Chart>
谢谢你帮助我!
你猜对了。这是列表项的清除和重新添加的问题。
一种不明确的解决方案是获取您的项目的索引并将其替换为索引器:
var pvI = AllocationList.Select(x => x.Key).ToList().IndexOf("Photovoltaik");
var pwkI = AllocationList.Select(x => x.Key).ToList().IndexOf("Windkraft");
var bmvI = AllocationList.Select(x => x.Key).ToList().IndexOf("Biomasse");
var wakI = AllocationList.Select(x => x.Key).ToList().IndexOf("Wasserkraft");
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
AllocationList[pvI] = new KeyValuePair<string, int>("Photovoltaik", tempPVValue);
AllocationList[pwkI] = new KeyValuePair<string, int>("Windkraft", tempWKValue);
AllocationList[bmvI] = new KeyValuePair<string, int>("Biomasse", tempBMValue);
AllocationList[wakI] = new KeyValuePair<string, int>("Wasserkraft", tempWaKValue);
}));
我使用饼图来显示我添加到 ObervableCollection 的四个值的分配情况。一切正常,但是当我更新 ObervableCollection 时,颜色的分配发生变化(更新意味着我清除集合并添加新值,这可能会导致问题。但不存在 OCs 的替换方法)。我还为饼系列定义了标准颜色,什么有效,但是分配发生了变化。
这就是我将第一个值填充到绑定到图表的 ObservableCollection 时我的饼图系列的样子:
更新 ObservableCollection 后,它看起来像这样:
如您所见,值位于同一位置。只有分配给值的颜色改变了它们的位置。
这是我填充 ObservableCollection 的地方: 私有 ObservableCollection> _allocationList;
public ObservableCollection<KeyValuePair<string, int>> AllocationList
{
get { return _allocationList; }
set
{
_allocationList = value;
OnPropertyChanged("AllocationList");
}
}
if (AllocationList != null)
{
ObservableCollection<KeyValuePair<string, int>> TempAllocationList = CheckAllocation.GetAllocation(Value1List, Value2List, Value3List, Value4List);
int temp1Value = TempAllocationList[0].Value + AllocationList[0].Value;
int temp2Value = TempAllocationList[1].Value + AllocationList[1].Value;
int temp3Value = TempAllocationList[2].Value + AllocationList[2].Value;
int temp4Value = TempAllocationList[3].Value + AllocationList[3].Value;
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
AllocationList.Clear();
AllocationList.Add(new KeyValuePair<string, int>("Value1", temp1Value));
AllocationList.Add(new KeyValuePair<string, int>("Value2", temp2Value));
AllocationList.Add(new KeyValuePair<string, int>("Value3", temp3Value));
AllocationList.Add(new KeyValuePair<string, int>("Value4", temp4Value));
}));
}
else
{
AllocationList = CheckAllocation.GetAllocation(Value1List, Value2List, Value3List, Value4List);
}
这是我写的XAML:
<chartingToolkit:Chart>
<chartingToolkit:PieSeries DependentValuePath="Value" IndependentValuePath="Key" ItemsSource="{Binding AllocationList, UpdateSourceTrigger=PropertyChanged}">
<chartingToolkit:PieSeries.Palette>
<visualizationToolkit:ResourceDictionaryCollection>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="Green"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF045704"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF0AB60A"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF246E24"/>
</Style>
</ResourceDictionary>
<ResourceDictionary>
<Style x:Key="DataPointStyle" TargetType="Control">
<Setter Property="Background" Value="#FF064006"/>
</Style>
</ResourceDictionary>
</visualizationToolkit:ResourceDictionaryCollection>
</chartingToolkit:PieSeries.Palette>
</chartingToolkit:PieSeries >
</chartingToolkit:Chart>
谢谢你帮助我!
你猜对了。这是列表项的清除和重新添加的问题。 一种不明确的解决方案是获取您的项目的索引并将其替换为索引器:
var pvI = AllocationList.Select(x => x.Key).ToList().IndexOf("Photovoltaik");
var pwkI = AllocationList.Select(x => x.Key).ToList().IndexOf("Windkraft");
var bmvI = AllocationList.Select(x => x.Key).ToList().IndexOf("Biomasse");
var wakI = AllocationList.Select(x => x.Key).ToList().IndexOf("Wasserkraft");
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
AllocationList[pvI] = new KeyValuePair<string, int>("Photovoltaik", tempPVValue);
AllocationList[pwkI] = new KeyValuePair<string, int>("Windkraft", tempWKValue);
AllocationList[bmvI] = new KeyValuePair<string, int>("Biomasse", tempBMValue);
AllocationList[wakI] = new KeyValuePair<string, int>("Wasserkraft", tempWaKValue);
}));