-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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: Renderer
- Loading branch information
1 parent
2c98f04
commit 73c2731
Showing
5 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
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,8 @@ | ||
import React from 'react'; | ||
import { RendererPropTypes } from '@/visualizations'; | ||
|
||
export default function CustomPlotlyChart() { | ||
return <div>Custom Plotly Chart</div>; | ||
} | ||
|
||
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,69 @@ | ||
import { isArray, isObject } from 'lodash'; | ||
import React, { useState, useEffect, useMemo } 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(props) { | ||
const [container, setContainer] = useState(null); | ||
|
||
const options = useMemo(() => { | ||
const result = { ...props.options }; | ||
if (['normal', 'percent'].indexOf(result.series.stacking) >= 0) { | ||
// Backward compatibility | ||
result.series = { | ||
...result.series, | ||
percentValues: result.series.stacking === 'percent', | ||
stacking: 'stack', | ||
}; | ||
} | ||
return result; | ||
}, [props.options]); | ||
|
||
const plotlyData = useMemo(() => { | ||
const series = getChartData(props.data.rows, options); | ||
return prepareData(series, options); | ||
}, [props.data, options]); | ||
|
||
const plotlyLayout = useMemo(() => { | ||
if (container) { | ||
return prepareLayout(container, options, plotlyData); | ||
} | ||
return null; | ||
}, [container, options, plotlyData]); | ||
|
||
useEffect(() => { | ||
if (container && plotlyLayout) { | ||
const plotlyOptions = { showLink: false, displaylogo: false }; | ||
|
||
// 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); | ||
} | ||
}); | ||
} | ||
}, [options, plotlyData, plotlyLayout, container]); | ||
|
||
useEffect(() => { | ||
if (container) { | ||
const unwatch = resizeObserver(container, () => { | ||
applyLayoutFixes(container, plotlyLayout, (e, u) => Plotly.relayout(e, u)); | ||
}); | ||
return unwatch; | ||
} | ||
}, [plotlyLayout, container]); | ||
|
||
return <div className="chart-visualization-container plotly-chart-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,14 @@ | ||
import React from 'react'; | ||
import { RendererPropTypes } from '@/visualizations'; | ||
|
||
import PlotlyChart from './PlotlyChart'; | ||
import CustomPlotlyChart from './CustomPlotlyChart'; | ||
|
||
export default function Renderer({ options, ...props }) { | ||
if (options.globalSeriesType === 'custom') { | ||
return <CustomPlotlyChart options={options} {...props} />; | ||
} | ||
return <PlotlyChart options={options} {...props} />; | ||
} | ||
|
||
Renderer.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
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