View Full Version : Dynamic chart theming

20 Dec 2011, 1:49 AM

I'm a new developer in JavaScript, and extjs framework.

I have several custom themes , and want to apply them to column chart, according to the number of series that shown in the chart.

It means that if I have 3 series- I need to match Theme-Three, and if I have 7 series- I need to match Theme-Seven.

Any idea how can I do that?


20 Dec 2011, 5:11 AM
You will need to create different Ext.chart.theme.Theme for each and then set the theme property on the chart when you create it.

20 Dec 2011, 11:53 AM
Here's a terse example of how to easily extend the Base theme:

// Custom colors, where it's assumed there will be four (or less) bars/columns on your chart.
var myColors = ['#000000', '#0000ee', '#dd0000', '#00cc00'];

// Extend the Base theme, using your custom colors
Ext.chart.theme.MyTheme = Ext.extend(Ext.chart.theme.Base, {
constructor: function(config) {
Ext.chart.theme.Base.prototype.constructor.call(this, Ext.apply({
seriesThemes: myColors,
colors: myColors
}, config));

To instruct a chart to use your custom theme:

Ext.create('Ext.chart.Chart', {
theme: 'MyTheme',

If your chart's 'series' config uses multiple data points (as illustrated below), the custom colors will be used for each bar/column drawn for each data point, based on ordinal position (i.e., the first custom color will be used for the first data point, the second color for the second data point, and so on).

series: [{
type: 'column',
axis: 'left',
highlight: true,
xField: 'category',
yField: ['data1', 'data2', 'data3', 'data4'],
title: ['Data 1', 'Data 2', 'Data 3', 'Data 4']

20 Dec 2011, 10:38 PM

Thanks for your quick reply, but I think you didn't understand me.

I've made my custom themes (not one theme)-to use in different cases.

My question now is how can I know which theme I have to put according to the number of series?

For example: If I have
yField: ['data1'],
than I need to match the theme:
theme: 'ColumnTheme1',

but if I have
yField: ['data1','data2','data3','data4','data5','data6','data7'],
than I need to match the theme:
theme: 'ColumnTheme7',

and so on. The number of the fields in yField can be 1-7.

Many thanks.

21 Dec 2011, 5:47 AM
Get the length of the array and append that to 'ColumnTheme'

26 Dec 2011, 2:30 AM
Thanks for the reply.

Now- I have to use just one theme- but I need to change the colors ,based on the series number.

Colors for chart series is determined by the chart base color provided in the theme defaults. Default base color is then used to calculate the colors for other series as needed. These calculations are based on dividing the hue value by the number of series colors needed. Saturation and lightness values are not changed.

My base color is: HSL: Hue=210deg; Saturation=43%; Lightness=55% , HEX: # 5588BB

For multi series charts the hue value (0 360 deg) will be divided by the number of required series distributing the colors across the spectrum.

For example:

3 series chart: 360/3=120, so:
base heu is: 210deg.
slice 2: H210+120deg= H300.
slice 3: H300+120deg=H90.

7 series chart: 360/7=~52, so:
base heu is: 210deg.
slice 2:H210+52deg=H262.
slice 2:H262+52deg=H314.
slice 2:H314+52deg=H6.
slice 2:H6+52deg=H58.
slice 2:H58+52deg=H110.
slice 2:H110+52deg=H162.

and so on...

It is not simple, I know.

Any idea? it will be helpful!!!

Many thanks.