Customizing Tooltips in JasperReports HTML5 Chart Components

Note: I am referencing the 5.5 versions of JasperServer, iReport, and JasperStudio in this post.

Since HTML5 Charts have been added to JasperReports in its 5.0 release, a slew of new graphing possibilities have been made available. Some of the abilities to customize these charts have not been implemented in the graphical reporting tools iReport and JasperStudio in the most intuitive way. Details on how to customize them have not been easy to find either. Here is an example of an HTML5 Stacked Bar Chart from the demo data in a report displayed in JasperServer:

Customizing Tooltips in JasperReports HTML5 Chart Components

Displaying the Category (“CA”), Series (“Male”), and Measure (“81,541.81”) when hovering over a chart measure is sufficient in most cases, but there are often more datapoints that would be nice to have displayed in the tooltip. For example, a percentage across the category the would be a great addition. To add this, right click on the chart element in iReport or JasperStudio and select “Edit Chart Properties,” the button indicated below:

Customizing_Tooltips_in_JasperReports_HTML5_Chart_Components_Chart_Properties

Here, you can add custom properties. You should see a list of currently set properties that were either predefined, or customized using the properties panel in iReport or JasperStudio. You can edit these properties by double-clicking a property to bring up an expression editor. To customize the tooltip, you need to click the add button as seen below:

Customizing_Tooltips_in_JasperReports_HTML5_Chart_Components_Chart_Properties_Advanced

Now an expression editor will display. In this example, we want to show what we are currently displaying with an additional percentage amount. Here, we are going to set the “Property Name” as tooltip.pointFormat, check the “Use an expression” checkbox, and type the below string as seen in the image that follows:

Customizing_Tooltips_in_JasperReports_HTML5_Chart_Components_Chart_Properties_Advanced_Expression

Click OK on the expression editor window. Click OK on the HTML5 Charts properties window. You should be able to run the report now (or export to JasperServer and display) to have a result like this:

Customizing_Tooltips_in_JasperReports_HTML5_Chart_Components_Result 1

This is essentially what we wanted, but the precision of the percentage decimal value is not what we were looking for. To fix this, go through the process of adding another HTML5 Chart property. Set tooltip.percentageDecimals to “2” to achieve the result below:

Customizing_Tooltips_in_JasperReports_HTML5_Chart_Components_Result 2

Since HTML5 Charts use the HighCharts library, I recommend viewing their API documentation to uncover more potential options in customizing tooltips in HTML5 Charts in JasperReports.

5 Comments on Customizing Tooltips in JasperReports HTML5 Chart Components

  1. kapil
    April 15, 2014 at 10:29 am (4 years ago)

    Thanks for the post. I see alignment issue when series name is displayed in the tooltip.
    “Male” (Series Name) is moving little right compare to other values.
    How do I align series name with other names displayed in tooltip.

    Thanks,
    Kapil

    • dhoppmann
      April 15, 2014 at 2:24 pm (4 years ago)

      Hi Kapil,

      The alignment of the series name is done by the css applied to the <span> tag if I’m not mistaken (I don’t have an instance running this report readily available right now). You can remove the <span></span> tags surrounding {series.name} to have no decorations, or apply other css rules in the application to override the alignment. See this article for details on customizing css in your JasperServer instance.

  2. Keivan
    June 4, 2014 at 1:33 am (3 years ago)

    Thank you for your post. It helped me a lot. However when I tried to use some formating for a number in the expression, jasper just did not display the number and displayed the variable name itself! Do you know how can I set decimals inside the expression?
    I tried to use this:
    {series.name}: {point.y} ({point.percentage:.0f}%)”
    but I ended up with “{point.percentage:.0f}” in the result!
    Thanks in advance,
    Keivan

    • dhoppmann
      June 4, 2014 at 2:12 pm (3 years ago)

      You can’t format the point percentage that way. You’ll need to specify precision using the property tooltip.percentageDecimals – at the bottom of the post, I specify setting this to 2 for a 2-point decimal precision.

  3. Madhu
    December 16, 2014 at 10:08 am (3 years ago)

    “{series.name}: {point.y} ({point.percentage:.2f}%)”. It works in 5.6.1