From 3c83c65cc0536f1eb8cc31960b3e70c115611db3 Mon Sep 17 00:00:00 2001 From: ulises-jeremias Date: Fri, 13 Oct 2023 00:52:21 -0300 Subject: [PATCH] Started new plot impl --- examples/data_analysis_example/main.v | 8 ++++---- examples/plot_bar/main.v | 2 +- examples/plot_basic_heatmap/main.v | 2 +- examples/plot_grouped_bar_chart/main.v | 4 ++-- examples/plot_scatter3d_2/main.v | 4 ++-- examples/plot_scatter_with_bars/main.v | 2 +- plot/plot.v | 11 ++++++++++- plot/plot_test.v | 2 +- plot/scripts/plotter.py | 12 +----------- plot/trace.v | 9 +++++---- 10 files changed, 28 insertions(+), 28 deletions(-) diff --git a/examples/data_analysis_example/main.v b/examples/data_analysis_example/main.v index f8a37f483..ea581a5f8 100644 --- a/examples/data_analysis_example/main.v +++ b/examples/data_analysis_example/main.v @@ -89,7 +89,7 @@ fn main() { trace_type: .scatter3d x: x1_class0 y: x2_class0 - z: [][]f64{len: x1_class0.len, init: [0.0]} + z: []f64{len: x1_class0.len, init: 0.0} mode: 'markers' marker: plot.Marker{ size: []f64{len: x1_class0.len, init: 8.0} @@ -102,7 +102,7 @@ fn main() { trace_type: .scatter3d x: x1_class1 y: x2_class1 - z: [][]f64{len: x1_class1.len, init: [0.0]} + z: []f64{len: x1_class1.len, init: 0.0} mode: 'markers' marker: plot.Marker{ size: []f64{len: x1_class1.len, init: 8.0} @@ -138,14 +138,14 @@ fn main() { plt_bars.add_trace( trace_type: .bar - x_str: []string{len: stat.mean_x.len, init: 'Class ${index}'} + x: []string{len: stat.mean_x.len, init: 'Class ${index}'} y: stat.mean_x name: 'Mean' ) plt_bars.add_trace( trace_type: .bar - x_str: []string{len: stat.sig_x.len, init: 'Class ${index}'} + x: []string{len: stat.sig_x.len, init: 'Class ${index}'} y: stat.sig_x name: 'Standard Deviation' ) diff --git a/examples/plot_bar/main.v b/examples/plot_bar/main.v index d6a44764f..e2972f32e 100644 --- a/examples/plot_bar/main.v +++ b/examples/plot_bar/main.v @@ -7,7 +7,7 @@ fn main() { plt.add_trace( trace_type: .bar - x_str: ['China', 'India', 'USA', 'Indonesia', 'Pakistan'] + x: ['China', 'India', 'USA', 'Indonesia', 'Pakistan'] y: [1411778724.0, 1379217184, 331989449, 271350000, 225200000] ) plt.set_layout( diff --git a/examples/plot_basic_heatmap/main.v b/examples/plot_basic_heatmap/main.v index 8dd3e05f1..f4341f9b1 100644 --- a/examples/plot_basic_heatmap/main.v +++ b/examples/plot_basic_heatmap/main.v @@ -10,7 +10,7 @@ mut plt := plot.new_plot() plt.add_trace( trace_type: .heatmap - x_str: x + x: x y_str: y z: z ) diff --git a/examples/plot_grouped_bar_chart/main.v b/examples/plot_grouped_bar_chart/main.v index 96886dd87..f16b8f21a 100644 --- a/examples/plot_grouped_bar_chart/main.v +++ b/examples/plot_grouped_bar_chart/main.v @@ -10,13 +10,13 @@ fn main() { mut plt := plot.new_plot() plt.add_trace( trace_type: .bar - x_str: categories + x: categories y: values1 name: 'Group 1' ) plt.add_trace( trace_type: .bar - x_str: categories + x: categories y: values2 name: 'Group 2' ) diff --git a/examples/plot_scatter3d_2/main.v b/examples/plot_scatter3d_2/main.v index c3ffb242d..b394b3c59 100644 --- a/examples/plot_scatter3d_2/main.v +++ b/examples/plot_scatter3d_2/main.v @@ -6,13 +6,13 @@ import vsl.plot fn main() { mut x := []f64{cap: 100} mut y := []f64{cap: 100} - mut z := [][]f64{cap: 100} + mut z := []f64{cap: 100} for i in 0 .. 100 { val := f64(i) * 0.1 x << math.cos(val) y << math.sin(val) - z << [val] + z << val } mut plt := plot.new_plot() diff --git a/examples/plot_scatter_with_bars/main.v b/examples/plot_scatter_with_bars/main.v index 2ac6ed0ad..fe742db9f 100644 --- a/examples/plot_scatter_with_bars/main.v +++ b/examples/plot_scatter_with_bars/main.v @@ -20,7 +20,7 @@ fn main() { ) plt.add_trace( trace_type: .bar - x_str: x.map(it.str()) + x: x.map(it.str()) y: y marker: plot.Marker{ color: []string{len: x.len, init: '#0000FF'} diff --git a/plot/plot.v b/plot/plot.v index 40e823cfb..98df2766f 100644 --- a/plot/plot.v +++ b/plot/plot.v @@ -1,5 +1,7 @@ module plot +import arrays + // Plot is the main structure that contains layout and traces // to generate plots pub struct Plot { @@ -13,7 +15,14 @@ pub fn new_plot() Plot { } pub fn (mut p Plot) add_trace(trace Trace) Plot { - p.traces << trace + mut next_trace := trace + if trace.trace_type == .scatter3d { + z := next_trace.z + if z is [][]f64 { + next_trace.z = arrays.flatten(z) + } + } + p.traces << next_trace return p } diff --git a/plot/plot_test.v b/plot/plot_test.v index 02f3d0e9c..b67b09591 100644 --- a/plot/plot_test.v +++ b/plot/plot_test.v @@ -5,7 +5,7 @@ fn test_bar() { plt.add_trace( trace_type: .bar - x_str: ['China', 'India', 'USA', 'Indonesia', 'Pakistan'] + x: ['China', 'India', 'USA', 'Indonesia', 'Pakistan'] y: [1411778724.0, 1379217184, 331989449, 271350000, 225200000] ) plt.set_layout( diff --git a/plot/scripts/plotter.py b/plot/scripts/plotter.py index 2b9f2b739..40e255e1d 100644 --- a/plot/scripts/plotter.py +++ b/plot/scripts/plotter.py @@ -73,11 +73,10 @@ def process_trace(trace): """ Process a trace to ensure only accepted keys are present. """ - custom_keys = ["x_str"] trace_type = trace.pop("trace_type") # Remove all JSON keys not accepted by Plotly. - accepted = dir(map_trace_type_to_plotly_object(trace_type)) + custom_keys + accepted = dir(map_trace_type_to_plotly_object(trace_type)) keys = list(trace.keys()) for k in keys: if k not in accepted: @@ -90,15 +89,6 @@ def process_trace(trace): trace["marker"].pop("opacity") trace["marker"].pop("colorscale") - if "x_str" in trace: - if trace_type == 'bar': - trace["x"] = trace["x_str"] - trace.pop("x_str") - - # Flatten 'z' when dealing with 3D scatters. - if trace_type == 'scatter3d': - trace["z"] = [item for sublist in trace["z"] for item in sublist] - return map_trace_type_to_plotly_object(trace_type)(trace) diff --git a/plot/trace.v b/plot/trace.v index 0bb4ca87a..e0e4a9695 100644 --- a/plot/trace.v +++ b/plot/trace.v @@ -10,16 +10,17 @@ pub enum TraceType { histogram } +type XType = []f64 | []string +type ZType = [][]f64 | []f64 + [params] pub struct Trace { pub mut: trace_type TraceType [required] - x []f64 - x_str []string - y_str []string + x XType xbins map[string]f32 y []f64 - z [][]f64 + z ZType values []f64 labels []string text []string