Skip to content

Commit

Permalink
feat: colors for mixed charts
Browse files Browse the repository at this point in the history
  • Loading branch information
dangreen committed Nov 17, 2022
1 parent 5144127 commit 1fa932a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 24 deletions.
50 changes: 26 additions & 24 deletions src/plugins/plugin.colors.ts
Expand Up @@ -4,8 +4,6 @@ export interface ColorsPluginOptions {
enabled?: boolean;
}

type DatasetColorizer = (dataset: ChartDataset, i: number) => void;

interface ColorsDescriptor {
backgroundColor?: unknown;
borderColor?: unknown;
Expand All @@ -32,38 +30,41 @@ function getBackgroundColor(i: number) {
return BACKGROUND_COLORS[i % BACKGROUND_COLORS.length];
}

function createDefaultDatasetColorizer() {
return (dataset: ChartDataset, i: number) => {
dataset.borderColor = getBorderColor(i);
dataset.backgroundColor = getBackgroundColor(i);
};
function colorizeDefaultDataset(dataset: ChartDataset, i: number) {
dataset.borderColor = getBorderColor(i);
dataset.backgroundColor = getBackgroundColor(i);

return ++i;
}

function createDoughnutDatasetColorizer() {
let i = 0;
function colorizeDoughnutDataset(dataset: ChartDataset, i: number) {
dataset.backgroundColor = dataset.data.map(() => getBorderColor(i++));

return (dataset: ChartDataset) => {
dataset.backgroundColor = dataset.data.map(() => getBorderColor(i++));
};
return i;
}

function colorizePolarAreaDataset(dataset: ChartDataset, i: number) {
dataset.backgroundColor = dataset.data.map(() => getBackgroundColor(i++));

return i;
}

function createPolarAreaDatasetColorizer() {
function getColorizer(chartType: string) {
let i = 0;

return (dataset: ChartDataset) => {
dataset.backgroundColor = dataset.data.map(() => getBackgroundColor(i++));
const type = dataset.type || chartType;

if (type === 'doughnut' || type === 'pie') {
i = colorizeDoughnutDataset(dataset, i);
} else if (type === 'polarArea') {
i = colorizePolarAreaDataset(dataset, i);
} else if (type) {
i = colorizeDefaultDataset(dataset, i);
}
};
}

function getColorizer(type: string) {
if (type === 'doughnut' || type === 'pie') {
return createDoughnutDatasetColorizer();
} else if (type === 'polarArea') {
return createPolarAreaDatasetColorizer();
}
return createDefaultDatasetColorizer();
}

function containsColorsDefinitions(
descriptors: ColorsDescriptor[] | Record<string, ColorsDescriptor>
) {
Expand Down Expand Up @@ -100,7 +101,8 @@ export default {
return;
}

const colorizer: DatasetColorizer = getColorizer(type);
const colorizer = getColorizer(type);

datasets.forEach(colorizer);
}
};
41 changes: 41 additions & 0 deletions test/fixtures/plugin.colors/mixed.js
@@ -0,0 +1,41 @@
module.exports = {
config: {
data: {
labels: [0, 1, 2, 3],
datasets: [
{
type: 'line',
data: [5, 20, 1, 10],
},
{
type: 'bar',
data: [6, 16, 3, 19]
},
{
type: 'pie',
data: [5, 20, 1, 10],
}
]
},
options: {
scales: {
x: {
ticks: {
display: false,
}
},
y: {
ticks: {
display: false,
}
}
},
plugins: {
legend: false,
colors: {
enabled: true
}
}
}
}
};
Binary file added test/fixtures/plugin.colors/mixed.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1fa932a

Please sign in to comment.