Skip to content

column names to meta #936

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

Merged
merged 3 commits into from
Jun 17, 2019
Merged
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-chart-editor",
"description": "plotly.js chart editor react component UI",
"version": "0.37.2",
"version": "0.38.0",
"author": "Plotly, Inc.",
"bugs": {
"url": "https://github.com/plotly/react-chart-editor/issues"
Expand All @@ -15,7 +15,7 @@
"fast-isnumeric": "^1.1.2",
"immutability-helper": "^3.0.0",
"plotly-icons": "1.3.12",
"plotly.js": "1.48.1",
"plotly.js": "1.48.3",
"prop-types": "^15.7.2",
"raf": "^3.4.1",
"react-color": "^2.17.0",
Expand Down
2 changes: 1 addition & 1 deletion src/components/containers/AnnotationAccordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AnnotationAccordion extends Component {
<AnnotationFold
key={i}
annotationIndex={i}
name={getParsedTemplateString(ann.text, meta)}
name={getParsedTemplateString(ann.text, {meta})}
canDelete={canAdd}
>
{children}
Expand Down
2 changes: 1 addition & 1 deletion src/components/containers/RangeSelectorAccordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RangeSelectorAccordion extends Component {
<RangeSelectorFold
key={i}
rangeselectorIndex={i}
name={getParsedTemplateString(btn.label, meta)}
name={getParsedTemplateString(btn.label, {meta})}
canDelete={true}
>
{children}
Expand Down
7 changes: 3 additions & 4 deletions src/components/fields/AxesCreator.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,9 @@ class UnconnectedAxesCreator extends Component {

function getOptions(axisType) {
return fullLayout._subplots[axisType].map(axisId => ({
label: getParsedTemplateString(
getAxisTitle(fullLayout[axisIdToAxisName(axisId)]),
fullLayout.meta
),
label: getParsedTemplateString(getAxisTitle(fullLayout[axisIdToAxisName(axisId)]), {
meta: fullLayout.meta,
}),
value: axisId,
}));
}
Expand Down
5 changes: 4 additions & 1 deletion src/components/fields/AxesSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import Dropdown from '../widgets/Dropdown';
import RadioBlocks from '../widgets/RadioBlocks';
import React, {Component} from 'react';
import {getParsedTemplateString} from 'lib';

class AxesSelector extends Component {
constructor(props, context) {
Expand Down Expand Up @@ -30,7 +31,9 @@ class AxesSelector extends Component {
option.value === 'allaxes'
? option
: {
label: option.title,
label: getParsedTemplateString(option.title, {
meta: fullLayout.meta,
}),
value: option.value,
}
)
Expand Down
9 changes: 9 additions & 0 deletions src/components/fields/DataSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Field from './Field';
import nestedProperty from 'plotly.js/src/lib/nested_property';
import {connectToContainer, maybeAdjustSrc, maybeTransposeData} from 'lib';
import {TRANSFORMS_LIST} from 'lib/constants';
import {getColumnNames} from 'lib/dereference';

export function attributeIsData(meta = {}) {
return meta.valType === 'data_array' || meta.arrayOk;
Expand Down Expand Up @@ -94,6 +95,14 @@ export class UnconnectedDataSelector extends Component {
fromSrc: this.context.srcConverters ? this.context.srcConverters.fromSrc : null,
});

if (this.props.container.type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

well, actually, it's possible not to provide trace.type in data, then plotly.js defaults to scatter..
maybe we should encode this in the editor.. this will only be potentially a problem for figures created with an api, where the person did not specify trace.type

Copy link
Contributor

Choose a reason for hiding this comment

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

oh actually nevermind, I think we'd be handling that here: https://github.com/plotly/react-chart-editor/blob/master/src/lib/customTraceType.js#L9

// this means we're at the top level of the trace
update['meta.columnNames.' + this.props.attr] = getColumnNames(
Array.isArray(adjustedValue) ? adjustedValue : [adjustedValue],
this.dataSourceOptions
);
}

this.props.updateContainer(update);
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/fields/__tests__/DataSelector-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('DataSelector', () => {
beforeUpdateTraces.mockClear();
wrapper.prop('onChange')('y1');
expect(beforeUpdateTraces.mock.calls[0][0]).toEqual({
update: {xsrc: 'y1', x: [2, 3, 4]},
update: {'meta.columnNames.x': 'yCol', xsrc: 'y1', x: [2, 3, 4]},
traceIndexes: [1],
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/lib/connectTraceToPlot.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function connectTraceToPlot(WrappedComponent) {

setLocals(props, context) {
const {traceIndexes} = props;
const {data, fullData, plotly, layout: meta} = context;
const {data, fullData, plotly} = context;

const trace = data[traceIndexes[0]];
const fullTrace = getFullTrace(props, context);
Expand Down Expand Up @@ -71,7 +71,7 @@ export default function connectTraceToPlot(WrappedComponent) {

if (trace && fullTrace) {
this.icon = renderTraceIcon(plotlyTraceToCustomTrace(trace));
this.name = getParsedTemplateString(fullTrace.name, meta);
this.name = getParsedTemplateString(fullTrace.name, {meta: fullTrace.meta});
}
}

Expand Down
42 changes: 35 additions & 7 deletions src/lib/dereference.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,26 @@ import {maybeTransposeData} from './index';

const SRC_ATTR_PATTERN = /src$/;

export default function dereference(container, dataSources, config = {deleteKeys: false}) {
export function getColumnNames(srcArray, dataSourceOptions) {
return srcArray
.map(src => {
const columns = dataSourceOptions.filter(dso => dso.value === src);
if (columns.length === 1) {
return columns[0].columnName || columns[0].label;
}
return '';
})
.join(' - ');
}

export default function dereference(
container,
dataSources,
config = {deleteKeys: false},
dataSourceOptions = null
) {
const containerIsData = Array.isArray(container);

const replacer = (key, parent, srcPath) => {
if (!SRC_ATTR_PATTERN.test(key)) {
return;
Expand Down Expand Up @@ -34,22 +53,31 @@ export default function dereference(container, dataSources, config = {deleteKeys
return;
}

if (Array.isArray(container)) {
// Case where we were originally given data to dereference
const traceType = parent.type;
parent[dataKey] = maybeTransposeData(dereferencedData, srcPath, traceType);
if (containerIsData) {
if (parent.type !== null) {
// we're at the top level of the trace
if (dataSourceOptions !== null) {
parent.meta = parent.meta || {};
parent.meta.columnNames = parent.meta.columnNames || {};
parent.meta.columnNames[dataKey] = getColumnNames(srcRef, dataSourceOptions);
}
parent[dataKey] = maybeTransposeData(dereferencedData, srcPath, parent.type);
} else {
parent[dataKey] = dereferencedData;
}
} else {
// This means we're dereferencing layout
// container is layout
parent[dataKey] = dereferencedData;
}
};

if (Array.isArray(container)) {
if (containerIsData) {
walkObject(container, replacer, {
walkArraysMatchingKeys: ['data', 'transforms'],
pathType: 'nestedProperty',
});
} else {
// container is layout
walkObject(container, replacer, {pathType: 'nestedProperty'});
}
}
6 changes: 3 additions & 3 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ function getFullTrace(props, context) {
return fullTrace;
}

function getParsedTemplateString(originalString, meta) {
function getParsedTemplateString(originalString, context) {
let text = originalString;

if (originalString && meta && meta.length) {
text = templateString(originalString, {meta});
if (originalString && context) {
text = templateString(originalString, context);
}

return text === '' && originalString ? originalString : text;
Expand Down