ECharts 柱形图:使用 markPoint 标记最大值并添加 Base64 图片图标
在 ECharts 中,可以使用'series-bar.markPoint' 来实现柱形图上方的最大值标记。同时,可以使用'graphic' 组件来添加一个基于 base64 图片的 icon。
以下是一个使用 ECharts 实现该效果的示例代码:
// 导入 ECharts 库
import echarts from 'echarts';
// 初始化 ECharts 实例
const chart = echarts.init(document.getElementById('chart'));
// 柱形图数据
const data = [120, 200, 150, 80, 70];
// 计算最大值及其索引
const maxIndex = data.indexOf(Math.max(...data));
const maxValue = Math.max(...data);
// 使用 markPoint 标记最大值
const option = {
xAxis: {
type: 'category',
data: ['A', 'B', 'C', 'D', 'E']
},
yAxis: {
type: 'value'
},
series: [{
type: 'bar',
data: data,
markPoint: {
data: [{
name: '最大值',
value: maxValue,
xAxis: maxIndex,
yAxis: maxValue,
label: {
show: true,
position: 'top',
formatter: `{@[${maxIndex}]}`
}
}]
}
}],
graphic: [{
type: 'image',
style: {
image: 'data:image/png;base64,iVBORw0KG...', // 这里替换成你的 base64 图片
width: 20,
height: 20,
x: chart.getWidth() / 2 - 10,
y: chart.getHeight() / 2 - 10
}
}]
};
// 使用配置项初始化图表
chart.setOption(option);
在上述代码中,通过'markPoint' 的'data' 属性来指定要标记的最大值,其中'xAxis' 和'yAxis' 属性指定了最大值的位置,'label' 属性用于显示最大值的数值。
同时,通过'graphic' 组件添加了一个'image' 类型的图形,其中'image' 属性指定了 base64 图片的内容,'width' 和'height' 属性指定了图标的宽度和高度,'x' 和'y' 属性指定了图标的位置。
你需要将代码中的'data' 和'image' 属性替换为你实际的柱形图数据和 base64 图片的内容。
注意:为了在 ECharts 中使用 ES6 模块导入语法,你可能需要使用相关的构建工具(如 Webpack、Babel 等)进行配置和构建。
原文地址: https://www.cveoy.top/t/topic/qx3M 著作权归作者所有。请勿转载和采集!