图表的背景颜色也会影响其他图表
Background color of the graph affects the other graphs too
App.js 下的图表有背景颜色。问题是,背景颜色也会在另一个组件的另一个图形中生效。如果只有 App.js 中的图形具有背景颜色,我该如何解决这个问题。我在 codesandbox 中重新创建了这个:https://codesandbox.io/s/color-the-canvas-fec5y?file=/src/App.js
这是我用来为图表背景着色的方法:
useEffect(() => {
Chart.register({
id: "custom_canvas_background_color",
beforeDraw: (chart) => {
const ctx = chart.canvas.getContext("2d");
ctx.save();
ctx.globalCompositeOperation = "destination-over";
ctx.fillStyle = "lightblue";
ctx.fillRect(0, 0, chart.width, chart.height);
ctx.restore();
}
});
}, []);
根据chart.js plugins documentation,使用Chart.register
全局注册插件并将其应用于所有图表。相反,将插件存储在变量中
const plugin = {
id: "custom_canvas_background_color",
beforeDraw: (chart) => {
const ctx = chart.canvas.getContext("2d");
ctx.save();
ctx.globalCompositeOperation = "destination-over";
ctx.fillStyle = "lightBlue";
ctx.fillRect(0, 0, chart.width, chart.height);
ctx.restore();
}
};
并将它作为 props 传递给 Line 组件,如下所示:
<Line
plugins={[plugin]}
/>
正如 Alexandre 在 Chart.register
中指出的那样,您的插件已在全球范围内注册到您的所有图表,并且由于您对 backgroundColor 进行了硬编码,它将应用于所有图表。您可以按照 Alexandre 的建议仅在本地将其注册到单个图表实例。
另一种更好的方法是使 backgroundColor 成为您可以在选项中设置的变量。首先你可以检查它是否被设置,如果没有你不画任何东西,如果它被设置你可以在不同的图表上有不同的颜色:
Chart.register({
id: "custom_canvas_background_color",
beforeDraw: (chart, args, opts) => {
if (!opts.color) {
return
}
const ctx = chart.canvas.getContext("2d");
ctx.save();
ctx.globalCompositeOperation = "destination-over";
ctx.fillStyle = opts.color;
ctx.fillRect(0, 0, chart.width, chart.height);
ctx.restore();
}
});
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'pink'
}]
},
options: {
plugins: {
custom_canvas_background_color: {
color: 'lightblue'
}
}
}
}
const options2 = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'orange'
}]
},
options: {
plugins: {
custom_canvas_background_color: {
color: 'pink'
}
}
}
}
const options3 = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'lightblue'
}]
},
options: {}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
const ctx2 = document.getElementById('chartJSContainer2').getContext('2d');
const ctx3 = document.getElementById('chartJSContainer3').getContext('2d');
new Chart(ctx, options);
new Chart(ctx2, options2);
new Chart(ctx3, options3);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<canvas id="chartJSContainer2" width="600" height="400"></canvas>
<canvas id="chartJSContainer3" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.js"></script>
</body>
App.js 下的图表有背景颜色。问题是,背景颜色也会在另一个组件的另一个图形中生效。如果只有 App.js 中的图形具有背景颜色,我该如何解决这个问题。我在 codesandbox 中重新创建了这个:https://codesandbox.io/s/color-the-canvas-fec5y?file=/src/App.js
这是我用来为图表背景着色的方法:
useEffect(() => {
Chart.register({
id: "custom_canvas_background_color",
beforeDraw: (chart) => {
const ctx = chart.canvas.getContext("2d");
ctx.save();
ctx.globalCompositeOperation = "destination-over";
ctx.fillStyle = "lightblue";
ctx.fillRect(0, 0, chart.width, chart.height);
ctx.restore();
}
});
}, []);
根据chart.js plugins documentation,使用Chart.register
全局注册插件并将其应用于所有图表。相反,将插件存储在变量中
const plugin = {
id: "custom_canvas_background_color",
beforeDraw: (chart) => {
const ctx = chart.canvas.getContext("2d");
ctx.save();
ctx.globalCompositeOperation = "destination-over";
ctx.fillStyle = "lightBlue";
ctx.fillRect(0, 0, chart.width, chart.height);
ctx.restore();
}
};
并将它作为 props 传递给 Line 组件,如下所示:
<Line
plugins={[plugin]}
/>
正如 Alexandre 在 Chart.register
中指出的那样,您的插件已在全球范围内注册到您的所有图表,并且由于您对 backgroundColor 进行了硬编码,它将应用于所有图表。您可以按照 Alexandre 的建议仅在本地将其注册到单个图表实例。
另一种更好的方法是使 backgroundColor 成为您可以在选项中设置的变量。首先你可以检查它是否被设置,如果没有你不画任何东西,如果它被设置你可以在不同的图表上有不同的颜色:
Chart.register({
id: "custom_canvas_background_color",
beforeDraw: (chart, args, opts) => {
if (!opts.color) {
return
}
const ctx = chart.canvas.getContext("2d");
ctx.save();
ctx.globalCompositeOperation = "destination-over";
ctx.fillStyle = opts.color;
ctx.fillRect(0, 0, chart.width, chart.height);
ctx.restore();
}
});
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'pink'
}]
},
options: {
plugins: {
custom_canvas_background_color: {
color: 'lightblue'
}
}
}
}
const options2 = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'orange'
}]
},
options: {
plugins: {
custom_canvas_background_color: {
color: 'pink'
}
}
}
}
const options3 = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'lightblue'
}]
},
options: {}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
const ctx2 = document.getElementById('chartJSContainer2').getContext('2d');
const ctx3 = document.getElementById('chartJSContainer3').getContext('2d');
new Chart(ctx, options);
new Chart(ctx2, options2);
new Chart(ctx3, options3);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<canvas id="chartJSContainer2" width="600" height="400"></canvas>
<canvas id="chartJSContainer3" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.js"></script>
</body>