简体   繁体   English

Highcharts - 如何在工具提示中显示图例符号

[英]Highcharts - How to display legend symbol inside the tooltip

I have a problem with Highcharts that I would like to ask. 我对Highcharts有疑问,我想问一下。

I have a Highcharts, and I want to copy the preview symbol from legends to the tooltip. 我有一个Highcharts,我想将预览符号从图例复制到工具提示。

I am doing this in 2 different case: 我在两个不同的情况下这样做:

  • Lines: I have 2 different series (1 with solid, 1 with dash). 线条:我有2个不同的系列(1个带实线,1个带短划线)。 This is the default settings of highcharts so I guess it would be easier a bit. 这是highcharts的默认设置,所以我想它会更容易一些。
  • Bars: For this, I am using Pattern Fill extension from Highcharts. 酒吧:为此,我正在使用Highcharts的Pattern Fill扩展。 This is officially release from Highcharts too but not too much information regarding what to customise. 这也是从Highcharts正式发布的,但没有太多关于定制内容的信息。

Extra information: 额外的信息:

  • Using highchart 4.1.9 使用highchart 4.1.9
  • Highcharts legends symbol provide you a <svg> and I don't actually know how to copy a <svg> Highcharts传奇符号为您提供<svg> ,我实际上并不知道如何复制<svg>

Thanks in advance 提前致谢

Here's how to display the the SVG from the legend item in the tooltip (works for columns and pattern fill): 以下是如何从工具提示中的图例项目显示SVG(适用于列和图案填充):

  tooltip: {
    useHTML: true,
    pointFormatter: function() {
      var point = this,
        series = point.series,
        legendSymbol = "<svg width='20' height='20'>" + series.legendSymbol.element.outerHTML + "</svg>";

      return legendSymbol + series.name + ": <b>" + point.y + "</b><br/>";
    }
  }

useHTML must be enabled to make it work. 必须启用useHTML才能使其正常工作。

Live demo: https://jsfiddle.net/kkulig/adr9otbg/ 现场演示: https //jsfiddle.net/kkulig/adr9otbg/

This goal can be achieve by using pointFormat or pointFormatter . 这个目标可以通过使用pointFormatpointFormatter来实现。 There are couple of example using pointFormatter , So i will use pointFormat attribute to achievethis goal.With this approch you dont need to enable useHTML property. 有几个使用pointFormatter的例子,所以我将使用pointFormat属性来实现这个目标。使用这个approch你不需要启用useHTML属性。

  tooltip: {
    pointFormat:
      '<svg width="10" height="10" style="color:{series.color}">●</svg> 
       <b>{series.name}: {point.percentage:.0f}%</b>
       <br/>',
    shared: true
  },

 Highcharts.chart("container", { chart: { type: "column" }, title: { text: null }, credits: { enabled: false }, xAxis: { categories: ["Apple", "Orange", "Grapes", "Mango", "Pinapple", "Papaya"], title: "Fruits" }, yAxis: { min: 0, floor: 0, ceiling: 100, title: { text: null }, labels: { align: "right", x: 0, y: -2 } }, tooltip: { pointFormat: '<svg width="10" height="10" style="color:{series.color}">●</svg> <b>{series.name}: {point.percentage:.0f}%</b><br/>', shared: true }, plotOptions: { series: { stacking: "normal" } }, series: [ { name: "Small", data: [5, 3, 4, 7, 2, 3], color: "#A2CD32" }, { name: "Large", data: [2, 2, 3, 2, 1, 2], color: "#FFF500" }, { name: "Medium", data: [3, 4, 4, 2, 5, 2], color: "#FF220" } ] }); 
 <script src="https://code.highcharts.com/highcharts.js"></script> <div id="container" style="min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM