How to Create a Chart From SQL Data
Recipe overview
This Business Intelligence (BI) recipe provides step-by-step instructions on building a pie chart based on a SQL data source – one of nearly limitless use cases for transforming raw data into meaningful and useful information for business analysis purposes. This recipe utilizes the following items:
The Chart macro, which comes natively with Confluence, to produce the pie chart.
The SQL for Confluence add-on, which comes in both Express and Pro editions, to query a SQL database to retrieve the data to be presented in the chart.
The Macro Security for Confluence add-on to safeguard the page and the database it uses by ensuring that only Administrators can use the SQL macro.
It will take you approximately 8-10 minutes to complete this recipe. Jump to Step 5 to see a preview of the final result!
See our other related BI recipes:
How to create a self-service report from SQL data (Intermediate)
How to Create a Business Intelligence (BI) Dashboard (Advanced)
Recipe level
Ingredients
Preparation
Install MySQL and the sample Examplegear Database, including defining the "examplegeardb" data source profile.
Steps
Create Confluence page
Estimated Time: 2 min
Create a Confluence page that will eventually contain the macros needed to produce the chart.
| Adding page restrictions: | |||||||||||||
Configure the Chart macro
Estimated Time: 1 min
Edit the Sales By Product page and do the following:
| Chart macro parameters:
| |||||||||||||
Configure the SQL Query macro
Estimated Time: 2 min
This step uses the SQL Query macro, which is part of the SQL for Confluence (Pro Edition) add-on, to retrieve the data that will be displayed in the pie chart. To configure the SQL Query macro, do the following:
| SQL Query macro parameters:
Sales By Product page, in Edit mode: | |||||||||||||
Configure Macro Security
Estimated Time: 2-3 min
Before you save the Sales By Product page, you should install the Macro Security for Confluence add-on (if you haven't do so already) and configure it with a property file that identifies who can run each security-enabled macro. Each macro security enabled macro accepts specific parameters to controls its use. This safeguards your Confluence instance and data from misuse or performance issues caused by inexperienced users or inappropriate use. Follow these steps to configure it:
| Example configuration file: macro-security.properties file# See the documentation space - https://appfire.atlassian.net/wiki/display/CMSP # More specifically: # - managed macros: https://appfire.atlassian.net/wiki/display/CMSP/Macro+Security+Managed+Macros # # Macro security is enabled/disabled from the UPM configure link for Macro Security for Confluence # Property setting are loaded from the same configuration screen. # # Property settings here can restrict use of some powerful macro capabilities to trusted users # while still allowing non-trusted users to view content created by using these macros # - this is accomplished by requiring those pages using the restricted macros # are controlled by trusted users # - a page is controlled by having edit capabilities restricted to a group # that is identified as being trusted in the properties file # - a page is also considered to be controlled if it is in a permitted space # # Property file # - The property file can be loaded from any file location on the Confluence server or from an attachment # # No specific configuration # - Add-ons can implement their own rules if there is no specific configuration found # depending on what they are restricting # - Standard/default is # - Configuration entry must be specified if the macro is restricted # - *ANY can be used to allow all use (run is an example where *ANY is a likely use) # - Some macros have additional, finer grained (parameter level) restrictions # - these MUST also have something specified, otherwise macro use is not authorized # - Some macros only restrict a specific parameter # - these generally do not have to be specifically configured for use # # Property keys and values are case sensitive # # Property values are a comma separated list of groups or space names # - the page edit restriction must match one of the groups listed # - no other users or groups can be permitted to edit the page # - use *ANY to indicate no restriction # # SQL - https://appfire.atlassian.net/wiki/display/SQL sql = confluence-administrators sql.datasource.* = confluence-administrators sql.datasource.testDS = *ANY sql.limit = confluence-administrators sql.disableAntiXss = confluence-administrators sql.querytimeout = confluence-administrators sql-query = confluence-administrators sql-query.datasource.* = confluence-administrators sql-query.datasource.testDS = *ANY sql-query.limit = confluence-administrators sql-query.disableAntiXss = confluence-administrators sql-query.querytimeout = confluence-administrators # Live template support - https://appfire.atlassian.net/wiki/display/CMSP/Live+Template+Support sql-query&live-template.datasource.* = *global # Script - https://appfire.atlassian.net/wiki/display/SCRP beanshell = confluence-administrators groovy = confluence-administrators gant = confluence-administrators jython = confluence-administrators # HTML - https://appfire.atlassian.net/wiki/display/HTML html = confluence-administrators xslt = confluence-administrators # Run CLI Actions - https://appfire.atlassian.net/wiki/display/CCLI cli = confluence-administrators cli.profile.* = confluence-administrators cli.product.* = confluence-administrators cli.directory.* = confluence-administrators cli.datasource.* = confluence-administrators include-remote = confluence-administrators include-remote.profile.* = confluence-administrators # Cache - https://appfire.atlassian.net/wiki/display/CACHE cache = *ANY future = *ANY future.timeout = confluence-administrators # Run - https://appfire.atlassian.net/wiki/display/RUN run = confluence-administrators run-now = confluence-administrators run.disableAntiXss = confluence-administrators # Advanced Tables - https://appfire.atlassian.net/wiki/display/TBL csv.url.* = *ANY csv.disableAntiXss = confluence-administrators json-table.url.* = *ANY json-table.disableAntiXss = confluence-administrators # Excel - https://appfire.atlassian.net/wiki/display/XL excel.url.* = *ANY excel.disableAntiXss = confluence-administrators # Flash - https://appfire.atlassian.net/wiki/display/FLASH flash.url.* = confluence-administrators # Markdown - https://appfire.atlassian.net/wiki/display/MARKDOWN markdown.allowHtml = confluence-administrators markdown-attachment.allowHtml = confluence-administrators markdown-url.allowHtml = confluence-administrators # Code Pro - https://appfire.atlassian.net/wiki/display/CODE code-pro.url = *ANY code-pro.profile.* = confluence-administrators
Macro Security Configuration page, after loading the macro-security.properties file: | |||||||||||||
Save and test the page
Estimated Time: 1-2 min
Now, go back to the Sales by Product page and click Save to save it. You should now see a page that appears as shown on the right. The pie chart is shown, with the data table appearing after it. If you see an error such as "Error rendering macro 'sql-query' : com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException", then check the syntax of the SELECT statement within the SQL Query macro container. If you see an error such as "Error rendering macro 'sql-query' : Unable to connect to database", then verify that the SQL Query macro's Data source name parameter is set to the correct value and that the examplegeardb data source profile was set up per the installation instructions.
| Sales by Product page: | |||||||||||||