Skip to content

Add grouping property to traces #1028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/plots/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ module.exports = {
'(provided that the legend itself is visible).'
].join(' ')
},
groups: {
valType: 'data_array',
description: [
'An array of strings corresponding to each respective datum. These strings are not' +
'inherently used by plotly for any purpose but may be used, for example, with transforms' +
'in order to filter or group points by an auxilliary property.'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely description. Thanks!

].join(' ')
},
showlegend: {
valType: 'boolean',
role: 'info',
Expand Down
1 change: 1 addition & 0 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ plots.supplyTraceDefaults = function(traceIn, traceIndex, layout) {
coerce('type');
coerce('uid');
coerce('name', 'trace ' + traceIndex);
coerce('groups');

// coerce subplot attributes of all registered subplot types
var subplotTypes = Object.keys(subplotsRegistry);
Expand Down
6 changes: 5 additions & 1 deletion src/traces/scatter/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ module.exports = {
},
ids: {
valType: 'data_array',
description: 'A list of keys for object constancy of data points during animation'
description: [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@etpinard is there a way to automatically cast these to strings, or do I have to manually loop through the array, if provided, and cast them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Meant to ask for groups, but same question applies to both.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to automatically cast these to strings

Not at the moment. Casting within data_array fields is done during the calc step at the moment.

As groups will only be used in filter transforms at first, we could simplify cast groups items in the filter calcTransform handler.

Alternatively, we could add a block to Plots.doCalcdata, casting groups items once and for all. In order to behave correctly during restyle and extendTraces, this would (unfortunately) require adding groups to the recalc-attribute list here.

I have no preference. Your call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think it's best to leave unspecified… It could be country and you could filter by inclusion in a list, or it could be a value and you could filter numerically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As opposed to ids, which must be strings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think it's best to leave unspecified…

very good point.

'A list of unique string ids for object constancy of data points during animation.' +
'For efficiency, the uniqueness of ids is not validated, but unexpected results may' +
'occur if ids are not unique. *ids* is equivalent to a *key* in d3.'
].join(' ')
},
text: {
valType: 'string',
Expand Down
4 changes: 3 additions & 1 deletion src/traces/scatter/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ module.exports = function calc(gd, trace) {
{x: x[i], y: y[i]} : {x: false, y: false};

if(trace.ids) {
cd[i].id = String(trace.ids[i]);
if(trace.ids[i] !== undefined && trace.ids[i] !== null) {
cd[i].id = String(trace.ids[i]);
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/transforms/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,13 @@ function getDataToCoordFunc(gd, trace, filtersrc) {
// -> use setConvert method
if(ax) return ax.d2c;

// special case for 'ids'
// special case for 'ids' or 'groups'
// -> cast to String
if(filtersrc === 'ids') return function(v) { return String(v); };
if(['ids', 'groups'].indexOf(filtersrc) !== -1) {
return function(v) {
return v === undefined ? undefined : String(v);
};
}

// otherwise
// -> cast to Number
Expand Down
11 changes: 11 additions & 0 deletions test/jasmine/tests/calcdata_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ describe('calculated data and points', function() {
});
});

describe('ids', function() {
it('should assign ids to points and cast them to strings in the process', function() {
Plotly.plot(gd, [{ x: [1, 2, 3, 5], y: [1, null, 3, 5], ids: [1, 'a', 'b', undefined]}], {});

expect(gd.calcdata[0][0]).toEqual(jasmine.objectContaining({ x: 1, y: 1, id: '1'}));
expect(gd.calcdata[0][1]).toEqual(jasmine.objectContaining({ x: false, y: false, id: 'a'}));
expect(gd.calcdata[0][2]).toEqual(jasmine.objectContaining({ x: 3, y: 3, id: 'b'}));
expect(gd.calcdata[0][3]).toEqual(jasmine.objectContaining({ x: 5, y: 5}));
});
});

describe('category ordering', function() {

describe('default category ordering reified', function() {
Expand Down