score:55
Is there a simple way to accomplish this?
YES !! There is a quite straightforward way to accomplish this. If you would have read the documentation, you could have found that pretty easily.
Anyway, basically you need to set the tooltips mode to index
in your chart options, in order to accomplish the behavior you want.
...
options: {
tooltips: {
mode: 'index'
}
}
...
Additionally, you probably want to set the following:
...
options: {
tooltips: {
mode: 'index',
intersect: false
},
hover: {
mode: 'index',
intersect: false
}
}
...
This will make it so all of the expected hover/label interactions will occur when hovering anywhere on the graph at the nearest x-value.
From the Documentation :
# index
Finds item at the same index. If the intersect setting is true, the first intersecting item is used to determine the index in the data. If intersect false the nearest item, in the x direction, is used to determine the index.
Here is a working example :
var ctx = document.getElementById('canvas').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: ['Jan 01', 'Jan 02', 'Jan 03'],
datasets: [{
label: 'Apples Sold',
data: [3, 5, 1],
borderColor: 'rgba(255, 99, 132, 0.8)',
fill: false
}, {
label: 'Oranges Sold',
data: [0, 10, 2],
borderColor: 'rgba(255, 206, 86, 0.8)',
fill: false
}, {
label: 'Gallons of Milk Sold',
data: [5, 7, 4],
borderColor: 'rgba(54, 162, 235, 0.8)',
fill: false
}]
},
options: {
tooltips: {
mode: 'index',
intersect: false
},
hover: {
mode: 'index',
intersect: false
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
<canvas id="canvas"></canvas>
score:-1
You could try using JavaScript to track the users mouse and based on the position, return the data at that vertice.
document.querySelector('.button').onmousemove = (e) => {
const x = e.pageX - e.target.offsetLeft
const y = e.pageY - e.target.offsetTop
e.target.style.setProperty('--x', `${ x }px`)
e.target.style.setProperty('--y', `${ y }px`)
}
score:0
You can achieve this after plotting the data like this: Html
<div class="container">
<h2>Chart.js — Line Chart Demo</h2>
<div>
<canvas id="myChart"></canvas>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.4/Chart.min.js">
</script>
CSS
.container {
width: 80%;
margin: 15px auto;
}
Javascript
var ctx = document.getElementById('myChart').getContext('2d');
function convert(str) {
var date = new Date(str),
mnth = ("0" + (date.getMonth() + 1)).slice(-2),
day = ("0" + date.getDate()).slice(-2);
return [date.getFullYear(), mnth, day].join("-");
}
var date = ["Tue Jun 25 2019 00:00:00 GMT+0530 (India Standard Time)"];
var y1 = [12];
var y2 = [32];
var y3 = [7];
var dataPoints1 = [], dataPoints2 = [], dataPoints3 = [], datep=[];
console.log(date.length)
if(date.length=="1"){
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ["",convert(date[0]),""],
datasets: [{
label:"Tweets",
backgroundColor: "rgba(153,255,51,0.4)",
fill:false,
borderColor:"rgba(153,255,51,0.4)",
data: [null,y1[0],null]
}, {
label:"Retweets",
backgroundColor: "rgba(255,153,0,0.4)",
fill:false,
borderColor:"rgba(255,153,0,0.4)",
data: [null,y2[0],null]
},{
label:"Favourites",
backgroundColor: "rgba(197, 239, 247, 1)",
fill:false,
borderColor:"rgba(197, 239, 247, 1)",
data:[null,y3[0],null]
}
]
},
options: {
scales: {
xAxes: [{
gridLines: {
display: false
}
}],
yAxes: [{
ticks: {
display: true
},
gridLines: {
display: false,
// drawBorder: false //maybe set this as well
}
}]
},
}
});}
else{
for (var i = 0; i < date.length; i++) {
datep.push(convert(date[i]))
dataPoints1.push(y1[i]);
dataPoints2.push(y2[i]);
dataPoints3.push(y3[i]);
}
console.log(datep)
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: datep,
datasets: [{
label:"Tweets",
backgroundColor: "rgba(153,255,51,0.4)",
fill:false,
borderColor:"rgba(153,255,51,0.4)",
data: dataPoints1
}, {
label:"Retweets",
backgroundColor: "rgba(255,153,0,0.4)",
fill:false,
borderColor:"rgba(255,153,0,0.4)",
data: dataPoints2
},{
label:"Favourites",
backgroundColor: "rgba(197, 239, 247, 1)",
fill:false,
borderColor:"rgba(197, 239, 247, 1)",
data:dataPoints3
}
]
},
options: {
scales: {
xAxes: [{
gridLines: {
display: false
}
}],
yAxes: [{
ticks: {
display: true
},
gridLines: {
display: false,
// drawBorder: false //maybe set this as well
}
}]
},
}
});
}
or chk this fiddle https://jsfiddle.net/gqozfb4L/
score:0
I know this is an old post, but in a time I needed to divide a bar on multiple datasets, but the labels to be keeped as original values:
eg:
dataset 1: Totals: 10 15 10
dataset 2: Red: 4 5 9
dataset 3: Blue: 4 2 1
In my chart I want to show the "Totals
" bar and to collor a part of it in red/blue or "the rest" (which is Totals
color). I'll don't write the code to modify the datasets, but I'll complete @busterroni answer for chartjs v3+
plugins: {
tooltip: {
mode: 'index',
intersect: false,
callbacks: {
label: (item) => item.dataset.label + ': ' +
this.originalValues[item.datasetIndex].data[item.dataIndex]
}
}
}
score:6
For Chart.js 3.3.2, you can use @baburao's approach with a few changes. You can check the documentation. Put tooltip
in plugins
. Example:
...
options: {
plugins: {
tooltip: {
mode: 'nearest',
intersect: false
}
}
}
...
Source: stackoverflow.com
Related Query
- Chart.js - Hover labels to display data for all data points on x-axis
- ChartJs line chart - display permanent icon above some data points with text on hover
- In Chart.js >3.0, on axis of type time, how to show labels and ticks only for existing data points (make labels reflect data)?
- ChartJS/High Charts Radar chart - Different radial axis labels for each category that appear on hover
- Line chart plotting multiple points for duplicate data on x and y axis using chart.js
- Chart.js: only show labels on x-axis for data points
- Chart JS show multiple data points for the same label
- How to display data labels outside in pie chart with lines in ionic
- Chart JS pass in custom data for points
- How do I customize y-axis labels and randomly pick the value from the data range for x-axis in Chart js
- Display data labels on a pie chart in angular-chart.js
- create different labels for different data chart js
- Chart.JS: How can I only display data labels when the bar width is big enough for the text?
- Chart.JS: How can I only display data labels when the doughnut size is big enough for the text?
- How to show tooltip value of all data falling on the same axis in chart js?
- Update chart axis labels after hover - chart.js
- getting additional value fields from data source for dx.chartjs doughnut chart
- Chart js padding isn't working for data labels
- Not all date data shows on x axis line chart
- Position of the x-axis labels is not in sync with the line chart data points
- How to display data on hover inside doughnut chart in Angular Chart?
- 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
- No chart display for Chart.js and JSON data
- Chart JS is using 1,2,3 as y values for my points for the second dataset instead of their passed in labels
- chart.js - Pie Chart doesn't display all data
- Chart for Real time data with duplicate x axis
- How can labels/legends be added for all chart types in chart.js (chartjs.org)?
- Chartjs random colors for each part of pie chart with data dynamically from database
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Chart.js how to show cursor pointer for labels & legends in line chart
More Query from same tag
- How to add tooltips to chart.js graph
- chartjs removing/redrawing canvas, graph not responsive
- moment.js is not loading before chart.js in Firefox extension
- Datas put one on each other on chart.js
- Chart.js How to set line height only to the points?
- Get X, Y onClick chart coordinates in ChartJS
- How to add external scripts and css in latest angular-cli
- Importing and using Echarts results in [object error] in Internet Explorer
- Uncaught TypeError: Cannot read property 'draw' of undefined for object scale
- dynamic line styling in chartjs
- ChartJS - aligning axis labels after rotation
- how to only show zero grid axes at center and hide all other gridlines in chart js
- Chartjs using array in label field
- How to chart missing data in chartjs
- Fix ChartJS Values not Well Displayed
- ChartJS tooltip position / placement
- Two tone colours in chart.js pie chart
- Multiple charts not working chart.js
- Chartjs - Set start and end value into a Bar Chart
- Minimum value for x Axis doesn't work for horizontal bar chart | ChartJS
- Chart js, Canvas is null polymer project
- How to set specific height for chartJs background color in terms of yAxis value
- How to add a dataset toggle to Chart.js?
- How to change label color of ng2 chart in angular
- How to dynamically use chartjs-plugin-annotation with ng2-charts?
- Why itemSort is not triggered when I hover a dataset point in Chart.js?
- Searchable label text Chartjs
- ng2-charts: How to set Title of chart dynamically in typescript
- Tooltips not working in chart.js. Any ideas?
- Chart.js not stretching width as per data