feat(plugin): add plugin-chart-cartodiagram (#25869)

Co-authored-by: Jakob Miksch <jakob@meggsimum.de>
This commit is contained in:
Jan Suleiman
2025-01-06 17:58:03 +01:00
committed by GitHub
parent 5484db34f9
commit a986a61b5f
72 changed files with 8434 additions and 193 deletions

View File

@@ -0,0 +1,54 @@
/**
* 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 { QueryFormData, getChartBuildQueryRegistry } from '@superset-ui/core';
export default function buildQuery(formData: QueryFormData) {
const {
selected_chart: selectedChartString,
geom_column: geometryColumn,
extra_form_data: extraFormData,
} = formData;
const selectedChart = JSON.parse(selectedChartString);
const vizType = selectedChart.viz_type;
const chartFormData = JSON.parse(selectedChart.params);
// Pass extra_form_data to chartFormData so that
// dashboard filters will also be applied to the charts
// on the map.
chartFormData.extra_form_data = {
...chartFormData.extra_form_data,
...extraFormData,
};
// adapt groupby property to ensure geometry column always exists
// and is always at first position
let { groupby } = chartFormData;
if (!groupby) {
groupby = [];
}
// add geometry column at the first place
groupby?.unshift(geometryColumn);
chartFormData.groupby = groupby;
// TODO: find way to import correct type "InclusiveLoaderResult"
const buildQueryRegistry = getChartBuildQueryRegistry();
const chartQueryBuilder = buildQueryRegistry.get(vizType) as any;
const chartQuery = chartQueryBuilder(chartFormData);
return chartQuery;
}

View File

@@ -0,0 +1,193 @@
/**
* 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 { t, validateNonEmpty } from '@superset-ui/core';
import { ControlPanelConfig } from '@superset-ui/chart-controls';
import { selectedChartMutator } from '../util/controlPanelUtil';
import { MAX_ZOOM_LEVEL, MIN_ZOOM_LEVEL } from '../util/zoomUtil';
const config: ControlPanelConfig = {
controlPanelSections: [
{
label: t('Configuration'),
expanded: true,
controlSetRows: [
[
{
name: 'selected_chart',
config: {
type: 'SelectAsyncControl',
mutator: selectedChartMutator,
multi: false,
label: t('Chart'),
validators: [validateNonEmpty],
description: t('Choose a chart for displaying on the map'),
placeholder: t('Select chart'),
onAsyncErrorMessage: t('Error while fetching charts'),
mapStateToProps: state => {
if (state?.datasource?.id) {
const datasourceId = state.datasource.id;
const query = {
columns: ['id', 'slice_name', 'params', 'viz_type'],
filters: [
{
col: 'datasource_id',
opr: 'eq',
value: datasourceId,
},
],
page: 0,
// TODO check why we only retrieve 100 items, even though there are more
page_size: 999,
};
const dataEndpoint = `/api/v1/chart/?q=${JSON.stringify(
query,
)}`;
return { dataEndpoint };
}
// could not extract datasource from map
return {};
},
},
},
],
[
{
name: 'geom_column',
config: {
type: 'SelectControl',
label: t('Geometry Column'),
renderTrigger: false,
description: t('The name of the geometry column'),
mapStateToProps: state => ({
choices: state.datasource?.columns.map(c => [
c.column_name,
c.column_name,
]),
}),
validators: [validateNonEmpty],
},
},
],
],
},
{
label: t('Map Options'),
expanded: true,
controlSetRows: [
[
{
name: 'map_view',
config: {
type: 'MapViewControl',
renderTrigger: true,
description: t(
'The extent of the map on application start. FIT DATA automatically sets the extent so that all data points are included in the viewport. CUSTOM allows users to define the extent manually.',
),
label: t('Extent'),
dontRefreshOnChange: true,
default: {
mode: 'FIT_DATA',
},
},
},
],
[
{
// name is referenced in 'index.ts' for setting default value
name: 'layer_configs',
config: {
type: 'LayerConfigsControl',
renderTrigger: true,
label: t('Layers'),
default: [],
description: t('The configuration for the map layers'),
},
},
],
],
},
{
label: t('Chart Options'),
expanded: true,
controlSetRows: [
[
{
name: 'chart_background_color',
config: {
label: t('Background Color'),
description: t('The background color of the charts.'),
type: 'ColorPickerControl',
default: { r: 255, g: 255, b: 255, a: 0.2 },
renderTrigger: true,
},
},
],
[
{
name: 'chart_background_border_radius',
config: {
label: t('Corner Radius'),
description: t('The corner radius of the chart background'),
type: 'SliderControl',
default: 10,
min: 0,
step: 1,
max: 100,
renderTrigger: true,
},
},
],
[
{
name: 'chart_size',
config: {
type: 'ZoomConfigControl',
// set this to true, if we are able to render it fast
renderTrigger: true,
default: {
type: 'FIXED',
configs: {
zoom: 6,
width: 100,
height: 100,
slope: 30,
exponent: 2,
},
// create an object with keys MIN_ZOOM_LEVEL - MAX_ZOOM_LEVEL
// that all contain the same initial value
values: {
...Array.from(
{ length: MAX_ZOOM_LEVEL - MIN_ZOOM_LEVEL + 1 },
() => ({ width: 100, height: 100 }),
),
},
},
label: t('Chart size'),
description: t('Configure the chart size for each zoom level'),
},
},
],
],
},
],
};
export default config;

View File

@@ -0,0 +1,66 @@
/**
* 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 { t, ChartMetadata, ChartPlugin } from '@superset-ui/core';
import buildQuery from './buildQuery';
import controlPanel from './controlPanel';
import transformProps from './transformProps';
import thumbnail from '../images/thumbnail.png';
import example1 from '../images/example1.png';
import example2 from '../images/example2.png';
import { CartodiagramPluginConstructorOpts } from '../types';
import { getLayerConfig } from '../util/controlPanelUtil';
export default class CartodiagramPlugin extends ChartPlugin {
constructor(opts: CartodiagramPluginConstructorOpts) {
const metadata = new ChartMetadata({
description:
'Display charts on a map. For using this plugin, users first have to create any other chart that can then be placed on the map.',
name: t('Cartodiagram'),
thumbnail,
tags: [t('Geo'), t('2D'), t('Spatial'), t('Experimental')],
category: t('Map'),
exampleGallery: [
{ url: example1, caption: t('Pie charts on a map') },
{ url: example2, caption: t('Line charts on a map') },
],
});
if (opts.defaultLayers) {
const layerConfig = getLayerConfig(controlPanel);
// set defaults for layer config if found
if (layerConfig) {
layerConfig.config.default = opts.defaultLayers;
} else {
// eslint-disable-next-line no-console
console.warn(
'Cannot set defaultLayers. layerConfig not found in control panel. Please check if the path to layerConfig should be adjusted.',
);
}
}
super({
buildQuery,
controlPanel,
loadChart: () => import('../CartodiagramPlugin'),
metadata,
transformProps,
});
}
}

View File

@@ -0,0 +1,63 @@
/**
* 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 { ChartProps, getChartTransformPropsRegistry } from '@superset-ui/core';
import {
getChartConfigs,
parseSelectedChart,
} from '../util/transformPropsUtil';
export default function transformProps(chartProps: ChartProps) {
const { width, height, formData, hooks, theme } = chartProps;
const {
geomColumn,
selectedChart: selectedChartString,
chartSize,
layerConfigs,
mapView,
chartBackgroundColor,
chartBackgroundBorderRadius,
} = formData;
const { setControlValue = () => {} } = hooks;
const selectedChart = parseSelectedChart(selectedChartString);
const transformPropsRegistry = getChartTransformPropsRegistry();
const chartTransformer = transformPropsRegistry.get(selectedChart.viz_type);
const chartConfigs = getChartConfigs(
selectedChart,
geomColumn,
chartProps,
chartTransformer,
);
return {
width,
height,
geomColumn,
selectedChart,
chartConfigs,
chartVizType: selectedChart.viz_type,
chartSize,
layerConfigs,
mapView,
chartBackgroundColor,
chartBackgroundBorderRadius,
setControlValue,
theme,
};
}