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

fix: propagate color mapping from dashboard to charts #7289

Merged
merged 3 commits into from
Apr 17, 2019
Merged
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
5 changes: 5 additions & 0 deletions superset/assets/backendSync.json
Original file line number Diff line number Diff line change
Expand Up @@ -3278,6 +3278,11 @@
"renderTrigger": true,
"description": "The color scheme for rendering chart"
},
"label_colors": {
"type": "ColorMapControl",
"label": "Color Map",
"default": {}
},
"significance_level": {
"type": "TextControl",
"label": "Significance Level",
Expand Down
2 changes: 2 additions & 0 deletions superset/assets/src/dashboard/components/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class Dashboard extends React.PureComponent {
chart,
dashboardMetadata: this.props.dashboardInfo.metadata,
filters: this.props.dashboardState.filters,
colorScheme: this.props.dashboardState.colorScheme,
colorNamespace: this.props.dashboardState.colorNamespace,
sliceId: chart.id,
});

Expand Down
3 changes: 2 additions & 1 deletion superset/assets/src/dashboard/containers/Chart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function mapStateToProps(
) {
const { id } = ownProps;
const chart = chartQueries[id] || {};
const { filters, colorScheme } = dashboardState;
const { filters, colorScheme, colorNamespace } = dashboardState;

return {
chart,
Expand All @@ -59,6 +59,7 @@ function mapStateToProps(
dashboardMetadata: dashboardInfo.metadata,
filters,
colorScheme,
colorNamespace,
sliceId: id,
}),
editMode: dashboardState.editMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
import { isEqual } from 'lodash';
import { CategoricalColorNamespace } from '@superset-ui/color';
import getEffectiveExtraFilters from './getEffectiveExtraFilters';

// We cache formData objects so that our connected container components don't always trigger
Expand All @@ -29,14 +31,21 @@ export default function getFormDataWithExtraFilters({
dashboardMetadata,
filters,
colorScheme,
colorNamespace,
sliceId,
}) {
// Propagate color mapping to chart
const scale = CategoricalColorNamespace.getScale(colorScheme, colorNamespace);
const labelColors = scale.getColorMap();

// if dashboard metadata + filters have not changed, use cache if possible
if (
(cachedDashboardMetadataByChart[sliceId] || {}) === dashboardMetadata &&
(cachedFiltersByChart[sliceId] || {}) === filters &&
(colorScheme == null ||
cachedFormdataByChart[sliceId].color_scheme === colorScheme) &&
cachedFormdataByChart[sliceId].color_namespace === colorNamespace &&
isEqual(cachedFormdataByChart[sliceId].label_colors, labelColors) &&
!!cachedFormdataByChart[sliceId]
) {
return cachedFormdataByChart[sliceId];
Expand All @@ -45,6 +54,7 @@ export default function getFormDataWithExtraFilters({
const formData = {
...chart.formData,
...(colorScheme && { color_scheme: colorScheme }),
label_colors: labelColors,
extra_filters: getEffectiveExtraFilters({
dashboardMetadata,
filters,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 PropTypes from 'prop-types';
import React from 'react';
import { CategoricalColorNamespace } from '@superset-ui/color';

const propTypes = {
onChange: PropTypes.func,
value: PropTypes.object,
colorScheme: PropTypes.string,
colorNamespace: PropTypes.string,
};

const defaultProps = {
onChange: () => {},
value: {},
colorScheme: undefined,
colorNamespace: undefined,
};

export default class ColorMapControl extends React.PureComponent {
constructor(props) {
super(props);
Object.keys(this.props.value).forEach((label) => {
CategoricalColorNamespace.getScale(
this.props.colorScheme,
this.props.colorNamespace,
).setColor(label, this.props.value[label]);
});
}

render() {
return null;
}
}

ColorMapControl.propTypes = propTypes;
ColorMapControl.defaultProps = defaultProps;
2 changes: 2 additions & 0 deletions superset/assets/src/explore/components/controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import AnnotationLayerControl from './AnnotationLayerControl';
import BoundsControl from './BoundsControl';
import CheckboxControl from './CheckboxControl';
import CollectionControl from './CollectionControl';
import ColorMapControl from './ColorMapControl';
import ColorPickerControl from './ColorPickerControl';
import ColorSchemeControl from './ColorSchemeControl';
import DatasourceControl from './DatasourceControl';
Expand All @@ -45,6 +46,7 @@ const controlMap = {
BoundsControl,
CheckboxControl,
CollectionControl,
ColorMapControl,
ColorPickerControl,
ColorSchemeControl,
DatasourceControl,
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Area.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default {
controlSetRows: [
['show_brush', 'show_legend'],
['line_interpolation', 'stacked_style'],
['color_scheme'],
['color_scheme', 'label_colors'],
['rich_tooltip', 'show_controls'],
],
},
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
['show_brush', 'show_legend', 'show_bar_value'],
['rich_tooltip', 'bar_stacked'],
['line_interpolation', 'show_controls'],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/BoxPlot.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
['whisker_options', 'x_ticks_layout'],
],
},
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
['show_legend', null],
],
},
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Chord.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default {
expanded: true,
controlSetRows: [
['y_axis_format', null],
['color_scheme'],
['color_scheme', 'label_colors'],
],
},
],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
],
},
{
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/DeckArc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default {
label: t('Arc'),
controlSetRows: [
['color_picker', 'target_color_picker'],
['dimension', 'color_scheme'],
['dimension', 'color_scheme', 'label_colors'],
['stroke_width', 'legend_position'],
],
},
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/DeckScatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default {
label: t('Point Color'),
controlSetRows: [
['color_picker', 'legend_position'],
['dimension', 'color_scheme'],
['dimension', 'color_scheme', 'label_colors'],
],
},
{
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/DistBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
['show_legend', 'show_bar_value'],
['bar_stacked', 'order_bars'],
['y_axis_format', 'y_axis_label'],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/DualLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
['x_axis_format'],
],
},
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
['link_length'],
['x_axis_label', 'y_axis_label'],
['global_opacity'],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
['show_brush', 'send_time_range', 'show_legend'],
['rich_tooltip', 'show_markers'],
['line_interpolation'],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/LineMulti.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
['prefix_metric_with_slice_name', null],
['show_legend', 'show_markers'],
['line_interpolation', null],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Partition.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
['number_format', 'date_time_format'],
['partition_limit', 'partition_threshold'],
['log_scale', 'equal_date_size'],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Pie.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default {
['pie_label_type', 'number_format'],
['donut', 'show_legend'],
['show_labels', 'labels_outside'],
['color_scheme'],
['color_scheme', 'label_colors'],
],
},
],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Rose.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
['number_format', 'date_time_format'],
['rich_tooltip', 'rose_area_proportion'],
],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Sankey.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
],
},
],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Sunburst.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
],
},
],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/Treemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
['treemap_ratio'],
['number_format'],
],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/WordCloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default {
controlSetRows: [
['size_from', 'size_to'],
['rotation'],
['color_scheme'],
['color_scheme', 'label_colors'],
],
},
],
Expand Down
2 changes: 1 addition & 1 deletion superset/assets/src/explore/controlPanels/sections.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const datasourceAndVizType = {
export const colorScheme = {
label: t('Color Scheme'),
controlSetRows: [
['color_scheme'],
['color_scheme', 'label_colors'],
],
};

Expand Down
10 changes: 10 additions & 0 deletions superset/assets/src/explore/controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,16 @@ export const controls = {
schemes: () => categoricalSchemeRegistry.getMap(),
},

label_colors: {
type: 'ColorMapControl',
label: t('Color Map'),
default: {},
mapStateToProps: state => ({
colorNamespace: state.form_data.color_namespace,
colorScheme: state.form_data.color_scheme,
}),
},

significance_level: {
type: 'TextControl',
label: t('Significance Level'),
Expand Down