Skip to content

Commit f5bbfa5

Browse files
committed
feat: Remove UI theme from PlotlyExpressChartModel
1 parent 5d6205c commit f5bbfa5

2 files changed

Lines changed: 25 additions & 71 deletions

File tree

plugins/plotly-express/src/js/src/PlotlyExpressChartModel.ts

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,24 @@ import type {
77
TableSubscription,
88
TableData,
99
} from '@deephaven/jsapi-types';
10-
import {
11-
ChartModel,
12-
ChartUtils,
13-
ChartTheme,
14-
defaultChartTheme,
15-
} from '@deephaven/chart';
10+
import { ChartModel, ChartUtils } from '@deephaven/chart';
1611
import Log from '@deephaven/log';
1712
import {
1813
PlotlyChartWidgetData,
19-
applyColorwayToData,
2014
getDataMappings,
2115
getWidgetData,
16+
removeColorsFromData,
2217
} from './PlotlyExpressChartUtils.js';
2318

2419
const log = Log.module('@deephaven/js-plugin-plotly-express.ChartModel');
2520

2621
export class PlotlyExpressChartModel extends ChartModel {
27-
constructor(
28-
dh: DhType,
29-
widget: Widget,
30-
refetch: () => Promise<Widget>,
31-
theme: ChartTheme = defaultChartTheme()
32-
) {
22+
constructor(dh: DhType, widget: Widget, refetch: () => Promise<Widget>) {
3323
super(dh);
3424

3525
this.widget = widget;
3626
this.refetch = refetch;
3727
this.chartUtils = new ChartUtils(dh);
38-
this.theme = theme;
3928

4029
this.handleFigureUpdated = this.handleFigureUpdated.bind(this);
4130
this.handleWidgetUpdated = this.handleWidgetUpdated.bind(this);
@@ -89,14 +78,10 @@ export class PlotlyExpressChartModel extends ChartModel {
8978
*/
9079
tableDataMap: Map<number, { [key: string]: unknown[] }> = new Map();
9180

92-
theme: ChartTheme;
93-
9481
plotlyData: Data[] = [];
9582

9683
layout: Partial<Layout> = {};
9784

98-
plotlyLayout: Partial<Layout> = {};
99-
10085
isPaused = false;
10186

10287
hasPendingUpdate = false;
@@ -197,22 +182,11 @@ export class PlotlyExpressChartModel extends ChartModel {
197182

198183
updateLayout(data: PlotlyChartWidgetData) {
199184
const { figure } = data;
200-
const { plotly, deephaven } = figure;
185+
const { plotly } = figure;
201186
const { layout: plotlyLayout = {} } = plotly;
202187

203-
const template = { layout: this.chartUtils.makeDefaultLayout(this.theme) };
204-
205-
// For now we will only use the plotly theme colorway since most plotly themes are light mode
206-
if (deephaven.is_user_set_template) {
207-
template.layout.colorway =
208-
plotlyLayout.template?.layout?.colorway ?? template.layout.colorway;
209-
}
210-
211-
this.plotlyLayout = plotlyLayout;
212-
213188
this.layout = {
214189
...plotlyLayout,
215-
template,
216190
};
217191
}
218192

@@ -225,17 +199,19 @@ export class PlotlyExpressChartModel extends ChartModel {
225199
new_references: newReferences,
226200
removed_references: removedReferences,
227201
} = data;
228-
const { plotly } = figure;
202+
const { plotly, deephaven } = figure;
203+
const { layout: plotlyLayout = {} } = plotly;
229204
this.tableColumnReplacementMap = getDataMappings(data);
230205

231206
this.plotlyData = plotly.data;
232207
this.updateLayout(data);
233208

234-
applyColorwayToData(
235-
this.layout?.template?.layout?.colorway ?? [],
236-
this.plotlyLayout?.template?.layout?.colorway ?? [],
237-
this.plotlyData
238-
);
209+
if (!deephaven.is_user_set_template) {
210+
removeColorsFromData(
211+
plotlyLayout?.template?.layout?.colorway ?? [],
212+
this.plotlyData
213+
);
214+
}
239215

240216
newReferences.forEach(async (id, i) => {
241217
this.tableDataMap.set(id, {}); // Plot may render while tables are being fetched. Set this to avoid a render error

plugins/plotly-express/src/js/src/PlotlyExpressChartUtils.ts

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import type { Data, PlotlyDataLayoutConfig } from 'plotly.js';
22
import type { Widget } from '@deephaven/jsapi-types';
3-
import Log from '@deephaven/log';
4-
5-
const log = Log.module('@deephaven/js-plugin-plotly-express.ChartUtils');
63

74
export interface PlotlyChartWidgetData {
85
type: string;
@@ -57,39 +54,20 @@ export function getDataMappings(
5754
}
5855

5956
/**
60-
* Applies the colorway to the data unless the data color is not its default value
61-
* Data color is not default if the user set the color specifically or the plot type sets it
57+
* Removes the default colors from the data
58+
* Data color is not removed if the user set the color specifically or the plot type sets it
59+
*
60+
* This only checks if the marker or line color is set to a color in the colorway.
61+
* This means it is not possible to change the order of the colorway and use the same colors.
6262
*
63-
* @param colorway The colorway from the web UI
64-
* @param plotlyColorway The colorway from plotly
65-
* @param data The data to apply the colorway to. This will be mutated
63+
* @param colorway The colorway from plotly
64+
* @param data The data to remove the colorway from. This will be mutated
6665
*/
67-
export function applyColorwayToData(
68-
colorway: string[],
69-
plotlyColorway: string[],
70-
data: Data[]
71-
): void {
72-
if (colorway.length === 0) {
73-
return;
74-
}
75-
76-
if (plotlyColorway.length > colorway.length) {
77-
log.warn(
78-
"Plotly's colorway is longer than the web UI colorway. May result in incorrect colors for some series"
79-
);
80-
}
81-
82-
const colorMap = new Map(
83-
plotlyColorway.map((color, i) => [
84-
color.toUpperCase(),
85-
colorway[i] ?? color,
86-
])
87-
);
88-
89-
const plotlyColors = new Set(
90-
plotlyColorway.map(color => color.toUpperCase())
91-
);
66+
export function removeColorsFromData(colorway: string[], data: Data[]): void {
67+
const plotlyColors = new Set(colorway.map(color => color.toUpperCase()));
9268

69+
// Just check if the colors are in the colorway at any point
70+
// Plotly has many different ways to layer/order series
9371
for (let i = 0; i < data.length; i += 1) {
9472
const trace = data[i];
9573

@@ -101,7 +79,7 @@ export function applyColorwayToData(
10179
typeof trace.marker.color === 'string'
10280
) {
10381
if (plotlyColors.has(trace.marker.color.toUpperCase())) {
104-
trace.marker.color = colorMap.get(trace.marker.color.toUpperCase());
82+
delete trace.marker.color;
10583
}
10684
}
10785

@@ -112,7 +90,7 @@ export function applyColorwayToData(
11290
typeof trace.line.color === 'string'
11391
) {
11492
if (plotlyColors.has(trace.line.color.toUpperCase())) {
115-
trace.line.color = colorMap.get(trace.line.color.toUpperCase());
93+
delete trace.line.color;
11694
}
11795
}
11896
}

0 commit comments

Comments
 (0)