Struct ChartContext

pub struct ChartContext<'a, DB, CT>{ /* private fields */ }
Expand description

The context of the chart. This is the core object of Plotters.

Any plot/chart is abstracted as this type, and any data series can be placed to the chart context.

See crate::series::LineSeries and ChartContext::configure_series_labels() for more information and examples

Implementations§

§

impl<'a, DB, XT, YT, X, Y> ChartContext<'a, DB, Cartesian2d<X, Y>>
where DB: DrawingBackend, X: Ranged<ValueType = XT> + ValueFormatter<XT>, Y: Ranged<ValueType = YT> + ValueFormatter<YT>,

pub fn configure_mesh(&mut self) -> MeshStyle<'a, '_, X, Y, DB>

Initialize a mesh configuration object and mesh drawing can be finalized by calling the function MeshStyle::draw.

§

impl<'a, DB, X, Y> ChartContext<'a, DB, Cartesian2d<X, Y>>
where DB: DrawingBackend, X: Ranged, Y: Ranged,

pub fn x_range(&self) -> Range<<X as Ranged>::ValueType>

Get the range of X axis

pub fn y_range(&self) -> Range<<Y as Ranged>::ValueType>

Get range of the Y axis

pub fn backend_coord( &self, coord: &(<X as Ranged>::ValueType, <Y as Ranged>::ValueType), ) -> (i32, i32)

Maps the coordinate to the backend coordinate. This is typically used with an interactive chart.

§

impl<'a, DB, X, Y> ChartContext<'a, DB, Cartesian2d<X, Y>>
where DB: DrawingBackend, X: Ranged, Y: Ranged,

pub fn set_secondary_coord<SX, SY>( self, x_coord: SX, y_coord: SY, ) -> DualCoordChartContext<'a, DB, Cartesian2d<X, Y>, Cartesian2d<<SX as AsRangedCoord>::CoordDescType, <SY as AsRangedCoord>::CoordDescType>>

Convert this chart context into a dual axis chart context and attach a second coordinate spec on the chart context. For more detailed information, see documentation for struct DualCoordChartContext

  • x_coord: The coordinate spec for the X axis
  • y_coord: The coordinate spec for the Y axis
  • returns The newly created dual spec chart context
§

impl<'a, DB, X, Y, Z, XT, YT, ZT> ChartContext<'a, DB, Cartesian3d<X, Y, Z>>
where DB: DrawingBackend, X: Ranged<ValueType = XT> + ValueFormatter<XT>, Y: Ranged<ValueType = YT> + ValueFormatter<YT>, Z: Ranged<ValueType = ZT> + ValueFormatter<ZT>,

pub fn configure_axes(&mut self) -> Axes3dStyle<'a, '_, X, Y, Z, DB>

Create an axis configuration object, to set line styles, labels, sizes, etc.

Default values for axis configuration are set by function Axes3dStyle::new().

§Example
use plotters::prelude::*;
let drawing_area = SVGBackend::new("configure_axes.svg", (300, 200)).into_drawing_area();
drawing_area.fill(&WHITE).unwrap();
let mut chart_builder = ChartBuilder::on(&drawing_area);
let mut chart_context = chart_builder.margin_bottom(30).build_cartesian_3d(0.0..4.0, 0.0..3.0, 0.0..2.7).unwrap();
chart_context.configure_axes().tick_size(8).x_labels(4).y_labels(3).z_labels(2)
    .max_light_lines(5).axis_panel_style(GREEN.mix(0.1)).bold_grid_style(BLUE.mix(0.3))
    .light_grid_style(BLUE.mix(0.2)).label_style(("Calibri", 10))
    .x_formatter(&|x| format!("x={x}")).draw().unwrap();

The resulting chart reflects the customizations specified through configure_axes():

All these customizations are Axes3dStyle methods.

In the chart, tick_size(8) produces tick marks 8 pixels long. You can use (5u32).percent().max(5).in_pixels(chart.plotting_area() to tell Plotters to calculate the tick mark size as a percentage of the dimensions of the figure. See crate::style::RelativeSize and crate::style::SizeDesc for more information.

x_labels(4) specifies a maximum of 4 tick marks and labels in the X axis. max_light_lines(5) specifies a maximum of 5 minor grid lines between any two tick marks. axis_panel_style(GREEN.mix(0.1)) specifies the style of the panels in the background, a light green color. bold_grid_style(BLUE.mix(0.3)) and light_grid_style(BLUE.mix(0.2)) specify the style of the major and minor grid lines, respectively. label_style() specifies the text style of the axis labels, and x_formatter(|x| format!("x={x}")) specifies the string format of the X axis labels.

§See also

ChartContext::configure_mesh(), a similar function for 2D plots

§

impl<'a, DB, X, Y, Z> ChartContext<'a, DB, Cartesian3d<X, Y, Z>>
where X: Ranged, Y: Ranged, Z: Ranged, DB: DrawingBackend,

pub fn with_projection<P>( &mut self, pf: P, ) -> &mut ChartContext<'a, DB, Cartesian3d<X, Y, Z>>

Override the 3D projection matrix. This function allows to override the default projection matrix.

  • pf: A function that takes the default projection matrix configuration and returns the projection matrix. This function will allow you to adjust the pitch, yaw angle and the centeral point of the projection, etc. You can also build a projection matrix which is not relies on the default configuration as well.

pub fn set_3d_pixel_range( &mut self, size: (i32, i32, i32), ) -> &mut ChartContext<'a, DB, Cartesian3d<X, Y, Z>>

Sets the 3d coordinate pixel range.

§

impl<'a, DB, CT> ChartContext<'a, DB, CT>

pub fn into_coord_trans(self) -> impl Fn((i32, i32))

Convert the chart context into an closure that can be used for coordinate translation

§

impl<'a, DB, CT> ChartContext<'a, DB, CT>

pub fn configure_series_labels<'b>( &'b mut self, ) -> SeriesLabelStyle<'a, 'b, DB, CT>
where DB: 'a,

