I have a program that graphs data onto an iced canvas. To clean up the canvas draw function I moved the code into a series of helper functions. In several of these functions I deal with displaying canvas text and to do so I apply values to all of the text structure elements. To further clean up the code, I would like to be able to create a style (I think that's the correct term) for the canvas text so that I can call out the content, location, and size elements then defer to a style for the balance. Below are 2 code snippets that I hope will be helpful. Any assistance will be appreciated.
impl<Message> canvas::Program<Message> for LineGraph {
type State = ();
fn draw(
&self,
_state: &(),
renderer: &Renderer,
_theme: &Theme,
bounds: iced::Rectangle,
_cursor: iced::advanced::mouse::Cursor,
) -> Vec<canvas::Geometry<Renderer>> {
let mut frame = canvas::Frame::new(renderer, bounds.size());
draw_heading(&mut frame);
draw_axis(&mut frame);
draw_grid(&mut frame);
draw_x_axis_ticks(&mut frame);
draw_x_axis_labels(&mut frame, &self.x_values);
draw_y_axis_ticks(&mut frame);
draw_y_axis_labels(&mut frame, &self.y_values);
draw_data_points(&mut frame, &self.points);
vec![frame.into_geometry()]
} // end of function draw
}
pub fn draw_x_axis_labels(frame: &mut Frame, x_labels: &Vec<String>) {
for (index, label) in x_labels.iter().enumerate() {
frame.fill_text(Text {
content: label.to_string(),
position: Point { x: 100.0 + (index as f32 * 120.0), y: 718.0 },
max_width: 60.0,
color: Color::from_rgb(0.0, 0.0, 0.0),
size: iced::Pixels(12.0),
line_height: iced::advanced::text::LineHeight::Relative(1.2),
font: iced::Font::default(),
align_x: iced::advanced::text::Alignment::Center,
align_y: iced::alignment::Vertical::Center,
shaping: iced::advanced::text::Shaping::Auto,
});
}
}