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

Message ListMessage List     Post MessagePost Message

  Handling mouse events
Posted by Paolo Giustinoni on Oct-14-2006 00:32
I'm evaluating your chart controls to include it in an MFC application, and I want to know how to handle and respond to the mouse events in a chart.
I'm using only the MFC examples in Visual Studio 6.0, but I can't figure how to handle the mouse messages..
Thanks, Paolo

  Re: Handling mouse events
Posted by Peter Kwan on Oct-14-2006 04:17
Hi Paolo,

If you are trying the ChartDirector MFC samle code (in the "mfcdemo" subdirectory), it contains examples on how to handle mouse messages.

The sample code uses a CChartViewer control, which is a derived class of the MFC CStatic control. The source code of CChartViewer is included in the sample code.

Basically, handle mouse messages for CChartViewer is the same as handling mouse messages in the standard MFC CStatic control. ChartDirector has not changed the standard MFC mouse messages. For example, to detect mouse clicks, simply listens to the BN_CLICK message, the same as the BN_CLICK message in other MFC CStatic controls.

If you want to detect if the user has clicked on any data point on the chart, after your recieve the mouse click, you may use the ImageMapHandler to check the user is clicking on a data point. This is the method used in all our sample code.

For example, a typical code may be like:

//the BN_CLICK handler for the CChartViewer control
void CZoomscrolldemoDlg::OnChartViewer_Click()
{
    ImageMapHandler *handler = m_ChartViewer.getImageMapHandler();
    if (0 != handler)
    {
        //
        // Query the ImageMapHandler to see if the mouse is on a clickable hot spot. We
        // consider the hot spot as clickable if its href ("path") parameter is not empty.
        //
        const char *path = handler->getValue("path");
        if ((0 != path) && (0 != *path))
        {
            // In this sample code, we just show all hot spot parameters.
            CHotSpotDlg hs;
            hs.SetData(handler);
            hs.DoModal();
        }
    }
}

Hope this can help.

Regards
Peter Kwan

  Re: Handling mouse events
Posted by Paolo Giustinoni on Oct-14-2006 05:44
Thanks..
When I click on whichever area of a graph (not necessary a data point) I have to convert the "point coordinates" relative to the CStatic control to the "graph coordinates".. How can I do this?

  Re: Handling mouse events
Posted by Peter Kwan on Oct-15-2006 08:27
Hi Paolo,

If you need to translate a pixel coordinates to data coordinates, you may get the scale at the axes by using Axis.getMinValue() and Axis.getMaxValue(). For example:

double dataY = maxAxisY - (pixelY - topOfPlotArea) / plotAreaHeight * (maxAxisY - minAxisY);

In the above, topOfPlotArea is the pixel y coordinates of the top of the plot area, and plotAreaHeight is the pixel height of the plot area. (These values are provided by your code in the setPlotArea call when you create the chart, so your code should already know these values.)

The maxAxisY, minAxisY are the maximum and minimum of the axis. You can get them using Axis.getMaxValue() and Axis.getMinValue() after you have created the chart (and may be store it in member variables so you may use them in the mouse event handler).

Hope this can help.

Regards
Peter Kwan

  Re: Handling mouse events
Posted by Paolo Giustinoni on Oct-17-2006 02:37
Thanks for your replay, but I have some problem to get the data coordinate...
With your sample code, the only values I can get are the max or the min of the Y axis..
I used your "helloworld" example and the XYChart (a scatter chart) in your manual.

  Re: Handling mouse events
Posted by Peter Kwan on Oct-17-2006 06:59
Attachments:
Hi Paolo,

You may obtain the mouse (x, y) coordinates using any common method (the same way you would get the mouse coordinates in a standard CStatic control). The topOfPlotArea and plotAreaHeight are provided by your code to ChartDirector, so your code should already know these values.

I have modified the helloworld example for your reference. In this example, I store the mouse position using the mouse down event, then use it in the click event to compute the data coordinates. Please try it to see how it works.

Regards
Peter Kwan
helloworld.zip
helloworld.zip

18.98 Kb

  Re: Handling mouse events
Posted by Paolo Giustinoni on Oct-17-2006 07:31
Finally I have what I want..
Thanks for your top class support!!