Skip to content

Commit be747b2

Browse files
committed
Parametrize formatting variables
1 parent d43bca2 commit be747b2

File tree

7 files changed

+200
-95
lines changed

7 files changed

+200
-95
lines changed

examples/hello_world.jpg

-2.18 KB
Loading

examples/hello_world.png

-5.78 KB
Loading

examples/hello_world.roc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ config =
77
{
88
outputFilePath: "./examples/hello_world.svg",
99
title: "Hello, World!",
10-
subtitle: "This data is coming from Roc ☺",
10+
subtitle: "",
1111
width: 1024,
1212
height: 768,
1313
lines: [
@@ -16,6 +16,26 @@ config =
1616
{ name: "sine", color: blue, points: sin },
1717
{ name: "sine x 2", color: red, points: sinX2 },
1818
],
19+
bounds: {
20+
xMin: -3.2,
21+
xMax: 3.2,
22+
yMin: -2.1,
23+
yMax: 2.1,
24+
},
25+
fonts: {
26+
titleFamily: "sans-serif",
27+
titleSize: 60,
28+
subtitleFamily: "sans-serif",
29+
subtitleSize: 40,
30+
},
31+
labels: {
32+
xCount: 20,
33+
yCount: 10,
34+
},
35+
layout: {
36+
chartMargin: 5,
37+
labelArea: 50,
38+
},
1939
}
2040

2141
pi = 3.141592653589793

examples/hello_world.svg

Lines changed: 78 additions & 81 deletions
Loading

platform/Config.roc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ interface Config
22
exposes [ Config, black, white, red, green, blue, cyan, magenta, yellow ]
33
imports []
44

