自定义颜色与样式
通过 Chart.js 提供的丰富配置,可以轻松自定义图表的颜色和样式。你可以设置数据集的背景色、边框色、线条样式,甚至调整图例和坐标轴的字体颜色和大小,打造符合需求的个性化图表。
(function () {
const ctx = document.getElementById("customStyleChart").getContext("2d");
const customStyleChart = new Chart(ctx, {
type: "bar",
data: {
labels: ["红", "蓝", "黄", "绿", "紫", "橙"],
datasets: [
{
label: "自定义样式数据",
data: [10, 20, 15, 25, 18, 30],
backgroundColor: [
"rgba(255, 99, 132, 0.8)",
"rgba(54, 162, 235, 0.8)",
"rgba(255, 206, 86, 0.8)",
"rgba(75, 192, 192, 0.8)",
"rgba(153, 102, 255, 0.8)",
"rgba(255, 159, 64, 0.8)",
],
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: 2,
},
],
},
options: {
responsive: true,
plugins: {
legend: {
display: true,
labels: {
color: "#333",
font: {
size: 14,
weight: "bold",
},
},
},
tooltip: {
enabled: true,
backgroundColor: "rgba(0,0,0,0.7)",
titleFont: { size: 16 },
bodyFont: { size: 14 },
},
},
scales: {
x: {
ticks: { color: "#555", font: { size: 12 } },
grid: { color: "#eee" },
},
y: {
ticks: { color: "#555", font: { size: 12 } },
grid: { color: "#eee" },
},
},
},
});
})();
源码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<title>Chart.js 自定义颜色与样式示例</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body {
max-width: 700px;
margin: 2rem auto;
font-family: Arial, sans-serif;
background: #fafafa;
padding: 1rem;
}
canvas {
background: #fff;
border: 1px solid #ccc;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="customStyleChart" width="700" height="400"></canvas>
<script>
(function () {
const ctx = document.getElementById(‘customStyleChart’).getContext(‘2d’);
new Chart(ctx, {
type: ‘bar’,
data: {
labels: [‘红’, ‘蓝’, ‘黄’, ‘绿’, ‘紫’, ‘橙’],
datasets: [{
label: ‘自定义样式数据’,
data: [10, 20, 15, 25, 18, 30],
backgroundColor: [
‘rgba(255, 99, 132, 0.8)’,
‘rgba(54, 162, 235, 0.8)’,
‘rgba(255, 206, 86, 0.8)’,
‘rgba(75, 192, 192, 0.8)’,
‘rgba(153, 102, 255, 0.8)’,
‘rgba(255, 159, 64, 0.8)’
],
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: 2
}]
},
options: {
responsive: true,
plugins: {
legend: {
display: true,
labels: {
color: ‘#333’,
font: {
size: 14,
weight: ‘bold’
}
}
},
tooltip: {
enabled: true,
backgroundColor: ‘rgba(0,0,0,0.7)’,
titleFont: { size: 16 },
bodyFont: { size: 14 }
}
},
scales: {
x: {
ticks: { color: ‘#555’, font: { size: 12 } },
grid: { color: ‘#eee’ }
},
y: {
ticks: { color: ‘#555’, font: { size: 12 } },
grid: { color: ‘#eee’ }
}
}
}
});
})();
</script>
</body>
</html>
相关内容
- 什么是 Chart.js? Chart.js 是一个简单易用且功能强大的 JavaScript 图表库,支持多种常见图表类型,帮助开发者快速创建美观且响应式的数据可视化效果,适合初学者和专...
- 安装与引入 介绍如何在项目中安装和引入 Chart.js,包括通过 CDN 快速使用和通过 npm 安装到前端构建环境的多种方式,帮助快速搭建图表开发环境。
- 基本结构与初始化 讲解如何创建图表的基本结构与初始化流程,包括配置画布、准备数据和实例化图表的步骤,帮助快速搭建图表展示。
- 示例:折线图 通过示例讲解如何创建折线图,涵盖数据配置、样式设置和基本交互,帮助快速掌握折线图的绘制技巧。
- 折线图 Line 介绍如何使用 Chart.js 创建折线图,详细讲解数据配置、样式调整及交互功能,帮助快速制作趋势图表。
- 柱状图 Bar 介绍如何使用 Chart.js 创建柱状图,详细讲解数据配置、颜色设置及图表样式,帮助快速绘制直观的条形数据展示。