英文:
React line chart background fill
问题
我在我的React应用中使用Chart.js库创建了一条折线图。该折线图应用了背景填充,但我遇到了一个问题,即填充覆盖了0和第一个数据点之间的y轴边框。我希望边框在背景颜色之上绘制,以便它可见。
import React from 'react';
import { Line } from 'react-chartjs-2';
import { Chart, registerables } from 'chart.js';
Chart.register(...registerables);
const Graph = ({ chartData, chartColour, chartFillColour }) => {
const options = {
plugins: {
legend: {
display: false,
}
},
scales: {
x: {
grid: {
display: true,
drawBorder: true,
drawOnChartArea: false,
},
ticks: {
display: true,
}
},
y: {
grid: {
display: false,
drawBorder: true,
drawOnChartArea: false,
},
ticks: {
display: true,
}
}
}
}
const data = {
labels: chartData.labels,
datasets: [
{
data: chartData.data,
backgroundColor: chartFillColour,
borderColor: chartColour,
pointRadius: 0,
fill: true,
}
]
}
return chartData.labels ? <Line data={data} options={options}/> : null;
}
export default Graph;
有办法确保边框在背景颜色之上绘制吗?任何帮助或建议将不胜感激。谢谢!
英文:
I have a line chart in my React application created using Chart.js library. The line chart has a background fill applied to it, but I'm facing an issue where the fill covers the y-axis border between 0 and the first data point. I want the border to be drawn over the background color so that it's visible.
import React from 'react';
import { Line } from 'react-chartjs-2';
import { Chart, registerables } from 'chart.js';
Chart.register(...registerables);
const Graph = ({ chartData, chartColour, chartFillColour }) => {
const options = {
plugins: {
legend: {
display: false,
}
},
scales: {
x: {
grid: {
display: true,
drawBorder: true,
drawOnChartArea: false,
},
ticks: {
display: true,
}
},
y: {
grid: {
display: false,
drawBorder: true,
drawOnChartArea: false,
},
ticks: {
display: true,
}
}
}
}
const data = {
labels: chartData.labels,
datasets: [
{
data: chartData.data,
backgroundColor: chartFillColour,
borderColor: chartColour,
pointRadius: 0,
fill: true,
}
]
}
return chartData.labels ? <Line data={data} options={options}/> : null;
}
export default Graph;
Is there a way to ensure that the border is drawn over the background color? Any help or suggestions would be greatly appreciated. Thank you!
答案1
得分: 1
你可以将fillerPlugin的drawTime设置为beforeDraw
,这样它会在chart.js绘制其自身组件之前绘制填充。
链接:https://www.chartjs.org/docs/4.3.0/charts/area.html#configuration
new Chart(ctx, {
data: {
datasets: []
},
options: {
plugins: {
filler: {
drawTime: 'beforeDraw'
}
}
}
});
英文:
You can set the drawTime of the fillerPlugin to beforeDraw
so it draws the fill before chart.js draws any of its own components.
https://www.chartjs.org/docs/4.3.0/charts/area.html#configuration
new Chart(ctx, {
data: {
datasets: []
},
options: {
plugins: {
filler: {
drawTime: 'beforeDraw'
}
}
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论