Skip to content
Open
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
44 changes: 24 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,38 +111,42 @@ pub enum Shape<'a> {
/// Points connected with lines.
Lines(&'a [(f32, f32)]),
/// Points connected in step fashion.
///
/// Note: the final point will not be drawn, only its x-coordinate determines how far the last bar extends.
Steps(&'a [(f32, f32)]),
/// Points represented with bars.
///
/// Note: the final point will not be drawn, only its x-coordinate determines how far the last bar extends.
Bars(&'a [(f32, f32)]),
}

/// Provides an interface for drawing plots.
pub trait Plot<'a> {
/// Draws a [line chart](https://en.wikipedia.org/wiki/Line_chart) of points connected by straight line segments.
fn lineplot(&'a mut self, shape: &'a Shape) -> &'a mut Chart;
fn lineplot(&mut self, shape: &'a Shape) -> &mut Chart<'a>;
}

/// Provides an interface for drawing colored plots.
pub trait ColorPlot<'a> {
/// Draws a [line chart](https://en.wikipedia.org/wiki/Line_chart) of points connected by straight line segments using the specified color
fn linecolorplot(&'a mut self, shape: &'a Shape, color: RGB8) -> &'a mut Chart;
fn linecolorplot(&mut self, shape: &'a Shape, color: RGB8) -> &mut Chart<'a>;
}

/// Provides a builder interface for styling axis.
pub trait AxisBuilder<'a> {
/// Specifies the style of x-axis.
fn x_axis_style(&'a mut self, style: LineStyle) -> &'a mut Chart<'a>;
fn x_axis_style(&mut self, style: LineStyle) -> &mut Chart<'a>;

/// Specifies the style of y-axis.
fn y_axis_style(&'a mut self, style: LineStyle) -> &'a mut Chart<'a>;
fn y_axis_style(&mut self, style: LineStyle) -> &mut Chart<'a>;
}

pub trait LabelBuilder<'a> {
/// Specifies the label format of x-axis.
fn x_label_format(&'a mut self, format: LabelFormat) -> &'a mut Chart<'a>;
fn x_label_format(&mut self, format: LabelFormat) -> &mut Chart<'a>;

/// Specifies the label format of y-axis.
fn y_label_format(&'a mut self, format: LabelFormat) -> &'a mut Chart<'a>;
fn y_label_format(&mut self, format: LabelFormat) -> &mut Chart<'a>;
}

/// Provides an interface for adding tick labels to the y-axis
Expand All @@ -151,7 +155,7 @@ pub trait TickDisplayBuilder<'a> {
/// Specifies the tick label density of y-axis.
/// TickDisplay::Sparse will change the canvas height to the nearest multiple of 16
/// TickDisplay::Dense will change the canvas height to the nearest multiple of 8
fn y_tick_display(&'a mut self, density: TickDisplay) -> &'a mut Chart<'a>;
fn y_tick_display(&mut self, density: TickDisplay) -> &mut Chart<'a>;
}

impl<'a> Default for Chart<'a> {
Expand Down Expand Up @@ -471,7 +475,7 @@ impl<'a> Chart<'a> {
.filter_map(|i| {
let x = x_scale.inv_linear(i as f32);
let y = f(x);
if y.is_normal() {
if y.is_normal() || y == 0.0 {
let j = y_scale.linear(y).round();
Some((i, self.height - j as u32))
} else {
Expand Down Expand Up @@ -524,11 +528,11 @@ impl<'a> Chart<'a> {

if let Some(color) = color {
let color = rgb_to_pixelcolor(color);
self.canvas.line_colored(x1, y2, x2, y2, color);
self.canvas.line_colored(x1, y1, x1, y2, color);
self.canvas.line_colored(x2, y1, x2, y2, color);
self.canvas.line_colored(x1, y1, x2, y1, color);
} else {
self.canvas.line(x1, y2, x2, y2);
self.canvas.line(x1, y1, x1, y2);
self.canvas.line(x2, y1, x2, y2);
self.canvas.line(x1, y1, x2, y1);
}
}
}
Expand All @@ -539,13 +543,13 @@ impl<'a> Chart<'a> {

if let Some(color) = color {
let color = rgb_to_pixelcolor(color);
self.canvas.line_colored(x1, y2, x2, y2, color);
self.canvas.line_colored(x1, y1, x1, y2, color);
self.canvas.line_colored(x1, y1, x2, y1, color);
self.canvas.line_colored(x2, y1, x2, y2, color);
self.canvas.line_colored(x1, self.height, x1, y1, color);
self.canvas.line_colored(x2, self.height, x2, y2, color);
} else {
self.canvas.line(x1, y2, x2, y2);
self.canvas.line(x1, y1, x1, y2);
self.canvas.line(x1, y1, x2, y1);
self.canvas.line(x2, y1, x2, y2);
self.canvas.line(x1, self.height, x1, y1);
self.canvas.line(x2, self.height, x2, y2);
}
Expand Down Expand Up @@ -603,7 +607,7 @@ impl<'a> Chart<'a> {
}

impl<'a> ColorPlot<'a> for Chart<'a> {
fn linecolorplot(&'a mut self, shape: &'a Shape, color: RGB8) -> &'a mut Chart {
fn linecolorplot(&mut self, shape: &'a Shape, color: RGB8) -> &mut Chart<'a> {
self.shapes.push((shape, Some(color)));
if self.y_ranging == ChartRangeMethod::AutoRange {
self.rescale(shape);
Expand All @@ -613,7 +617,7 @@ impl<'a> ColorPlot<'a> for Chart<'a> {
}

impl<'a> Plot<'a> for Chart<'a> {
fn lineplot(&'a mut self, shape: &'a Shape) -> &'a mut Chart {
fn lineplot(&mut self, shape: &'a Shape) -> &mut Chart<'a> {
self.shapes.push((shape, None));
if self.y_ranging == ChartRangeMethod::AutoRange {
self.rescale(shape);
Expand All @@ -631,12 +635,12 @@ fn rgb_to_pixelcolor(rgb: &RGB8) -> PixelColor {
}

impl<'a> AxisBuilder<'a> for Chart<'a> {
fn x_axis_style(&'a mut self, style: LineStyle) -> &'a mut Chart {
fn x_axis_style(&mut self, style: LineStyle) -> &mut Chart<'a> {
self.x_style = style;
self
}

fn y_axis_style(&'a mut self, style: LineStyle) -> &'a mut Chart {
fn y_axis_style(&mut self, style: LineStyle) -> &mut Chart<'a> {
self.y_style = style;
self
}
Expand Down