javascript - 如何避免悬停元素chartjs触发下一个元素悬停
问题描述
我需要为图表 js 中的条显示独立的悬停效果。
问题
当我悬停其中一个栏时,下一个将获得他的悬停样式。
如果我将黑色悬停,它应该会得到较浅的颜色,但除非我悬停它,否则蓝色不能改变。
这是我的代码。没有模拟数据
import React from 'react';
import { Bar } from 'react-chartjs-2';
import { Wrapper, LegendsWrapper } from './style';
import Legend from './Legend/Legend';
const Chart = ({ labels, graphData, colors = ['#17C7D6', '#02193D'] }) => {
const innerData = {
labels,
datasets: graphData.map(({ category, data }, i) => ({
label: category,
data,
backgroundColor: Array(data.length).fill(colors[i]),
yAxisID: category,
})),
};
const options = {
tooltips: {
filter: tooltipItem => tooltipItem.datasetIndex === 0,
backgroundColor: `#fff`,
titleFontFamily: `var(--main-font)`,
titleFontColor: '#000',
bodyFontFamily: `var(--main-font)`,
bodyFontSize: 10,
bodyFontColor: `#888888`,
// mode: 'nearest',
cornerRadius: 4,
position: 'nearest',
},
onHover: (e, el) => {
const section = el[0];
const currentStyle = e.target.style;
console.log(e.target);
currentStyle.color = section && section._start ? 'red' : 'green'; // <-- default here
currentStyle.cursor = section ? 'pointer' : 'default'; // <-- default here
},
layout: {
padding: {
left: 0,
right: 0,
top: 0,
bottom: 10,
},
},
legend: {
display: false,
},
maintainAspectRatio: false,
scales: {
pointLabels: {
fontStyle: 'bold',
},
xAxes: [
{
categoryThickness: 200,
barThickness: 12,
gridLines: {
// display: false,
drawOnChartArea: false,
tickMarkLength: 4,
color: `#052442`,
},
ticks: {
min: 0,
fontFamily: `var(--main-font)`,
fontSize: 8,
fontColor: `#9E9E9E`,
padding: 15,
maxRotation: -45,
minRotation: -45,
},
},
],
yAxes: [
{
id: graphData[0].category,
ticks: {
min: 0,
fontFamily: `var(--main-font)`,
fontSize: 8,
fontColor: `#6e6e6e`,
padding: 5,
},
gridLines: {
drawOnChartArea: false,
drawBorder: true,
tickMarkLength: 4,
color: `#052442`,
},
},
{
id: graphData[1].category,
position: 'right',
ticks: {
fontFamily: `var(--main-font)`,
fontSize: 8,
fontColor: `#6e6e6e`,
padding: 5,
},
gridLines: {
// display: false,g
drawOnChartArea: false,
drawBorder: true,
tickMarkLength: 4,
color: `#052442`,
},
},
],
},
};
return (
<Wrapper>
<LegendsWrapper>
{graphData.map(({ category }, i) => (
<Legend text={category} color={colors[i]} />
))}
</LegendsWrapper>
<Bar data={innerData} options={options} />
</Wrapper>
);
};
export default Chart;
模拟数据
labels: ['1950', '1960', '1970', '1980', '1990', '2000', '2010'],
graphData: [
{
category: 'EHR Patients',
data: [11500, 11600, 14000, 19700, 13000, 3000, 2300],
},
{
category: 'IGM Patients',
data: [1.2, 3.8, 16.2, 19, 11.7, 1.4, 1.2],
},
],
我已经尝试过hover:() => {}
,hoverBackgroundColor
但它总是会触发机器人栏的悬停样式。
这是完整的图表。
预期行为
当我悬停黑色条时,它会将他的颜色更改为任何其他颜色,例如“红色”。
但是蓝色的应该保持不变,当我将蓝色悬停时,它应该将他的颜色更改为任何其他颜色,例如“绿色”。
解决方案
推荐阅读
- python - 再次将导出为文本的 numpy 数组转换为 numpy 数组
- java - 详细的初始化过程类变量初始化器
- python - 在Django中显示从应用程序到网站主页的单个记录
- python - 如何使用 python-docx 或任何其他类似的包从 Word 文档中保存字体样式(粗体和斜体)
- html - 如何在wordpress中循环出html代码?
- javascript - 在通过开发工具手动提交之前,Vuex 不会提交更改
- python - 使用 python 将 powerpoint 演示文稿分成一组幻灯片
- node.js - 如何使用 .sh 文件配置 heroku psql 数据库?
- python - 根据 csv 文件移动文件
- javascript - Three.js 对象不显示,只显示黑屏