Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: colors for mixed charts #10870

Merged
merged 1 commit into from Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wont this test also pass the old one case, see my screenshot from yesterday?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LeeLenaleee fixed

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.