score:11
Try to read DOCS
They have pretty good and understandable explanation of use.
There-are built-in 2 event handlers:
Events
chartClick
: fires when click on a chart has occurred, returns information regarding active points and labels
chartHover
: fires when mousemove (hover) on a chart has occurred, returns information regarding active points and labels
In code it looks like that:
<base-chart class="chart"
[datasets]="lineChartData"
[labels]="lineChartLabels"
[options]="lineChartOptions"
[colors]="lineChartColours"
[legend]="lineChartLegend"
[chartType]="lineChartType"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)"></base-chart>
</div>
that chartHovered
and chartClicked
are your custom functions, which could has another names, and do custom things like showing modal, redirect to url etc.
score:0
public chartClicked(e: any): void {
console.log(e);
}
e.active[0]._model
and e.active[0]._view
contain information about the part of the chart you clicked (i.e. label
).
score:0
I hope my answer is correct. After much searching for the only solution I found was:
public chartClicked(e:any):void {
if(e.active.length > 0){
var points = [];
var pointSelected = e.active[0]._chart.tooltip._model.caretY;
var legends = e.active[0]._chart.legend.legendItems;
for (var i = 0; i < e.active.length; ++i) {
points.push(e.active[i]._model.y);
}
let position = points.indexOf(pointSelected);
let label = legends[position].text
console.log("Point: "+label)
}}
score:0
After checking multiple places, I got it working like this for click event.
HTML:
<div class="chart">
<canvas
baseChart
[data]="pieChartData"
[type]="pieChartType"
[options]="pieChartOptions"
[plugins]="pieChartPlugins"
(chartHover)="chartHovered($event)"
>
</canvas>
</div>
TS:
public chartHovered(e: any): void {
if (e.event.type == "click") {
const clickedIndex = e.active[0]?.index;
console.log("Clicked index=" + clickedIndex);
}
}
score:19
I found this solution at https://github.com/valor-software/ng2-charts/issues/489
public chartClicked(e: any): void {
if (e.active.length > 0) {
const chart = e.active[0]._chart;
const activePoints = chart.getElementAtEvent(e.event);
if ( activePoints.length > 0) {
// get the internal index of slice in pie chart
const clickedElementIndex = activePoints[0]._index;
const label = chart.data.labels[clickedElementIndex];
// get value by index
const value = chart.data.datasets[0].data[clickedElementIndex];
console.log(clickedElementIndex, label, value)
}
}
}
Source: stackoverflow.com
Related Query
- Chart.js & Angular 2 - ng2-charts Custom on Click Event
- Chart.js and Angular - Click Event on Chart
- ng2 charts bar chart need spacing between 2 bars in series Angular
- Angular Chart Js legends click event not working
- How to add an on click event to my Line chart using Chart.js
- Click event on stacked bar chart - ChartJs
- Chart.js click event to open a custom tooltip does not work sometimes
- Removing chart on click event produces 'removeHoverStyle' of null error
- Click event of stacked line chart not working
- Chart Click Event - Clicking label of doughnut chart, doesn't return label ng2-charts
- On click event to show name of pie chart slice in chartsJS
- How to customize tooltip on mouse hover of a polar area chart of Angular Charts
- Angular + Ng2 Charts: Chart not displaying as Child Component
- Ng2 charts - Animation doesn't work on line chart onInit
- OnClick event on Custom legends Chart.js / Angular 5
- data in charts of charts.js is changing when I click in the line chart
- Alter angular js chart wrapper to support drawing custom lines
- How to add on click event to chart js
- Building Multiple Charts Using Chart JS in an Angular Application
- Chart.js Console JS Error while destroy the Chart on click event
- How to change label color of ng2 chart in angular
- How to add a custom event on legend click
- The dataset for bar graph is not set properly using ng2 charts and ng5-slider in Angular
- Facing difficulty in opening a dialog from onClick event on a pie chart in angular 8 (using chart.js)
- Angular 8: Creating a custom horizontal bar chart
- Angular 8 and ng2 charts - Updating labels and data
- Donut Chart : Trigger legend or pie click event while selecting outside filter state change
- Getting x,y values from chart click event
- Add a click event to the bar chart of chartjs
- how to not repeat code while creating multiple charts in chart js
More Query from same tag
- Chartjs time plot xAxis shows year 1970
- Chart.js 3.x migration problem – thin white border / padding appears around chart on first load
- chart js - bar chart with time scale on Y axis in Hours and Minutes
- Add codeigniter php data to Chart js
- Height for chart area (not the canvas size)?
- mysql sum query for chartjs
- Chartjs is not rendering
- Why chart is not working with Bootstrap Modal?
- Chart.js v2 formatting time labels
- How to align legend in angular-chart.js
- Trigger axispointer function on existing chart js via console
- Chart.js, increase space between bars and its labels when increasing the charts width
- Chartjs 1 to n relationship
- Creating Chart.js using API data in react
- TimeSeries scale in ChartJS 3.0.2. brings error "This method is not implemented: either no adapter can be found or an incomplete integration was ..."
- Why ChartJs is not working in IE
- How to add additional label in the middle of each bar using ChartJS
- Chart.js: passing objects instead of int values as data
- Angular chartjs directive
- How to split a List into View in C#
- How could i change my JSON structure into chartJS barchart JSON structure?
- Cannot read properties of undefined with react-chartjs-2 and chart js problem
- Vue-Charts label numbers to the side of a bar chart
- Drawing pie segment percentage in middle of doughnut with chartjs issue
- Show multiple charts at HTML page?
- chartnew.js - remove lines from pie chart on 100%
- ng2-Chart: can we show the tooltip data of pie chart on load?
- Using Chart.js with Pyramid Web Framework
- Remove blank space in chart.js chart
- Chart.js time scale not showing data