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

Message ListMessage List     Post MessagePost Message

  Click on Legend
Posted by Ola on Jun-12-2020 11:37
I'm using Windows Forms and C#.

Do anyone have an idea on how I can capture what legend the user is clicking on?

I have a scrollable xy line chart with several lines. I want to make changes to a line when the user click the legend.

I can use the mouse click event, but I don't know where the legendens are since the form can be resized.

I do not want to make the graph into an image and use ClickHotSpot since this will disable the scrollability as far as I understand.

Any idea on how I can try to solve this would be appreciated.

  Re: Click on Legend
Posted by Peter Kwan on Jun-12-2020 13:22
Hi Ola,

You can create an image map for the legend box. This will not disable scrollability or has any effect to the chart.

For example, consider the "Zooming and Scrolling with Track Line (2)" sample code:

https://www.advsofteng.com/doc/cdnet.htm#zoomscrolltrack2.htm

You just need to append a line to the code (after the line "viewer.Chart = c;"):

viewer.ImageMap = c.getLegend().getHTMLImageMap("clickable");

For testing, in the ClickHotSpot event handler, you can use the following code:

MessageBox.Show(e["dataSetName"]);

This should pop-up a message box displaying the legend item being clicked.

In your other message, you mentioned you may want to hide/show the line by clicking on the legend item. The complication is that you need to keep the legend item even if the line is not displayed, and you need some way to let the user know the line is disabled.

To hide the line but not the legend item, you may add the line as usual, but use null as the data array. To let the user know the line is disabled, may be you can add a "X" on the line icon. The code would be like:

if (line_is_disable)
     layer.addDataSet(null, 0xff0000, "My Line").setDataSymbol(Chart.Cross2Shape(0.1), 9, 0x000000);
else
     layer.addDataSet(myViewPortData, 0xff0000, "My Line");

Hope this can help.

Regards
Peter Kwan

  Re: Click on Legend
Posted by Ola on Jun-14-2020 01:09
Peter,

Thank you so much for the suggested solution, it works perfect!

/Ola

  Re: Click on Legend
Posted by Ola on Jun-14-2020 07:05
Peter,

Maybe you can help me with this issue too?

I was using the zoom and scroll 1 as a template and I want the values after the legend like you see in sample 1, in sample 2 it's shown on the trackline. I have too many lines and it doesn't look good like that.

How can I update the new legendbox (LegendBox b = c.addLegend(55, 25, false, "Arial Bold", 8) with the values for the trackline without using the dynamic layer? Or can I update the legendbox with the dynamic layer?

Thanks,

Ola

  Re: Click on Legend
Posted by Peter Kwan on Jun-15-2020 02:11
Hi Ola,

For the legend box drawn in the dynamic layer, it is drawn by your own code (which probably is based on the same code). So your code can know where are all the legend entries (since it draws them). You just need to keep track of their positions so they can be used in the mouse move or mouse click handlers. In the handlers, you can then check use a loop to check if the mouse is on any legend entry.

I have attached an example for your reference. In the original sample code, there is already a "legendEntries" container to include all legend entries. I extended it so that the legend entry also stores the legend entry positions (in terms of (x, y) and (width, height)). In the mouse handlers, I use a loop to get the legend entry under the mouse, if any.

https://www.advsofteng.com/support/zoomscrolltrack_clickable_legend_winforms.zip

Hope this can help.

Regards
Peter Kwan

  Re: Click on Legend
Posted by Ola on Jun-16-2020 23:27
Peter,

Thank you so much for your help. This is exactly what I was looking for, it works perfect now.

/Ola