Skip to content

Commit

Permalink
Merge pull request #3594 from plotly/sunburst
Browse files Browse the repository at this point in the history
Sunburst traces
  • Loading branch information
etpinard committed Mar 28, 2019
2 parents e0e7a1a + 4a2f7a5 commit 20553d5
Show file tree
Hide file tree
Showing 37 changed files with 4,282 additions and 223 deletions.
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ Plotly.register([
require('./histogram'),
require('./histogram2d'),
require('./histogram2dcontour'),
require('./pie'),
require('./contour'),
require('./scatterternary'),
require('./violin'),

require('./pie'),
require('./sunburst'),

require('./scatter3d'),
require('./surface'),
require('./isosurface'),
Expand Down
11 changes: 11 additions & 0 deletions lib/sunburst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright 2012-2019, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

module.exports = require('../src/traces/sunburst');
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"country-regex": "^1.1.0",
"d3": "^3.5.12",
"d3-force": "^1.0.6",
"d3-hierarchy": "^1.1.8",
"d3-interpolate": "1",
"d3-sankey-circular": "0.33.0",
"delaunay-triangulate": "^1.1.6",
Expand Down
3 changes: 2 additions & 1 deletion src/components/fx/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ var pointKeyMap = {
locations: 'location',
labels: 'label',
values: 'value',
'marker.colors': 'color'
'marker.colors': 'color',
parents: 'parent'
};

function getPointKey(astr) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/angles.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function rad2deg(rad) { return rad / PI * 180; }
* @return {boolean}
*/
function isFullCircle(aBnds) {
return Math.abs(aBnds[1] - aBnds[0]) > twoPI - 1e-15;
return Math.abs(aBnds[1] - aBnds[0]) > twoPI - 1e-14;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2528,6 +2528,7 @@ var traceUIControlPatterns = [
{pattern: /(^|value\.)visible$/, attr: 'legend.uirevision'},
{pattern: /^dimensions\[\d+\]\.constraintrange/},
{pattern: /^node\.(x|y)/}, // for Sankey nodes
{pattern: /^level$/}, // for Sunburst traces

// below this you must be in editable: true mode
// TODO: I still put name and title with `trace.uirevision`
Expand Down Expand Up @@ -3874,6 +3875,9 @@ function makePlotFramework(gd) {
// single pie layer for the whole plot
fullLayout._pielayer = fullLayout._paper.append('g').classed('pielayer', true);

// single sunbursrt layer for the whole plot
fullLayout._sunburstlayer = fullLayout._paper.append('g').classed('sunburstlayer', true);

// fill in image server scrape-svg
fullLayout._glimages = fullLayout._paper.append('g').classed('glimages', true);

Expand Down
3 changes: 2 additions & 1 deletion src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2723,8 +2723,9 @@ plots.doCalcdata = function(gd, traces) {
gd._hmpixcount = 0;
gd._hmlumcount = 0;

// for sharing colors across pies (and for legend)
// for sharing colors across pies / sunbursts (and for legend)
fullLayout._piecolormap = {};
fullLayout._sunburstcolormap = {};

// If traces were specified and this trace was not included,
// then transfer it over from the old calcdata:
Expand Down
64 changes: 36 additions & 28 deletions src/traces/pie/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ var tinycolor = require('tinycolor2');
var Color = require('../../components/color');
var helpers = require('./helpers');

exports.calc = function calc(gd, trace) {
var pieExtendedColorWays = {};

function calc(gd, trace) {
var vals = trace.values;
var hasVals = isArrayOrTypedArray(vals) && vals.length;
var labels = trace.labels;
var colors = trace.marker.colors || [];
var cd = [];
var fullLayout = gd._fullLayout;
var colorMap = fullLayout._piecolormap;
var allThisTraceLabels = {};
var vTotal = 0;
var hiddenLabels = fullLayout.hiddenlabels || [];
Expand All @@ -36,18 +37,7 @@ exports.calc = function calc(gd, trace) {
}
}

function pullColor(color, label) {
if(!color) return false;

color = tinycolor(color);
if(!color.isValid()) return false;

color = Color.addOpacity(color, color.getAlpha());
if(!colorMap[label]) colorMap[label] = color;

return color;
}

var pullColor = makePullColorFn(fullLayout._piecolormap);
var seriesLen = (hasVals ? vals : labels).length;

for(i = 0; i < seriesLen; i++) {
Expand Down Expand Up @@ -121,7 +111,21 @@ exports.calc = function calc(gd, trace) {
}

return cd;
};
}

function makePullColorFn(colorMap) {
return function pullColor(color, id) {
if(!color) return false;

color = tinycolor(color);
if(!color.isValid()) return false;

color = Color.addOpacity(color, color.getAlpha());
if(!colorMap[id]) colorMap[id] = color;

return color;
};
}

/*
* `calc` filled in (and collated) explicit colors.
Expand All @@ -130,45 +134,41 @@ exports.calc = function calc(gd, trace) {
* This is done after sorting, so we pick defaults
* in the order slices will be displayed
*/
exports.crossTraceCalc = function(gd) {
function crossTraceCalc(gd) {
var fullLayout = gd._fullLayout;
var calcdata = gd.calcdata;
var pieColorWay = fullLayout.piecolorway;
var colorMap = fullLayout._piecolormap;

if(fullLayout.extendpiecolors) {
pieColorWay = generateExtendedColors(pieColorWay);
pieColorWay = generateExtendedColors(pieColorWay, pieExtendedColorWays);
}
var dfltColorCount = 0;

var i, j, cd, pt;
for(i = 0; i < calcdata.length; i++) {
cd = calcdata[i];
for(var i = 0; i < calcdata.length; i++) {
var cd = calcdata[i];
if(cd[0].trace.type !== 'pie') continue;

for(j = 0; j < cd.length; j++) {
pt = cd[j];
for(var j = 0; j < cd.length; j++) {
var pt = cd[j];
if(pt.color === false) {
// have we seen this label and assigned a color to it in a previous trace?
if(colorMap[pt.label]) {
pt.color = colorMap[pt.label];
}
else {
} else {
colorMap[pt.label] = pt.color = pieColorWay[dfltColorCount % pieColorWay.length];
dfltColorCount++;
}
}
}
}
};
}

/**
* pick a default color from the main default set, augmented by
* itself lighter then darker before repeating
*/
var extendedColorWays = {};

function generateExtendedColors(colorList) {
function generateExtendedColors(colorList, extendedColorWays) {
var i;
var colorString = JSON.stringify(colorList);
var pieColors = extendedColorWays[colorString];
Expand All @@ -187,3 +187,11 @@ function generateExtendedColors(colorList) {

return pieColors;
}

module.exports = {
calc: calc,
crossTraceCalc: crossTraceCalc,

makePullColorFn: makePullColorFn,
generateExtendedColors: generateExtendedColors
};
2 changes: 1 addition & 1 deletion src/traces/pie/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var calcModule = require('./calc');
Pie.calc = calcModule.calc;
Pie.crossTraceCalc = calcModule.crossTraceCalc;

Pie.plot = require('./plot');
Pie.plot = require('./plot').plot;
Pie.style = require('./style');
Pie.styleOne = require('./style_one');

Expand Down
Loading

0 comments on commit 20553d5

Please sign in to comment.