score:8
I have something close, tooltips can accept a position
which is an alias for a function stored in Chart.Tooltip.positioners
. This function returns the x
and y
position for the tooltip.
You can add a custom one to adjust the x at an offset.
The only issue is that by adjust the x the layout (left/right direction) of the tooltip can change meaning that even after working out if the tool tip is below half way or above half way bu adjusting the x it then switches its layout meaning on tooltip in the middle will look odd as it is offset in the wrong direction.
This could be fixed by knowing the width of the tooltip and taking this into account but looking through the data provided to the function this is not given.
Anyway leaving this as an answer it gets you most of the way there and you/someone might be able to figure out how to get rid of that annoying last part
//Global Chart.js options
Chart.defaults.global.defaultFontFamily = 'Lato';
Chart.defaults.global.elements.responsive = true;
Chart.defaults.global.tooltips.xPadding = 10;
Chart.defaults.global.tooltips.yPadding = 10;
Chart.defaults.global.tooltips.titleMarginBottom = 10;
Chart.defaults.global.tooltips.position = 'average';
//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
}
}
//Individual chart config
var ctx = "myChart";
var myChart = new Chart(ctx, {
type: 'line',
options: {
title: {
display: true,
text: 'Precision-Recall Curve',
},
layout: {
padding: 32
},
tooltips: {
//use our new custom position
position: 'custom'
},
},
data: {
labels: ['0%', '10%', '20%', '30%', '40%', '50%', '60%', '70%', '80%', '90%', '100%'],
datasets: [{
label: 'Precision',
data: [2, 42, 55, 50, 42, 38, 32, 24, 20, 18, 18],
borderColor: '#1abc9c',
backgroundColor: 'RGBA(26, 188, 156, .4)',
pointBorderColor: "#4BC0C0",
pointBackgroundColor: "#fff",
pointHitRadius: 10
}, {
label: 'Recall',
data: [2, 12, 24, 30, 39, 58, 70, 82, 86, 89, 93],
borderColor: '#34495e',
backgroundColor: 'RGBA(52, 73, 94, .3)',
pointBorderColor: "#34495e",
pointBackgroundColor: "#fff",
pointHitRadius: 10
}]
}
});
<div class="container">
<div>
<canvas id="myChart"></canvas>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.bundle.min.js"></script>
score:2
I think it's simpler by change the number of caretPadding. We can increase the distance from tooltip to data point by caretPadding
option: {
tooltip: {
caretPadding: 20,
}
}
Source: stackoverflow.com
Related Query
- Move tooltip further from data point for Chart.js?
- getting additional value fields from data source for dx.chartjs doughnut chart
- Chartjs random colors for each part of pie chart with data dynamically from database
- How to use 'time' (data from database, data type: timestamp ) for plotting graph in Chart JS
- Add a text as tooltip for each point on a line chart
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How do I customize y-axis labels and randomly pick the value from the data range for x-axis in Chart js
- Charts JS: Doughnut chart and hiding tooltip for specific data index within each dataset
- Having trouble fetching data from database for chart in ASP .NET Core
- Show label for every data point in line chart
- concatenate on tooltip from data array in chart js
- Chart JS: Donut/Doughnut Chart: Tooltip to be shown always for all the data. All tooltip is not shown when multiple data are with 0 data
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- line chart with {x, y} point data displays only 2 values
- chart.js scatter chart - displaying label specific to point in tooltip
- show label in tooltip but not in x axis for chartjs line chart
- chart js tooltip how to control the data that show
- Chart JS show multiple data points for the same label
- Chart.js tooltip hover customization for mixed chart
- Add all data in the tooltip of Chart JS
- ng2-charts customize data and whole html content of tooltip displayed when hovering on bar chart
- Chartjs - data format for bar chart with multi-level x-axes
- ChartJS: Draw vertical line at data point on chart on mouseover
- (Vue, ChartJS) Create gradient background for chart from child component canvas context
- How to access specific data values from tooltip - Chart.js
- Show "No Data" message for Pie chart with no data
- Chart JS: Ignoring x values and putting point data on first available labels
- Chartjs - Insert additional data into chart tooltip
- Using data from API with Chart JS
- ChartJS bar chart fixed width for dynamic data sets
More Query from same tag
- Chart.js : how I can adjust Pie chart radius?
- custom tooltip opacity always 1
- Charts in wicket
- Want only line not area under it in chart js
- Chart JS title passed from Django - issue with apostrophe '
- Chartjs 2 - Stacked bar with marker on top
- Show point values in Radar Chart using chart.js
- Convert two lists into a dictionary of X and Y Format
- How to change orientation of the main y-axis label in charts.js?
- Chart JS horizontal bar chart width of bars and the chart
- Force ChartJS to show Doughnut chart with null values
- Connect 2 line in chart using chartjs
- Error trying to switch between different charts
- Providing server side data for Chart.js charts
- Line chart plotting multiple points for duplicate data on x and y axis using chart.js
- How to reduce the distance between yAxes in Chart.js?
- Display Chart(.js) when mouse hover over: TypeError: t is null
- Cannot call a function in Javascript - Chart.js
- Updating Chartjs Data with Response from POST Call?
- chart.js scatter chart - displaying label specific to point in tooltip
- AmCharts: Clustered bar chart with sub-categories
- ERROR TypeError: Cannot read property 'nativeElement' of undefined in Ionic 5 and chart.js
- Is there a way to trigger point hit on an xAxis hover
- Can we draw a Line Chart with both solid and dotted line in it?
- Can not update bar chart values in Chart.js 2.0.0 alpha3
- Execute chartjs after an ajax call with PHP
- I have a chart but it is not updating the chart with react chart-js-2
- ChartJS is slow
- Sort a Chart from Lowest to Highest Value in Chart JS
- How to get values inside a chart without clicking