Trait Chart

Source
pub trait Chart<Message> {
    type State: Default + 'static;

    // Required method
    fn build_chart<DB: DrawingBackend>(
        &self,
        state: &Self::State,
        builder: ChartBuilder<'_, '_, DB>,
    );

    // Provided methods
    fn draw_chart<DB: DrawingBackend>(
        &self,
        state: &Self::State,
        root: DrawingArea<DB, Shift>,
    ) { ... }
    fn draw<R: Renderer, F: Fn(&mut Frame)>(
        &self,
        renderer: &R,
        size: Size,
        f: F,
    ) -> Geometry { ... }
    fn update(
        &self,
        state: &mut Self::State,
        event: Event,
        bounds: Rectangle,
        cursor: Cursor,
    ) -> (Status, Option<Message>) { ... }
    fn mouse_interaction(
        &self,
        state: &Self::State,
        bounds: Rectangle,
        cursor: Cursor,
    ) -> Interaction { ... }
}
Expand description

Chart View Model

§Example

use plotters::prelude::*;
use plotters_iced::{Chart,ChartWidget};
struct MyChart;
impl Chart<Message> for MyChart {
    type State = ();
    fn build_chart<DB:DrawingBackend>(&self, state: &Self::State, builder: ChartBuilder<DB>) {
        //build your chart here, please refer to plotters for more details
    }
}

impl MyChart {
    fn view(&mut self)->Element<Message> {
        ChartWidget::new(self)
            .width(Length::Fixed(200))
            .height(Length::Fixed(200))
            .into()
    }
}

Required Associated Types§

Source

type State: Default + 'static

state data of chart

Required Methods§

Source

fn build_chart<DB: DrawingBackend>( &self, state: &Self::State, builder: ChartBuilder<'_, '_, DB>, )

draw chart with ChartBuilder

for simple chart, you impl this method

Provided Methods§

Source

fn draw_chart<DB: DrawingBackend>( &self, state: &Self::State, root: DrawingArea<DB, Shift>, )

override this method if you want more freedom of drawing area

§Example
use plotters::prelude::*;
use plotters_iced::{Chart,ChartWidget};

struct MyChart{}

impl Chart<Message> for MyChart {
    // leave it empty
    fn build_chart<DB: DrawingBackend>(&self, state: &Self::State, builder: ChartBuilder<DB>){}
    fn draw_chart<DB: DrawingBackend>(&self, state: &Self::State, root: DrawingArea<DB, Shift>){
         let children = root.split_evenly((3,3));
         for (area, color) in children.into_iter().zip(0..) {
               area.fill(&Palette99::pick(color)).unwrap();
         }
     }
}
Source

fn draw<R: Renderer, F: Fn(&mut Frame)>( &self, renderer: &R, size: Size, f: F, ) -> Geometry

draw on iced_widget::canvas::Canvas

override this method if you want to use iced_widget::canvas::Cache

§Example

impl Chart<Message> for CpuUsageChart {

      #[inline]
      fn draw<R: Renderer, F: Fn(&mut Frame)>(&self, renderer: &R, bounds: Size, draw_fn: F) -> Geometry {
           R::draw_cache(renderer, &self.cache, size, draw_fn)
      }
     //...
}
Source

fn update( &self, state: &mut Self::State, event: Event, bounds: Rectangle, cursor: Cursor, ) -> (Status, Option<Message>)

react on event

Source

fn mouse_interaction( &self, state: &Self::State, bounds: Rectangle, cursor: Cursor, ) -> Interaction

Returns the current mouse interaction of the Chart

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<Message, C> Chart<Message> for &C
where C: Chart<Message> + ?Sized,

Source§

type State = <C as Chart<Message>>::State

Source§

fn build_chart<DB: DrawingBackend>( &self, state: &Self::State, builder: ChartBuilder<'_, '_, DB>, )

Source§

fn draw_chart<DB: DrawingBackend>( &self, state: &Self::State, root: DrawingArea<DB, Shift>, )

Source§

fn draw<R: Renderer, F: Fn(&mut Frame)>( &self, renderer: &R, size: Size, f: F, ) -> Geometry

Source§

fn update( &self, state: &mut Self::State, event: Event, bounds: Rectangle, cursor: Cursor, ) -> (Status, Option<Message>)

Source§

fn mouse_interaction( &self, state: &Self::State, bounds: Rectangle, cursor: Cursor, ) -> Interaction

Implementors§