Expand description

The one-dimensional coordinate system abstraction.

Plotters build complex coordinate system with a combinator pattern and all the coordinate system is built from the one dimensional coordinate system. This module defines the fundamental types used by the one-dimensional coordinate system.

The key trait for a one dimensional coordinate is Ranged. This trait describes a set of values which served as the 1D coordinate system in Plotters. In order to extend the coordinate system, the new coordinate spec must implement this trait.

The following example demonstrate how to make a customized coordinate specification

use plotters::coord::ranged1d::{Ranged, DefaultFormatting, KeyPointHint};
use std::ops::Range;

struct ZeroToOne;

impl Ranged for ZeroToOne {
  type ValueType = f64;
  type FormatOption = DefaultFormatting;

  fn map(&self, &v: &f64, pixel_range: (i32, i32)) -> i32 {
     let size = pixel_range.1 - pixel_range.0;
     let v = v.min(1.0).max(0.0);
     ((size as f64) * v).round() as i32
  }

  fn key_points<Hint:KeyPointHint>(&self, hint: Hint) -> Vec<f64> {
      if hint.max_num_points() < 3 {
          vec![]
      } else {
          vec![0.0, 0.5, 1.0]
      }
  }

  fn range(&self) -> Range<f64> {
      0.0..1.0
  }
}

use plotters::prelude::*;

let mut buffer = vec![0; 1024 * 768 * 3];
let root = BitMapBackend::with_buffer(&mut buffer, (1024, 768)).into_drawing_area();

let chart = ChartBuilder::on(&root)
  .build_cartesian_2d(ZeroToOne, ZeroToOne)
  .unwrap();

Structs

The key point hint indicates we only need key point for the bold grid lines

This makes the ranged coord uses the default Debug based formatting

The key point hint indicates that we are using the key points for the light grid lines

This markers prevent Plotters to implement the default Debug based formatting

A SegmentedCoord is a decorator on any discrete coordinate specification. This decorator will convert the discrete coordiante in two ways:

Enums

Specify the weight of key points.

The value that used by the segmented coordinate.

Traits

The trait for the type that can be converted into a ranged coordinate axis

Since stable Rust doesn’t have specialization, it’s very hard to make our own trait that automatically implemented the value formatter. This trait uses as a marker indicates if we should automatically implement the default value formater based on it’s Debug trait

The trait indicates the coordinate is discrete This means we can bidirectionally map the range value to 0 to N in which N is the number of distinct values of the range.

The trait for types that can decorated by SegmentedCoord decorator.

The trait for a hint provided to the key point algorithm used by the coordinate specs. The most important constraint is the max_num_points which means the algorithm could emit no more than specific number of key points weight is used to determine if this is used as a bold grid line or light grid line bold_points returns the max number of coresponding bold grid lines

The trait that indicates we have a ordered and ranged value Which is used to describe any 1D axis.

The trait indicates the ranged value can be map reversely, which means an pixel-based coordinate is given, it’s possible to figure out the underlying logic value.

Determine how we can format a value in a coordinate system by default