score:7
Accepted answer
You would need to either override the pie chart or create a new chart type that inherits from pie.
Here is an example of a new chart type, i have passed a new attribute called id and then the only thing that needs to be different in this chart is that when the data is being added this id needs to passed to the chart
Chart.types.Pie.extend({
name: "PieUnique",
addData: function (segment, atIndex, silent) {
var index = atIndex || this.segments.length;
this.segments.splice(index, 0, new this.SegmentArc({
value: segment.value,
outerRadius: (this.options.animateScale) ? 0 : this.outerRadius,
innerRadius: (this.options.animateScale) ? 0 : (this.outerRadius / 100) * this.options.percentageInnerCutout,
fillColor: segment.color,
highlightColor: segment.highlight || segment.color,
showStroke: this.options.segmentShowStroke,
strokeWidth: this.options.segmentStrokeWidth,
strokeColor: this.options.segmentStrokeColor,
startAngle: Math.PI * this.options.startAngle,
circumference: (this.options.animateRotate) ? 0 : this.calculateCircumference(segment.value),
label: segment.label,
//add option passed
id: segment.id
}));
if (!silent) {
this.reflow();
this.update();
}
},
});
var pieData = [{
value: 300,
color: "#F7464A",
highlight: "#FF5A5E",
label: "Red",
id: "1-upi"
}, {
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green",
id: "2-upi"
}, {
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow",
id: "3-upi"
}, {
value: 40,
color: "#949FB1",
highlight: "#A8B3C5",
label: "Grey",
id: "4-upi"
}, {
value: 120,
color: "#4D5360",
highlight: "#616774",
label: "Dark Grey",
id: "5-upi"
}
];
var ctx = document.getElementById("chart-area").getContext("2d");
window.myPie = new Chart(ctx).PieUnique(pieData);
document.getElementById("chart-area").onclick = function (evt) {
var activePoints = window.myPie.getSegmentsAtEvent(evt);
//you can now access the id at activePoints[0].id
console.log(activePoints);
};
<script src="https://raw.githack.com/leighquince/Chart.js/master/Chart.js"></script>
<canvas id="chart-area" width="400"></canvas>
score:2
To get an slice that was clicked on, getElementAtEvent can be used. Worked for me!
stackedBar(datasets) {
var c = $("#canvas-bar");
var ctx = c[0].getContext("2d");
let __this = this;
this.chart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["something"],
datasets: datasets,
},
options: {
onClick: function (e, item) {
__this.onClickAction(e, item);
},
title: {},
legend: {},
scales: {}
}
});
}
onClickAction(event, elements) {
if (elements && elements.length) {
let selected = this.chart.getElementAtEvent(event)[0];
let sliceIndex = selected['_datasetIndex'];
}
}
Source: stackoverflow.com
Related Query
- Embed unique identifier in Chart.js segments?
- How to get labels on ChartJS Pie chart segments
- Chart.js How can I embed additional values to each data point in radar chart
- what is wrong with my code ? java script chart position
- How to print a chart rendered by code
- VueJS + Chartjs - Chart only renders after code change
- How do I destroy/update Chart Data in this chart.js code example?
- Is it possible to remove the border between the segments but leave the border around the pie chart in react-chartjs-2?
- getting additional value fields from data source for dx.chartjs doughnut chart
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- Unique identifier in Chartjs Bar segments?
- Getting the HTML code of a chart created by chart.js
- How to run Chart.js samples using source code
- how to not repeat code while creating multiple charts in chart js
- How we embed google analytics chart from google analytic with angular 4?
- How to embed php code in min.js file
- Set height of chart in Chart.js
- Dynamically update values of a chartjs chart
- How to add text inside the doughnut chart using Chart.js?
- How to clear a chart from a canvas so that hover events cannot be triggered?
- In Chart.js set chart title, name of x axis and y axis?
- Limit labels number on Chart.js line chart
- Chart.js - How to set a line chart dataset as disabled on load
- chart js 2 how to set bar width
- How can labels/legends be added for all chart types in chart.js (chartjs.org)?
- Chartjs Bar Chart showing old data when hovering
- Chart.js Show labels on Pie chart
- Chart Js Change Label orientation on x-Axis for Line Charts
- Chart area background color chartjs
- Chart.js - Increase spacing between legend and chart
More Query from same tag
- How do i have a single Chart.vue component instead of different chart components and then display charts from it by changing id of the API
- Add Horizontal crosshairs using Chart.js
- Vue3 Chart.js not rendering
- Chart JS, Choose different Y-axis for different datasets
- chart.js aspect ratio not square
- How to slice list items in Django Template using javascript (or similar)
- How can I format chart.js data labels while using chart.js datalabels plugin?
- How to sort XY line chart tooltip items based on value and add thousands separators?
- How to destroy chart object in order to change chart data dynamically (Chart.js)?
- Allowing Chart.js to work with jQuery Tabs
- Page break in Angular with Chartjs and tables
- how to change object properties by on-click
- Chartjs not displaying timed data
- Line is rendering only vertical line instead of sloped line
- How to manipulate different charts by angular
- Chartjs Custom Legend with Time on Y-axis
- Chart.js v2, remove padding/margin from radar chart
- Tooltip backgroundColor depending on chart color ChartJS
- Chart.js data not initially loaded
- Chart.js. Values in a big range. The smallest values are not available
- react-chart-js-2 in combination with TypeScript for LineCharts: Uncaught Error: "point" is not a registered element
- create Chart using Chartjs and PHP
- Set Global Config on Angular Charts Not Working
- Retrieving daily & monthly totals from MySQL for use in a chart
- ChartJS date reformatting for use?
- I want to see all the labels on the X-axis of my graph in Chart.js, but only a few of the labels are coming.
- Chart JS can't access data from array within an object
- Android WebView HTML5 canvas error
- How to show gradient vertically on chart js grouped bar chart?
- How can I use npm-plugins with react-chartjs-2?