Skip to content

Commit

Permalink
Started new plot impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ulises-jeremias committed Oct 13, 2023
1 parent 910906d commit 3c83c65
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 28 deletions.
8 changes: 4 additions & 4 deletions examples/data_analysis_example/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand Down Expand Up @@ -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'
)
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_bar/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_basic_heatmap/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions examples/plot_grouped_bar_chart/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down
4 changes: 2 additions & 2 deletions examples/plot_scatter3d_2/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_scatter_with_bars/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand Down
11 changes: 10 additions & 1 deletion plot/plot.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module plot

import arrays

// Plot is the main structure that contains layout and traces
// to generate plots
pub struct Plot {
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion plot/plot_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 1 addition & 11 deletions plot/scripts/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)


Expand Down
9 changes: 5 additions & 4 deletions plot/trace.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3c83c65

Please sign in to comment.