最近有读者问我 bar3D 颜色渐变的问题,昨天尝试了下,发现 ECharts GL 的 bar3D 似乎不支持 colorStops,似乎也不支持贴图(github 上 2018 年有个 issue 问过,pissang 回答暂不支持,昨天尝试了下也没成功……),于是只好用堆叠柱图凑合一下了。
思路大概是这样:
关键代码(颜色计算,N 个 series[i]-bar3D 生成)
function barSeriesInit(color0, color1, barData, step) {
ret = [];
// 16进制颜色转换成 RGB 颜色,并计算 color0 和 color1 之间的 RGB 差值
startR = color0.length === 7 ? parseInt(color0.substr(1, 2), 16) : parseInt(color0.substr(1, 1) + color0.substr(1, 1), 16);
startG = color0.length === 7 ? parseInt(color0.substr(3, 2), 16) : parseInt(color0.substr(2, 1) + color0.substr(2, 1), 16);
startB = color0.length === 7 ? parseInt(color0.substr(3, 2), 16) : parseInt(color0.substr(2, 1) + color0.substr(2, 1), 16);
endR = color1.length === 7 ? parseInt(color1.substr(1, 2), 16) : parseInt(color1.substr(1, 1) + color1.substr(1, 1), 16);
endG = color1.length === 7 ? parseInt(color1.substr(3, 2), 16) : parseInt(color1.substr(2, 1) + color1.substr(2, 1), 16);
endB = color1.length === 7 ? parseInt(color1.substr(3, 2), 16) : parseInt(color1.substr(2, 1) + color1.substr(2, 1), 16);
distanceR = endR - startR;
distanceG = endG - startG;
distanceB = endB - startB;
// 把原数据的 value 值变成 1/N
stepData = barData.map(function(item) {
return {
value: [item[1], item[0], item[2] / step],
};
});
// 生成 N 个 series[i]-bar3D
for (var i = 0; i <= step; i++) {
ret.push({
type: 'bar3D',
data: stepData,
stack: 'stack',
color: '#' + Math.round(startR + i * distanceR / step).toString(16) + Math.round(startG + i * distanceG / step).toString(16) + Math.round(startB + i * distanceG / step).toString(16),
label: {
textStyle: {
fontSize: 16,
borderWidth: 1
}
},
silent: true,
shading: 'lambert',
});
}
return ret;
}
提示框暂时还没做好……
bar3D 的 tooltip.trigger = 'axis' 不生效,也许不支持,如果真的不支持,可能要重写鼠标事件。最近事情多,顾不上细细研究……有空再看看
本文分享自 ZXand618的ECharts之旅 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有