Customising chart colours in APEX 5.1
One particular issue I recently came across was the removal of two chart region attributes that allowed easy customisation of a chart, namely COLOR SCHEME and CUSTOM COLORS. Prior to APEX 5.1 when creating a chart, you had several pre-set colour schemes to choose from along with the option to define your own set of custom colour as shown here.
So how do you override the default colour scheme now these attributes have been removed?
The following example is a simple pie chart using the default colour scheme to show the percentage of employees in each department from the EMP and DEPT tables.
After adding this code, the chart now looks like this.
The previous example assumes the chart has a fixed number of data series, to override the colours of a chart with a variable number you can simply alter the code to loop through each data series and set the colour using a custom function.
To see the other configuration options take a look at the ojChart documentation at apex.oracle.com. For example,
Fixing colours for multiple charts
If you have a page consisting of multiple charts based upon the same data, you may want to ensure the colours for each series match. Colours are assigned to the series of a chart in sequence, so if your charts don’t have the same number of series, the colours won’t match.
The following example is a page with two pie charts, one detailing the percentage of employees per department and the other detailing the pool cars.
As you can see the colour assigned to each department do not match as there are no pool cars allocated the departments Research or Work Experience. To resolve this, we simply need to modify the SQL for the pool cars query to ensure a row with zero cars is returned for each department. Each query should also be updated to ensure the departments are ordered in the same sequence.
When APEX renders a pie chart, any segments with a value of zero are automatically hidden, however they are still allocated a colour and included within the legend.
About the Author
Oracle APEX Development Consultant
Explorer (UK) Ltd – Oracle awarding winning Platinum Partner
Colin is a Development Consultant at Explorer with 20 years’ experience of analysis, design and development of bespoke Oracle applications for a wide variety of business functions. Building on his previous experience of Forms and PL/SQL he is now focusing on developing high quality fit for purpose solutions using APEX.