5+
Bounds :
6+
{
7+
xMin: F64,
8+
xMax: F64,
9+
yMin: F64,
10+
yMax: F64,
11+
}
12+
513
# TODO: probably move some of this into a different module.
614
Color :
715
{
@@ -19,6 +27,26 @@ cyan = { r: 0, g: 255, b: 255 }
1927
magenta = { r: 255, g: 0, b: 255 }
2028
yellow = { r: 255, g: 255, b: 0 }
2129

30+
Fonts :
31+
{
32+
titleFamily: Str,
33+
titleSize: U32,
34+
subtitleFamily: Str,
35+
subtitleSize: U32,
36+
}
37+
38+
Labels :
39+
{
40+
xCount: Nat,
41+
yCount: Nat,
42+
}
43+
44+
Layout :
45+
{
46+
chartMargin: U32,
47+
labelArea: U32,
48+
}
49+
2250
Line :
2351
{
2452
name: Str,
@@ -35,4 +63,8 @@ Config :
3563
width : U32,
3664
height : U32,
3765
lines : List Line,
66+
bounds : Bounds,
67+
fonts : Fonts,
68+
labels : Labels,
69+
layout : Layout,
3870
}

platform/src/config.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ pub struct P2 {
77
pub y: f64,
88
}
99

10+
#[derive(Default, Debug, Copy, Clone)]
11+
#[repr(C)]
12+
pub struct Bounds {
13+
pub xMax: f64,
14+
pub xMin: f64,
15+
pub yMax: f64,
16+
pub yMin: f64,
17+
}
18+
1019
#[derive(Default, Debug, Copy, Clone)]
1120
#[repr(C)]
1221
pub struct Color {
@@ -15,6 +24,29 @@ pub struct Color {
1524
pub r: u8,
1625
}
1726

27+
#[derive(Default, Debug)]
28+
#[repr(C)]
29+
pub struct Fonts {
30+
pub subtitleFamily: RocStr,
31+
pub titleFamily: RocStr,
32+
pub subtitleSize: u32,
33+
pub titleSize: u32,
34+
}
35+
36+
#[derive(Default, Debug)]
37+
#[repr(C)]
38+
pub struct Labels {
39+
pub xCount: usize,
40+
pub yCount: usize,
41+
}
42+
43+
#[derive(Default, Debug)]
44+
#[repr(C)]
45+
pub struct Layout {
46+
pub chartMargin: u32,
47+
pub labelArea: u32,
48+
}
49+
1850
#[derive(Default, Debug)]
1951
#[repr(C)]
2052
pub struct Line {
@@ -26,11 +58,15 @@ pub struct Line {
2658
#[derive(Default, Debug)]
2759
#[repr(C)]
2860
pub struct Config {
61+
pub bounds: Bounds,
62+
pub fonts: Fonts,
63+
pub labels: Labels,
2964
pub lines: RocList<Line>,
3065
pub output_file_path: RocStr,
3166
pub subtitle: RocStr,
3267
pub title: RocStr,
3368
pub height: u32,
69+
pub layout: Layout,
3470
pub width: u32,
3571
}
3672

platform/src/plot.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,30 @@ fn construct_area<Backend: DrawingBackend>(
1919
) -> Result<DrawingArea<Backend, Shift>, DrawingAreaErrorKind<Backend::ErrorType>> {
2020
let area = backend.into_drawing_area();
2121
area.fill(&WHITE)?;
22-
let area = area.titled(config.title.as_str(), ("sans-serif", 60))?;
22+
let area = area.titled(
23+
config.title.as_str(),
24+
(config.fonts.titleFamily.as_str(), config.fonts.titleSize),
25+
)?;
2326
Ok(area)
2427
}
2528

29+
fn construct_builder<'a, Backend: DrawingBackend>(
30+
area: &'a DrawingArea<Backend, Shift>,
31+
config: &'a Config,
32+
) -> ChartBuilder<'a, 'a, Backend> {
33+
let subtitle = config.subtitle.as_str();
34+
let mut builder = ChartBuilder::on(area);
35+
builder.margin(config.layout.chartMargin)
36+
.set_all_label_area_size(config.layout.labelArea);
37+
if subtitle.len() > 0 {
38+
builder.caption(
39+
config.subtitle.as_str(),
40+
(config.fonts.subtitleFamily.as_str(), config.fonts.subtitleSize),
41+
);
42+
}
43+
return builder;
44+
}
45+
2646
fn construct_bitmap_backend(config: &Config) -> BitMapBackend {
2747
BitMapBackend::new(
2848
config.output_file_path.as_str(),
@@ -42,26 +62,26 @@ fn plot_with<Backend: DrawingBackend>(
4262
config: &Config,
4363
) -> Result<(), DrawingAreaErrorKind<Backend::ErrorType>> {
4464
let area = construct_area(backend, config)?;
45-
let mut cc = ChartBuilder::on(&area)
46-
.margin(5)
47-
.set_all_label_area_size(50)
48-
.caption(config.subtitle.as_str(), ("sans-serif", 40))
49-
.build_cartesian_2d(-3.2f64..3.2f64, -2.1f64..2.1f64)?;
50-
cc.configure_mesh()
51-
.x_labels(20)
52-
.y_labels(10)
65+
let mut builder = construct_builder(&area, config);
66+
let mut context = builder.build_cartesian_2d(
67+
config.bounds.xMin..config.bounds.xMax,
68+
config.bounds.yMin..config.bounds.yMax,
69+
)?;
70+
context.configure_mesh()
71+
.x_labels(config.labels.xCount)
72+
.y_labels(config.labels.yCount)
5373
.disable_mesh()
54-
.x_label_formatter(&|v| format!("{:.1}", v))
55-
.y_label_formatter(&|v| format!("{:.1}", v))
74+
.x_label_formatter(&|v| format!("{:?}", v)) // TODO: Format labels in Roc.
75+
.y_label_formatter(&|v| format!("{:?}", v)) // TODO: Format labels in Roc.
5676
.draw()?;
5777
for line in config.lines.iter() {
5878
let v: Vec<(f64, f64)> = line.points.iter().map(|point| (point.x, point.y)).collect();
5979
let color = RGBColor(line.color.r, line.color.g, line.color.b);
60-
cc.draw_series(LineSeries::new(v, color))?
80+
context.draw_series(LineSeries::new(v, color))?
6181
.label(line.name.as_str())
6282
.legend(move |(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], color));
6383
}
64-
cc.configure_series_labels().border_style(&BLACK).draw()?;
84+
context.configure_series_labels().border_style(&BLACK).draw()?;
6585
area.present().unwrap_or_else(|_| {
6686
panic!(
6787
"I failed to draw your plot to {} !",

0 commit comments

Comments
 (0)