Jump to content
We've recently updated our Privacy Statement, available here ×
  • How to add a HTML 5 Chart into a table detail row


    sgeissle
    • Features: Charts Version: v7.1 Product: JasperReports® Server

    Requirement

    The requirements are the following:

    • To show a HTML 5 Chart for each detail row in the table.
    • Each chart should only represent one value of this specific row of the table.
    • As a table row offers not too much space, the chart should be minimalistic. No legend, axis etc.

    General approach

    The general secret to this is to use a sub-report element in the row and use a chart in the sub-report.


    Documentation:

    Create sub-report

    Things to consider when creating the HTML 5 Chart sub-report:

    • Get rid of all white space of the report. If the table row is 100px long and 30px wide then create the chart of this size in the chart properties. Also check the report properties delete all the margins and make the complete report that size.
    • The subreport needs at least measure and category in the data set.
    • As with all charts the measure is the numerical value of what you show (sales / percent of stock / etc.).
    • The category is the descriptive value of the numerical value you are currently. The description (customer / part number / office / etc.) so that the number makes sense.
    • The category needs to be passed into the subreport as a parameter. Therefore, create the parameter and add it to the query with a WHERE clause.
    • We need to adapt the following properties (double click on chart => chart formatting => advanced properties) so that we get rid of all fuss that we cannot show in a table row:
      • chart.spacingBottom, chart.spacingTop, chart.spacingLeft, chart.spacingRight – set to 1 px maybe to get rid of white space
      • legend.enabled = false (no space to show the legend)
      • tooltip.headerFormat => delete all
      • tooltip.pointFormat = <b>{point.y}
      • xAxis.visible = false / yAxis.visible = false

    Create main-report

    Things to consider when creating the main report.

    • Create main report with table element (instead of the table you can also use the detail band directly)
    • Add table and subdata set. Make sure to add the category in the detail rows, because it has to be passed as a parameter to the sub-report.
    • Add the chart by rightclicking one of the columns and choose to add a column before or after.
    • Drag a subreport from the Palette to the new empty detail field of the unnamed column.
    • Chose the subreport that you had created before and map the category as a parameter.
    • Name the column.

    This should do the trick.

    Specific example

    I wanted to show a bar chart showing the difference of total sales to a customer between 2018 and 2017.

    Difficulties of this example:

    • The difference of total sales can be positive or negative. If negative the chart starts on the right margin of the cell, if positive from the left. That might not be the best user experience there is. To work around that, I added one other column with the same subreport inside directly beside the first sub-report column. Now I added a print when expression in the sub-report elements properties. For the right column when the difference was positive, for the left column when the difference was negative.
    • The bar should show red color when negative, green color when positive. To do that we need to go again to the advanced options of the chart formatting in the subreport:
      • plotOptions.series.threshold = 0
      • plotOptions.series.negativeColor = hex color for red
      • Furthermore, add a green hex code as the first color of the color palette of the simple chart formatting properties.
      • This works perfectly in the preview of the sub-report alone. If you preview the main report this configuration is not shown, but it shows the standard colors. So, this might be a defect of the v7.10 version.

    User Feedback

    Recommended Comments



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...