forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate Chart visualization to React Part 1: Renderer (getredash#4130)
* Migrate Chart visualization: Renderer * Refine PlotlyChart component; move stylesheets to visualization's folder * Migrate Custom JS Chart to React * Cleanup
- Loading branch information
1 parent
3719ae4
commit 4ee7cae
Showing
12 changed files
with
186 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
client/app/visualizations/chart/Renderer/CustomPlotlyChart.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React, { useState, useEffect, useMemo } from 'react'; | ||
import { RendererPropTypes } from '@/visualizations'; | ||
|
||
import { clientConfig } from '@/services/auth'; | ||
import resizeObserver from '@/services/resizeObserver'; | ||
|
||
import getChartData from '../getChartData'; | ||
import { Plotly, prepareCustomChartData, createCustomChartRenderer } from '../plotly'; | ||
|
||
export default function CustomPlotlyChart({ options, data }) { | ||
if (!clientConfig.allowCustomJSVisualizations) { | ||
return null; | ||
} | ||
|
||
const [container, setContainer] = useState(null); | ||
|
||
const renderCustomChart = useMemo( | ||
() => createCustomChartRenderer(options.customCode, options.enableConsoleLogs), | ||
[options.customCode, options.enableConsoleLogs], | ||
); | ||
|
||
const plotlyData = useMemo( | ||
() => prepareCustomChartData(getChartData(data.rows, options)), | ||
[options, data], | ||
); | ||
|
||
useEffect(() => { | ||
if (container) { | ||
const unwatch = resizeObserver(container, () => { | ||
// Clear existing data with blank data for succeeding codeCall adds data to existing plot. | ||
Plotly.purge(container); | ||
renderCustomChart(plotlyData.x, plotlyData.ys, container, Plotly); | ||
}); | ||
return unwatch; | ||
} | ||
}, [container, plotlyData]); | ||
|
||
// Cleanup when component destroyed | ||
useEffect(() => { | ||
if (container) { | ||
return () => Plotly.purge(container); | ||
} | ||
}, [container]); | ||
|
||
return <div className="chart-visualization-container" ref={setContainer} />; | ||
} | ||
|
||
CustomPlotlyChart.propTypes = RendererPropTypes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { isArray, isObject } from 'lodash'; | ||
import React, { useState, useEffect } from 'react'; | ||
import { RendererPropTypes } from '@/visualizations'; | ||
import resizeObserver from '@/services/resizeObserver'; | ||
|
||
import getChartData from '../getChartData'; | ||
import { Plotly, prepareData, prepareLayout, updateData, applyLayoutFixes } from '../plotly'; | ||
|
||
export default function PlotlyChart({ options, data }) { | ||
const [container, setContainer] = useState(null); | ||
|
||
useEffect(() => { | ||
if (container) { | ||
const plotlyOptions = { showLink: false, displaylogo: false }; | ||
|
||
const chartData = getChartData(data.rows, options); | ||
const plotlyData = prepareData(chartData, options); | ||
const plotlyLayout = prepareLayout(container, options, plotlyData); | ||
|
||
// It will auto-purge previous graph | ||
Plotly.newPlot(container, plotlyData, plotlyLayout, plotlyOptions).then(() => { | ||
applyLayoutFixes(container, plotlyLayout, (e, u) => Plotly.relayout(e, u)); | ||
}); | ||
|
||
container.on('plotly_restyle', (updates) => { | ||
// This event is triggered if some plotly data/layout has changed. | ||
// We need to catch only changes of traces visibility to update stacking | ||
if (isArray(updates) && isObject(updates[0]) && updates[0].visible) { | ||
updateData(plotlyData, options); | ||
Plotly.relayout(container, plotlyLayout); | ||
} | ||
}); | ||
|
||
const unwatch = resizeObserver(container, () => { | ||
applyLayoutFixes(container, plotlyLayout, (e, u) => Plotly.relayout(e, u)); | ||
}); | ||
return unwatch; | ||
} | ||
}, [options, data, container]); | ||
|
||
// Cleanup when component destroyed | ||
useEffect(() => { | ||
if (container) { | ||
return () => Plotly.purge(container); | ||
} | ||
}, [container]); | ||
|
||
return <div className="chart-visualization-container" ref={setContainer} />; | ||
} | ||
|
||
PlotlyChart.propTypes = RendererPropTypes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
import { RendererPropTypes } from '@/visualizations'; | ||
|
||
import PlotlyChart from './PlotlyChart'; | ||
import CustomPlotlyChart from './CustomPlotlyChart'; | ||
|
||
import './renderer.less'; | ||
|
||
export default function Renderer({ options, ...props }) { | ||
if (options.globalSeriesType === 'custom') { | ||
return <CustomPlotlyChart options={options} {...props} />; | ||
} | ||
return <PlotlyChart options={options} {...props} />; | ||
} | ||
|
||
Renderer.propTypes = RendererPropTypes; |
2 changes: 1 addition & 1 deletion
2
...assets/less/inc/visualizations/chart.less → ...sualizations/chart/Renderer/renderer.less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.plotly-chart-container { | ||
.chart-visualization-container { | ||
height: 400px; | ||
overflow: hidden; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
client/app/visualizations/chart/plotly/customChartUtils.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { each } from 'lodash'; | ||
import { normalizeValue } from './utils'; | ||
|
||
export function prepareCustomChartData(series) { | ||
const x = []; | ||
const ys = {}; | ||
|
||
each(series, ({ name, data }) => { | ||
ys[name] = []; | ||
each(data, (point) => { | ||
x.push(normalizeValue(point.x)); | ||
ys[name].push(normalizeValue(point.y)); | ||
}); | ||
}); | ||
|
||
return { x, ys }; | ||
} | ||
|
||
export function createCustomChartRenderer(code, logErrorsToConsole = false) { | ||
// Create a function from custom code; catch syntax errors | ||
let render = () => {}; | ||
try { | ||
render = new Function('x, ys, element, Plotly', code); // eslint-disable-line no-new-func | ||
} catch (err) { | ||
if (logErrorsToConsole) { | ||
console.log(`Error while executing custom graph: ${err}`); // eslint-disable-line no-console | ||
} | ||
} | ||
|
||
// Return function that will invoke custom code; catch runtime errors | ||
return (x, ys, element, Plotly) => { | ||
try { | ||
render(x, ys, element, Plotly); | ||
} catch (err) { | ||
if (logErrorsToConsole) { | ||
console.log(`Error while executing custom graph: ${err}`); // eslint-disable-line no-console | ||
} | ||
} | ||
}; | ||
} |
Oops, something went wrong.