13693261870
2022-09-16 354b3dbfbffb3df45212a2a44dbbf48b4acc2594
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module.
    define([], function () {
      return (root.returnExportsGlobal = factory());
    });
  } else if (typeof exports === 'object') {
    // Node. Does not work with strict CommonJS, but
    // only CommonJS-like enviroments that support module.exports,
    // like Node.
    module.exports = factory();
  } else {
    root['Chartist.plugins.tooltips'] = factory();
  }
}(this, function () {
 
  /**
   * Chartist.js plugin to display a data label on top of the points in a line chart.
   *
   */
  /* global Chartist */
  (function(window, document, Chartist) {
    'use strict';
 
    var defaultOptions = {
      prefix: undefined,
      suffix: undefined
      // showTooltips: true,
      // tooltipEvents: ['mousemove', 'touchstart', 'touchmove'],
      // labelClass: 'ct-label',
      // labelOffset: {
      //   x: 0,
      //   y: -10
      // },
      // textAnchor: 'middle'
    };
 
    Chartist.plugins = Chartist.plugins || {};
    Chartist.plugins.tooltip = function(options) {
 
      options = Chartist.extend({}, defaultOptions, options);
 
      return function tooltip(chart) {
        var tooltipSelector = '.ct-point';
        if (chart instanceof Chartist.Bar) {
          tooltipSelector = '.ct-bar';
        } else if (chart instanceof Chartist.Pie) {
          tooltipSelector = '.ct-slice';
        }
 
        var $chart = $(chart.container);
        var $toolTip = $chart
        .append('<div class="chartist-tooltip"></div>')
        .find('.chartist-tooltip')
        .hide();
 
        $chart.on('mouseenter', tooltipSelector, function() {
          var $point = $(this);
          var tooltipText = '';
 
          if ($point.attr('ct:meta')) {
            tooltipText += $point.attr('ct:meta') + ': ';
          } else {
            // For Pie Charts also take the labels into account
            // Could add support for more charts here as well!
            if (chart instanceof Chartist.Pie) {
              var label = $point.next('.ct-label');
              if (label.length > 0) {
                tooltipText += label.text() + ': ';
              }
            }
          }
 
          var value = $point.attr('ct:value');
          if (options.prefix !== undefined && options.prefix) {
            value = options.prefix + value.replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1,");
          }
          tooltipText += value;
 
          if (options.suffix !== undefined && options.suffix) {
            tooltipText += options.suffix;
          }
 
          $toolTip.html(tooltipText).show();
        });
 
        $chart.on('mouseleave', tooltipSelector, function() {
          $toolTip.hide();
        });
 
        $chart.on('mousemove', function(event) {
          $toolTip.css({
            left: (event.offsetX || event.originalEvent.layerX) - $toolTip.width() / 2 + 5,
            top: (event.offsetY || event.originalEvent.layerY) - $toolTip.height() - 10
          });
        });
      }
    };
 
  }(window, document, Chartist));
 
  return Chartist.plugins.tooltips;
 
}));