Configure the styles for drawing series labels in the chart

§Example
use plotters::prelude::*;
let data = [(1.0, 3.3), (2., 2.1), (3., 1.5), (4., 1.9), (5., 1.0)];
let drawing_area = SVGBackend::new("configure_series_labels.svg", (300, 200)).into_drawing_area();
drawing_area.fill(&WHITE).unwrap();
let mut chart_builder = ChartBuilder::on(&drawing_area);
chart_builder.margin(7).set_left_and_bottom_label_area_size(20);
let mut chart_context = chart_builder.build_cartesian_2d(0.0..5.5, 0.0..5.5).unwrap();
chart_context.configure_mesh().draw().unwrap();
chart_context.draw_series(LineSeries::new(data, BLACK)).unwrap().label("Series 1")
    .legend(|(x,y)| Rectangle::new([(x - 15, y + 1), (x, y)], BLACK));
chart_context.configure_series_labels().position(SeriesLabelPosition::UpperRight).margin(20)
    .legend_area_size(5).border_style(BLUE).background_style(BLUE.mix(0.1)).label_font(("Calibri", 20)).draw().unwrap();

The result is a chart with one data series labeled “Series 1” in a blue legend box:

§See also

See crate::series::LineSeries for more information and examples

pub fn plotting_area(&self) -> &DrawingArea<DB, CT>

Get a reference of underlying plotting area

pub fn as_coord_spec(&self) -> &CT

Cast the reference to a chart context to a reference to underlying coordinate specification.

pub fn draw_series<B, E, R, S>( &mut self, series: S, ) -> Result<&mut SeriesAnno<'a, DB>, DrawingAreaErrorKind<<DB as DrawingBackend>::ErrorType>>
where B: CoordMapper, &'b E: for<'b> PointCollection<'b, <CT as CoordTranslate>::From, B>, E: Drawable<DB, B>, R: Borrow<E>, S: IntoIterator<Item = R>,

Draws a data series. A data series in Plotters is abstracted as an iterator of elements.

See crate::series::LineSeries and ChartContext::configure_series_labels() for more information and examples.

§

impl<'a, DB, CT> ChartContext<'a, DB, CT>

pub fn into_chart_state(self) -> ChartState<CT>

Convert a chart context into a chart state, by doing so, the chart context is consumed and a saved chart state is created for later use. This is typically used in incremental rendering. See documentation of ChartState for more detailed example.

pub fn into_shared_chart_state(self) -> ChartState<Arc<CT>>

Convert the chart context into a sharable chart state. Normally a chart state can not be clone, since the coordinate spec may not be able to be cloned. In this case, we can use an Arc get the coordinate wrapped thus the state can be cloned and shared by multiple chart context

§

impl<'a, DB, CT> ChartContext<'a, DB, CT>

pub fn to_chart_state(&self) -> ChartState<CT>

Make the chart context, do not consume the chart context and clone the coordinate spec

Trait Implementations§

§

impl<'a, DB, CT1, CT2> Borrow<ChartContext<'a, DB, CT1>> for DualCoordChartContext<'a, DB, CT1, CT2>

§

fn borrow(&self) -> &ChartContext<'a, DB, CT1>

Immutably borrows from an owned value. Read more
§

impl<'a, DB, CT1, CT2> BorrowMut<ChartContext<'a, DB, CT1>> for DualCoordChartContext<'a, DB, CT1, CT2>

§

fn borrow_mut(&mut self) -> &mut ChartContext<'a, DB, CT1>

Mutably borrows from an owned value. Read more
§

impl<'a, DB, CT> From<&ChartContext<'a, DB, CT>> for ChartState<CT>

§

fn from(chart: &ChartContext<'a, DB, CT>) -> ChartState<CT>

Converts to this type from the input type.
§

impl<'a, DB, CT> From<ChartContext<'a, DB, CT>> for ChartState<CT>

§

fn from(chart: ChartContext<'a, DB, CT>) -> ChartState<CT>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, DB, CT> Freeze for ChartContext<'a, DB, CT>
where CT: Freeze,

§

impl<'a, DB, CT> !RefUnwindSafe for ChartContext<'a, DB, CT>

§

impl<'a, DB, CT> !Send for ChartContext<'a, DB, CT>

§

impl<'a, DB, CT> !Sync for ChartContext<'a, DB, CT>

§

impl<'a, DB, CT> Unpin for ChartContext<'a, DB, CT>
where CT: Unpin,

§

impl<'a, DB, CT> !UnwindSafe for ChartContext<'a, DB, CT>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more