Skip to content

Commit

Permalink
Passed fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ulises-jeremias committed Jul 11, 2021
1 parent cdf35df commit 484abb3
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 109 deletions.
16 changes: 8 additions & 8 deletions plot/annotation.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module plot
// Annotation handles all the information needed to annotate plots
pub struct Annotation {
pub mut:
x f64
y f64
text string
showarrow bool
arrowhead int
arrowcolor string = 'black'
align string = 'center'
font Font
x f64
y f64
text string
showarrow bool
arrowhead int
arrowcolor string = 'black'
align string = 'center'
font Font
}
14 changes: 7 additions & 7 deletions plot/axis.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ module plot
// Axis handles axis data
pub struct Axis {
pub mut:
title AxisTitle
tickmode string = 'auto'
tick0 f64
dtick f64
tickvals []f64
ticktext []string
title AxisTitle
tickmode string = 'auto'
tick0 f64
dtick f64
tickvals []f64
ticktext []string
}

// AxisTitle handles needed data to render an axis title
pub struct AxisTitle {
pub mut:
text string
text string
}
18 changes: 8 additions & 10 deletions plot/examples/bar.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ fn main() {
// but all the others expect it as a list of numbers.
// Therefore, we added `x_str` which is handled by the
// Python code, and it will be used as `x`.
plot.new_plot()
.add_trace(
trace_type: .bar,
x_str: ['China', 'India', 'USA', 'Indonesia', 'Pakistan'],
y: [1411778724., 1379217184, 331989449, 271350000, 225200000]
)
.set_layout(
title: 'Countries by population'
)
.show() or { panic(err) }

plot.new_plot().add_trace(
trace_type: .bar
x_str: ['China', 'India', 'USA', 'Indonesia', 'Pakistan']
y: [1411778724., 1379217184, 331989449, 271350000, 225200000]
).set_layout(
title: 'Countries by population'
).show() or { panic(err) }
}
25 changes: 11 additions & 14 deletions plot/examples/pie.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ module main
import vsl.plot

fn main() {
plot.new_plot()
.add_trace(
trace_type: .pie,
labels: ['Nitrogen', 'Oxygen', 'Argon', 'Other'],
values: [78., 21, 0.9, 0.1],
pull: [0., 0.1, 0, 0],
hole: 0.25
)
.set_layout(
title: 'Gases in the atmosphere',
width: 750,
height: 750
)
.show() or { panic(err) }
plot.new_plot().add_trace(
trace_type: .pie
labels: ['Nitrogen', 'Oxygen', 'Argon', 'Other']
values: [78., 21, 0.9, 0.1]
pull: [0., 0.1, 0, 0]
hole: 0.25
).set_layout(
title: 'Gases in the atmosphere'
width: 750
height: 750
).show() or { panic(err) }
}
47 changes: 28 additions & 19 deletions plot/examples/scatter.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ import vsl.util

