score:0

u use this in addition to externaltooltiphandler from chartjs this will modify position only for latest tooltip

  var currenttooltip = tooltip.datapoints[0].dataindex;
  var keys = object.keys(tooltip.datapoints[0].dataset.data);
  var latesttooltip = keys[keys.length - 1];

  if (currenttooltip == latesttooltip) {
    tooltipel.style.left = chart.canvas.offsetleft + tooltip.caretx - 70 + "px";
  } else {
    tooltipel.style.left = positionx + tooltip.caretx + "px";
  }

score:3

i did this: subtract the pixels by way of centering it or putting it in the position.

tooltipel.style.left = canvas.offsetleft + tooltip.caretx - 55 + 'px';

score:3

i had the same issue and i didn't find a good solution, so i had to dot it myself.

actually, it's simple than i thought, wish it helps someone.

demo: https://codepen.io/themustafaomar/pen/wvwzrod

 const labels = ["1 april","2 april","3 april","4 april","5 april","6 april","7 april","8 april","9 april","10 april","11 april","12 april","13 april","14 april","15 april","16 april","17 april","18 april","19 april","20 april","21 april","22 april","23 april","24 april","25 april","26 april","27 april","28 april","29 april","30 april","31 april"]
const data = [ 95, 57, 72, 54, 73, 53, 98, 75, 52, 93, 50, 65, 99, 67, 77, 61, 74, 65, 86, 92, 64, 89, 82, 62, 64, 89, 59, 75, 56, 63 ];

function customtooltips(tooltipmodel) {
// tooltip element
var tooltipel = document.getelementbyid("chartjs-tooltip");

const yalign = tooltipmodel.yalign;
const xalign = tooltipmodel.xalign;

// create element on first render
if (!tooltipel) {
  tooltipel = document.createelement("div");
  tooltipel.id = "chartjs-tooltip";
  tooltipel.innerhtml = "<table></table>";
  document.body.appendchild(tooltipel);
}

// hide if no tooltip
if (tooltipmodel.opacity === 0) {
  tooltipel.style.opacity = 0;
  return;
}

// set caret position
tooltipel.classlist.remove("top", "bottom", "center", "left", "right");
if (tooltipmodel.yalign || tooltipmodel.xalign) {
  tooltipel.classlist.add(tooltipmodel.yalign);
  tooltipel.classlist.add(tooltipmodel.xalign);
}

// set text
if (tooltipmodel.body) {
  var titlelines = tooltipmodel.title || [];
  var bodylines = tooltipmodel.body.map((bodyitem) => {
    return bodyitem.lines;
  });

  var innerhtml = "<thead>";

  titlelines.foreach(function (title) {
    innerhtml += '<tr><th><div class="mb-1">' + title + "</div></th></tr>";
  });
  innerhtml += "</thead><tbody>";

  bodylines.foreach((body, i) => {
    var colors = tooltipmodel.labelcolors[i];
    // var style = 'background-color:' + colors.bordercolor
    var style =
      "background-color:" + this._chart.data.datasets[i].bordercolor;
    var value = tooltipmodel.datapoints[i].value;
    var label = this._chart.data.datasets[i].label;

    style += "; border-color:" + colors.bordercolor;
    style += "; border-color:" + this._chart.data.datasets[i].bordercolor;
    style += "; border-width: 2px";

    var span =
      '<span class="chartjs-tooltip-key" style="' + style + '"></span>';

    innerhtml += `<tr><td> ${span} $${value}k </td></tr>`;
  });
  innerhtml += "</tbody>";

  var tableroot = tooltipel.queryselector("table");
  tableroot.innerhtml = innerhtml;
}

// tooltip height and width
const { height, width } = tooltipel.getboundingclientrect();

// chart canvas positions
const positiony = this._chart.canvas.offsettop;
const positionx = this._chart.canvas.offsetleft;

// carets
const carety = tooltipmodel.carety;
const caretx = tooltipmodel.caretx;

// final coordinates
let top = positiony + carety - height;
let left = positionx + caretx - width / 2;
let space = 8; // this for making space between the caret and the element.

// yalign could be: `top`, `bottom`, `center`
if (yalign === "top") {
  top += height + space;
} else if (yalign === "center") {
  top += height / 2;
} else if (yalign === "bottom") {
  top -= space;
}
// xalign could be: `left`, `center`, `right`
if (xalign === "left") {
  left = left + width / 2 - tooltipmodel.xpadding - space / 2;
  if (yalign === "center") {
    left = left + space * 2;
  }
} else if (xalign === "right") {
  left -= width / 2;
  if (yalign === "center") {
    left = left - space;
  } else {
    left += space;
  }
}

// display, position, and set styles for font
tooltipel.style.opacity = 1;

// left and right
tooltipel.style.top = `${top}px`;
tooltipel.style.left = `${left}px`;

// font
tooltipel.style.fontfamily = tooltipmodel._bodyfontfamily;
tooltipel.style.fontsize = tooltipmodel.bodyfontsize + "px";
tooltipel.style.fontstyle = tooltipmodel._bodyfontstyle;

// paddings
tooltipel.style.padding =
  tooltipmodel.ypadding + "px " + tooltipmodel.xpadding + "px";
}

