使用 ChartJs 和 React 设计背景(填充)
Styling background (fill) with ChartJs and React
我想使用 chartJS 库在 React 中创建一个折线图,样式为面积图 ('react-chartjs-2')
这就是我想要实现的,例如背景(填充)线下方有一些不透明度:
代码:
import React from 'react';
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend,
} from 'chart.js';
import { Line } from 'react-chartjs-2';
import faker from 'faker';
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend
);
export const options = {
responsive: true,
plugins: {
legend: {
position: 'top' as const,
},
title: {
display: true,
text: 'Chart.js Line Chart',
},
},
};
const labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
export const data = {
labels,
datasets: [
{
label: 'Dataset 1',
data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })),
borderColor: 'rgb(255, 99, 132)',
backgroundColor: '#000000',
fill: {
target: 'origin',
above: 'rgb(255, 0, 0)', // Area will be red above the origin
below: '#000000' // And blue below the origin
}
},
{
label: 'Dataset 2',
data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })),
borderColor: 'rgb(53, 162, 235)',
backgroundColor: 'rgba(53, 162, 235, 0.5)',
},
],
};
export function App() {
return <Line options={options} data={data} />;
}
如何使用 ChartJS ('react-chartjs-2') 实现此目的?
是的——ChartJS/react-chartjs-2确实支持这一点。为此,您需要:
- Import/registerchart.js
Filler
插件(在docs for Area Charts中提到)
- 设置线条的
tension
(使线条弯曲),并且;
- 为数据集设置
fill
选项。
按照这些步骤将产生:
例如:
import React from "react";
import { Line } from "react-chartjs-2";
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend,
Filler // 1. Import Filler plugin
} from "chart.js";
import faker from "faker";
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend,
Filler // 1. Register Filler plugin
);
export const options = {
responsive: true,
tension: 0.3 // 2. Set the tension (curvature) of the line to your liking. (You may want to lower this a smidge.)
};
const labels = ["January", "February", "March", "April", "May", "June", "July"];
export const data = {
labels,
datasets: [
{
label: "Dataset 1",
data: labels.map(() => faker.datatype.number({ min: 0, max: 500 })),
borderColor: "rgb(255, 99, 132)",
backgroundColor: "rgba(255, 0, 0)",
fill: {
target: "origin", // 3. Set the fill options
above: "rgba(255, 0, 0, 0.3)"
}
},
{
label: "Dataset 2",
data: labels.map(() => faker.datatype.number({ min: 0, max: 500 })),
borderColor: "rgb(53, 162, 235)",
backgroundColor: "rgba(53, 162, 235, 0.3)",
fill: "origin" // 3. Set the fill options
}
]
};
export function App() {
return <Line options={options} data={data} />;
}
工作代码Sanbox:https://codesandbox.io/s/chartjs-area-chart-p1o023?file=/App.tsx:817-846
我想使用 chartJS 库在 React 中创建一个折线图,样式为面积图 ('react-chartjs-2')
这就是我想要实现的,例如背景(填充)线下方有一些不透明度:
代码:
import React from 'react';
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend,
} from 'chart.js';
import { Line } from 'react-chartjs-2';
import faker from 'faker';
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend
);
export const options = {
responsive: true,
plugins: {
legend: {
position: 'top' as const,
},
title: {
display: true,
text: 'Chart.js Line Chart',
},
},
};
const labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
export const data = {
labels,
datasets: [
{
label: 'Dataset 1',
data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })),
borderColor: 'rgb(255, 99, 132)',
backgroundColor: '#000000',
fill: {
target: 'origin',
above: 'rgb(255, 0, 0)', // Area will be red above the origin
below: '#000000' // And blue below the origin
}
},
{
label: 'Dataset 2',
data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })),
borderColor: 'rgb(53, 162, 235)',
backgroundColor: 'rgba(53, 162, 235, 0.5)',
},
],
};
export function App() {
return <Line options={options} data={data} />;
}
如何使用 ChartJS ('react-chartjs-2') 实现此目的?
是的——ChartJS/react-chartjs-2确实支持这一点。为此,您需要:
- Import/registerchart.js
Filler
插件(在docs for Area Charts中提到) - 设置线条的
tension
(使线条弯曲),并且; - 为数据集设置
fill
选项。
按照这些步骤将产生:
例如:
import React from "react";
import { Line } from "react-chartjs-2";
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend,
Filler // 1. Import Filler plugin
} from "chart.js";
import faker from "faker";
ChartJS.register(
CategoryScale,
LinearScale,
PointElement,
LineElement,
Title,
Tooltip,
Legend,
Filler // 1. Register Filler plugin
);
export const options = {
responsive: true,
tension: 0.3 // 2. Set the tension (curvature) of the line to your liking. (You may want to lower this a smidge.)
};
const labels = ["January", "February", "March", "April", "May", "June", "July"];
export const data = {
labels,
datasets: [
{
label: "Dataset 1",
data: labels.map(() => faker.datatype.number({ min: 0, max: 500 })),
borderColor: "rgb(255, 99, 132)",
backgroundColor: "rgba(255, 0, 0)",
fill: {
target: "origin", // 3. Set the fill options
above: "rgba(255, 0, 0, 0.3)"
}
},
{
label: "Dataset 2",
data: labels.map(() => faker.datatype.number({ min: 0, max: 500 })),
borderColor: "rgb(53, 162, 235)",
backgroundColor: "rgba(53, 162, 235, 0.3)",
fill: "origin" // 3. Set the fill options
}
]
};
export function App() {
return <Line options={options} data={data} />;
}
工作代码Sanbox:https://codesandbox.io/s/chartjs-area-chart-p1o023?file=/App.tsx:817-846