0

I am modify the tooltip of Highchart JS in graph. But output is not coming as I expect. I want to print the percentage at the right side of tool tip.

Example: Satisfied to Loyal: 4 (20%)

I have added two array to add the valueSuffix at the right side of tooltip according to series of graph. but it is printing all the array value on single tooltip.

I have tried below code to modify the Highchart.

$(function() {
  $('#container').highcharts({
    title: {
      text: ''
    },
    charts: {
      zoomType: 'xy',
    },
    exporting: {
      enabled: false
    },
    xAxis: {
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
      ],
    },
    yAxis: {
      title: '',
    },
    labels: {
      items: [{
        style: {
          left: '50px',
          top: '18px',
        }
      }]
    },
    tooltip: {
      shared: true,
    },
    credits: {
      enabled: false
    },
    plotOptions: {
      series: {
        label: {
          enabled: false
        },
      },
      column: {
        states: {
          hover: {
            color: '#90D0FF'
          }
        }
      }
    },
    series: [{
        type: 'column',
        name: 'Provide Feedback',
        data: [10, 4, 5, 6, 8, 9, 2, 3, 4, 5, 6, 9],
        color: 'yellow'
      },
      {
        name: 'Satisfied to Loyal',
        type: 'spline',
        data: [2, 5, 4, 3, 2, 3, 7, 8, 9, 5, 4, 3],
        color: '#55BF3B',
        dataLabels: {
          enabled: false
        },
        tooltip: {
          valueSuffix: [' (1%)', ' (2%)', ' (18%)', ' (10%)', ' (3%)', ' (1%)', ' (1%)', ' (6%)', ' (4%)', ' (1%)', ' (8%)', ' (70%)'],
          // valueSuffix: ' (val %)',
        },
        marker: {
          lineWidth: 4,
          fillColor: 'white',
          width: 16,
          height: 16
        }
      },
      {
        name: 'Unhappy to Satisfied',
        type: 'spline',
        data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 5, 4],
        color: '#FFC800',
        tooltip: {
          valueSuffix: [' (10%)', ' (12%)', ' (1%)', ' (100%)', ' (30%)', ' (10%)', ' (10%)', ' (60%)', ' (34%)', ' (10%)', ' (98%)', ' (40%)'],
          // valueSuffix: ' (val %)',
        },
        marker: {
          lineWidth: 4,
          fillColor: 'white',
          width: 16,
          height: 16
        }
      }
    ]
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

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

Any help much appreciated.

2 Answers 2

1

You can get suffix from an array which is out of the chart in pointFormatter function:

var valueSuffix1 = [' (1%)', ' (2%)', ' (18%)', ' (10%)', ' (3%)', ' (1%)', ' (1%)', ' (6%)', ' (4%)', ' (1%)', ' (8%)', ' (70%)'];

var valueSuffix2 = [' (10%)', ' (12%)', ' (1%)', ' (100%)', ' (30%)', ' (10%)', ' (10%)', ' (60%)', ' (34%)', ' (10%)', ' (98%)', ' (40%)'];

$('#container').highcharts({

    ...

    tooltip: {
        shared: true,
        pointFormatter: function() {
            var suffix = '';

            if (this.series.index === 1) {
                suffix = valueSuffix1[this.index]
            } else if (this.series.index === 2) {
                suffix = valueSuffix2[this.index]
            }

            return '<span style="color:' + this.color + '">\u25CF</span> ' + this.series.name + ': <b>' + this.y + '</b>' + suffix + '<br/>'
        }
    }
});

Live demo: http://jsfiddle.net/BlackLabel/8L5q7h4d/

API Reference: https://api.highcharts.com/highcharts/tooltip.pointFormatter

Sign up to request clarification or add additional context in comments.

Comments

0

You can use formatter function of tool tip from XAxis. Instead giving percentage you can calculate dynamic with data during tool tip formatting here is plunker

enter code here
$(function () {
       $('#container').highcharts({
           title: {
       text: ''
     },
     charts: {
       zoomType: 'xy',
     },
     exporting: { enabled: false },
     xAxis: {
       categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                   'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
     },
     yAxis: {
       title: '',
     },
     labels: {
       items: [{
         style: {
           left: '50px',
           top: '18px',
         }
       }]
     },
     tooltip: {
       formatter: function () {
            var s = '<b>' + this.x + '</b>';
            let total = this.points[0].y;
            let happy =  this.points[1].y;
            let unhappy =  this.points[2].y;
            s += '<br/> Provide Feedback : ' +
                    total + '';
            s += '<br/>Satisfied to Loyal: ' +
            (happy/total)*100 + '%';
            s += '<br/>Unhappy to Satisfied: ' +
                    (unhappy/total)*100 + '%';
            return s;
        },
       shared: true,
     },
     credits: {
       enabled: false
     },
     plotOptions: {
       series: {
         label: {
           enabled: false
         },
       },
       column: {
         states: {
           hover: {
             color: '#90D0FF'
           }
         }
       }
     },
     series:
       [
         {
           type: 'column',
           name: 'Provide Feedback',
           data: [10,4,5,6,8,9,2,3,4,5,6,9],
           color: 'yellow'
         },
         {
           name: 'Satisfied to Loyal',
           type: 'spline',
           data: [2,5,4,3,2,3,7,8,9,5,4,3],
           color: '#55BF3B',
           dataLabels: {
             enabled: false
           },
           marker: {
             lineWidth: 4,
             fillColor: 'white',
             width: 16,
             height: 16
           }
         },
         {
           name: 'Unhappy to Satisfied',
           type: 'spline',
           data: [1,2,3,4,5,6,7,8,9,0,5,4],
           color: '#FFC800',
           marker: {
             lineWidth: 4,
             fillColor: 'white',
             width: 16,
             height: 16
           }
         }
       ]
       });
   });

1 Comment

Thanks for the answer. But I am not able to get the icon in tooltip and my percentage calculation is coming from backend code. the calculation is not associated with unhappy, happy and total.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.