如何从WinRT XAML工具箱图表中设置和获取线条系列颜色

如何从WinRT XAML工具箱图表中设置和获取线条系列颜色,xaml,windows-8,charts,windows-runtime,winrt-xaml-toolkit,Xaml,Windows 8,Charts,Windows Runtime,Winrt Xaml Toolkit,回答 谢谢菲利普,我终于找到了设置颜色的方法。我只需要在DataPointStyle中添加Background属性。我把我的答案贴在这里。还找到了如何修改默认工具提示的方法 问题1 我正在图表中创建多个折线图系列。现在WinRT XAML工具包以随机方式为每个系列分配颜色。我对数据点使用自定义样式,所以当我使用自定义样式时,颜色的随机性会消失。那么我如何设置或获取序列的随机颜色呢?如果我可以得到颜色,那么我可以在数据点中使用该颜色,如果我可以设置颜色,那么我将自己生成随机颜色 问题2

回答

谢谢菲利普,我终于找到了设置颜色的方法。我只需要在
DataPointStyle
中添加
Background
属性。我把我的答案贴在这里。还找到了如何修改默认工具提示的方法



问题1

我正在图表中创建多个折线图系列。现在WinRT XAML工具包以随机方式为每个系列分配颜色。我对数据点使用自定义样式,所以当我使用自定义样式时,颜色的随机性会消失。那么我如何设置或获取序列的随机颜色呢?如果我可以得到颜色,那么我可以在数据点中使用该颜色,如果我可以设置颜色,那么我将自己生成随机颜色

问题2

此外,当鼠标悬停在数据点上时,工具提示会显示相关值,但我想显示更多细节,如何实现这一点

这是我的自定义样式代码

<charting:Chart x:Name="LineChart" Title="Line Chart" Margin="70,0">
    <charting:LineSeries
                Title="Population 1"
                IndependentValueBinding="{Binding Name}"
                DependentValueBinding="{Binding Value}"
                IsSelectionEnabled="True">
            <charting:LineSeries.DataPointStyle>
                <Style TargetType="charting:LineDataPoint">
                    <Setter Property="Width" Value="17" />
                    <Setter Property="Height" Value="17" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="charting:LineDataPoint">
                                <Ellipse Fill="Green" Stroke="Green" StrokeThickness="3" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </charting:LineSeries.DataPointStyle>
        </charting:LineSeries>

    <charting:LineSeries
            Title="Population 2"
            IndependentValueBinding="{Binding Name}"
            DependentValueBinding="{Binding Value}"
            IsSelectionEnabled="True" Foreground="Blue">
        <charting:LineSeries.DataPointStyle>
            <Style TargetType="charting:LineDataPoint">
                <Setter Property="Width" Value="17" />
                <Setter Property="Height" Value="17" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="charting:LineDataPoint">
                            <Ellipse Fill="Red" Stroke="Red" StrokeThickness="3" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </charting:LineSeries.DataPointStyle>
    </charting:LineSeries>
</charting:Chart>

随机颜色图表(无自定义数据点样式)

没有随机颜色的图表(具有自定义数据点样式)[您可以看到两条线都是黄色]

尝试Bing获取一些可能有用的页面,如。它与WinRT XAML工具包的工作原理基本相同,但在自定义基于Silverlight的模板时,您需要自定义基于WinRT XAML工具包的模板,您可以尝试使用Blend/Visual Studio designer提取这些模板,也可以从源代码获取原始模板

<charting:Chart x:Name="LineChart" Title="Line Chart" Margin="70,0">
    <charting:LineSeries
                Title="Population 1"
                IndependentValueBinding="{Binding Name}"
                DependentValueBinding="{Binding Value}"
                IsSelectionEnabled="True">
            <charting:LineSeries.DataPointStyle>
                <Style TargetType="charting:LineDataPoint">
                    <Setter Property="Width" Value="17" />
                    <Setter Property="Height" Value="17" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="charting:LineDataPoint">
                                <Ellipse Fill="Green" Stroke="Green" StrokeThickness="3" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </charting:LineSeries.DataPointStyle>
        </charting:LineSeries>

    <charting:LineSeries
            Title="Population 2"
            IndependentValueBinding="{Binding Name}"
            DependentValueBinding="{Binding Value}"
            IsSelectionEnabled="True" Foreground="Blue">
        <charting:LineSeries.DataPointStyle>
            <Style TargetType="charting:LineDataPoint">
                <Setter Property="Width" Value="17" />
                <Setter Property="Height" Value="17" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="charting:LineDataPoint">
                            <Ellipse Fill="Red" Stroke="Red" StrokeThickness="3" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </charting:LineSeries.DataPointStyle>
    </charting:LineSeries>
</charting:Chart>