Bubble chart with labels

This post was written by jimrobson on March 17, 2009
Posted Under: Flex

In January I had the opportunity to visit the Queen City to provide some Flex training to a great team of developers under the queen’s crown, and to spend time with some dear friends as well. They (the developers, not the friends) had a requirement to produce bubble charts that had labels inside the bubbles. It occurred to me that other people could probably use this feature, too, so I decided to polish it up a bit and make it available. The result is two classes, LabeledBubbleRenderer and LabeledBubbleSeries.

To implement the functionality, simply create a BubbleChart, and then put a LabeledBubbleSeries tag where you would normally put your BubbleSeries. You should never need to explicitly reference the LabeledBubbleRenderer, because the LabeledBubbleSeries class uses it as the default renderer.

<mx:BubbleChart> <mx:series> <rd:LabeledBubbleSeries /> </mx:series> </mx:BubbleChart>

The primary departure from the standard BubbleSeries class is that the LabeledBubbleSeries has a fourth data field: In addition to xField, yField, and radiusField, there is also a labelField property. As the name suggests, this is the name of the field that stores the string that should be displayed in each bubble’s label.

<mx:BubbleChart> <mx:series> <rd:LabeledBubbleSeries labelField="name" xField="leads" yField="sales" radiusField="commission" /> </mx:series> </mx:BubbleChart>

Secondarily, there are also a couple of additional styles, which control the location of the text within the bubble. These values, of course, are passed on to the LabeledBubbleRenderer instances which draw the circles and display the text.

<mx:BubbleChart> <mx:series> <rd:LabeledBubbleSeries textAlign="center" textVAlign="middle" /> </mx:series> </mx:BubbleChart>

I have posted a very simple demo app that shows how this all fits together.

Demo screen shot

If you look at the code, you will see a rather tight coupling between the two classes. I did not view this as undesirable because I could not think of a reason why you would want to use one without the other. However, the code is free for you to modify as needed. If you do make a significant modification that you think might benefit others, please let me know, as I would like to integrate it.

Finally, a disclaimer: As you have probably already noticed, this code is not independent of Adobe’s Data Visualization components. So if you don’t have those, this won’t do you much good. But if you do have them, then you may find this to be a useful little enhancement. Enjoy!

This being the 17th of March, allow me to close with this wish: Erin Go Bragh.

Reader Comments

I get it! They’re the Beatles. Nice work.

#1 
Written By Jim on March 22nd, 2009 @ 7:18 pm

what if the Bubble Size is too small and the Label to be shown is quite long. How will the Label appear in such a case?

#2 
Written By Sumeet Mahajan on July 20th, 2009 @ 1:34 am

Sumeet,

The text in the Label component will be truncated, with a “…” at the end. If you need different behavior, you are free to modify it to suit!

#3 
Written By jimrobson on July 20th, 2009 @ 6:31 pm

Do you have any idea how to make the label ‘overflow’ if the label is wider than the radius. I added truncateToFit=false and it helps against the … but it still limits it to the bubble width. Thanks

#4 
Written By Nir on March 21st, 2011 @ 11:03 pm

Hi,
it’s possible to set the label to be always on top? So that if the upper bubble cover part of the lower one both the labels are always visible?

Here a screen that show may problem:

http://img218.imageshack.us/i/screenbz.png/

Thank you,

Bye

#5 
Written By MonsterMash on April 4th, 2011 @ 11:15 am

Is there a way we put the label above the bubble instead of inside it?
@MONSTERMASH; have you been able to do it?

#6 
Written By Navin on October 17th, 2011 @ 6:38 am

Add a Comment

required, use real name
required, will not be published
optional, your blog address