about summary refs log tree commit diff
path: root/src/griph/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/griph/mod.rs')
-rw-r--r--src/griph/mod.rs32
1 files changed, 24 insertions, 8 deletions
diff --git a/src/griph/mod.rs b/src/griph/mod.rs
index 636ffd9..377ad90 100644
--- a/src/griph/mod.rs
+++ b/src/griph/mod.rs
@@ -35,7 +35,7 @@ pub enum Style {
 pub fn make_1line(min: usize, max: usize, values: &[Option<usize>], style: Style) -> Gif {
 	let range = max - min;
 	// this assumes a range of values that is >1 per pixel
-	let vpp = range / HEIGHT;
+	let vpp = range as f32 / HEIGHT as f32;
 
 	let mut raster = vec![0; SIZE];
 	let mut standard = Gif::new(WIDTH as u16, HEIGHT as u16);
@@ -64,20 +64,36 @@ pub fn make_1line(min: usize, max: usize, values: &[Option<usize>], style: Style
 	standard
 }
 
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum TwoLineOrder {
+	SeriesAFirst,
+	SeriesBFirst,
+}
+
 pub fn make_2line(
 	min: usize,
 	max: usize,
 	values1: &[Option<usize>],
 	values2: &[Option<usize>],
+	draw_order: TwoLineOrder,
 ) -> Gif {
 	let range = max - min;
 	// this assumes a range of values that is >1 per pixel
-	let vpp = range / HEIGHT;
+	let vpp = range as f32 / HEIGHT as f32;
 
 	let mut raster = vec![0; SIZE];
 	draw_grid(&mut raster);
-	draw_line_underfill(&mut raster, values1, vpp, LINE1, LINE1_FILL);
-	draw_line_underfill(&mut raster, values2, vpp, LINE2, LINE2_FILL);
+
+	match draw_order {
+		TwoLineOrder::SeriesAFirst => {
+			draw_line_underfill(&mut raster, values1, vpp, LINE1, LINE1_FILL);
+			draw_line_underfill(&mut raster, values2, vpp, LINE2, LINE2_FILL);
+		}
+		TwoLineOrder::SeriesBFirst => {
+			draw_line_underfill(&mut raster, values2, vpp, LINE2, LINE2_FILL);
+			draw_line_underfill(&mut raster, values1, vpp, LINE1, LINE1_FILL);
+		}
+	}
 
 	let mut standard = Gif::new(WIDTH as u16, HEIGHT as u16);
 	standard.set_palette(Some(DARK_PALETTE.try_into().unwrap()));
@@ -109,14 +125,14 @@ fn draw_grid(raster: &mut [u8]) {
 	}
 }
 
-fn draw_line(raster: &mut [u8], values: &[Option<usize>], vpp: usize, colour: u8) {
+fn draw_line(raster: &mut [u8], values: &[Option<usize>], vpp: f32, colour: u8) {
 	// Draw Line
 	// this will be discontinuous and i think that's okay. we
 	// could make it a proper line by keeping track what value
 	// was last and drawing the whole vertical there
 	for (x, maybe) in values.iter().enumerate() {
 		if let Some(value) = maybe {
-			let value_height = value / vpp;
+			let value_height = (*value as f32 / vpp) as usize;
 			if value_height > (HEIGHT - 1) {
 				continue;
 			}
@@ -130,13 +146,13 @@ fn draw_line(raster: &mut [u8], values: &[Option<usize>], vpp: usize, colour: u8
 fn draw_line_underfill(
 	raster: &mut [u8],
 	values: &[Option<usize>],
-	vpp: usize,
+	vpp: f32,
 	colour: u8,
 	colour_fill: u8,
 ) {
 	for (x, maybe) in values.iter().enumerate() {
 		if let Some(value) = maybe {
-			let value_height = value / vpp;
+			let value_height = (*value as f32 / vpp) as usize;
 			if value_height > (HEIGHT - 1) {
 				continue;
 			}