-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -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'; | ||
|
||
|
@@ -26,7 +40,7 @@ export class ChartLayer extends Layer { | |
|
||
chartSizeValues: ChartSizeValues = {}; | ||
|
||
chartVizType: SupportedVizTypes; | ||
chartVizType: string; | ||
|
||
div: HTMLDivElement; | ||
|
||
|
@@ -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); | ||
|
@@ -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'; | ||
|
@@ -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(); | ||
|
@@ -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)), | ||
chartWidth, | ||
chartHeight, | ||
this.theme, | ||
); | ||
ReactDOM.render(chartComponent, container); | ||
|
||
|
@@ -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]) { | ||
|
@@ -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)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: maybe lodash.clone(..) could be used |
||
chartWidth, | ||
chartHeight, | ||
this.theme, | ||
); | ||
ReactDOM.render(chartComponent, chart.htmlElement); | ||
|
||
|
@@ -204,6 +233,8 @@ export class ChartLayer extends Layer { | |
height: chartHeight, | ||
}; | ||
}); | ||
|
||
this.charts = charts; | ||
} | ||
|
||
render(frameState: FrameState | null) { | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. writing the full variable name is better to understand IMHO
Suggested change
|
||||||||||
}; | ||||||||||
|
||||||||||
useEffect(() => { | ||||||||||
getChartFromRegistry(vizType); | ||||||||||
}, [vizType]); | ||||||||||
|
||||||||||
return ( | ||||||||||
<ThemeProvider theme={theme}> | ||||||||||
{Chart === undefined ? ( | ||||||||||
<></> | ||||||||||
) : ( | ||||||||||
<Chart {...chartConfig.properties} height={height} width={width} /> | ||||||||||
)} | ||||||||||
</ThemeProvider> | ||||||||||
); | ||||||||||
}; | ||||||||||
|
||||||||||
export default ChartWrapper; |
There was a problem hiding this comment.
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 usedhttps://www.geeksforgeeks.org/lodash-_-clone-method/