score:2

Accepted answer

a solution for chartjs 2.0. we can provide a function to build a html tooltip.

here are some examples of using custom tooltips.

we turn off canvas tooltips.

enabled: false

we use the tooltips opacity state to show or hide our custom tooltip. the state is updated when a hover event is triggered.

our tooltiptitlecallback and tooltiplabelcallback functions will need to be modified depending on the parameters we're passing in. in this example, we're using the tooltip's datapoints property in our tooltiptitlecallback function. the items which are available in the data property is determined by the interaction mode used.

here are some other properties in the tooltip model.

to prevent the tooltip from going over the right hand side of the screen, we set the right position property instead of the left.

working example

const ctx = document.getelementbyid("mychart").getcontext("2d");

const customtooltip = {
  mode: "index",
  enabled: false,
  custom: function(model) {
    const tooltip = document.getelementbyid("tooltip");

    if (model.opacity === 0) {
      tooltip.style.opacity = 0;
      return;
    }

    if (model.body) {
      const title = "<strong>" + tooltiptitlecallback(model.datapoints) + "</strong>";
      const label =
        "lorem ipsum dolor sit amet, consectetur adipiscing elit. duis pellentesque eros non massa consequat, ac. lorem ipsum dolor sit amet, consectetur adipiscing elit. duis pellentesque eros non massa consequat, ac. lorem ipsum dolor sit amet, consectetur adipiscing elit. duis pellentesque eros non massa consequat, ac.";

      tooltip.innerhtml = title + "<br />" + label;

      tooltip.style.left = "auto";
      tooltip.style.right = "auto";

      const pos = this._chart.canvas.getboundingclientrect();

      if (model.xalign === "left") {
        tooltip.style.left = pos.left + model.caretx + "px";
      } else {
        tooltip.style.right = pos.right - model.caretx + "px";
      }

      tooltip.style.top = -50 + pos.top + model.carety + "px";
      tooltip.style.opacity = 1;
    }
  }
};

const mychart = new chart(ctx, {
  type: "line",
  data: {
    labels: ["january", "february", "march", "april", "may", "june", "july"],
    datasets: [{
      label: "my first dataset",
      backgroundcolor: "rgb(255, 99, 132)",
      bordercolor: "rgb(255, 99, 132)",
      data: [0, 10, 5, 2, 20, 30, 45],
      fill: false
    }]
  },
  options: {
    tooltips: customtooltip
  }
});

function tooltiptitlecallback(items) {
  if (items.length > 0) {
    const item = items[0];

    if (item.label) {
      return item.label;
    }

    return "no title";
  }
}
#tooltip {
  opacity: 0;
  position: absolute;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  border-radius: 3px;
  -webkit-transition: all 0.1s ease;
  transition: all 0.1s ease;
  pointer-events: none;
  padding: 4px;
  margin: 15px;
  font-family: 'helvetica neue', 'helvetica', 'arial', sans-serif;
  font-size: 13px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.4/chart.min.js"></script>
<canvas id="mychart" width="400" height="200"></canvas>
<div id="tooltip"></div>

codepen


Related Query