Skip to content

Commit

Permalink
- pie plots now propagate clicks
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Burato committed Nov 25, 2016
1 parent ca3ead1 commit a6f5b62
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 7 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ https://github.com/plotly/plotly.js/compare/vX.Y.Z...master

where X.Y.Z is the semver of most recent plotly.js release.


## [1.20.2-d26] -- 2016-11-25

### Fixed

- Fix pie plots not propagating original click event

## [1.20.2] -- 2016-11-17

### Fixed
Expand Down
6 changes: 3 additions & 3 deletions dist/plotly-cartesian.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plotly.js",
"version": "1.20.2-d25",
"version": "1.20.2-d26",
"description": "The open source javascript graphing library that powers plotly",
"license": "MIT",
"main": "./lib/index.js",
Expand Down
117 changes: 117 additions & 0 deletions sample/chart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
var trace1 = {
x: ['A', 'B', 'C'],
y: [2.5, 1.5, 2],
type: 'bar',
};
var trace2 = {
x: ['A', 'B', 'C'],
y: [-2.5, -1.5, -2],
type: 'bar',
};
var trace3 = {
x: ['A', 'B', 'C'],
y: [0.5, 3.5, 1],
type: 'bar',
marker: {
line: {
color: ['red', 'green', 'purple'],
width: [1, 4, 10]
}
}
};

var barLayout = {
barmode: 'relative',
hovermode: 'closest',
width: 600,
};
var barData = [trace1, trace2, trace3];
Plotly.newPlot('barDiv', barData, barLayout);
var myPlot = document.getElementById('barDiv');
myPlot.on('plotly_click', function (data) {
var point = data.points[0];
var singleColors = [];
var singleWidths = [];
for (var i = 0; i < barData[0].x.length; i++) {
if (i === point.pointNumber) {
singleColors.push('red');
singleWidths.push(2);
} else {
singleColors.push('white');
singleWidths.push(0);
}
}

var curves = [];
var colors = [];
var widths = [];

for (var j = 0; j < barData.length; j++) {
curves.push(j);
var sc = [];
var sw = [];
for (var k = 0; k < barData[j].x.length; k++) {
// single trace
if (j === point.curveNumber && k === point.pointNumber) {
sc.push('red');
sw.push(2);
} else {
sc.push(null);
sw.push(0);
}
}
colors.push(sc);
widths.push(sw);
}

Plotly.restyle('barDiv', {
'marker.line.color': colors,
'marker.line.width': widths,
}, curves);
console.log(data);
});

var pieData = [{
sort: false,
values: [27, 11, 25, 8, 1, 3, 25],
labels: ['US', 'China', 'European Union', 'Russian Federation', 'Brazil', 'India', 'Rest of World'],
text: 'CO2',
textposition: 'inside',
name: 'CO2 Emissions',
hoverinfo: 'label+percent+name',
hole: .4,
type: 'pie',
marker: {
line: {
color: 'white',
width: 2
}
}
}];

var pieLayout = {
title: 'Global Emissions 1990-2011',
height: 400,
width: 480
};

Plotly.newPlot('pieDiv', pieData, pieLayout);
var myPlot = document.getElementById('pieDiv');
myPlot.on('plotly_click', function (data) {
var point = data.points[0];
var colors = [];
var pull = [];
for (var i = 0; i < pieData[0].values.length; i++) {
if (i === point.i) {
colors.push('red');
pull.push(0.05);
} else {
colors.push('white');
pull.push(0);
}
}
Plotly.restyle('pieDiv', {
'marker.line.color': [colors],
'pull': [pull]
}, [0]);
});
21 changes: 21 additions & 0 deletions sample/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>

<head>
<!-- Plotly.js -->
<script src="../dist/plotly-cartesian.js" charset="utf-8"></script>
<style></style>
</head>

<body>

<div id="barDiv" style="width: 480px; height: 350px;" class="js-plotly-plot">
<!-- Plotly chart will be drawn inside this DIV -->
</div>
<div id="pieDiv" style="width: 480px; height: 350px;" class="js-plotly-plot">
<!-- Plotly chart will be drawn inside this DIV -->
</div>
<script src="./chart.js"></script>

</body>

</html>
2 changes: 1 addition & 1 deletion src/assets/geo_assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ var saneTopojson = require('sane-topojson');


// package version injected by `npm run preprocess`
exports.version = '1.20.2-d25';
exports.version = '1.20.2-d26';

exports.topojson = saneTopojson;
2 changes: 1 addition & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
var Plotly = require('./plotly');

// package version injected by `npm run preprocess`
exports.version = '1.20.2-d25';
exports.version = '1.20.2-d26';

// inject promise polyfill
require('es6-promise').polyfill();
Expand Down
2 changes: 1 addition & 1 deletion src/traces/pie/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ module.exports = function plot(gd, cdpie) {
gd._hoverdata = [pt];
pt.curveNumber = cd[0].trace.index;
gd._hoverdata.trace = cd[0].trace;
Fx.click(gd, { target: true });
Fx.click(gd, window.event || { target: true });
}

slicePath.enter().append('path')
Expand Down

0 comments on commit a6f5b62

Please sign in to comment.