【问题标题】:Removing (collapsing) DataPoints in a LineSeries?删除(折叠)LineSeries 中的数据点?
【发布时间】:2010-02-05 08:38:21
【问题描述】:

我想从我的 Silverlight 图表中的 LineSeries 中删除数据点标记。我在网上找到的唯一方法是将 VisibilityProperty 设置为 Collapse。

//在当前的 SL 工具包版本中不起作用 var collapseDataPointSetter = new Setter(Control.VisibilityProperty, Visibility.Collapsed);

但这不适用于当前版本的 SL 工具包。如何在当前版本中删除或隐藏数据点标记?

【问题讨论】:

    标签: silverlight charts silverlight-toolkit


    【解决方案1】:

    潘塔尔黑,

    使用以下图表样式(带有引用的模板)来隐藏数据点。我已经包含了 LineSeries 和 AreaSeries 的样式。

    祝你好运,吉姆

    <ControlTemplate x:Key="CommonAreaSeriesDataPointTemplate" TargetType="charting:AreaDataPoint">
        <!--Comment out data points from the default template; just an empty template-->
        <Grid x:Name="Root" Opacity="1">
            <!--<ToolTipService.ToolTip>
                <StackPanel Margin="2,2,2,2">
                    <ContentControl Content="{TemplateBinding FormattedDependentValue}" />
                    <ContentControl Content="{TemplateBinding FormattedIndependentValue}" />
                </StackPanel>
            </ToolTipService.ToolTip>
            <Ellipse StrokeThickness="{TemplateBinding BorderThickness}" Stroke="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}" />-->
        </Grid>
    </ControlTemplate>
    <Style x:Key="CommonAreaSeriesDataPoint" TargetType="charting:AreaDataPoint">
        <Setter Property="Background" Value="{StaticResource CommonAreaSeriesBackground}" />
        <Setter Property="Template" Value="{StaticResource CommonAreaSeriesDataPointTemplate}" />
    </Style>
    <Style x:Key="CommonAreaSeriesPath" TargetType="Path">
        <Setter Property="StrokeThickness" Value="1" />
        <Setter Property="Stroke" Value="DarkGray" />
        <Setter Property="Effect" Value="{StaticResource DialogDropShadow}" />
    </Style>
    <ControlTemplate x:Key="CommonLineSeriesDataPointTemplate" TargetType="charting:LineDataPoint">
        <!--Comment out data points from the default template; just an empty template-->
        <Grid x:Name="Root" Opacity="1">
            <!--<ToolTipService.ToolTip>
                <StackPanel Margin="2,2,2,2">
                    <ContentControl Content="{TemplateBinding FormattedDependentValue}" />
                    <ContentControl Content="{TemplateBinding FormattedIndependentValue}" />-->
            <!--Example of how to access the bound business object-->
            <!--<ContentControl Content="{Binding Amount}" DataContext="{TemplateBinding DataContext}" />-->
            <!--</StackPanel>
            </ToolTipService.ToolTip>-->
            <!--<Ellipse StrokeThickness="{TemplateBinding BorderThickness}" Stroke="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}" />-->
        </Grid>
    </ControlTemplate>
    <Style x:Key="CommonLineSeriesDataPoint" TargetType="charting:LineDataPoint">
        <Setter Property="IndependentValueStringFormat" Value="{}{0:yyyy}" />
        <Setter Property="DependentValueStringFormat" Value="{}{0:c0}" />
        <Setter Property="Background" Value="#FF0077CC" />
        <Setter Property="BorderBrush" Value="White" />
        <Setter Property="BorderThickness" Value="2" />
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Template" Value="{StaticResource CommonLineSeriesDataPointTemplate}" />
    </Style>
    <Style x:Key="CommonLineSeriesPolyline" TargetType="Polyline">
        <Setter Property="StrokeThickness" Value="5" />
        <Setter Property="Effect" Value="{StaticResource DialogDropShadow}" />
    </Style>
    <!-- Implicit non-Key'd Styles BasedOn Common Explicit Key'd Styles above -->
    <Style TargetType="charting:AreaSeries">
        <Setter Property="DataPointStyle" Value="{StaticResource CommonAreaSeriesDataPoint}" />
        <Setter Property="PathStyle" Value="{StaticResource CommonAreaSeriesPath}" />
    </Style>
    <Style TargetType="charting:LineSeries">
        <Setter Property="DataPointStyle" Value="{StaticResource CommonLineSeriesDataPoint}" />
        <Setter Property="PolylineStyle" Value="{StaticResource CommonLineSeriesPolyline}" />
    </Style>
    

    【讨论】:

    • 谢谢。会试试的。但似乎是我正在寻找的。​​span>
    • 在 WPF 中也能像魅力一样工作......非常感谢。
    • 非常感谢@Jim 提供上面的代码。不过,我使用这种方法缺少的一件事是 LineSeries 中的线条颜色固定为图表中所有系列的 LineDataPoint 样式中设置的背景值。删除背景设置器也无济于事,它只是一种不同的修复颜色。有没有办法通过上述方法使不同线系列的线颜色自动不同?
    • @Anders,如果您注释掉 CommonAreaSeriesDataPoint 中的 Setter for Background 属性,它可能会起作用。或者可能将 Background 属性绑定到另一个 Brush 元素
    【解决方案2】:

    在我看来,使用样式并不是最好的方法,因为当您还拥有大量数据点(例如股票图表)时,您仍然拥有大量的视觉效果。

    public class LineSeriesEx : LineSeries
    {
        protected override DataPoint CreateDataPoint()
        {
            return new EmptyDataPoint();
        }
    }
    
    public class EmptyDataPoint : DataPoint
    {
        // As the method name says, this DataPoint is empty.
    }
    

    这样做,您的视觉效果几乎比您设置一些样式时少五倍。

    【讨论】:

    • 与触摸 XAML 相比,这是一个简单的解决方案。
    【解决方案3】:

    我使用了 Jim 的解决方案(顺便说一句,非常感谢你,那里提供了巨大的帮助)并将其应用于默认图表模板。

    在调色板区域中,您可以找到系列中每一行的资源字典。

    这是我使用 Jim 的控件模板摆脱它的方法,我可以将它放在每个 ResourceDictonary 中,这样我就不必逐行进行

    <toolkit:ResourceDictionaryCollection>
    <ResourceDictionary>
    <!-- I wanted a solid color brush so I just went ahead and defined it in the palette-->
    <SolidColorBrush x:Key="Background" Color="Green"/>
    <Style x:Key="DataPointStyle" TargetType="Control">
    <Setter Property="Background" Value="{StaticResource Background}"/>
    <!-- below is where I entered Jim's control template into the default palette defined-->
    <Setter Property="Template">
    <ControlTemplate TargetType="charting:LineDataPoint">
    <Grid x:Name="Root" Opacity="1"/>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>
    </ResourceDictionary>
    </toolkit:ResourceDictionaryCollection>
    

    这至少对我有用,它会为我节省很多时间(并且在我拔掉头发之前已经节省了很多头发)

    【讨论】:

      【解决方案4】:
      <charting:LineSeries.DataPointStyle>
                                  <Style TargetType="charting:LineDataPoint">
                                      <Setter Property="Visibility" Value="Collapsed"/>
                                      <Setter Property="Background" Value="violet"/>
                                      <Setter Property="Opacity" Value="0" />
                                  </Style>
                              </charting:LineSeries.DataPointStyle>
      

      【讨论】:

        猜你喜欢
        • 2013-06-20
        • 2015-12-10
        • 1970-01-01
        • 2016-02-04
        • 2015-03-26
        • 2021-07-21
        • 2022-12-15
        • 1970-01-01
        • 2016-06-01
        相关资源
        最近更新 更多