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.
Source: stackoverflow.com
Related Query
- ChartJs custom tooltip position
- ChartJS tooltip position / placement
- Create an interactive custom tooltip for chartjs
- ChartJS v2 custom tooltip for rLabel
- ChartJS - Custom tooltip with icon
- Chartjs tooltip anchor point position on bar charts
- How to create a custom tooltip for chartJS graph with data in JSON array in JavaScript?
- Custom text in tooltip and legend: ChartJs
- How to toggle between Custom tooltip and normal tooltip in chartjs and angular
- ChartJS add custom tooltip to a stacked bar chart
- ChartJS custom tooltip doesn't render background on labels (only the title)
- Updating Chartjs to 2.5 with custom code
- How to set Custom tooltip event with Chartjs version 2.X
- Chartjs v3 tooltip label not showing tooltip label color on custom calbacks
- chartjs - How to access chart instance in a custom tooltip callback
- ChartJs (ver: 2.8.0): Custom tooltip does not hide if clicked (or mouse pointer is moved) outside the chart canvas area
- ChartJS - line graph, position tooltip
- Angular-ChartJs : Use a service inside a custom tooltip of a graphic chartJs
- ChartJS - Help me custom tooltip
- JavaScript Chart.js - Custom data formatting to display on tooltip
- Custom Legend with ChartJS v2.0
- Chartjs Tooltip Line Breaks
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- How to disable a tooltip for a specific dataset in ChartJS
- How to modify chartjs tooltip so i can add customized strings in tooltips
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- Chart JS custom tooltip option?
- chartjs : how to set custom scale in bar chart
- chartjs tooltip is cut off when out of canvas
- show label in tooltip but not in x axis for chartjs line chart
More Query from same tag
- Chart.js is it possible to fire javascript when clicking on point on the graph?
- Chart Js Line chart with fill on click with full information of its legend text
- How to extend chartjs line charts in ReactJS to show solid and dashed line together?
- Create a weekly line chart (52 labels)
- Vue.js Vue-chart.js linear gradient background fill
- Non-static values for chart using CanvasJS?
- Angular and ChartJS colors
- Chart.js line chart with area range
- How to remove trial version watermark in canvasjs using Jquery (without license)
- chart.js not allowing y axis steps with logarithmic scale
- "Process is not defined" with react-chartjs-2 CDN
- How to remove the Legend of chart from angular Chart.js
- How can I load multiple Chartjs charts with different data on the same page?
- Change height of chartjs dynamically
- Chart.js: Set different options to datasets
- Changing default values for Chart JS
- How to display date as label on x-axis in chart.js
- Yii2 unable to view chart
- Chart.js - Increase space between y Axes and the first column bar
- How to create datasets dynamically for chart.js Line chart?
- How to wait for data in useEffect hook before displaying it?
- Using data from API with Chart JS
- Chart.js two categorical scales
- Chart JS Y-axis labeling
- 2 or more charts on same page?
- chartjs-plugin-dragdata isn''t working with scatter charts
- chart.js draw custom grid lines
- Angular ChartJs does not show Data for multiple Charts
- Issue loading chart.js zoom plugin with require.js
- Change the color of the legend box and the color of the x grid lines in chart.js