Merge pull request #7237 from Budibase/feature/custom-colour-charts

Custom colour palette for charts
This commit is contained in:
melohagan 2022-08-12 15:33:57 +01:00 committed by GitHub
commit 812bddcd61
6 changed files with 276 additions and 14 deletions

View File

@ -1466,10 +1466,11 @@
}, },
{ {
"type": "select", "type": "select",
"label": "Colours", "label": "Colors",
"key": "palette", "key": "palette",
"defaultValue": "Palette 1", "defaultValue": "Palette 1",
"options": [ "options": [
"Custom",
"Palette 1", "Palette 1",
"Palette 2", "Palette 2",
"Palette 3", "Palette 3",
@ -1482,6 +1483,51 @@
"Palette 10" "Palette 10"
] ]
}, },
{
"type": "color",
"label": "C1",
"key": "c1",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C2",
"key": "c2",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C3",
"key": "c3",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C4",
"key": "c4",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C5",
"key": "c5",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{ {
"type": "boolean", "type": "boolean",
"label": "Stacked", "label": "Stacked",
@ -1581,10 +1627,11 @@
}, },
{ {
"type": "select", "type": "select",
"label": "Colours", "label": "Colors",
"key": "palette", "key": "palette",
"defaultValue": "Palette 1", "defaultValue": "Palette 1",
"options": [ "options": [
"Custom",
"Palette 1", "Palette 1",
"Palette 2", "Palette 2",
"Palette 3", "Palette 3",
@ -1597,6 +1644,51 @@
"Palette 10" "Palette 10"
] ]
}, },
{
"type": "color",
"label": "C1",
"key": "c1",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C2",
"key": "c2",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C3",
"key": "c3",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C4",
"key": "c4",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C5",
"key": "c5",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{ {
"type": "select", "type": "select",
"label": "Curve", "label": "Curve",
@ -1695,10 +1787,11 @@
}, },
{ {
"type": "select", "type": "select",
"label": "Colours", "label": "Colors",
"key": "palette", "key": "palette",
"defaultValue": "Palette 1", "defaultValue": "Palette 1",
"options": [ "options": [
"Custom",
"Palette 1", "Palette 1",
"Palette 2", "Palette 2",
"Palette 3", "Palette 3",
@ -1711,6 +1804,51 @@
"Palette 10" "Palette 10"
] ]
}, },
{
"type": "color",
"label": "C1",
"key": "c1",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C2",
"key": "c2",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C3",
"key": "c3",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C4",
"key": "c4",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C5",
"key": "c5",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{ {
"type": "select", "type": "select",
"label": "Curve", "label": "Curve",
@ -1800,10 +1938,11 @@
}, },
{ {
"type": "select", "type": "select",
"label": "Colours", "label": "Colors",
"key": "palette", "key": "palette",
"defaultValue": "Palette 1", "defaultValue": "Palette 1",
"options": [ "options": [
"Custom",
"Palette 1", "Palette 1",
"Palette 2", "Palette 2",
"Palette 3", "Palette 3",
@ -1816,6 +1955,51 @@
"Palette 10" "Palette 10"
] ]
}, },
{
"type": "color",
"label": "C1",
"key": "c1",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C2",
"key": "c2",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C3",
"key": "c3",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C4",
"key": "c4",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C5",
"key": "c5",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{ {
"type": "boolean", "type": "boolean",
"label": "Data Labels", "label": "Data Labels",
@ -1882,10 +2066,11 @@
}, },
{ {
"type": "select", "type": "select",
"label": "Colours", "label": "Colors",
"key": "palette", "key": "palette",
"defaultValue": "Palette 1", "defaultValue": "Palette 1",
"options": [ "options": [
"Custom",
"Palette 1", "Palette 1",
"Palette 2", "Palette 2",
"Palette 3", "Palette 3",
@ -1898,6 +2083,51 @@
"Palette 10" "Palette 10"
] ]
}, },
{
"type": "color",
"label": "C1",
"key": "c1",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C2",
"key": "c2",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C3",
"key": "c3",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C4",
"key": "c4",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{
"type": "color",
"label": "C5",
"key": "c5",
"dependsOn": {
"setting": "palette",
"value": "Custom"
}
},
{ {
"type": "boolean", "type": "boolean",
"label": "Data Labels", "label": "Data Labels",

View File

@ -10,7 +10,9 @@
</script> </script>
{#if options} {#if options}
<div use:chart={options} use:styleable={$component.styles} /> {#key options.customColor}
<div use:chart={options} use:styleable={$component.styles} />
{/key}
{:else if $builderStore.inBuilder} {:else if $builderStore.inBuilder}
<div use:styleable={$component.styles}> <div use:styleable={$component.styles}>
<Placeholder /> <Placeholder />

View File

@ -62,8 +62,14 @@ export class ApexOptionsBuilder {
return this.setOption(["title", "text"], title) return this.setOption(["title", "text"], title)
} }
color(color) { colors(colors) {
return this.setOption(["colors"], [color]) if (!colors) {
delete this.options.colors
this.options["customColor"] = false
return this
}
this.options["customColor"] = true
return this.setOption(["colors"], colors)
} }
width(width) { width(width) {

View File

@ -16,6 +16,7 @@
export let stacked export let stacked
export let yAxisUnits export let yAxisUnits
export let palette export let palette
export let c1, c2, c3, c4, c5
export let horizontal export let horizontal
$: options = setUpChart( $: options = setUpChart(
@ -33,9 +34,13 @@
stacked, stacked,
yAxisUnits, yAxisUnits,
palette, palette,
horizontal horizontal,
c1 && c2 && c3 && c4 && c5 ? [c1, c2, c3, c4, c5] : null,
customColor
) )
$: customColor = palette === "Custom"
const setUpChart = ( const setUpChart = (
title, title,
dataProvider, dataProvider,
@ -51,7 +56,9 @@
stacked, stacked,
yAxisUnits, yAxisUnits,
palette, palette,
horizontal horizontal,
colors,
customColor
) => { ) => {
const allCols = [labelColumn, ...(valueColumns || [null])] const allCols = [labelColumn, ...(valueColumns || [null])]
if ( if (
@ -85,6 +92,7 @@
.stacked(stacked) .stacked(stacked)
.palette(palette) .palette(palette)
.horizontal(horizontal) .horizontal(horizontal)
.colors(customColor ? colors : null)
// Add data // Add data
let useDates = false let useDates = false

View File

@ -17,6 +17,7 @@
export let legend export let legend
export let yAxisUnits export let yAxisUnits
export let palette export let palette
export let c1, c2, c3, c4, c5
// Area specific props // Area specific props
export let area export let area
@ -40,9 +41,13 @@
palette, palette,
area, area,
stacked, stacked,
gradient gradient,
c1 && c2 && c3 && c4 && c5 ? [c1, c2, c3, c4, c5] : null,
customColor
) )
$: customColor = palette === "Custom"
const setUpChart = ( const setUpChart = (
title, title,
dataProvider, dataProvider,
@ -60,7 +65,9 @@
palette, palette,
area, area,
stacked, stacked,
gradient gradient,
colors,
customColor
) => { ) => {
const allCols = [labelColumn, ...(valueColumns || [null])] const allCols = [labelColumn, ...(valueColumns || [null])]
if ( if (
@ -96,6 +103,7 @@
.legend(legend) .legend(legend)
.yUnits(yAxisUnits) .yUnits(yAxisUnits)
.palette(palette) .palette(palette)
.colors(customColor ? colors : null)
// Add data // Add data
let useDates = false let useDates = false

View File

@ -13,6 +13,7 @@
export let legend export let legend
export let donut export let donut
export let palette export let palette
export let c1, c2, c3, c4, c5
$: options = setUpChart( $: options = setUpChart(
title, title,
@ -25,9 +26,13 @@
animate, animate,
legend, legend,
donut, donut,
palette palette,
c1 && c2 && c3 && c4 && c5 ? [c1, c2, c3, c4, c5] : null,
customColor
) )
$: customColor = palette === "Custom"
const setUpChart = ( const setUpChart = (
title, title,
dataProvider, dataProvider,
@ -39,7 +44,9 @@
animate, animate,
legend, legend,
donut, donut,
palette palette,
colors,
customColor
) => { ) => {
if ( if (
!dataProvider || !dataProvider ||
@ -70,6 +77,7 @@
.legend(legend) .legend(legend)
.legendPosition("right") .legendPosition("right")
.palette(palette) .palette(palette)
.colors(customColor ? colors : null)
// Add data if valid datasource // Add data if valid datasource
const series = data.map(row => parseFloat(row[valueColumn])) const series = data.map(row => parseFloat(row[valueColumn]))