You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LightningChartJS中如何自定义ButtonBox的setPictureOff/On图标?

自定义LightningChartJS ButtonBox图标方法

完全可以为ButtonBox创建并添加自定义图片,setPictureOff和setPictureOn方法支持传入SVG字符串或Image对象作为自定义图标,以下是具体实现方式:


1. 使用SVG字符串(推荐,矢量图不失真)

直接编写SVG代码作为图标参数,适合自定义简单图标(如箭头、符号等):

// 自定义向上箭头SVG
const upArrowOff = `
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  <path d="M12 5L12 19M12 5L17 10M12 5L7 10" stroke="#555" stroke-width="2" stroke-linecap="round"/>
</svg>
`;

// 自定义向下箭头SVG(点击后状态)
const downArrowOn = `
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  <path d="M12 19L12 5M12 19L17 14M12 19L7 14" stroke="#222" stroke-width="2" stroke-linecap="round"/>
</svg>
`;

const container = chart.addUIElement(UILayoutBuilders.Row);

container
  .addElement(UIElementBuilders.ButtonBox
    .setPictureOff(upArrowOff)
    .setPictureOn(downArrowOn)
  )
  .setText("")

2. 使用Image对象(适合已有图片文件)

如果已有本地或网络图片资源,可创建Image对象传入:

// 初始化图标图片对象
const offIcon = new Image();
offIcon.src = './assets/arrow-up.png';
const onIcon = new Image();
onIcon.src = './assets/arrow-down.png';

// 确保图片加载完成后再渲染按钮
Promise.all([
  new Promise(resolve => offIcon.onload = resolve),
  new Promise(resolve => onIcon.onload = resolve)
]).then(() => {
  const container = chart.addUIElement(UILayoutBuilders.Row);

  container
    .addElement(UIElementBuilders.ButtonBox
      .setPictureOff(offIcon)
      .setPictureOn(onIcon)
    )
    .setText("")
});

注意事项

  • SVG需设置合适的viewBox和尺寸,保证在按钮内显示比例正常
  • 使用外部图片时需注意跨域权限,确保资源可访问
  • 可通过修改SVG的stroke、fill属性匹配图表主题风格

内容的提问来源于stack exchange,提问作者Karolis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 09:57:04