mirror of
https://gitlab.com/feliix42/embedded-plots.git
synced 2024-11-25 10:46:31 +00:00
Move text style building out of Axis
This commit is contained in:
parent
975cf9b38a
commit
7790a4fa4d
2 changed files with 36 additions and 24 deletions
|
@ -13,37 +13,43 @@ use embedded_graphics_simulator::{
|
||||||
use embedded_plots::{
|
use embedded_plots::{
|
||||||
axis::{Axis,Orientation,Scale},
|
axis::{Axis,Orientation,Scale},
|
||||||
};
|
};
|
||||||
|
use embedded_graphics::style::TextStyleBuilder;
|
||||||
|
use embedded_graphics::fonts::Font6x8;
|
||||||
|
|
||||||
fn main() -> Result<(), core::convert::Infallible> {
|
fn main() -> Result<(), core::convert::Infallible> {
|
||||||
let mut display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(480, 272));
|
let mut display: SimulatorDisplay<Rgb565> = SimulatorDisplay::new(Size::new(480, 272));
|
||||||
|
|
||||||
Axis::new("X Fixed 0-100(10)",Orientation::X{x1: 10, x2: 230, y: 10},0..100,Scale::Fixed(10),RgbColor::WHITE)
|
let text_style = TextStyleBuilder::new(Font6x8)
|
||||||
|
.text_color(RgbColor::WHITE)
|
||||||
|
.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)
|
||||||
.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)
|
Axis::new("X Fixed 0-200(100)",Orientation::X{x1: 240, x2: 470, y: 10},0..200,Scale::Fixed(100),RgbColor::YELLOW, text_style)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("X Fixed 0-100(7)",Orientation::X{x1: 20, x2: 220, y: 30},0..100,Scale::RangeFraction(7),RgbColor::BLUE)
|
Axis::new("X Frac 0-100(7)",Orientation::X{x1: 20, x2: 220, y: 30},0..100,Scale::RangeFraction(7),RgbColor::BLUE, text_style)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("X Fixed 0-200(4)",Orientation::X{x1: 250, x2: 460, y: 30},0..200,Scale::RangeFraction(4),RgbColor::RED)
|
Axis::new("X Frac 0-200(4)",Orientation::X{x1: 250, x2: 460, y: 30},0..200,Scale::RangeFraction(4),RgbColor::RED, text_style)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("Y Fixed 0-100(10)",Orientation::Y{y1: 60, y2: 250, x: 90},0..100,Scale::Fixed(10),RgbColor::WHITE)
|
Axis::new("Y Fixed 0-100(10)",Orientation::Y{y1: 60, y2: 250, x: 110},0..100,Scale::Fixed(10),RgbColor::WHITE, text_style)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("Y Fixed 0-200(100)",Orientation::Y{y1: 70, y2: 230, x: 210},0..200,Scale::Fixed(100),RgbColor::YELLOW)
|
Axis::new("Y Fixed 0-200(100)",Orientation::Y{y1: 70, y2: 230, x: 230},0..200,Scale::Fixed(100),RgbColor::YELLOW, text_style)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("Y Fixed 0-100(7)",Orientation::Y{y1: 60, y2: 180, x: 330},0..100,Scale::RangeFraction(7),RgbColor::BLUE)
|
Axis::new("Y Frac 0-100(7)",Orientation::Y{y1: 60, y2: 180, x: 350},0..100,Scale::RangeFraction(7),RgbColor::BLUE, text_style)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
Axis::new("Y Fixed 0-200(4)",Orientation::Y{y1: 90, y2: 260, x: 450},0..200,Scale::RangeFraction(4),RgbColor::RED)
|
Axis::new("Y Frac 0-200(4)",Orientation::Y{y1: 90, y2: 260, x: 470},0..200,Scale::RangeFraction(4),RgbColor::RED, text_style)
|
||||||
.draw(&mut display)?;
|
.draw(&mut display)?;
|
||||||
|
|
||||||
|
|
||||||
let output_settings = OutputSettingsBuilder::new()
|
let output_settings = OutputSettingsBuilder::new()
|
||||||
// .pixel_spacing(1)
|
.pixel_spacing(1)
|
||||||
.build();
|
.build();
|
||||||
Window::new("Basic plot", &output_settings).show_static(&display);
|
Window::new("Basic plot", &output_settings).show_static(&display);
|
||||||
|
|
||||||
|
|
36
src/axis.rs
36
src/axis.rs
|
@ -3,9 +3,9 @@ use embedded_graphics::drawable::Drawable;
|
||||||
use embedded_graphics::DrawTarget;
|
use embedded_graphics::DrawTarget;
|
||||||
use core::ops::Range;
|
use core::ops::Range;
|
||||||
use embedded_graphics::prelude::*;
|
use embedded_graphics::prelude::*;
|
||||||
use embedded_graphics::style::{PrimitiveStyle, TextStyleBuilder};
|
use embedded_graphics::style::{PrimitiveStyle, TextStyle};
|
||||||
use crate::range_conv::Scalable;
|
use crate::range_conv::Scalable;
|
||||||
use embedded_graphics::fonts::{Text, Font6x8};
|
use embedded_graphics::fonts::Text;
|
||||||
use heapless::{consts::*, String};
|
use heapless::{consts::*, String};
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
|
|
||||||
|
@ -27,27 +27,37 @@ pub enum Scale {
|
||||||
RangeFraction(usize),
|
RangeFraction(usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Axis<'a, C> {
|
pub struct Axis<'a, C, F>
|
||||||
|
where
|
||||||
|
C: PixelColor,
|
||||||
|
F: Font,
|
||||||
|
TextStyle<C,F>: Clone,
|
||||||
|
{
|
||||||
title: &'a str,
|
title: &'a str,
|
||||||
orientation: Orientation,
|
orientation: Orientation,
|
||||||
range: Range<i32>,
|
range: Range<i32>,
|
||||||
scale: Scale,
|
scale: Scale,
|
||||||
color: C,
|
color: C,
|
||||||
|
text_style: TextStyle<C,F>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, C> Axis<'a, C>
|
impl<'a, C, F> Axis<'a, C, F>
|
||||||
where
|
where
|
||||||
C: PixelColor,
|
C: PixelColor,
|
||||||
|
F: Font,
|
||||||
|
TextStyle<C,F>: Clone,
|
||||||
{
|
{
|
||||||
pub fn new(title: &'a str, orientation: Orientation, range: Range<i32>, scale: Scale, color: C) -> Axis<'a, C> {
|
pub fn new(title: &'a str, orientation: Orientation, range: Range<i32>, scale: Scale, color: C, text_style: TextStyle<C,F>) -> Axis<'a, C, F> {
|
||||||
Axis { title, orientation, range, scale, color }
|
Axis { title, orientation, range, scale, color, text_style }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl<'a, C> Drawable<C> for Axis<'a, C>
|
impl<'a, C, F> Drawable<C> for Axis<'a, C, F>
|
||||||
where
|
where
|
||||||
C: PixelColor,
|
C: PixelColor,
|
||||||
|
F: Font + Copy,
|
||||||
|
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 lines = match self.scale {
|
||||||
|
@ -59,17 +69,13 @@ impl<'a, C> Drawable<C> for Axis<'a, C>
|
||||||
self.range.clone().into_iter().step_by(len / fraction)
|
self.range.clone().into_iter().step_by(len / fraction)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Create a new text style
|
|
||||||
let style = TextStyleBuilder::new(Font6x8)
|
|
||||||
.text_color(self.color)
|
|
||||||
.build();
|
|
||||||
match self.orientation {
|
match self.orientation {
|
||||||
Orientation::X { x1, x2, y } => {
|
Orientation::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)?;
|
||||||
let title = Text::new(self.title, Point { x: x1, y: y + 10 })
|
let title = Text::new(self.title, Point { x: x1, y: y + 10 })
|
||||||
.into_styled(style);
|
.into_styled(self.text_style);
|
||||||
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)?;
|
||||||
|
|
||||||
|
@ -80,7 +86,7 @@ impl<'a, C> Drawable<C> for Axis<'a, C>
|
||||||
.draw(display)?;
|
.draw(display)?;
|
||||||
let mut buf: String::<U8> = String::new();
|
let mut buf: String::<U8> = String::new();
|
||||||
write!(buf, "{}", line).unwrap();
|
write!(buf, "{}", line).unwrap();
|
||||||
Text::new(&buf, Point { x: x + 1, y: y + 1 }).into_styled(style).draw(display)?;
|
Text::new(&buf, Point { x: x + 1, y: y + 1 }).into_styled(self.text_style).draw(display)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Orientation::Y { y1, y2, x } => {
|
Orientation::Y { y1, y2, x } => {
|
||||||
|
@ -88,7 +94,7 @@ impl<'a, C> Drawable<C> for Axis<'a, C>
|
||||||
.into_styled(PrimitiveStyle::with_stroke(self.color, 1))
|
.into_styled(PrimitiveStyle::with_stroke(self.color, 1))
|
||||||
.draw(display)?;
|
.draw(display)?;
|
||||||
let title = Text::new(self.title, Point { x, y: y1 })
|
let title = Text::new(self.title, Point { x, y: y1 })
|
||||||
.into_styled(style);
|
.into_styled(self.text_style);
|
||||||
let title = title.translate(Point { x: -(title.size().width as i32) - 5, y: (y2-y1)/2 });
|
let title = title.translate(Point { x: -(title.size().width as i32) - 5, y: (y2-y1)/2 });
|
||||||
title.draw(display)?;
|
title.draw(display)?;
|
||||||
|
|
||||||
|
@ -99,7 +105,7 @@ impl<'a, C> Drawable<C> for Axis<'a, C>
|
||||||
.draw(display)?;
|
.draw(display)?;
|
||||||
let mut buf: String::<U8> = String::new();
|
let mut buf: String::<U8> = String::new();
|
||||||
write!(buf, "{}", line).unwrap();
|
write!(buf, "{}", line).unwrap();
|
||||||
let tick = Text::new(&buf, Point { x, y}).into_styled(style);
|
let tick = Text::new(&buf, Point { x, y}).into_styled(self.text_style);
|
||||||
let tick = tick.translate(Point{ x: -(tick.size().width as i32), y: 0 });
|
let tick = tick.translate(Point{ x: -(tick.size().width as i32), y: 0 });
|
||||||
tick.draw(display)?;
|
tick.draw(display)?;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue