案例一:基本柱状图绘制
在React中,使用react-chartjs-2库可以轻松绘制柱状图。以下是一个简单的示例:
import React from 'react';
import { Bar } from 'react-chartjs-2';
const data = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'Sales',
backgroundColor: 'rgba(0, 123, 255, 0.5)',
borderColor: 'rgba(0, 123, 255, 1)',
data: [10, 20, 30, 40, 50, 60, 70],
}]
};
const options = {
scales: {
y: {
beginAtZero: true
}
}
};
function BasicBarChart() {
return <Bar data={data} options={options} />;
}
export default BasicBarChart;
案例二:折线图展示趋势
折线图非常适合展示数据随时间变化的趋势。以下是如何使用react-chartjs-2创建折线图的示例:
import React from 'react';
import { Line } from 'react-chartjs-2';
const data = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'Temperature',
fill: false,
borderColor: 'rgb(75, 192, 192)',
tension: 0.1,
data: [10, 20, 30, 40, 50, 60, 70],
}]
};
const options = {
scales: {
y: {
beginAtZero: true
}
}
};
function LineChart() {
return <Line data={data} options={options} />;
}
export default LineChart;
案例三:饼图展示比例
饼图非常适合展示各部分占总体的比例。以下是如何使用react-chartjs-2创建饼图的示例:
import React from 'react';
import { Pie } from 'react-chartjs-2';
const data = {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1,
}]
};
const options = {
responsive: true,
maintainAspectRatio: false,
};
function PieChart() {
return <Pie data={data} options={options} />;
}
export default PieChart;
案例四:散点图展示关系
散点图适用于展示两个变量之间的关系。以下是如何使用react-chartjs-2创建散点图的示例:
import React from 'react';
import { Scatter } from 'react-chartjs-2';
const data = {
labels: ['A', 'B', 'C', 'D', 'E', 'F'],
datasets: [{
label: 'Score',
data: [
{ x: 1, y: 2 },
{ x: 2, y: 3 },
{ x: 3, y: 5 },
{ x: 4, y: 4 },
{ x: 5, y: 7 },
{ x: 6, y: 6 }
],
backgroundColor: 'rgba(255,99,132,0.2)',
borderColor: 'rgba(255,99,132,1)',
borderWidth: 1
}]
};
const options = {
scales: {
y: {
beginAtZero: true
}
}
};
function ScatterChart() {
return <Scatter data={data} options={options} />;
}
export default ScatterChart;
案例五:雷达图展示多维度数据
雷达图适用于展示多个维度上的数据。以下是如何使用react-chartjs-2创建雷达图的示例:
import React from 'react';
import { Radar } from 'react-chartjs-2';
const data = {
labels: ['Eating', 'Drinking', 'Sleeping', 'Designing', 'Coding', 'Cycling', 'Running'],
datasets: [{
label: 'John',
data: [65, 59, 90, 81, 56, 55, 40],
fill: true,
borderColor: 'rgb(75, 192, 192)',
pointBorderColor: 'rgb(75, 192, 192)',
pointBackgroundColor: 'rgba(255,255,255,0.2)',
pointBorderWidth: 1
}, {
label: 'Jane',
data: [28, 48, 40, 19, 96, 27, 100],
fill: true,
borderColor: 'rgb(255,99,132)',
pointBorderColor: 'rgb(255,99,132)',
pointBackgroundColor: 'rgba(255,255,255,0.2)',
pointBorderWidth: 1
}]
};
const options = {
responsive: true,
maintainAspectRatio: false,
};
function RadarChart() {
return <Radar data={data} options={options} />;
}
export default RadarChart;
案例六:极坐标图展示环形数据
极坐标图适合展示环形数据,如饼图的变形。以下是如何使用react-chartjs-2创建极坐标图的示例:
import React from 'react';
import { PolarArea } from 'react-chartjs-2';
const data = {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255,99,132,0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1,
}]
};
const options = {
responsive: true,
maintainAspectRatio: false,
};
function PolarAreaChart() {
return <PolarArea data={data} options={options} />;
}
export default PolarAreaChart;
案例七:气泡图展示三维数据
气泡图适用于展示三维数据,如价格、数量和尺寸。以下是如何使用react-chartjs-2创建气泡图的示例:
import React from 'react';
import { Bubble } from 'react-chartjs-2';
const data = {
labels: ['A', 'B', 'C', 'D', 'E', 'F'],
datasets: [{
label: 'Score',
data: [
{ x: 1, y: 2, r: 2 },
{ x: 2, y: 3, r: 3 },
{ x: 3, y: 5, r: 5 },
{ x: 4, y: 4, r: 4 },
{ x: 5, y: 7, r: 7 },
{ x: 6, y: 6, r: 6 }
],
backgroundColor: 'rgba(255,99,132,0.2)',
borderColor: 'rgba(255,99,132,1)',
borderWidth: 1
}]
};
const options = {
scales: {
y: {
beginAtZero: true
}
}
};
function BubbleChart() {
return <Bubble data={data} options={options} />;
}
export default BubbleChart;
案例八:3D图表展示数据
使用react-chartjs-2和chart.js的3D插件,可以创建3D图表。以下是如何创建一个3D柱状图的示例:
import React from 'react';
import { Bar } from 'react-chartjs-2';
import { Chart as ChartJS } from 'chart.js';
import { Bar3DController, BarElement, CategoryScale, LinearScale } from 'chart.js';
ChartJS.register(Bar3DController, BarElement, CategoryScale, LinearScale);
const data = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'Sales',
data: [10, 20, 30, 40, 50, 60, 70],
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132, 1)',
borderWidth: 1
}]
};
const options = {
type: 'bar3d',
scales: {
x: {
type: 'category',
labels: data.labels
},
y: {
type: 'linear',
display: false,
beginAtZero: true
},
z: {
type: 'linear',
display: false,
beginAtZero: true
}
},
plugins: {
legend: {
display: false
}
}
};
function ThreeDBarChart() {
return <Bar data={data} options={options} />;
}
export default ThreeDBarChart;
案例九:动态图表实时更新
动态图表可以实时更新数据,非常适合展示实时数据。以下是如何使用react-chartjs-2创建动态图表的示例:
import React, { useState, useEffect } from 'react';
import { Line } from 'react-chartjs-2';
const data = {
labels: [],
datasets: [{
label: 'Real-time Data',
data: [],
fill: false,
borderColor: 'rgb(75, 192, 192)',
tension: 0.1
}]
};
const options = {
scales: {
y: {
beginAtZero: true
}
}
};
function DynamicLineChart() {
const [chartData, setChartData] = useState(data);
useEffect(() => {
const interval = setInterval(() => {
const newData = {
x: new Date().toLocaleTimeString(),
y: Math.floor(Math.random() * 100)
};
setChartData(prevData => ({
...prevData,
labels: [...prevData.labels, newData.x],
datasets: [{
...prevData.datasets[0],
data: [...prevData.datasets[0].data, newData.y]
}]
}));
}, 1000);
return () => clearInterval(interval);
}, []);
return <Line data={chartData} options={options} />;
}
export default DynamicLineChart;
案例十:自定义图表样式
自定义图表样式可以让你的图表更具有个性。以下是如何使用react-chartjs-2自定义图表样式的示例:
import React from 'react';
import { Line } from 'react-chartjs-2';
const data = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'Sales',
data: [10, 20, 30, 40, 50, 60, 70],
borderColor: 'red',
tension: 0.1,
pointBackgroundColor: 'blue',
pointBorderColor: 'blue',
pointHoverBackgroundColor: 'blue',
pointHoverBorderColor: 'blue'
}]
};
const options = {
plugins: {
legend: {
display: false
}
},
scales: {
y: {
beginAtZero: true
}
}
};
function CustomStyleChart() {
return <Line data={data} options={options} />;
}
export default CustomStyleChart;
以上是使用React图表库实现数据可视化的10个实用案例。通过这些案例,你可以从入门到精通,掌握React图表库的强大功能。祝你学习愉快!
