Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

pie chart selected effect

Open hon opened this issue 11 years ago • 16 comments

I am using the react version of this lib. I want this effect: if I selected a part of the pie, the selected part will move a little out from the center. Is there an option to do that? Thanks.

hon avatar Sep 13 '15 04:09 hon

Agreed, explodable pie slices or explode the selected slice is a very much needed feature. I am able to create a drilldown effect by editing the segments, but not the radius or stokeWidth.

wadacoder avatar Dec 03 '15 22:12 wadacoder

+1

mikabuka avatar Apr 03 '16 10:04 mikabuka

Closing: We will not implement this in the core at this time. I'm happy to help someone develop a plugin to achieve this functionality.

The plugin would need to do the following:

  1. Add something to the segment that is selected
  2. Trigger an update of the chart
  3. Adjust the x,y coordinate of the selected arc only after the update has finished (using afterDatasetsUpdate plugin call)

etimberg avatar Jun 29 '16 22:06 etimberg

I would very much like to write this plugin. Is there documentation somewhere on how to write a Chart.js plugin? The basic documentation at http://www.chartjs.org/docs/#advanced-usage-creating-plugins doesn't quite help

wadacoder avatar Aug 11 '16 19:08 wadacoder

@wadacoder there isn't much documentation at the moment but we do have some samples:

https://github.com/chartjs/Chart.Zoom.js https://github.com/chartjs/Chart.Annotation.js

etimberg avatar Aug 11 '16 23:08 etimberg

It's not a plug-in, just a quick js function (for anyone who needs the explode effect quick): link. Please feel free to correct it.

lkwfahrer avatar Nov 14 '16 13:11 lkwfahrer

@lkwfahrer the jsfiddle link doesn't work

wadacoder avatar Nov 14 '16 15:11 wadacoder

@wadacoder , sorry, updated now: link

lkwfahrer avatar Nov 14 '16 16:11 lkwfahrer

@lkwfahrer, there is a problem with edge of pie. Exploded pie slice is cut by canvas border. Click blue slice for example. I don't know any way to create reserve space between pie and canvas border. Pie always is inscribed in canvas. I guess, it is impossible. Such question has been answered already.

greatromul avatar Nov 29 '16 13:11 greatromul

Agreed, I'm aware of the problem, my solution is just a quick workaround and has more problems. Ideally just having a pie chart option to switch the explode effect on/off would be perfect. For this workaround I was thinking to reduce the radius of the whole pie once it's loaded (similarly to how it's done once a piece is clicked), but I haven't yet looked into that.

lkwfahrer avatar Nov 29 '16 13:11 lkwfahrer

@lkwfahrer, #3476 fixed it. So, there is option for layout padding:

options: {
  layout: {
    padding: 10
  }
}

I've made plunk to demonstrate it.

@hon, you can write this code to solve your task:

var ctx = document.getElementById("myChart");
var selectedIndex = null;
var myChart = new Chart(ctx, {
    type: 'pie',
    data: {
        labels: ["Honey", "Milk", "Sugar"],
        datasets: [
			{
				data: [300, 50, 100],
				backgroundColor: [
					"#FFCE56",
					"#36A2EB",
					"#FF6384"

				],
				hoverBackgroundColor: [
					"#EEBD47",
					"#2591DA",
					"#EE5273"
				]
			}]
    },
    options: {
		onClick: function (evt, elements) {
			if (elements && elements.length) {
				var segment = elements[0];
				myChart.update();
				if (selectedIndex !== segment["_index"]) {
					selectedIndex = segment["_index"];
					segment._model.outerRadius += 10;
				}
				else {
					selectedIndex = null;
				}
			}
		},
		layout: {
			padding: 30
		}
    }
});

greatromul avatar Nov 30 '16 14:11 greatromul

If it's of any value, i created a pie with 'exploded' slice and dropshadow. Created a new chart type and modified pie update methods and Arc draw.

https://codepen.io/yoeri/pen/YxewjY?editors=0010

yoerivdm avatar Aug 18 '17 09:08 yoerivdm

Is there an option to have all the slices selected programmatically so that depending on the case the inital view would show all labels on - this could be easier on the eye that looking down at the legen and back up to the pie, or having to press on each slice to understand what each one represents.

dovk avatar Oct 05 '17 10:10 dovk

I found a more general solution that creates this effect statically (without clicking the graph). You can easily re-purpose it: https://stackoverflow.com/a/57291638/3103891

galabra avatar Jul 31 '19 13:07 galabra

Oh my word the docs on this project are confusing as hell. EIther way, I managed to to implement this with just the hover offset. It'd be great if we can create the exploded effect still, but chartjs just point blank refuses to animate the radius property. However much I try:

https://jsfiddle.net/ortonomy/vyzh1npg/346/

ortonomy avatar Dec 15 '21 17:12 ortonomy

Oh my word the docs on this project are confusing as hell. EIther way, I managed to to implement this with just the hover offset. It'd be great if we can create the exploded effect still, but chartjs just point blank refuses to animate the radius property. However much I try:

https://jsfiddle.net/ortonomy/vyzh1npg/346/

This worked perfectly for me, thanks @ortonomy

camerontbelt avatar Feb 18 '22 15:02 camerontbelt