我为我们的组制作了一个小仪表板,用户希望在条形图上悬停以获得不同实现阶段的定义。我有悬停的部分要做,但我正在清理遗漏的东西。所有三个工具提示显示每个数据点。我有三个数据点:无实现、预实现和实现/维持。因此,我有三个定义。然而,这三个定义都显示在一个数据点上。例如:在“无实现”(No Implementation )上方,有人看到“无实现定义”、“预实现定义”和“实现定义”。它应该只显示“无实现定义”。
我肯定这是个简单的解决办法,而且我只是忽略了一些事情。任何帮助都是非常感谢的。这是代码库,下面是我的代码。Codepen:https://codepen.io/tenebris_silentio/pen/GRZPedx
</head>
<body>
<div class="container">
<main>
<div class="dashboard-container">
<div class="card-1">
<h4 class="chart-lbl">
<h4> Title 1<h4>
</h4>
<div class="divider">
</div>
<div class="doughnut-chart-container">
<canvas class="doughnut-chart" id="doughnut">
</canvas>
</div>
</div>
<div class="card-2">
<h4 class="chart-lbl">
<h4>Title 2</h4>
</h4>
<div class="divider">
</div>
<div class="pie-chart-container">
<canvas class="pie-chart" id="pie">
</canvas>
</div>
</div>
<div class="card-3">
<h4 class="chart-lbl">
<h4>Implementation stage at end of projects</h4>
</h4>
<div class="divider">
</div>
<div class="bar-chart-container">
<canvas class="bar-chart" id="bar">
</canvas>
</div>
</div>
</div>
</main>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js">
</script>
<script src="js/generate_chart.js">
</script>
</body>
</html>
<style>
:root {
--grey-d1: #585858;
--grey-d2: #F4F4F4;
--grey-d3: #000000;
--red-1: #F2B8D1;
--red-2: #F04B92;
--red-3: #EB1E77;
--red-4: #AD1257;
--white: #FFFFFF;
--blue: #329EF4;
--grey: #eeeeee;
}
html,
body {
font-family: sans-serif;
height: 100%;
background-color: var(--grey);
}
.card-1,
.card-2,
.card-3 {
background-color: white;
border-radius: 15px;
box-shadow: 2px 2px 5px 2px #D7D7D7;
}
.chart-lbl {
margin: 5px;
color: var(--grey-d3);
opacity: 0.8;
}
h4 {
font-size: 16px;
font-weight: bold;
}
.divider {
background-color: var(--grey-d2);
height: 1px;
margin: auto;
width: 70%;
}
.container {
margin: 5px auto;
}
.dashboard-container {
display: grid;
grid-template: 33% / 100%;
grid-gap: 10px;
}
.divider + div {
padding: 15px;
height: calc(100% - 41px);
}
@media only screen and (min-width: 220px) {
.card-1,
.card-2,
.card-3 {
border-radius: 8px;
}
.card-1 {
grid-row: 1 / 3;
}
.card-2 {
grid-row: 3 / 5;
}
.card-3 {
grid-row: 5 / 7;
}
}
@media only screen and (min-width: 792px) {
.dashboard-container {
grid-template: 19% 19% 19% 10% 10% 10% / repeat(2, 50%);
}
.card-1 {
grid-column: 1 / 2;
grid-row: 1 / 3;
}
.card-2 {
grid-column: 2 / 3;
grid-row: 1 / 3;
}
.card-3 {
grid-column: 1 / 3;
grid-row: 3 / 7;
}
}
@media only screen and (min-width: 1100px) {
.dashboard-container {
grid-template: repeat(5, 1fr) / repeat(11, 1fr);
grid-gap: 8px;
margin: 0;
padding: 5px;
}
.card-1 {
grid-column: 1 / 6;
grid-row: 1 / 3;
}
.card-2 {
grid-column: 1 / 6;
grid-row: 3 / 5;
}
.card-3 {
grid-column: 6 / 12;
grid-row: 1 / 5
}
.container {
max-width: 1270px;
}
}
@media screen and (min-width: 1200px) {
.dashboard-container {
max-width: 1500px;
}
}
</style>
<script>
var doughnut = document.getElementById('doughnut');
var doughnutConfig = new Chart(doughnut, {
type: 'horizontalBar',
data: {
labels: ['data-1', 'data-2', 'data-3', 'data-4'],
datasets: [{
label: '# of data',
data: [11, 30, 20, 1],
backgroundColor: ['rgba(0, 0, 89)', 'rgba(0, 0, 89)', 'rgba(0, 0, 89)', 'rgba(0, 0, 89)', 'rgba(0, 0, 89)'],
borderWidth: 1
}]
},
options: {
legend: {
display: false,
},
responsive: true, // Instruct chart js to respond nicely.
maintainAspectRatio: true, // Add to prevent default behaviour of full-width/height
}
});
//pie chart
var pie = document.getElementById('pie');
var doughnutConfig = new Chart(pie, {
type: 'horizontalBar',
data: {
labels: ['d1', 'd2', 'd3', 'd4', 'd5', 'd6'],
datasets: [{
label: '# of data',
data: [11, 30, 20, 14, 11, 3],
backgroundColor: ['rgba(0, 0, 89)', 'rgba(0, 0, 89)', 'rgba(0, 0, 89)', 'rgba(0, 0, 89)', 'rgba(0, 0, 89)', 'rgba(0, 0, 89)'],
borderWidth: 1
}]
},
options: {
legend: {
display: false,
},
responsive: true, // Instruct chart js to respond nicely.
maintainAspectRatio: true, // Add to prevent default behaviour of full-width/height
}
});
//bar chart
var bar = document.getElementById('bar');
var barConfig = new Chart(bar, {
type: 'bar',
data: {
labels: ['Implementation not part of objectives', 'Pre-Implementation', 'Implementation/Sustainment'],
datasets: [{
label: '# of data',
data: [30, 25, 20],
info: [
['No Implementation Definition', 'Pre-Implementation Definition', 'Implementation Definition'],
],
backgroundColor: ['rgba(0, 0, 89)', 'rgba(0, 0, 89)', 'rgba(0, 0, 89)'],
borderWidth: 1
}]
},
options: {
tooltips: {
callbacks: {
title: (tooltipItems, data) => data.labels[tooltipItems[0].index],
label: (tooltipItems, data) => data.datasets[tooltipItems.datasetIndex].label + ' ' + data.datasets[tooltipItems.datasetIndex].data[tooltipItems.index],
footer: (tooltipItems, data) => ['', 'Description:'].concat(data.datasets[tooltipItems[0].datasetIndex].info)
}
},
legend: {
display: false,
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
},
responsive: true, // Instruct chart js to respond nicely.
maintainAspectRatio: false, // Add to prevent default behaviour of full-width/height
}
})
</script>发布于 2020-09-23 16:50:08
将info作为数组的数组,.Instead只需拥有[ ]单个数组,然后为单个图片组获取info数组,如下面的data.datasets[tooltipItems[0].datasetIndex].info[tooltipItems[0].index]。
演示代码:
//bar chart
var bar = document.getElementById('bar');
var barConfig = new Chart(bar, {
type: 'bar',
data: {
labels: ['Implementation not part of objectives', 'Pre-Implementation', 'Implementation/Sustainment'],
datasets: [{
label: '# of data',
data: [30, 25, 20],
info: [
'No Implemention Definition', 'Pre-Implementation Definition', 'Implementation Definition'
],//simply have array
backgroundColor: ['rgba(0, 0, 89)', 'rgba(0, 0, 89)', 'rgba(0, 0, 89)'],
borderWidth: 1
}]
},
options: {
tooltips: {
callbacks: {
title: (tooltipItems, data) => data.labels[tooltipItems[0].index],
label: (tooltipItems, data) => data.datasets[tooltipItems.datasetIndex].label + ' ' + data.datasets[tooltipItems.datasetIndex].data[tooltipItems.index],
footer: (tooltipItems, data) => 'Description:' + (data.datasets[tooltipItems[0].datasetIndex].info[tooltipItems[0].index])
}
},
legend: {
display: false,
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
},
responsive: true, // Instruct chart js to respond nicely.
maintainAspectRatio: false, // Add to prevent default behaviour of full-width/height
}
})<script src="https://code.jquery.com/jquery-3.4.1.slim.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.css" rel="stylesheet" type="text/css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<body>
<div class="container">
<main>
<div class="dashboard-container">
<div class="card-3">
<h4 class="chart-lbl">
<h4>Implementation stage at end of projects</h4>
</h4>
<div class="divider">
</div>
<div class="bar-chart-container">
<canvas class="bar-chart" id="bar">
</canvas>
</div>
</div>
</div>
</main>
</div>
https://stackoverflow.com/questions/64030583
复制相似问题