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:
- use the charting APIs to listen to mouse events to trigger custom behavior.
- built a custom renderer to change the way a pre-built series types displays its data
- built a custom element to add new data overlays or interactive behavior to the chart.
- 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.
April 3rd, 2006 at 9:19 pm
Cool! How can sum the values within the selected data region?
Example: “40 points selected”
April 16th, 2006 at 5:00 pm
Really nice. Superb demo. Keep up these great contributions to the community.
April 17th, 2006 at 6:40 pm
Awesome sample! How hard would it be to gather all the data points in the selected region and display them in a DataGrid?
May 15th, 2006 at 2:00 pm
Any chance you can provide the source for this and/or update it to beta3 of flex2?
May 26th, 2006 at 1:59 am
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? :-(
May 26th, 2006 at 8:50 am
Yikes! Good Catch, Danny. That’s a bug in the chart source code. It’ll be fixed for GMC.
May 30th, 2006 at 12:33 am
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! :)
August 30th, 2006 at 9:16 am
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
November 1st, 2006 at 5:25 pm
Sounds nice, but the link to the source isn’t working.
July 5th, 2007 at 9:58 am
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.
Thanks again man.
Sandeep
August 2nd, 2007 at 12:40 pm
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