3

I am looking for a sample of Javascript code to visualise pictures in a tooltip of a pie chart from Highcharts.

I would like to see different pictures according to the sectors I am navigating on...

Thanks in advance.

4

2 回答 2

4

根据文档,tooltip.formatter是:

... HTML 的一个子集。除非useHTML为 true,否则工具提示的 HTML 会被解析并转换为 SVG,因此这不是一个完整的 HTML 渲染器。支持以下标签:<b>, <strong>, <i>, <em>, <br/>, <span>.
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>

于 2018-09-12T02:17:58.283 回答
-1

您可以使用 tootlip 格式化程序和 useHTML,然后返回您定制的内容,包括图像。

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

于 2013-10-10T09:52:38.803 回答