mirror of
https://gitlab.com/feliix42/embedded-plots.git
synced 2024-11-22 09:56:31 +00:00
Rename axis internals and add XY axis to example
This commit is contained in:
parent
3dd3d856b7
commit
098f89d908
2 changed files with 28 additions and 23 deletions
|
@ -12,7 +12,7 @@ use embedded_graphics_simulator::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use embedded_plots::{
|
use embedded_plots::{
|
||||||
axis::{Axis,Orientation,Scale},
|
axis::{Axis, Placement, Scale},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() -> Result<(), core::convert::Infallible> {
|
fn main() -> Result<(), core::convert::Infallible> {
|
||||||
|
@ -26,28 +26,33 @@ fn main() -> Result<(), core::convert::Infallible> {
|
||||||
.text_color(RgbColor::YELLOW)
|
.text_color(RgbColor::YELLOW)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
Axis::new("X Fixed 0-100(10)",Orientation::X{x1: 10, x2: 230, y: 10},0..100,Scale::Fixed(10),RgbColor::WHITE, text_style_white, 2)
|
Axis::new("X Fixed 0-100(10)", Placement::X{x1: 40, x2: 230, y: 10}, 0..100, Scale::Fixed(10), RgbColor::WHITE, text_style_white, 2)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("X Fixed 0-200(100)",Orientation::X{x1: 240, x2: 470, y: 10},0..200,Scale::Fixed(100),RgbColor::YELLOW, text_style_yellow_compact, 1)
|
Axis::new("X Fixed 0-200(100)", Placement::X{x1: 240, x2: 470, y: 10}, 0..200, Scale::Fixed(100), RgbColor::YELLOW, text_style_yellow_compact, 1)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("X Frac 0-100(7)",Orientation::X{x1: 20, x2: 220, y: 30},0..100,Scale::RangeFraction(7),RgbColor::BLUE, text_style_white, 3)
|
Axis::new("X Frac 0-100(7)", Placement::X{x1: 50, x2: 220, y: 30}, 0..100, Scale::RangeFraction(7), RgbColor::BLUE, text_style_white, 3)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("X Frac 0-200(4)",Orientation::X{x1: 250, x2: 460, y: 30},0..200,Scale::RangeFraction(4),RgbColor::RED, text_style_yellow_compact, 7)
|
Axis::new("X Frac 0-200(4)", Placement::X{x1: 250, x2: 460, y: 40}, 0..200, Scale::RangeFraction(4), RgbColor::RED, text_style_yellow_compact, 7)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("Y Fixed 0-100(10)",Orientation::Y{y1: 70, y2: 250, x: 120},0..100,Scale::Fixed(10),RgbColor::WHITE, text_style_white, 2)
|
Axis::new("Y Fixed 0-100(10)", Placement::Y{y1: 70, y2: 230, x: 160}, 0..100, Scale::Fixed(10), RgbColor::WHITE, text_style_white, 2)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("Y Fixed 0-200(100)",Orientation::Y{y1: 70, y2: 230, x: 230},0..200,Scale::Fixed(100),RgbColor::YELLOW, text_style_yellow_compact, 1)
|
Axis::new("Y Fixed 0-200(100)", Placement::Y{y1: 70, y2: 210, x: 260}, 0..200, Scale::Fixed(100), RgbColor::YELLOW, text_style_yellow_compact, 1)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("Y Frac 0-100(7)",Orientation::Y{y1: 60, y2: 180, x: 350},0..100,Scale::RangeFraction(7),RgbColor::BLUE, text_style_white, 3)
|
Axis::new("Y Frac 0-100(7)", Placement::Y{y1: 60, y2: 180, x: 370}, 0..100, Scale::RangeFraction(7), RgbColor::BLUE, text_style_white, 3)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("Y Frac 0-200(4)",Orientation::Y{y1: 90, y2: 260, x: 470},0..200,Scale::RangeFraction(4),RgbColor::RED, text_style_yellow_compact, 7)
|
Axis::new("Y Frac 0-200(4)", Placement::Y{y1: 90, y2: 220, x: 470}, 0..200, Scale::RangeFraction(4), RgbColor::RED, text_style_yellow_compact, 7)
|
||||||
|
.draw(&mut display)?;
|
||||||
|
|
||||||
|
Axis::new("X", Placement::X{x1: 30, x2: 470, y: 250}, 123..2137, Scale::Fixed(150), RgbColor::YELLOW, text_style_white, 2)
|
||||||
|
.draw(&mut display)?;
|
||||||
|
Axis::new("Y", Placement::Y{y1: 10, y2: 250, x: 30}, 0..2137, Scale::RangeFraction(15), RgbColor::WHITE, text_style_white, 2)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
|
|
||||||
|
|
28
src/axis.rs
28
src/axis.rs
|
@ -9,7 +9,7 @@ use embedded_graphics::fonts::Text;
|
||||||
use heapless::{consts::*, String};
|
use heapless::{consts::*, String};
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
|
|
||||||
pub enum Orientation {
|
pub enum Placement {
|
||||||
X {
|
X {
|
||||||
x1: i32,
|
x1: i32,
|
||||||
x2: i32,
|
x2: i32,
|
||||||
|
@ -34,7 +34,7 @@ pub struct Axis<'a, C, F>
|
||||||
TextStyle<C,F>: Clone,
|
TextStyle<C,F>: Clone,
|
||||||
{
|
{
|
||||||
title: &'a str,
|
title: &'a str,
|
||||||
orientation: Orientation,
|
placement: Placement,
|
||||||
range: Range<i32>,
|
range: Range<i32>,
|
||||||
scale: Scale,
|
scale: Scale,
|
||||||
color: C,
|
color: C,
|
||||||
|
@ -48,8 +48,8 @@ impl<'a, C, F> Axis<'a, C, F>
|
||||||
F: Font,
|
F: Font,
|
||||||
TextStyle<C,F>: Clone,
|
TextStyle<C,F>: Clone,
|
||||||
{
|
{
|
||||||
pub fn new(title: &'a str, orientation: Orientation, range: Range<i32>, scale: Scale, color: C, text_style: TextStyle<C,F>, tick_height: usize) -> Axis<'a, C, F> {
|
pub fn new(title: &'a str, orientation: Placement, range: Range<i32>, scale: Scale, color: C, text_style: TextStyle<C,F>, tick_height: usize) -> Axis<'a, C, F> {
|
||||||
Axis { title, orientation, range, scale, color, text_style, tick_size: tick_height }
|
Axis { title, placement: orientation, range, scale, color, text_style, tick_size: tick_height }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ impl<'a, C, F> Drawable<C> for Axis<'a, C, F>
|
||||||
TextStyle<C,F>: Clone,
|
TextStyle<C,F>: Clone,
|
||||||
{
|
{
|
||||||
fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> {
|
fn draw<D: DrawTarget<C>>(self, display: &mut D) -> Result<(), D::Error> {
|
||||||
let lines = match self.scale {
|
let scale_marks = match self.scale {
|
||||||
Scale::Fixed(interval) => {
|
Scale::Fixed(interval) => {
|
||||||
self.range.clone().into_iter().step_by(interval)
|
self.range.clone().into_iter().step_by(interval)
|
||||||
}
|
}
|
||||||
|
@ -70,8 +70,8 @@ impl<'a, C, F> Drawable<C> for Axis<'a, C, F>
|
||||||
self.range.clone().into_iter().step_by(len / fraction)
|
self.range.clone().into_iter().step_by(len / fraction)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
match self.orientation {
|
match self.placement {
|
||||||
Orientation::X { x1, x2, y } => {
|
Placement::X { x1, x2, y } => {
|
||||||
Line { start: Point { x: x1, y }, end: Point { x: x2, y } }
|
Line { start: Point { x: x1, y }, end: Point { x: x2, y } }
|
||||||
.into_styled(PrimitiveStyle::with_stroke(self.color, 1))
|
.into_styled(PrimitiveStyle::with_stroke(self.color, 1))
|
||||||
.draw(display)?;
|
.draw(display)?;
|
||||||
|
@ -80,29 +80,29 @@ impl<'a, C, F> Drawable<C> for Axis<'a, C, F>
|
||||||
let title = title.translate(Point { x: (x2 - x1) / 2 - title.size().width as i32 / 2, y: 0 });
|
let title = title.translate(Point { x: (x2 - x1) / 2 - title.size().width as i32 / 2, y: 0 });
|
||||||
title.draw(display)?;
|
title.draw(display)?;
|
||||||
|
|
||||||
for line in lines {
|
for mark in scale_marks {
|
||||||
let x = line.scale_between_ranges(&self.range, &(x1..x2));
|
let x = mark.scale_between_ranges(&self.range, &(x1..x2));
|
||||||
Line { start: Point { x, y: y - self.tick_size as i32 }, end: Point { x, y: y + self.tick_size as i32 } }
|
Line { start: Point { x, y: y - self.tick_size as i32 }, end: Point { x, y: y + self.tick_size as i32 } }
|
||||||
.into_styled(PrimitiveStyle::with_stroke(self.color, 1))
|
.into_styled(PrimitiveStyle::with_stroke(self.color, 1))
|
||||||
.draw(display)?;
|
.draw(display)?;
|
||||||
let mut buf: String::<U8> = String::new();
|
let mut buf: String::<U8> = String::new();
|
||||||
write!(buf, "{}", line).unwrap();
|
write!(buf, "{}", mark).unwrap();
|
||||||
Text::new(&buf, Point { x: x + 2, y: y + 2 }).into_styled(self.text_style).draw(display)?;
|
Text::new(&buf, Point { x: x + 2, y: y + 2 }).into_styled(self.text_style).draw(display)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Orientation::Y { y1, y2, x } => {
|
Placement::Y { y1, y2, x } => {
|
||||||
Line { start: Point { x, y: y1 }, end: Point { x, y: y2 } }
|
Line { start: Point { x, y: y1 }, end: Point { x, y: y2 } }
|
||||||
.into_styled(PrimitiveStyle::with_stroke(self.color, 1))
|
.into_styled(PrimitiveStyle::with_stroke(self.color, 1))
|
||||||
.draw(display)?;
|
.draw(display)?;
|
||||||
|
|
||||||
let mut max_tick_text_width = 0;
|
let mut max_tick_text_width = 0;
|
||||||
for line in lines {
|
for mark in scale_marks {
|
||||||
let y = line.scale_between_ranges(&self.range, &(y2..y1));
|
let y = mark.scale_between_ranges(&self.range, &(y2..y1));
|
||||||
Line { start: Point { x: x - self.tick_size as i32, y }, end: Point { x: x + self.tick_size as i32, y} }
|
Line { start: Point { x: x - self.tick_size as i32, y }, end: Point { x: x + self.tick_size as i32, y} }
|
||||||
.into_styled(PrimitiveStyle::with_stroke(self.color, 1))
|
.into_styled(PrimitiveStyle::with_stroke(self.color, 1))
|
||||||
.draw(display)?;
|
.draw(display)?;
|
||||||
let mut buf: String::<U8> = String::new();
|
let mut buf: String::<U8> = String::new();
|
||||||
write!(buf, "{}", line).unwrap();
|
write!(buf, "{}", mark).unwrap();
|
||||||
let tick_val = Text::new(&buf, Point { x, y}).into_styled(self.text_style);
|
let tick_val = Text::new(&buf, Point { x, y}).into_styled(self.text_style);
|
||||||
let tick_val = tick_val.translate(Point{ x: -(tick_val.size().width as i32) -2, y: 2 });
|
let tick_val = tick_val.translate(Point{ x: -(tick_val.size().width as i32) -2, y: 2 });
|
||||||
if tick_val.size().width > max_tick_text_width { max_tick_text_width = tick_val.size().width }
|
if tick_val.size().width > max_tick_text_width { max_tick_text_width = tick_val.size().width }
|
||||||
|
|
Loading…
Reference in a new issue