Guest User

ChartJS Custom Tooltip

a guest
Sep 22nd, 2015
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # This file overloads the ChartJS tooltip because we need more customizability.
  3. #
  4. # Note: At the current time it only supports the Pie / Doughnut chart.
  5. # Note: You are responsible for creating the CSS yourself.
  6. #
  7. # @see http://stackoverflow.com/questions/32703404/custom-data-in-label-on-chartjs-pie-chart?noredirect=1
  8. #
  9.  
  10. class ChartJsCustomTooltip
  11.  
  12.     # The unique ID of this item.
  13.     id: null
  14.  
  15.     # The tooltip container we're working with.
  16.     tooltip: null
  17.  
  18.     # The HTML we'll inject into the tooltip. Note that it MUST contain this container div with its ID, or this class
  19.     # won't be able to identify it. Aside from that, fill the container with whatever HTML you need. and use strings in
  20.     # curly braces for replacements. Unidentifiable replaces will be ignored and printed as written in the supplied HTML
  21.     # string.
  22.     html: "<div class='customTooltip' style='display: none;' id='customTooltipContainer_{_id}'></div>"
  23.  
  24.     # The canvas element on which the chart we are working with is rendered.
  25.     canvasElement: null
  26.  
  27.     # The ChartJS object we're working with.
  28.     chartElement: null
  29.  
  30.     # The ChartJS data object from which we'll attempt to find values.
  31.     data: null
  32.  
  33.  
  34.     # Sets the HTML we'll be working with.
  35.     # Use curly braces for replaces, i.e. "{label}" will be replaced with the "label" value from @data.
  36.     # replaces starting with an underscore (e.g. "{_id}" are internal ones for this class.
  37.     setHtml: (html) ->
  38.         @html = html
  39.  
  40.  
  41.     # As long as myChart.getSegmentsAtEvent() does not return all keys from dataset, we can search an specif dataset
  42.     # entry by "value" and "value" keys. In fact, you could provide an unique ID so it would be a lot easier to
  43.     # implement (findById(id, dataset)), because you could to search by ID, instead of label/value keys together.
  44.     findEntryByLabelAndValue: (label, value) ->
  45.         for item in @data
  46.             return item if item.label is label and item.value is value
  47.  
  48.         return null
  49.  
  50.  
  51.     # Creates an element that we can work with.
  52.     createElement: (e) ->
  53.         segmentData = @chartElement.getSegmentsAtEvent e
  54.  
  55.         return if not segmentData[0]?
  56.  
  57.         html = @html
  58.         data = @findEntryByLabelAndValue segmentData[0].label, segmentData[0].value
  59.         matches = html.match /\{[^\}]+\}/g
  60.         for match in matches
  61.             # Remove curly braces to get the actual value.
  62.             matchValue = match.substr 1, match.length - 2
  63.             if data[matchValue]?
  64.                 html = html.replace match, data[matchValue]
  65.  
  66.         html = html.replace '{_id}', @id
  67.         $el = $(html)
  68.  
  69.         @tooltip = $el
  70.  
  71.         css = {
  72.             position: "absolute"
  73.  
  74.             # Fix positioning.
  75.             top: (event.clientY + ($el.outerHeight(true) / 3)) + "px"
  76.             left: (event.clientX - ($el.outerWidth(true) / 2)) + "px"
  77.         }
  78.         $el.css(css).show() # @todo Animate?
  79.  
  80.         # If we haven't already created the element in the DOM, do so.
  81.         existingElement = $("body").find("#customTooltipContainer_" + @id)
  82.         if existingElement.length is 1
  83.             existingElement.replaceWith $el
  84.         else
  85.             $("body").append $el
  86.  
  87.  
  88.     # Displays the custom tool tip.
  89.     displayCustomTooltip: (e) ->
  90.         # @todo Just display data here? What about if the data changes?
  91.         @createElement e
  92.  
  93.  
  94.     # Attaches the events displaying and hiding the tooltip when moving the mouse.
  95.     attachEvents: ->
  96.         @canvasElement.on "mousemove", (e) =>
  97.             @displayCustomTooltip e
  98.  
  99.         @canvasElement.on "mouseout", (e) =>
  100.             @tooltip.hide()
  101.  
  102.  
  103.     # Constructor.
  104.     constructor: (canvasElement, chartElement, data) ->
  105.         @canvasElement = canvasElement
  106.         @chartElement = chartElement
  107.         @data = data
  108.         @id = (new Date()).getTime()
  109.  
  110.         @attachEvents()
Advertisement
Add Comment
Please, Sign In to add comment