// chart
new chart("chart", {
type: "line",
data: {
  labels,
  datasets: [
    {
      label: "custom tooltip demo",
      bordercolor: "#f66",
      backgroundcolor: "transparent",
      linetension: 0,
      borderwidth: 1.5,
      pointradius: 2,
      data
    }
  ]
},
options: {
  responsive: true,
  maintainaspectratio: false,
  legend: { display: false },
  scales: {
    // yaxes
    yaxes: [{ display: false }],

    // xaxes
    xaxes: [
      {
        display: false,
        gridlines: { display: false },
        ticks: {
          padding: 20,
          autoskippadding: 30,
          maxrotation: 0
        }
      }
    ]
  },
  tooltips: {
    enabled: false,
    intersect: false,
    mode: "index",
    position: "average",
    custom: customtooltips
  }
}
});
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}

.chartjs-wrapper {
height: 90px;
width: 300px;
margin: 25px auto 0;
border: 1px solid #e6e6e6;
}

#chartjs-tooltip {
opacity: 1;
position: absolute;
color: #fff;
background-color: #000;
border-radius: 6px;
transition: all 0.25s ease-in-out;
pointer-events: none;
}

#chartjs-tooltip:after {
content: "";
display: block;
position: absolute;
margin: auto;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 6px;
}

/* top center */
#chartjs-tooltip.top.center:after {
border-bottom-color: #000;
top: -11px;
left: 0;
right: 0;
}
/* top left */
#chartjs-tooltip.top.left:after {
border-bottom-color: #000;
left: 5px;
top: -11px;
}
/* top right */
#chartjs-tooltip.top.right:after {
border-bottom-color: #000;
right: 5px;
top: -11px;
}

/* bottom center */
#chartjs-tooltip.bottom.center:after {
border-top-color: #000;
bottom: -11px;
right: 0;
left: 0;
}
/* bottom left */
#chartjs-tooltip.bottom.left:after {
border-top-color: #000;
bottom: -11px;
}
/* bottom right */
#chartjs-tooltip.bottom.right:after {
border-top-color: #000;
bottom: -11px;
right: 5px;
}

/* center left */
#chartjs-tooltip.center.left:after {
border-right-color: #000;
margin: auto;
left: -11px;
bottom: 0;
top: 0;
}
/* center right */
#chartjs-tooltip.center.right:after {
border-left-color: #000;
margin: auto;
right: -11px;
bottom: 0;
top: 0;
}

.chartjs-tooltip-key {
display: inline-block;
border-radius: 50%;
width: 10px;
height: 10px;
margin-right: 7px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.4/chart.min.js"></script>
<div class="chartjs-wrapper">
  <canvas id="chart"></canvas>
</div>

score:11

new modes can be defined by adding functions to the chart.tooltip.positioners map (doc). this function returns the x and y position for the tooltip.

you can add a custom one to adjust the x at an offset. one way to do this would be to be:

    //register custome positioner
chart.tooltip.positioners.custom = function(elements, position) {
    if (!elements.length) {
      return false;
    }
    var offset = 0;
    //adjust the offset left or right depending on the event position
    if (elements[0]._chart.width / 2 > position.x) {
      offset = 20;
    } else {
      offset = -20;
    }
    return {
      x: position.x + offset,
      y: position.y
    }
  }

fiddle example that i created

i hope it helps.


Related Query

More Query from same tag