|
| 1 | +/*! |
| 2 | + The one-dimensional coordinate system abstraction. |
| 3 | +
|
| 4 | + Plotters build complex coordinate system with a combinator pattern and all the coordinate system is |
| 5 | + built from the one dimensional coordinate system. This module defines the fundamental types used by |
| 6 | + the one-dimensional coordinate system. |
| 7 | +
|
| 8 | + The key trait for a one dimensional coordinate is [Ranged](trait.Ranged.html). This trait describes a |
| 9 | + set of values which served as the 1D coordinate system in Plotters. In order to extend the coordinate system, |
| 10 | + the new coordinate spec must implement this trait. |
| 11 | +
|
| 12 | + The following example demonstrate how to make a customized coordinate specification |
| 13 | + ``` |
| 14 | +use plotters::coord::ranged1d::{Ranged, DefaultFormatting, KeyPointHint}; |
| 15 | +use std::ops::Range; |
| 16 | +
|
| 17 | +struct ZeroToOne; |
| 18 | +
|
| 19 | +impl Ranged for ZeroToOne { |
| 20 | + type ValueType = f64; |
| 21 | + type FormatOption = DefaultFormatting; |
| 22 | +
|
| 23 | + fn map(&self, &v: &f64, pixel_range: (i32, i32)) -> i32 { |
| 24 | + let size = pixel_range.1 - pixel_range.0; |
| 25 | + let v = v.min(1.0).max(0.0); |
| 26 | + ((size as f64) * v).round() as i32 |
| 27 | + } |
| 28 | +
|
| 29 | + fn key_points<Hint:KeyPointHint>(&self, hint: Hint) -> Vec<f64> { |
| 30 | + if hint.max_num_points() < 3 { |
| 31 | + vec![] |
| 32 | + } else { |
| 33 | + vec![0.0, 0.5, 1.0] |
| 34 | + } |
| 35 | + } |
| 36 | +
|
| 37 | + fn range(&self) -> Range<f64> { |
| 38 | + 0.0..1.0 |
| 39 | + } |
| 40 | +} |
| 41 | +
|
| 42 | +use plotters::prelude::*; |
| 43 | +
|
| 44 | +let mut buffer = vec![0; 1024 * 768 * 3]; |
| 45 | +let root = BitMapBackend::with_buffer(&mut buffer, (1024, 768)).into_drawing_area(); |
| 46 | +
|
| 47 | +let chart = ChartBuilder::on(&root) |
| 48 | + .build_cartesian_2d(ZeroToOne, ZeroToOne) |
| 49 | + .unwrap(); |
| 50 | +
|
| 51 | + ``` |
| 52 | +*/ |
1 | 53 | use std::fmt::Debug;
|
2 | 54 | use std::ops::Range;
|
3 | 55 |
|
|
0 commit comments