Skip to content
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

bfs: support all charts #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion superset-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export interface TimeseriesDataRecord extends DataRecord {
__timestamp: number | string | Date | null;
}

export const isTimeseriesDataRecord = (
item: any,
): item is TimeseriesDataRecord => Object.keys(item).includes('__timestamp');

export const isTimeseriesDataRecordList = (
items: any[],
): items is TimeseriesDataRecord[] => items.every(isTimeseriesDataRecord);

// data record value filters from FilterBox
export interface DataRecordFilters {
[key: string]: DataRecordValue[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"@ant-design/icons": "^4.2.2",
"@superset-ui/chart-controls": "*",
"@superset-ui/core": "*",
"@superset-ui/plugin-chart-echarts": "*",
"antd": "^4.9.4",
"echarts": "^5.3.2",
"polished": "^3.7.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import Layer from 'ol/layer/Layer';
import { FrameState } from 'ol/Map';
import { apply as applyTransform } from 'ol/transform';
import ReactDOM from 'react-dom';
import {
ChartConfig,
ChartLayerOptions,
ChartSizeValues,
SupportedVizTypes,
} from '../types';
import { SupersetTheme } from '@superset-ui/core';
import { ChartConfig, ChartLayerOptions, ChartSizeValues } from '../types';
import { createChartComponent } from '../util/chartUtil';
import { getProjectedCoordinateFromPointGeoJson } from '../util/geometryUtil';

Expand All @@ -26,7 +40,7 @@ export class ChartLayer extends Layer {

chartSizeValues: ChartSizeValues = {};

chartVizType: SupportedVizTypes;
chartVizType: string;

div: HTMLDivElement;

Expand All @@ -36,18 +50,21 @@ export class ChartLayer extends Layer {

chartBackgroundBorderRadius = 0;

theme: SupersetTheme;

/**
* Create a ChartLayer.
*
* @param {ChartLayerOptions} options The options to create a ChartLayer
* @param {ChartHtmlElement[]} options.charts An array with the chart objects containing the HTML element and the coordinate
* @param {ChartConfig} options.chartConfigs The chart configuration for the charts
* @param {ChartSizeValues} options.chartSizeValues The values for the chart sizes
* @param {SupportedVizTypes} options.chartVizType The viztype of the charts
* @param {String} options.chartVizType The viztype of the charts
* @param {String} options.chartBackgroundCssColor The color of the additionally added chart background
* @param {Number} options.chartBackgroundBorderRadius The border radius in percent of the additionally added chart background
* @param {Function} options.onMouseOver The handler function to execute when the mouse entering a HTML element
* @param {Function} options.onMouseOut The handler function to execute when the mouse leaves a HTML element
* @param {SupersetTheme} options.theme The superset theme
*/
constructor(options: ChartLayerOptions) {
super(options);
Expand All @@ -70,6 +87,10 @@ export class ChartLayer extends Layer {
this.chartBackgroundBorderRadius = options.chartBackgroundBorderRadius;
}

if (options.theme) {
this.theme = options.theme;
}

const spinner = document.createElement('img');
spinner.src = Loader;
spinner.style.position = 'relative';
Expand Down Expand Up @@ -103,7 +124,7 @@ export class ChartLayer extends Layer {
}
}

setChartVizType(chartVizType: SupportedVizTypes, silent = false) {
setChartVizType(chartVizType: string, silent = false) {
this.chartVizType = chartVizType;
if (!silent) {
this.changed();
Expand Down Expand Up @@ -158,9 +179,13 @@ export class ChartLayer extends Layer {

const chartComponent = createChartComponent(
this.chartVizType,
feature,
// Seems like the registered chart components change some
// props in-place, which leads to unwanted side effects. Therefore,
// we only pass a copy.
JSON.parse(JSON.stringify(feature)),

Choose a reason for hiding this comment

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

minor: maybe lodash.clone(..) could be used

https://www.geeksforgeeks.org/lodash-_-clone-method/

chartWidth,
chartHeight,
this.theme,
);
ReactDOM.render(chartComponent, container);

Expand All @@ -177,7 +202,7 @@ export class ChartLayer extends Layer {
}

updateCharts(zoom: number) {
this.charts = this.charts.map(chart => {
const charts = this.charts.map(chart => {
let chartWidth = 0;
let chartHeight = 0;
if (this.chartSizeValues[zoom]) {
Expand All @@ -192,9 +217,13 @@ export class ChartLayer extends Layer {

const chartComponent = createChartComponent(
this.chartVizType,
chart.feature,
// Seems like the registered chart components change some
// props in-place, which leads to unwanted side effects. Therefore,
// we only pass a copy.
JSON.parse(JSON.stringify(chart.feature)),

Choose a reason for hiding this comment

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

minor: maybe lodash.clone(..) could be used

https://www.geeksforgeeks.org/lodash-_-clone-method/

chartWidth,
chartHeight,
this.theme,
);
ReactDOM.render(chartComponent, chart.htmlElement);

Expand All @@ -204,6 +233,8 @@ export class ChartLayer extends Layer {
height: chartHeight,
};
});

this.charts = charts;
}

render(frameState: FrameState | null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { getChartComponentRegistry, ThemeProvider } from '@superset-ui/core';
import React, { useEffect, useState } from 'react';
import { ChartWrapperProps } from '../types';

export const ChartWrapper: React.FC<ChartWrapperProps> = ({
vizType,
theme,
height,
width,
chartConfig,
}) => {
const [Chart, setChart] = useState<any>();

const getChartFromRegistry = async (vizType: string) => {
const registry = getChartComponentRegistry();
const c = await registry.getAsPromise(vizType);
setChart(() => c);
Comment on lines +34 to +35

Choose a reason for hiding this comment

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

writing the full variable name is better to understand IMHO

Suggested change
const c = await registry.getAsPromise(vizType);
setChart(() => c);
const chart = await registry.getAsPromise(vizType);
setChart(() => chart);

};

useEffect(() => {
getChartFromRegistry(vizType);
}, [vizType]);

return (
<ThemeProvider theme={theme}>
{Chart === undefined ? (
<></>
) : (
<Chart {...chartConfig.properties} height={height} width={width} />
)}
</ThemeProvider>
);
};

export default ChartWrapper;
Loading
Loading