开发者

Wpf Toolkit Chart Invert Highlighting

开发者 https://www.devze.com 2023-02-06 16:05 出处:网络
How can I highlight other pieces (columns, bars etc.) in a chart created with wpf toolkit. I am using a control template to style my own chart. So far I used a trigger to get a fading effect on the el

How can I highlight other pieces (columns, bars etc.) in a chart created with wpf toolkit. I am using a control template to style my own chart. So far I used a trigger to get a fading effect on the element on which the mouse is residing. I want to invert this; to fade other elements (a popular charting visual gimmick) on to which mouse is not pointing. Following i开发者_Python百科mage shows the selected column Faded, I want it to be the other way around

Wpf Toolkit Chart Invert Highlighting

.


Just set the default value to faded and use the trigger to bring it up to full opacity. You have done some other styling but here is an example based on the default style:

<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>

Edit:

If you want to change all the other data points except the data point the mouse is over, that is a bit harder and can't be done simply by restyling the controls. But you can create your own series control that has that capability. Here is a chart with an unstyled column series class called MouseNotOverColumnSeries with a new MouseNotOverOpacity property:

    <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>

Here is the MouseNotOverColumnSeries class:

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;
    }
}

We just pay attention to when the data points change and register mouse enter/leave handlers that manipulate the opacity of all the other data points that the mouse is not over. This could be expanded to support storyboards, etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消