Skip to main content
 首页 » 编程设计

c#之WPF 工具包图表 : how to remove line series data points and still get different line series colors

2024年02月23日10rubylouvre

我使用了 Jim McCurdy 对这个 StackOverflow question 的非常有用的回答以避免在具有多行系列的 WPF 工具包图表中绘制数据点。

但是:如果我不应用 Jim 的 XAML 样式,所有图表线条都会获得不同的颜色(当然是以显示数据点为代价),但是当我应用“无数据点”样式时,线条颜色是不变的,导致图表中的所有线条都具有相同的颜色。

是否可以扩展 XAML 样式,以便图表中每个新线条系列的线条颜色自动不同?

如果我解释这个 StackOverflow question 的答案正确地,问题很容易以编程方式解决,但如果可能的话,我更喜欢基于 XAML 的解决方案。

请您参考如下方法:

要在 XAML 中解决此问题,一种方法是为要分配给 LineSeries 的每种颜色创建不同的 DataPoint 样式。例如,我只是这样做了:

<Style x:Key="InvisibleDataPointBlue" TargetType="{x:Type charting:DataPoint}"> 
    <Setter Property="Background" Value="Blue"/> 
    <Setter Property="Template" Value="{x:Null}"/> 
</Style> 
 
<Style x:Key="InvisibleDataPointRed" TargetType="{x:Type charting:DataPoint}"> 
    <Setter Property="Background" Value="Red"/> 
    <Setter Property="Template" Value="{x:Null}"/> 
</Style> 
 
<Style x:Key="InvisibleDataPointGreen" TargetType="{x:Type charting:DataPoint}"> 
    <Setter Property="Background" Value="Green"/> 
    <Setter Property="Template" Value="{x:Null}"/> 
</Style> 

动态分配新颜色可能有更好的方法,但如果您的需求不大,这就可以了。