fn main() {
y := [
0., 1, 3, 1, 0, -1, -3, -1, 0, 1, 3, 1, 0
0.,
1,
3,
1,
0,
-1,
-3,
-1,
0,
1,
3,
1,
0,
]
x := util.arange(y.len).map(f64(it))

Expand All @@ -14,22 +26,19 @@ fn main() {
// -' '- -' '_
// '.'

plot.new_plot()
.add_trace(
trace_type: .scatter,
x: x,
y: y,
mode: 'lines+markers',
marker: {
size: []f64{len: x.len, init: 10.},
color: []string{len: x.len, init: '#FF0000'},
}
line: {
color: '#FF0000',
}
)
.set_layout(
title: 'Scatter plot example'
)
.show() or { panic(err) }
plot.new_plot().add_trace(
trace_type: .scatter
x: x
y: y
mode: 'lines+markers'
marker: {
size: []f64{len: x.len, init: 10.}
color: []string{len: x.len, init: '#FF0000'}
}
line: {
color: '#FF0000'
}
).set_layout(
title: 'Scatter plot example'
).show() or { panic(err) }
}
6 changes: 3 additions & 3 deletions plot/font.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module plot
// Font handles data to customize fonts
pub struct Font {
pub mut:
color string = 'black'
family string = 'monospace'
size f64 = 16.
color string = 'black'
family string = 'monospace'
size f64 = 16.
}
14 changes: 7 additions & 7 deletions plot/layout.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module plot
// Layout
pub struct Layout {
pub mut:
title string
title_x f64 = 0.5
autosize bool = true
width int = 500
height int = 500
xaxis Axis
yaxis Axis
title string
title_x f64 = 0.5
autosize bool = true
width int = 500
height int = 500
xaxis Axis
yaxis Axis
annotations []Annotation
}
36 changes: 19 additions & 17 deletions plot/plot.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ module plot
import json
import os
import time

import vsl.errors

const (
schema_version = 'v1'
venv_dir_name = '.plotvenv_$schema_version'
data_dir_name = '.data_$schema_version'
venv_dir_name = '.plotvenv_$schema_version'
data_dir_name = '.data_$schema_version'
)

// Plot is the main structure that contains layout and traces
// to generate plots
pub struct Plot {
pub mut:
traces []Trace
layout Layout
traces []Trace
layout Layout
}

pub fn new_plot() Plot {
Expand All @@ -43,30 +42,33 @@ pub fn (p Plot) show() ? {
ts := time.now().format_ss_micro()
plot_str := json.encode_pretty(p.traces)

venv_path := solve_mod_path(venv_dir_name)
venv_path := solve_mod_path(plot.venv_dir_name)
if !os.is_dir(venv_path) {
return errors.error('plotly virtualenv not found', .efailed)
}
data_dir_path := solve_mod_path(data_dir_name)

data_dir_path := solve_mod_path(plot.data_dir_name)
if !os.is_dir(data_dir_path) {
os.mkdir(data_dir_path) or {
return errors.error('could not create needed dir path at ${data_dir_path}.', .efailed)
return errors.error('could not create needed dir path at ${data_dir_path}.',
.efailed)
}
}

data_path := solve_mod_path(data_dir_name, 'data-${ts}.json')
layout_path := solve_mod_path(data_dir_name, 'layout-${ts}.json')
data_path := solve_mod_path(plot.data_dir_name, 'data-${ts}.json')
layout_path := solve_mod_path(plot.data_dir_name, 'layout-${ts}.json')
run_path := solve_mod_path('run.sh')

os.write_file(data_path, plot_str) or {
return errors.error('could not save the plot to a JSON file at ${data_path}.', .efailed)
return errors.error('could not save the plot to a JSON file at ${data_path}.',
.efailed)
}
layout_str := json.encode(p.layout)
os.write_file(layout_path, layout_str) or {
return errors.error('could not save the layout to a JSON file at ${layout_path}.', .efailed)
return errors.error('could not save the layout to a JSON file at ${layout_path}.',
.efailed)
}
result := os.execute('bash ${run_path} --venv="${venv_path}" --data="${data_path}" --layout="${layout_path}"')
result := os.execute('bash $run_path --venv="$venv_path" --data="$data_path" --layout="$layout_path"')
if result.exit_code != 0 {
return error_with_code(result.output, result.exit_code)
}
Expand All @@ -75,12 +77,12 @@ pub fn (p Plot) show() ? {

// init will ensure that all dependencies are correctly installed and venv initiallized
fn init() {
venv_path := solve_mod_path(venv_dir_name)
venv_path := solve_mod_path(plot.venv_dir_name)
if !os.is_dir(venv_path) {
println('Creating plotly virtualenv...')
}
init_path := solve_mod_path('create-venv.sh')
result := os.execute('bash $init_path --venv="${venv_path}"')
result := os.execute('bash $init_path --venv="$venv_path"')
if result.exit_code != 0 {
panic(result.output)
}
Expand All @@ -89,4 +91,4 @@ fn init() {

fn solve_mod_path(dirs ...string) string {
return os.join_path(@VMODROOT, ...dirs)
}
}
48 changes: 24 additions & 24 deletions plot/trace.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ pub enum TraceType {

pub struct Trace {
pub mut:
trace_type TraceType [required]
x []f64
x_str []string
y []f64
z [][]f64
values []f64
labels []string
text []string
customdata [][]string
name string
mode string
marker Marker
line Line
pull []f64
hole f64
colorscale string = 'Viridis'
hovertemplate string
trace_type TraceType [required]
x []f64
x_str []string
y []f64
z [][]f64
values []f64
labels []string
text []string
customdata [][]string
name string
mode string
marker Marker
line Line
pull []f64
hole f64
colorscale string = 'Viridis'
hovertemplate string
}

pub struct Marker {
pub mut:
size []f64
color []string
opacity f64 = 0.8
colorscale string = 'Viridis'
size []f64
color []string
opacity f64 = 0.8
colorscale string = 'Viridis'
}

pub struct Line {
pub mut:
color string
width f64 = 2.0
color string
width f64 = 2.0
// check Plotly docs for more dash types
dash string = 'solid'
dash string = 'solid'
}

0 comments on commit 484abb3

Please sign in to comment.