diff --git a/src/PlotlyEditor.js b/src/PlotlyEditor.js index 5e7868711..0ed87ec38 100644 --- a/src/PlotlyEditor.js +++ b/src/PlotlyEditor.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import React, {Component} from 'react'; import dictionaries from './locales'; import {bem} from './lib'; -import {noShame} from './shame'; +import {noShame, maybeClearAxisTypes} from './shame'; import {EDITOR_ACTIONS} from './lib/constants'; import isNumeric from 'fast-isnumeric'; import nestedProperty from 'plotly.js/src/lib/nested_property'; @@ -60,6 +60,11 @@ class PlotlyEditor extends Component { if (this.props.onUpdateTraces) { this.props.onUpdateTraces(payload); } + + // until we start utilizing Plotly.react in `react-plotly.js` + // force clear axes types when a `src` has changed. + maybeClearAxisTypes(graphDiv, payload.traceIndexes, payload.update); + for (let i = 0; i < payload.traceIndexes.length; i++) { for (const attr in payload.update) { const traceIndex = payload.traceIndexes[i]; diff --git a/src/shame.js b/src/shame.js index d433effb3..37cf3ccae 100644 --- a/src/shame.js +++ b/src/shame.js @@ -1,12 +1,51 @@ /* * DELETE THIS FILE. EVERYTHING NEEDS TO FIND A HOME. */ -import {list} from 'plotly.js/src/plots/cartesian/axis_ids'; +import {list, getFromId} from 'plotly.js/src/plots/cartesian/axis_ids'; +import nestedProperty from 'plotly.js/src/lib/nested_property'; export function noShame(opts) { if (opts.plotly && !opts.plotly.Axes) { - opts.plotly.Axes = { - list, - }; + opts.plotly.Axes = {list}; + } +} + +// Temporary fix for: +// https://github.com/plotly/react-plotly.js-editor/issues/103 +// We should be able to remove this once the plotly.react method has +// been integrated into react-plotly.js and released: +// https://github.com/plotly/react-plotly.js/issues/2 +export const maybeClearAxisTypes = (graphDiv, traceIndexes, update) => { + if (!Array.isArray(graphDiv._fullData)) { + return; + } + let hasSrc = false; + for (const key in update) { + if (key.substr(key.length - 3) === 'src') { + hasSrc = true; + } + } + if (hasSrc) { + clearAxisTypes(graphDiv, traceIndexes); + } +}; + +var axLetters = ['x', 'y', 'z']; +function clearAxisTypes(gd, traces) { + for (var i = 0; i < traces.length; i++) { + var trace = gd._fullData[i]; + for (var j = 0; j < 3; j++) { + const type = axLetters[j]; + const ax = getFromId(gd, trace[type + 'axis'] || type); + + // Do not clear log type. + // Log types is never an auto result so must have been intentional. + // We are also skipping clearing 3D which could cause bugs with 3D. + if (ax && ax.type !== 'log') { + const axAttr = ax._name; + const typeAttr = axAttr + '.type'; + nestedProperty(gd.layout, typeAttr).set(null); + } + } } }