Skip to content

Commit 72b4287

Browse files
committed
refactor: streamline parameter handling in Plotly method calls
- Updated multiple Plotly method implementations to use a single Object for parameters, improving readability and maintainability. - Added checks to only include non-empty parameters, ensuring cleaner calls to the Plotly API. Signed-off-by: Y.Hisaki <yhisaki31@gmail.com>
1 parent 94dbc6f commit 72b4287

File tree

2 files changed

+74
-30
lines changed

2 files changed

+74
-30
lines changed

src/plotly.cpp

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,15 @@ class Figure::Impl {
158158

159159
auto newPlot(const Object &data, const Object &layout,
160160
const Object &config) const -> bool {
161-
auto result =
162-
callPlotly("Plotly.newPlot",
163-
{{"data", data}, {"layout", layout}, {"config", config}});
161+
Object params;
162+
params["data"] = data;
163+
if (!layout.empty()) {
164+
params["layout"] = layout;
165+
}
166+
if (!config.empty()) {
167+
params["config"] = config;
168+
}
169+
auto result = callPlotly("Plotly.newPlot", params);
164170
return result.has_value();
165171
}
166172

@@ -171,14 +177,20 @@ class Figure::Impl {
171177
-> bool {
172178
Object params;
173179
params["traceUpdate"] = traceUpdate;
174-
params["layoutUpdate"] = layoutUpdate;
180+
if (!layoutUpdate.empty()) {
181+
params["layoutUpdate"] = layoutUpdate;
182+
}
175183

176184
auto result = callPlotly("Plotly.update", params);
177185
return result.has_value();
178186
}
179187

180188
auto downloadImage(const Object &opts) const -> bool {
181-
auto result = callPlotly("Plotly.downloadImage", {{"opts", opts}});
189+
Object params;
190+
if (!opts.empty()) {
191+
params["opts"] = opts;
192+
}
193+
auto result = callPlotly("Plotly.downloadImage", params);
182194
if (!result.has_value()) {
183195
return false;
184196
}
@@ -222,7 +234,11 @@ class Figure::Impl {
222234
/// Updates the figure layout
223235
/// @param layout Layout updates to apply
224236
auto relayout(const Object &layout) const -> bool {
225-
auto result = callPlotly("Plotly.relayout", {{"layout", layout}});
237+
Object params;
238+
if (!layout.empty()) {
239+
params["layout"] = layout;
240+
}
241+
auto result = callPlotly("Plotly.relayout", params);
226242
return result.has_value();
227243
}
228244

@@ -242,24 +258,34 @@ class Figure::Impl {
242258
/// @param aobj Style updates to apply
243259
/// @param traces Optional trace indices to target
244260
auto restyle(const Object &aobj, const Object &traces) const -> bool {
245-
auto result =
246-
callPlotly("Plotly.restyle", {{"aobj", aobj}, {"traces", traces}});
261+
Object params;
262+
params["aobj"] = aobj;
263+
if (!traces.empty()) {
264+
params["traces"] = traces;
265+
}
266+
auto result = callPlotly("Plotly.restyle", params);
247267
return result.has_value();
248268
}
249269

250270
/// Adds new traces to the plot
251271
/// @param traces New trace data to add
252272
/// @param newIndices Optional indices where to insert the traces
253273
auto addTraces(const Object &traces, const Object &newIndices) const -> bool {
254-
auto result = callPlotly("Plotly.addTraces",
255-
{{"traces", traces}, {"newIndices", newIndices}});
274+
Object params;
275+
params["traces"] = traces;
276+
if (!newIndices.empty()) {
277+
params["newIndices"] = newIndices;
278+
}
279+
auto result = callPlotly("Plotly.addTraces", params);
256280
return result.has_value();
257281
}
258282

259283
/// Deletes traces from the plot
260284
/// @param indices Indices of traces to delete
261285
auto deleteTraces(const Object &indices) const -> bool {
262-
auto result = callPlotly("Plotly.deleteTraces", {{"indices", indices}});
286+
Object params;
287+
params["indices"] = indices;
288+
auto result = callPlotly("Plotly.deleteTraces", params);
263289
return result.has_value();
264290
}
265291

@@ -268,9 +294,10 @@ class Figure::Impl {
268294
/// @param newIndices New indices for the traces
269295
auto moveTraces(const Object &currentIndices, const Object &newIndices) const
270296
-> bool {
271-
auto result =
272-
callPlotly("Plotly.moveTraces", {{"currentIndices", currentIndices},
273-
{"newIndices", newIndices}});
297+
Object params;
298+
params["currentIndices"] = currentIndices;
299+
params["newIndices"] = newIndices;
300+
auto result = callPlotly("Plotly.moveTraces", params);
274301
return result.has_value();
275302
}
276303

@@ -280,9 +307,13 @@ class Figure::Impl {
280307
/// @param maxPoints Optional maximum number of points to keep
281308
auto extendTraces(const Object &update, const Object &indices,
282309
const Object &maxPoints) const -> bool {
283-
auto result = callPlotly(
284-
"Plotly.extendTraces",
285-
{{"update", update}, {"indices", indices}, {"maxPoints", maxPoints}});
310+
Object params;
311+
params["update"] = update;
312+
params["indices"] = indices;
313+
if (!maxPoints.empty()) {
314+
params["maxPoints"] = maxPoints;
315+
}
316+
auto result = callPlotly("Plotly.extendTraces", params);
286317
return result.has_value();
287318
}
288319

@@ -291,8 +322,10 @@ class Figure::Impl {
291322
/// @param indices Trace indices to prepend to
292323
auto prependTraces(const Object &update, const Object &indices) const
293324
-> bool {
294-
auto result = callPlotly("Plotly.prependTraces",
295-
{{"update", update}, {"indices", indices}});
325+
Object params;
326+
params["update"] = update;
327+
params["indices"] = indices;
328+
auto result = callPlotly("Plotly.prependTraces", params);
296329
return result.has_value();
297330
}
298331

@@ -302,23 +335,33 @@ class Figure::Impl {
302335
/// @param config Optional configuration
303336
auto react(const Object &data, const Object &layout,
304337
const Object &config) const -> bool {
305-
auto result =
306-
callPlotly("Plotly.react",
307-
{{"data", data}, {"layout", layout}, {"config", config}});
338+
Object params;
339+
params["data"] = data;
340+
if (!layout.empty()) {
341+
params["layout"] = layout;
342+
}
343+
if (!config.empty()) {
344+
params["config"] = config;
345+
}
346+
auto result = callPlotly("Plotly.react", params);
308347
return result.has_value();
309348
}
310349

311350
/// Adds animation frames to the plot
312351
/// @param frames Frame data to add
313352
auto addFrames(const Object &frames) const -> bool {
314-
auto result = callPlotly("Plotly.addFrames", {{"frames", frames}});
353+
Object params;
354+
params["frames"] = frames;
355+
auto result = callPlotly("Plotly.addFrames", params);
315356
return result.has_value();
316357
}
317358

318359
/// Deletes animation frames from the plot
319360
/// @param frames Frame names or indices to delete
320361
auto deleteFrames(const Object &frames) const -> bool {
321-
auto result = callPlotly("Plotly.deleteFrames", {{"frames", frames}});
362+
Object params;
363+
params["frames"] = frames;
364+
auto result = callPlotly("Plotly.deleteFrames", params);
322365
return result.has_value();
323366
}
324367

@@ -327,11 +370,12 @@ class Figure::Impl {
327370
/// @param opts Animation options
328371
auto animate(const Object &frameOrGroupNameOrFrameList,
329372
const Object &opts) const -> bool {
330-
auto result = callPlotly(
331-
"Plotly.animate",
332-
{{"frameOrGroupNameOrFrameList", frameOrGroupNameOrFrameList},
333-
{"opts", opts}},
334-
24h);
373+
Object params;
374+
params["frameOrGroupNameOrFrameList"] = frameOrGroupNameOrFrameList;
375+
if (!opts.empty()) {
376+
params["opts"] = opts;
377+
}
378+
auto result = callPlotly("Plotly.animate", params, 24h);
335379
return result.has_value();
336380
}
337381

webapp/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)