HTML
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>Chart.js Events</title>
<meta name="keywords" content="LightYear,光年,后台模板,后台管理系统,光年HTML模板">
<meta name="description" content="LightYear是一个基于Bootstrap的后台管理系统的HTML模板。">
<meta name="author" content="yinqi">
<link rel="stylesheet" type="text/css" href="http://lyear.itshubao.com/v4/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="http://lyear.itshubao.com/v4/css/style.min.css">
</head>
<body>
<div class="chart-example">
<div class="tab-content">
<div class="chart-view"><canvas id="myChart"></canvas></div>
<div class="chart-actions mt-3"></div>
<ul class="nav nav-tabs mt-3">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#myChart-config" aria-selected="true">Config</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#myChart-setup" aria-selected="false">HandleHover</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#myChart-actions" aria-selected="false">HandleLeave</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#myChart-data" aria-selected="false">Data</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active tab-pane-code" id="myChart-config">
<pre>
const config = {
type: 'pie',
data: data,
options: {
plugins: {
legend: {
onHover: handleHover,
onLeave: handleLeave
}
}
}
};
</pre>
</div>
<div class="tab-pane fade tab-pane-code" id="myChart-setup">
<pre>
// Append '4d' to the colors (alpha channel), except for the hovered index
function handleHover(evt, item, legend) {
legend.chart.data.datasets[0].backgroundColor.forEach((color, index, colors) => {
colors[index] = index === item.index || color.length === 9 ? color : color + '4D';
});
legend.chart.update();
}
</pre>
</div>
<div class="tab-pane fade tab-pane-code" id="myChart-actions">
<pre>
// Removes the alpha channel from background colors
function handleLeave(evt, item, legend) {
legend.chart.data.datasets[0].backgroundColor.forEach((color, index, colors) => {
colors[index] = color.length === 9 ? color.slice(0, -2) : color;
});
legend.chart.update();
}
</pre>
</div>
<div class="tab-pane fade tab-pane-code" id="myChart-data">
<pre>
const data = {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1,
backgroundColor: ['#CB4335', '#1F618D', '#F1C40F', '#27AE60', '#884EA0', '#D35400'],
}]
};
</pre>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="http://lyear.itshubao.com/v4/js/jquery.min.js"></script>
<script type="text/javascript" src="http://lyear.itshubao.com/v4/js/popper.min.js"></script>
<script type="text/javascript" src="http://lyear.itshubao.com/v4/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/Chart.js/4.1.1/chart.umd.min.js"></script>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/luxon/3.2.0/luxon.min.js"></script>
<script type="text/javascript" src="http://libs.itshubao.com/chartjs/utils.js"></script>
</body>
</html>
JavaScript
const data = {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1,
backgroundColor: ['#CB4335', '#1F618D', '#F1C40F', '#27AE60', '#884EA0', '#D35400'],
}]
};
// Removes the alpha channel from background colors
function handleLeave(evt, item, legend) {
legend.chart.data.datasets[0].backgroundColor.forEach((color, index, colors) => {
colors[index] = color.length === 9 ? color.slice(0, -2) : color;
});
legend.chart.update();
}
// Append '4d' to the colors (alpha channel), except for the hovered index
function handleHover(evt, item, legend) {
legend.chart.data.datasets[0].backgroundColor.forEach((color, index, colors) => {
colors[index] = index === item.index || color.length === 9 ? color : color + '4D';
});
legend.chart.update();
}
const config = {
type: 'pie',
data: data,
options: {
plugins: {
legend: {
onHover: handleHover,
onLeave: handleLeave
}
}
}
};
const ctx = document.getElementById('myChart');
const chart = new Chart(ctx, config);