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

在Highcharts中渲染含动态HTML的标题并在图表后方添加数字

How to Implement Dynamic HTML Title and Background Number in Highcharts

I’ll walk you through solving both of your requirements step by step with code examples that fit your existing configuration.

1. Render Title with Dynamic HTML

Highcharts uses SVG for rendering by default, so to enable HTML in your title, you just need to add the useHTML: true property to your title configuration. This allows the text value to parse HTML tags dynamically.

Here’s how to update your title code:

title: {
  text: title, // Your dynamic HTML string goes here (e.g., '<span style="color: #2ECC71">Sales</span> Report Q3')
  align: 'left',
  x: 10,
  useHTML: true, // Critical line to enable HTML rendering
  style: {
    color: '#000000',
    fontSize: '20px',
    fontWeight: '600',
    lineHeight: '29px'
  },
},

Just make sure your title variable contains valid HTML (like spans, links, or inline styles) and it will render correctly.

2. Add a Number to the Chart's Blank Background Area

To place a number behind your chart content, you can use Highcharts' built-in renderer API to draw custom text elements. We’ll hook into the chart’s load event to ensure the text is added after the chart is fully rendered.

Add this to your chart configuration:

chart: {
  events: {
    load: function() {
      // Adjust the position and styling to match your needs
      this.renderer.text('42', // The number you want to display
        this.plotWidth - 60, // X position (right-aligned within the plot area)
        this.plotTop + 60 // Y position (top-aligned within the plot area)
      )
      .attr({
        fill: '#f0f0f0', // Light gray for background visibility
        fontSize: '50px',
        fontWeight: 'bold',
        opacity: 0.3 // Lower opacity so it doesn't distract from chart data
      })
      .add();
    }
  }
},
  • this.plotWidth and this.plotTop reference the dimensions of the chart’s plot area (where data is drawn), so adjusting the offsets will let you position the number exactly where you want it.
  • Tweak the fill, fontSize, and opacity values to match your design.

Putting it all together, your full chart options would include both updates. Test with your dynamic HTML title and adjust the number's position/styling until it looks perfect!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:05:54