使用HTML的Highcharts工具提示允许像图像一样的高级格式

时间:2013-10-10 08:58:37

标签: highcharts

我正在寻找一个Javascript代码示例,用于在Highcharts的饼图工具提示中可视化图片。

我希望根据我正在浏览的行业看到不同的图片......

提前致谢。

2 个答案:

答案 0 :(得分:3)

根据文档,tooltip.formatter为:

  

... HTML的子集。除非useHTML为真,否则将解析工具提示的HTML并将其转换为SVG,因此这不是完整的HTML渲染器。支持以下标记:<b><strong><i><em><br/><span>
   可以使用style属性设置跨度样式,但是仅处理与SVG共享的与文本相关的CSS。

所以您应该这样设置工具提示:

tooltip: {
  useHTML: true,
  formatter: function () {
    return '<b>' + this.point.name + "</b>: " + this.point.percentage + "%<br/>" + 
           "<img src='" + this.point.img + "'></img>";
  }
},

这是Stack Snippets中的一个演示

Highcharts.chart('container', {
    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false,
        type: 'pie'
    },
    title: {
        text: 'Favorite Fruits'
    },
    tooltip: {
      useHTML: true,
      formatter: function () {
        return '<b>' + this.point.name + "</b>: " + this.point.percentage + "%<br/>" + 
               "<img src='" + this.point.img + "'></img>";
      }
    },
    plotOptions: {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: false
            },
            showInLegend: true
        }
    },
    series: [{
        name: 'Fruits',
        colorByPoint: true,
        data: [{
            name: 'Apple',
            y: 50,
            img: "https://i.imgur.com/MmK9Xkc.png"
        }, {
            name: 'Banana',
            y: 25,
            img: "https://i.imgur.com/0G6GXWf.png"
        }, {
            name: 'Orange',
            y: 15,
            img: "https://i.imgur.com/Dv4KoD5.png"
        }, {
            name: 'Watermelon',
            y: 10,
            img: "https://i.imgur.com/2LN8PfD.png"
            
        }, ]
    }]
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/highcharts/6.1.2/css/highcharts.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highcharts/6.1.2/highcharts.js"></script>


<div id="container" style="min-width: 310px; height: 400px; max-width: 600px; margin: 0 auto"></div>

答案 1 :(得分:-1)

您可以使用工具提示格式化程序并使用HTML,然后返回您的自定义内容,包括图像。

http://api.highcharts.com/highcharts#tooltip.formatter

相关问题