【问题标题】:Wpf Toolkit Chart Invert HighlightingWpf Toolkit 图表反转突出显示
【发布时间】:2011-06-15 19:48:27
【问题描述】:

如何在使用 wpf 工具包创建的图表中突出显示其他部分(列、条形等)。我正在使用控件模板来设置我自己的图表的样式。到目前为止,我使用触发器来对鼠标所在的元素产生淡化效果。我想反转这个;淡化鼠标未指向的其他元素(一种流行的图表视觉噱头)。下图显示了选定的列 Faded,我希望它是相反的。

【问题讨论】:

    标签: wpf wpftoolkit charts


    【解决方案1】:

    只需将默认值设置为淡入淡出并使用触发器将其设置为完全不透明。您已经完成了一些其他样式,但这里是一个基于默认样式的示例:

    <Grid>
        <Grid.Resources>
            <PointCollection x:Key="sampleData">
                <Point>1,20</Point>
                <Point>2,40</Point>
                <Point>3,30</Point>
            </PointCollection>
            <Style x:Key="dimEffectStyle" TargetType="{x:Type charting:ColumnDataPoint}" BasedOn="{StaticResource {x:Type charting:ColumnDataPoint}}">
                <Setter Property="Opacity" Value="0.25"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.25" Duration="0:0:0.1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
        <charting:Chart>
            <charting:ColumnSeries
                Title="A"
                ItemsSource="{StaticResource sampleData}"
                IndependentValueBinding="{Binding X}"
                DependentValueBinding="{Binding Y}"
                DataPointStyle="{StaticResource dimEffectStyle}"
                />
        </charting:Chart>
    </Grid>
    

    编辑:

    如果您想更改除鼠标悬停的数据点之外的所有其他数据点,这有点困难,并且不能简单地通过重新设置控件来完成。但是您可以创建自己的具有该功能的系列控件。这是一个名为MouseNotOverColumnSeries 的无样式列系列类的图表,带有一个新的MouseNotOverOpacity 属性:

        <Grid.Resources>
            <PointCollection x:Key="sampleData">
                <Point>1,20</Point>
                <Point>2,40</Point>
                <Point>3,30</Point>
            </PointCollection>
        </Grid.Resources>
        <charting:Chart Name="chart1">
            <local:MouseNotOverColumnSeries
                Title="A"
                ItemsSource="{StaticResource sampleData}"
                IndependentValueBinding="{Binding X}"
                DependentValueBinding="{Binding Y}"
                MouseNotOverOpacity="0.5"
                />
        </charting:Chart>
    

    这是MouseNotOverColumnSeries 类:

    public class MouseNotOverColumnSeries : ColumnSeries
    {
        public double MouseNotOverOpacity { get; set; }
    
        protected override void OnDataPointsChanged(IList<DataPoint> newDataPoints, IList<DataPoint> oldDataPoints)
        {
            base.OnDataPointsChanged(newDataPoints, oldDataPoints);
            foreach (var dataPoint in oldDataPoints)
            {
                dataPoint.MouseEnter -= new MouseEventHandler(dataPoint_MouseEnter);
                dataPoint.MouseLeave -= new MouseEventHandler(dataPoint_MouseLeave);
            }
            foreach (var dataPoint in newDataPoints)
            {
                dataPoint.MouseEnter += new MouseEventHandler(dataPoint_MouseEnter);
                dataPoint.MouseLeave += new MouseEventHandler(dataPoint_MouseLeave);
            }
        }
    
        void dataPoint_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            foreach (var dataPoint in ActiveDataPoints)
                if (e.OriginalSource != dataPoint) dataPoint.Opacity = MouseNotOverOpacity;
        }
    
        void dataPoint_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            foreach (var dataPoint in ActiveDataPoints)
                dataPoint.Opacity = 1;
        }
    }
    

    我们只关注数据点何时发生变化并注册鼠标进入/离开处理程序,这些处理程序操纵鼠标未经过的所有其他数据点的不透明度。这可以扩展为支持故事板等。

    【讨论】:

    • 嗨瑞克,非常感谢您的回答。然而,我正在寻找的效果有些不同。请查看此链接以获取示例:examples.idashboards.com/idashboards/?guestuser=wpsc1。发生的情况是,所有其他列都会消失,而聚焦的列仍然是亮的。再次感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 2010-12-07
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 2012-08-21
    相关资源
    最近更新 更多