Skip to content

Commit

Permalink
Refine PlotlyChart component; move stylesheets to visualization's folder
Browse files Browse the repository at this point in the history
  • Loading branch information
kravets-levko committed Sep 10, 2019
1 parent 73c2731 commit ee47078
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 44 deletions.
1 change: 0 additions & 1 deletion client/app/assets/less/main.less
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
@import 'inc/visualizations/sankey';
@import 'inc/visualizations/pivot-table';
@import 'inc/visualizations/map';
@import 'inc/visualizations/chart';
@import 'inc/visualizations/sunburst';
@import 'inc/visualizations/cohort';
@import 'inc/visualizations/misc';
Expand Down
2 changes: 1 addition & 1 deletion client/app/pages/dashboards/dashboard.less
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
.map-visualization-container,
.word-cloud-visualization-container,
.box-plot-deprecated-visualization-container,
.plotly-chart-container {
.chart-visualization-container {
position: absolute;
left: 0;
top: 0;
Expand Down
1 change: 1 addition & 0 deletions client/app/visualizations/EditVisualizationDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ function EditVisualizationDialog({ dialog, visualization, query, queryResult })
options={options}
visualizationName={name}
onOptionsChange={onOptionsChanged}
context="query"
/>
</div>
</Grid.Col>
Expand Down
50 changes: 16 additions & 34 deletions client/app/visualizations/chart/Renderer/PlotlyChart.jsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,22 @@
import { isArray, isObject } from 'lodash';
import React, { useState, useEffect, useMemo } from 'react';
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(props) {
export default function PlotlyChart({ options, data }) {
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) {
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));
Expand All @@ -51,19 +30,22 @@ export default function PlotlyChart(props) {
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]);
}, [options, data, container]);

// Cleanup when component destroyed
useEffect(() => {
if (container) {
return () => Plotly.purge(container);
}
}, [container]);

return <div className="chart-visualization-container plotly-chart-container" ref={setContainer} />;
return <div className="chart-visualization-container" ref={setContainer} />;
}

PlotlyChart.propTypes = RendererPropTypes;
2 changes: 2 additions & 0 deletions client/app/visualizations/chart/Renderer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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} />;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.plotly-chart-container {
.chart-visualization-container {
height: 400px;
overflow: hidden;
}
18 changes: 14 additions & 4 deletions client/app/visualizations/chart/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,20 @@ export default function init(ngModule) {
type: 'CHART',
name: 'Chart',
isDefault: true,
getOptions: options => merge({}, DEFAULT_OPTIONS, {
showDataLabels: options.globalSeriesType === 'pie',
dateTimeFormat: clientConfig.dateTimeFormat,
}, options),
getOptions: (options) => {
const result = merge({}, DEFAULT_OPTIONS, {
showDataLabels: options.globalSeriesType === 'pie',
dateTimeFormat: clientConfig.dateTimeFormat,
}, options);

// Backward compatibility
if (['normal', 'percent'].indexOf(result.series.stacking) >= 0) {
result.series.percentValues = result.series.stacking === 'percent';
result.series.stacking = 'stack';
}

return result;
},
Renderer,
Editor: angular2react('chartEditor', ChartEditor, $injector),

Expand Down
6 changes: 3 additions & 3 deletions client/app/visualizations/chart/plotly/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ Plotly.setPlotConfig({

const PlotlyChart = () => ({
restrict: 'E',
template: '<div class="plotly-chart-container" resize-event="handleResize()"></div>',
template: '<div class="chart-visualization-container" resize-event="handleResize()"></div>',
scope: {
options: '=',
series: '=',
},
link(scope, element) {
const plotlyElement = element[0].querySelector('.plotly-chart-container');
const plotlyElement = element[0].querySelector('.chart-visualization-container');
const plotlyOptions = { showLink: false, displaylogo: false };
let layout = {};
let data = [];
Expand Down Expand Up @@ -85,7 +85,7 @@ const PlotlyChart = () => ({

const CustomPlotlyChart = clientConfig => ({
restrict: 'E',
template: '<div class="plotly-chart-container" resize-event="handleResize()"></div>',
template: '<div class="chart-visualization-container" resize-event="handleResize()"></div>',
scope: {
series: '=',
options: '=',
Expand Down

0 comments on commit ee47078

Please sign in to comment.