ASE Home Page Products Download Purchase Support About ASE
ChartDirector General
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  ContextMenu and .NET
Posted by Philip on Oct-09-2020 23:50
I am able to create a context menu for an XYChart (there is one XYChart in the WPFChartViewer control). However if there are multiple XYCharts in the WPFChartViewer using a MultiChart, is there a way to identify which XYChart I was hovering over when I right-clicked the mouse to get the context menu?

<ChartDirector:WPFChartViewer x:Name="WPFChartViewer1" DockPanel.Dock="Top" Width="640" ...>
    <ChartDirector:WPFChartViewer.ContextMenu>
        <ContextMenu>
            <MenuItem Header="background" IsCheckable="True" Unchecked="Background_Unchecked" Checked="Background_Checked"></MenuItem>
        </ContextMenu>
    </ChartDirector:WPFChartViewer.ContextMenu>
</ChartDirector:WPFChartViewer>

  Re: ContextMenu and .NET
Posted by Peter Kwan on Oct-11-2020 11:23
Hi Philip,

You can obtain the mouse coordinates relative to the WPFChartViewer, and then iterate the MultiChart to see which XYChart the mouse is hover on. The following is an example on how to iterate the MultiChart:

private int getChartIndexAt(MultiChart m, Point p)
{
    BaseChart b;
    for (int i = 0; (b = m.getChart(i)) != null; ++i)
    {
        if ((p.X >= b.getAbsOffsetX()) && (p.X < b.getAbsOffsetX() + b.getWidth()) &&
            (p.Y >= b.getAbsOffsetY()) && (p.Y < b.getAbsOffsetY() + b.getHeight()))
            return i;
    }
    return -1;
}

For you case, to obtain the chart that the mouse hovers on at the time of the click, you may use the above function in an mouse click event handler.

Regards
Peter Kwan