Custom Chart Annotations

April 3rd, 2006

I’ve been fielding a lot of questions about extending charts in Flex 2 as more and more people are downloading and getting excited about the new Flex beta. Developers who want to build custom charting rendering and behavior have a couple of options in Flex 2. In order of complexity, they can:

  1. use the charting APIs to listen to mouse events to trigger custom behavior.
  2. built a custom renderer to change the way a pre-built series types displays its data
  3. built a custom element to add new data overlays or interactive behavior to the chart.
  4. extend the pre-built series types to add to or modify its interaction, data, and display.

You might be surprised that I listed building a custom element as easier than extending the pre-built series that ship with the charting package. But writing a one-off component intended for a specific use is a a lot simpler than building a reusable component that many different developers will use and abuse in many different applications. Writing your own custom element, however, can actually be a pretty straightforward thing to do.

So in the spirit of making my point, I thought it was maybe time to post a sample of a custom annotation element that I had lying around. This annotation element allows the user to click and drag on the chart to hilight a region of the chart, with feedback about what region they selected.

A couple of hilights to point out:

  • the annotation is written as a custom chart element, not a custom chart. That means it can be dropped into any other chart, combined with other custom elements or series, etc.
  • the annotation stores the selection as data coordinates, not screen coordinates. Try resizing the browser while you have a selection on the chart and you’ll see that the selection stays correct even as the range of the chart changes. Look at the source and you can see some basic use of the chart extension APIs to see how this is done.
  • the annotation puts some big labels on screen showing the bounds of the selection, in data space. But the chart still manages to adjust the auto-generated ranges to make sure that both the selected region, and the labels, stay inside the bounds of the chart. Try clicking and dragging outside the chart to see an example of this. All chart elements have the ability to ‘describe’ their data to the cart to make sure the ranges are generated correctly. Look at the ‘DescribeData’ implementation in the source to see how this is done.
  • Many applications might not need to go all the way to building custom annotations to add interactivity to their charts. There are also two methods on the chart, called dataToLocal and localToData, that an application can use to add overlays at certain points, etc. This application uses those APIs to display the current mouse position, in both screen and data coordinates, on the left hand side of the screen. Check in the source to see how it’s done.
  • The demo has view source enabled — right click and choose ‘View Source..’ to see it (make sure your popup blocker doesn’t block it). From there you can download the source and play with it yourself (see the link in the lower left corner of the source viewer).

One more note…this application was built against the beta 2 release of the Flex SDK. Please make sure you have the beta 2 player installed before viewing it.

Flex 2 Beta 2 Custom Chart Annotation

Questions, Comments, Improvements? Leave ‘em here, and let me know what you think.

11 Responses to “Custom Chart Annotations”

  1. dude Says:

    Cool! How can sum the values within the selected data region?

    Example: “40 points selected”

  2. Thomas Burleson Says:

    Really nice. Superb demo. Keep up these great contributions to the community.

  3. David Hassoun Says:

    Awesome sample! How hard would it be to gather all the data points in the selected region and display them in a DataGrid?

  4. Steve Says:

    Any chance you can provide the source for this and/or update it to beta3 of flex2?

  5. Danny Tuppeny Says:

    I’m having a problem with the Y co-ordinate of dataToLocal coming out wrong, and your sample seems to have the same issue. Notice in your “screenPos” label, the Y co-ordinate actually changes with the mouse’s X co-ordinate. You can move the mouse up and down in a straight line without the screenPos value changing at all.

    I have the same issue in a component I’m writing, but I can’t find any mention of it on Google. Are we both using dataToLocal wrong? :-(

  6. Ely Greenfield Says:

    Yikes! Good Catch, Danny. That’s a bug in the chart source code. It’ll be fixed for GMC.

  7. Danny Tuppeny Says:

    What’s GMC?

    I didn’t realise who’s blog this was - I thought it was just someone playing with the betas. Some good examples here, keep up the good work! :)

  8. Brendan Says:

    Hey Ely,

    Noticed this example isn’t working anymore… but it sounds like the same example that’s in your ChartSampler app now… one and the same?

    Thx,

    Brendan

  9. Dmitriy Says:

    Sounds nice, but the link to the source isn’t working.

  10. Sandeep Says:

    Hi Ely,

    Great job man. I was just looking exactly for the same thing in my application. Bless you! However, as a feedback I noticed two things:
    1. Somehow in the source of RangeSelector, a call to modelChangedHandler() is made, but method doesn’t exist.

    1. You have used parentApplication for mouseMove events. In case of a chart drawn in a pop up, it does not work. I replaced parentApplication with systemManager, and it worked like charm.

    Thanks again man.

    Sandeep

  11. pradeep narahari Says:

    i would like to show some picture even there is no data in pie chart. like in bar chart where we show emty x and y axies lines and stick points

Leave a Reply