Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/gauss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ fn main() {
let title: des::figure::Title =
format!("Normal distribution (\u{03bc}={}, \u{03c3}={})", MU, SIGMA).into();

let ticks = vec![5.0, 9.0, 11.0, 13.0, 15.0, 17.0, 21.0];

let x_axis = des::Axis::new()
.with_title("x".into())
.with_ticks(Default::default());
.with_ticks(des::axis::Ticks::new().with_locator(ticks.into()));
let y_axis = des::Axis::new().with_title("y".into()).with_ticks(
des::axis::Ticks::new()
.with_formatter(Some(des::axis::ticks::PercentFormatter::default().into())),
Expand Down
24 changes: 24 additions & 0 deletions src/des/axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ pub mod ticks {
/// on the axis data range (bounds) and whether the ticks are major or minor
#[default]
Auto,
/// Specifies the exact locations of the ticks in data space.
List(ListLocator),
/// Places ticks automatically, using the specified number of bins and steps
MaxN(MaxNLocator),
/// Places the ticks automatically, using the specified number of bins and multiples of PI.
Expand All @@ -403,6 +405,28 @@ pub mod ticks {
TimeDelta(TimeDeltaLocator),
}

/// A locator that places ticks at the specified locations in data space
#[derive(Debug, Clone)]
pub struct ListLocator(pub Vec<f64>);

impl From<Vec<f64>> for Locator {
fn from(loc: Vec<f64>) -> Self {
Locator::List(ListLocator(loc))
}
}

impl From<Vec<f64>> for ListLocator {
fn from(loc: Vec<f64>) -> Self {
ListLocator(loc)
}
}

impl From<ListLocator> for Locator {
fn from(loc: ListLocator) -> Self {
Locator::List(loc)
}
}

/// A locator that places ticks automatically, using the specified number of bins and steps
#[derive(Debug, Clone)]
pub struct MaxNLocator {
Expand Down
3 changes: 2 additions & 1 deletion src/drawing/ticks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn locate_num(
(Locator::Auto, Scale::Log(LogScale { base, .. })) => {
Ok(LogLocator::new_major(*base).ticks(nb))
}
(Locator::List(locator), _) => Ok(locator.0.clone()),
(Locator::MaxN(locator), Scale::Auto | Scale::Linear { .. }) => {
let ticker = MaxN::new(locator.bins, locator.steps.as_slice());
Ok(ticker.ticks(nb))
Expand Down Expand Up @@ -530,7 +531,7 @@ fn auto_label_formatter(
(Locator::Auto, Scale::Log(LogScale { base, .. })) if *base == 10.0 => {
Arc::new(SciLabelFormat)
}
(Locator::Auto, _) => {
(Locator::Auto, _) | (Locator::List(..), _) => {
let max = ab.start().abs().max(ab.end().abs());
if max >= 100000.0 || max < 0.001 {
Arc::new(SciLabelFormat)
Expand Down
Loading