score:10
This demo gives the value in the bar chart when you click on it. I'm not a chart.js expert so there may be better solutions.
https://codepen.io/newschapmj1/pen/PerOzM
/* from https://github.com/chartjs/Chart.js/issues/2292 */
document.getElementById("myChart").onclick = function (evt) {
var activePoints = myChart.getElementsAtEventForMode(evt, 'point', myChart.options);
var firstPoint = activePoints[0];
var label = myChart.data.labels[firstPoint._index];
var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
alert(label + ": " + value);
};
score:0
I needed a way to get the chart from the callback because I'm using the same callback for multiple charts, so I can't just say myChart
This works, passing to chart options - the second parameter is called "activeElements" and can be used to get the chart:
onClick: (event, activeElements) => {
if(activeElements.length === 0){
alert("Chart is clickable but you must click a data point to drill-down")
}
const chart = activeElements[0]._chart
const activePoints = chart.getElementsAtEventForMode(event, 'point', chart.options);
const firstPoint = activePoints[0];
const label = chart.data.labels[firstPoint._index];
const value = chart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];
alert(label + ": " + value);
}
score:5
Chart.js version = 3.2.1
From the official documentation:
onClick: (evt) => { const points = myChart.getElementsAtEventForMode(evt, 'nearest', { intersect: true }, true); if (points.length) { const firstPoint = points[0]; var label = myChart.data.labels[firstPoint.index]; var value = myChart.data.datasets[firstPoint.datasetIndex].data[firstPoint.index]; alert(label +" : "+ value); } }
Source: stackoverflow.com
Related Query
- get yLabel value onclick chart js
- Get X, Y onClick chart coordinates in ChartJS
- How to access or get value of specific graph on chart plot by click event?
- Click on interactive chart.js bar chart and get value for labels and groups in JS
- Get Value for point clicked on Pie Chart in Chart.js 3
- How to get line x value from Label chart js
- get JSON data from function to give value to Chart JS
- How can I get my Chart.JS bar chart to stack two data values together on each bar, and print a calculated value on each bar?
- Get value of min/lowest label from y-axis chart
- getting additional value fields from data source for dx.chartjs doughnut chart
- ChartJS: Get value from another another Chart
- How can i get the Chart JS Bar Graph Bar Label and Value on click?
- How to get a value from function javascript and print it to the chart
- Get bar value when onClick Reactjs
- How to get x-axis value in chart as string?
- Get hash value Rails / Chart JS / Google Chart
- Get Chart js object from selector
- How to display value of only one datapoint in line chart
- Obtain max value of y axis of line chart rendered with Chart.js
- Can't get bar chart colors in Chart js working in React Js
- Chart js - Get bar width after render
- Remove the label and show only value in tooltips of a bar chart
- How to create single value Doughnut or Pie chart using Chart.js?
- Angular chart.js onClick is not working - how to get all elements of a bar when it is clicked on
- Map event position to y axis value in chartjs line chart
- Drawing line chart in chart.js placing dots only when value changes
- React ChartJS 2 : Get data on clicking the chart
- How to get onClick Event for a Label in ChartJS and React?
- Chart.js: Minimum value for x-axis at horizontal stacked bar chart
- chart.js bar chart color change based on value
More Query from same tag
- chartJS doesn't show graphs on iphone/ipad
- Chart.js Dropdown to select 1 day, yesterday and 7 days
- Chart.js v2 hide dataset labels
- Ng2-charts crosshair plugin not working in Angular
- Chart.js is it possible to style the labels?
- How to sort color segments by value within the 100% stacked bars rather than by value across all the stacked bars in Highcharts or ChartJs
- How to install chartjs on laravel 6 via npm
- ChartJSNodeCanvas plugin chartjs-plugin-annotation creates error: HTMLCanvasElement is not defined
- Chart.js repeating colors?
- dyamically constructing javascript object
- zingchart heatmap not show all hours on y axes
- ChartJS: Can interior of polar area chart be hollow?
- Have all label in Chartjs be at fixed positions
- Getting nice chart.js animation when adding data with unshift()?
- Dynamically Setting Height of Chart
- Drawing a horizontal line on a stacked bar chart on hover (chart js)
- Chart js data to start at zero
- Chart.js same Y axis on left and right
- ChartDataLabels is globally active for some reason
- ChartJS display legend
- How to hide Chart.js data labels for small screens
- Fix ChartJS Values not Well Displayed
- chart js with angular2 loading dynamic data only after zoomin
- Chartjs 2.x - Dataset color changing when redrawing chart
- ChartJS fails to render horizontal bar chart for timeseries data
- graph (chartjs.org) does not stretch properly when sidebar(menu) is collapsed
- Primeng line chart not updating automatically,only update on window refresh
- How to change format of DateTime reading into chart.js chart?
- How to place extra text on canvas (not using HTML) beside chart?
- how to resize specific gridline(s) in chart.js