• Home
  • Events
  • About
  • Friday Fun XLIX SunburstChart

    Aloha again,

    Last weekend I was looking for dashboards on the web and stumbled upon some nice charts that I found really interesting.
    These charts are called RadialTreeMaps or SunburstCharts and one that I’ve found (guess it is from Excel) looks as follows…

    It really represents a forward directed tree structure in a radial layout where the root node itself is not really visible. First of all I thought that might be easy because it looks like a multi-donut chart and in principle this is correct but the difference is the tree structure of this chart.

    So it took some early morning hours to figure out how to get it right. So the first thing that I needed was a tree structure that is able to hold the data of this chart.
    Therefor I created one which works good enough for my chart and is build from one class.

    The TreeNode class that I use to build the tree structure will also fire TreeNodeEvents of Type EventType.PARENT_CHANGED and EventType.CHILDREN_CHANGED so that you can register a listener to those events and react on changes of the tree.

    I’ve changed the behavior in the way that all tree nodes fire their event via the root node of the tree. Means you just have to hook up a listener to the root of the tree to get informations.

    The final idea behind this chart is to add it to TilesFX so I will use the same ChartData class in the tree structure.
    I’ve decided to make use of the JavaFX canvas node again to keep the number of nodes on the scene graph low. This comes with the drawback that it is not trivial to implement mouse event handling for the segments. So in this version I don’t support any mouse event handling. But I think I will also create a node based version…maybe during the next weekend 🙂

    To create a graph like this you first have to setup the underlying tree. With my TreeNode class this will look like follows:

    With this we have setup the tree and now we only have to create the control itself. To make it easier I’ve created a SunburstChartBuilder. So the code to create a sunburst chart like the one on the image above will look like follows:

    Now you just have to add this node to your scene graph and you should see something like this…

    Not too bad 🙂

    As you can see the control comes with the following properties

    • backgroundColor
    • textColor
    • visibleData (NONE, VALUE, NAME, NAME_VALUE)
    • decimals (0 – 5)
    • useColorFromParent (true, false)
    • interactive (true, false)
    • autoTextColor (true, false)
    • brightTextColor
    • darkTextColor
    • useChartDataTextColor (true, false)

    One can define a text color in the ChartData objects and if you would like to use the color defined in there you should set useChartDataTextColor to true.

    You also have the ability to only define the fill color in the ChartData objects and let the chart automatically adjust the text color dependent on the segment fill color.
    Therefor you can define a brightTextColor that will be used for dark segment fills and a darkTextColor that will be used for bright segment fills. The default bright text color is Color.WHITE and the default dark text color is Color.BLACK.

    The useColorFromParent property can be used to set the color of all children of one group to the same color as the group root. Means the color of each ChartData object will be ignored except the one of the group root node.
    So if I switch it on for the example above the result will look like this…

    As you can see now all children of each group have the same color as the root node of this group. If you use this feature you just have to define the colors for the four root nodes (1st, 2nd, 3rd, 4th).

    Means the code of the tree for the above chart would look like follows:

    Here you see that we defined the colors only for the four root nodes of the chart.

    After I spend another early morning now the control also supports interactivity. I’ve added an interactive property to the control which default value is false. In this case the chart is drawn by using the JavaFX canvas node only (for all charts this leads to 3 nodes on the scene graph).

    If you switch interactivity on the chart will be drawn using JavaFX Path elements. In this case the number of nodes on the scene graph will depend on the number of segments in the chart (for the the chart above it leads to 31 nodes).

    The control contains a demo that you can start on the console with

    gradle demo

    This will start the following application:

    The chart on the left side is the Canvas based chart where you don’t have any interaction at all. The chart on the right side is the interactive chart that makes use of Path elements.
    Here you have a tooltip when you hover over a segment and if you click a segment it will fire a TreeNodeEvent of type EventType.NODE_SELECTED. With this feature you could hook up a listener to the root node of your tree structure and get notified when someone clicks on a segment.

    So in principle this control contains two approaches, one that saves nodes on the scene graph with the cost of no interaction and the other which comes with interaction but with the cost of more nodes on the scene graph…it’s your choice 🙂

    As always you can find the code on github and you would like to start the demo that is part of the source code you simply have to pull the source code from github and start the demo on the console with

    gradle demo

    That’s it for today, so enjoy the upcoming weekend and do not forget…keep coding… 😉

    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • email
    • Print
    • Twitter
    • LinkedIn
    • XING
    • Facebook
    • Google Bookmarks

    Leave a Comment

    Time limit is exhausted. Please reload the CAPTCHA.