Skip to content

Commit

Permalink
Parametrize formatting variables
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCVanB committed Feb 19, 2022
1 parent d43bca2 commit be747b2
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 95 deletions.
Binary file modified examples/hello_world.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/hello_world.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 21 additions & 1 deletion examples/hello_world.roc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ config =
{
outputFilePath: "./examples/hello_world.svg",
title: "Hello, World!",
subtitle: "This data is coming from Roc ☺",
subtitle: "",
width: 1024,
height: 768,
lines: [
Expand All @@ -16,6 +16,26 @@ config =
{ name: "sine", color: blue, points: sin },
{ name: "sine x 2", color: red, points: sinX2 },
],
bounds: {
xMin: -3.2,
xMax: 3.2,
yMin: -2.1,
yMax: 2.1,
},
fonts: {
titleFamily: "sans-serif",
titleSize: 60,
subtitleFamily: "sans-serif",
subtitleSize: 40,
},
labels: {
xCount: 20,
yCount: 10,
},
layout: {
chartMargin: 5,
labelArea: 50,
},
}

pi = 3.141592653589793
Expand Down
159 changes: 78 additions & 81 deletions examples/hello_world.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions platform/Config.roc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ interface Config
exposes [ Config, black, white, red, green, blue, cyan, magenta, yellow ]
imports []

Bounds :
{
xMin: F64,
xMax: F64,
yMin: F64,
yMax: F64,
}

# TODO: probably move some of this into a different module.
Color :
{
Expand All @@ -19,6 +27,26 @@ cyan = { r: 0, g: 255, b: 255 }
magenta = { r: 255, g: 0, b: 255 }
yellow = { r: 255, g: 255, b: 0 }

Fonts :
{
titleFamily: Str,
titleSize: U32,
subtitleFamily: Str,
subtitleSize: U32,
}

Labels :
{
xCount: Nat,
yCount: Nat,
}

Layout :
{
chartMargin: U32,
labelArea: U32,
}

Line :
{
name: Str,
Expand All @@ -35,4 +63,8 @@ Config :
width : U32,
height : U32,
lines : List Line,
bounds : Bounds,
fonts : Fonts,
labels : Labels,
layout : Layout,
}
36 changes: 36 additions & 0 deletions platform/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ pub struct P2 {
pub y: f64,
}

#[derive(Default, Debug, Copy, Clone)]
#[repr(C)]
pub struct Bounds {
pub xMax: f64,
pub xMin: f64,
pub yMax: f64,
pub yMin: f64,
}

#[derive(Default, Debug, Copy, Clone)]
#[repr(C)]
pub struct Color {
Expand All @@ -15,6 +24,29 @@ pub struct Color {
pub r: u8,
}

#[derive(Default, Debug)]
#[repr(C)]
pub struct Fonts {
pub subtitleFamily: RocStr,
pub titleFamily: RocStr,
pub subtitleSize: u32,
pub titleSize: u32,
}

#[derive(Default, Debug)]
#[repr(C)]
pub struct Labels {
pub xCount: usize,
pub yCount: usize,
}

#[derive(Default, Debug)]
#[repr(C)]
pub struct Layout {
pub chartMargin: u32,
pub labelArea: u32,
}

#[derive(Default, Debug)]
#[repr(C)]
pub struct Line {
Expand All @@ -26,11 +58,15 @@ pub struct Line {
#[derive(Default, Debug)]
#[repr(C)]
pub struct Config {
pub bounds: Bounds,
pub fonts: Fonts,
pub labels: Labels,
pub lines: RocList<Line>,
pub output_file_path: RocStr,
pub subtitle: RocStr,
pub title: RocStr,
pub height: u32,
pub layout: Layout,
pub width: u32,
}

Expand Down
46 changes: 33 additions & 13 deletions platform/src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,30 @@ fn construct_area<Backend: DrawingBackend>(
) -> Result<DrawingArea<Backend, Shift>, DrawingAreaErrorKind<Backend::ErrorType>> {
let area = backend.into_drawing_area();
area.fill(&WHITE)?;
let area = area.titled(config.title.as_str(), ("sans-serif", 60))?;
let area = area.titled(
config.title.as_str(),
(config.fonts.titleFamily.as_str(), config.fonts.titleSize),
)?;
Ok(area)
}

fn construct_builder<'a, Backend: DrawingBackend>(
area: &'a DrawingArea<Backend, Shift>,
config: &'a Config,
) -> ChartBuilder<'a, 'a, Backend> {
let subtitle = config.subtitle.as_str();
let mut builder = ChartBuilder::on(area);
builder.margin(config.layout.chartMargin)
.set_all_label_area_size(config.layout.labelArea);
if subtitle.len() > 0 {
builder.caption(
config.subtitle.as_str(),
(config.fonts.subtitleFamily.as_str(), config.fonts.subtitleSize),
);
}
return builder;
}

fn construct_bitmap_backend(config: &Config) -> BitMapBackend {
BitMapBackend::new(
config.output_file_path.as_str(),
Expand All @@ -42,26 +62,26 @@ fn plot_with<Backend: DrawingBackend>(
config: &Config,
) -> Result<(), DrawingAreaErrorKind<Backend::ErrorType>> {
let area = construct_area(backend, config)?;
let mut cc = ChartBuilder::on(&area)
.margin(5)
.set_all_label_area_size(50)
.caption(config.subtitle.as_str(), ("sans-serif", 40))
.build_cartesian_2d(-3.2f64..3.2f64, -2.1f64..2.1f64)?;
cc.configure_mesh()
.x_labels(20)
.y_labels(10)
let mut builder = construct_builder(&area, config);
let mut context = builder.build_cartesian_2d(
config.bounds.xMin..config.bounds.xMax,
config.bounds.yMin..config.bounds.yMax,
)?;
context.configure_mesh()
.x_labels(config.labels.xCount)
.y_labels(config.labels.yCount)
.disable_mesh()
.x_label_formatter(&|v| format!("{:.1}", v))
.y_label_formatter(&|v| format!("{:.1}", v))
.x_label_formatter(&|v| format!("{:?}", v)) // TODO: Format labels in Roc.
.y_label_formatter(&|v| format!("{:?}", v)) // TODO: Format labels in Roc.
.draw()?;
for line in config.lines.iter() {
let v: Vec<(f64, f64)> = line.points.iter().map(|point| (point.x, point.y)).collect();
let color = RGBColor(line.color.r, line.color.g, line.color.b);
cc.draw_series(LineSeries::new(v, color))?
context.draw_series(LineSeries::new(v, color))?
.label(line.name.as_str())
.legend(move |(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], color));
}
cc.configure_series_labels().border_style(&BLACK).draw()?;
context.configure_series_labels().border_style(&BLACK).draw()?;
area.present().unwrap_or_else(|_| {
panic!(
"I failed to draw your plot to {} !",
Expand Down

0 comments on commit be747b2

Please sign in to comment.