diff --git a/.gitignore b/.gitignore index c6695f5e..4f2e1070 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,9 @@ -/target -.*.sw* +# According to the new guideline from the cargo team, we should ignore the lock file for libraries. +**/Cargo.lock + +# Build diectories **/target -.vscode/* -Cargo.lock + +# Editor/IDE temporary files +.*.sw* +.vscode/* \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 86186ab4..051bd409 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,297 +1,284 @@ # Changelog -## Plotters 0.3.4 (?) - -## Plotters 0.3.3 (2022-08-19) - -### Improved - -- The EvcxR integration now supports bitmap output. (Thanks to @lisanhu) - -### Fixed - -- Fix the plotters crate's edition requirement +## Plotters latest ## Plotters 0.3.2 (2022-07-05) -### Added - -- Pie element (by @hhamana) +- Added + - Pie element (by @hhamana) -### Improved +- Improved -- Improved documentaiton for predefined colors. (Thanks to @siefkenj) -- Introduce the full Material Design 2014 Color Palette. (Thanks to @siefkenj) -- Massive documentation improvements (Thanks to @facorread and many others) + - Improved documentaiton for predefined colors. (Thanks to @siefkenj) + - Introduce the full Material Design 2014 Color Palette. (Thanks to @siefkenj) + - Massive documentation improvements (Thanks to @facorread and many others) -### Fixed +- Fixed -- More float point axis key point algorithm fixes (@38) -- Default rasterizer fixes (@shinmili and @38) + - More float point axis key point algorithm fixes (@38) + - Default rasterizer fixes (@shinmili and @38) ## Plotters 0.3.1 (2021-05-21) -### Improved +- Improved + - Surface series now supports more customizations + - Z-orde support for 3D plots + - Allow passing colors by value and reference (thanks to chrisduerr) -- Surface series now supports more customizations -- Z-orde support for 3D plots -- Allow passing colors by value and reference (thanks to chrisduerr) +- Fixed -### Fixed - -- Infinte loop / panic when the axis range is zero -- UB in minifb example (thanks to TheEdward162) -- Examples that uses old APIs (thanks to chrisduerr) + - Infinte loop / panic when the axis range is zero + - UB in minifb example (thanks to TheEdward162) + - Examples that uses old APIs (thanks to chrisduerr) ## Plotters 0.3.0 (2020-09-03) This is the next major release of Plotters, see [release notes](./RELEASE-NOTES.md) for more detials. -### Added +- Added -- The plotters backend API crate is introduced which allows third-party backend. -- Allow slice used as a coordinate if the item type implements `PartialOrd`. -- Linspace coordinate type, which allows define a discrete coordinate on continous value types (such as f32, DateTime, etc) with a step specification -- Nested coordinate type, now we support define pair of category and nested values as nested coordinate. -- Introduce backend crates: plotters-bitmap, plotters-svg, plotters-cairo, plotters-canvas, plotters-console -- 3D Plotting features + - The plotters backend API crate is introduced which allows third-party backend. + - Allow slice used as a coordinate if the item type implements `PartialOrd`. + - Linspace coordinate type, which allows define a discrete coordinate on continous value types (such as f32, DateTime, etc) with a step specification + - Nested coordinate type, now we support define pair of category and nested values as nested coordinate. + - Introduce backend crates: plotters-bitmap, plotters-svg, plotters-cairo, plotters-canvas, plotters-console + - 3D Plotting features -### Fixed +- Fixed -- Adjust Canvas backend size with DPR (Thanks to Marius-Mueller) + - Adjust Canvas backend size with DPR (Thanks to Marius-Mueller) -### Improvement +- Improvement -- Enhanced key point algorithms: New key point hint trait are used for key point algorithms && many improvment on key point algorithms for different types -- Renamed `MeshStyle::line\_style\_1` and `MeshStyle::line\_style\_2` to `bold\_line\_style` and `light\_line\_style` -- Reorganize the "plotters::coord" namespace -- Improved discrete coordinate trait -- Split backend code into isolated crates and can be maintained indepdendenly -- Category coordinate is now replaced by slice coordinate -- Removing the requirement for `Debug` trait for chart coordinate, allows ranged coordinate define its own formatting function. + - Enhanced key point algorithms: New key point hint trait are used for key point algorithms && many improvment on key point algorithms for different types + - Renamed `MeshStyle::line\_style\_1` and `MeshStyle::line\_style\_2` to `bold\_line\_style` and `light\_line\_style` + - Reorganize the "plotters::coord" namespace + - Improved discrete coordinate trait + - Split backend code into isolated crates and can be maintained indepdendenly + - Category coordinate is now replaced by slice coordinate + - Removing the requirement for `Debug` trait for chart coordinate, allows ranged coordinate define its own formatting function. -### Removed +- Removed -- The `Path` name alias for `PathElement` -- Most code `plotters::drawing::backend\_impl::\*` (expect `MockedBackend` for testing purpose) is removed due to crate split. -- Piston backend due to the Piston project seems not actively developing + - The `Path` name alias for `PathElement` + - Most code `plotters::drawing::backend\_impl::\*` (expect `MockedBackend` for testing purpose) is removed due to crate split. + - Piston backend due to the Piston project seems not actively developing ## Plotters 0.2.15 (2020-05-26) -### Fixed +- Fixed -- Division by zero with logarithmic coord (issue #143) -- Update dependencies + - Division by zero with logarithmic coord (issue #143) + - Update dependencies ## Plotters 0.2.14 (2020-05-05) -### Fixed +- Fixed -- Compile error with older rustc which causing breaks + - Compile error with older rustc which causing breaks ## Plotters 0.2.13 (2020-05-04) -### Added - -- Line width is supported for SVG -### Fixed +- Added + - Line width is supported for SVG -- Updated dependencies -- Default rasterizer causing bitmap backend draw out-of-range pixels -- Depdendicy fix +- Fixed + - Updated dependencies + - Default rasterizer causing bitmap backend draw out-of-range pixels + - Depdendicy fix ## Plotters 0.2.12 (2019-12-06) -### Added -- BitMapBackend now is able to support different pixel format natively. Check our new minifb demo for details. -- Incremental Rendering by saving the previous chart context into a state and restore it on a different drawing area. -- BoxPlot support (See boxplot example for more details) (Thanks to @nuald) -- Category coordinate spec which allows use a list of given values as coordinate (Thanks to @nuald) -- New text positioning model which allows develvoper sepecify the anchor point. This is critical for layouting SVG correctly. See `plotters::style::text::text_anchor` for details. (Thanks to @nuald) +- Added + - BitMapBackend now is able to support different pixel format natively. Check our new minifb demo for details. + - Incremental Rendering by saving the previous chart context into a state and restore it on a different drawing area. + - BoxPlot support (See boxplot example for more details) (Thanks to @nuald) + - Category coordinate spec which allows use a list of given values as coordinate (Thanks to @nuald) + - New text positioning model which allows develvoper sepecify the anchor point. This is critical for layouting SVG correctly. See `plotters::style::text::text_anchor` for details. (Thanks to @nuald) -### Improved +- Improved -- Faster bitmap blending algorithm, which is 5x faster than the original one. -- Text alignment improvement, now we can suggest the invariant point by giving `TextAlignment` to the text style (Thanks to @nauld) -- More controls on features, allows opt in and out series types -- Remove dependency to svg crate, since it doesn't provide more feature than a plain string. + - Faster bitmap blending algorithm, which is 5x faster than the original one. + - Text alignment improvement, now we can suggest the invariant point by giving `TextAlignment` to the text style (Thanks to @nauld) + - More controls on features, allows opt in and out series types + - Remove dependency to svg crate, since it doesn't provide more feature than a plain string. ## Plotters 0.2.11 (2019-10-27) -### Added +- Added -- Add font style support, now we are able to set font variations: normal, oblique, italic or bold. + - Add font style support, now we are able to set font variations: normal, oblique, italic or bold. -### Improved +- Improved -- Font description is greatly improved, general font family is supported: `serif`, `serif-sans`, `monospace` (Thanks to @Tatrix) -- Tested the font loading on Linux, OSX and Windowns. Make font loading more reliable. -- `BitMapBackend` isn't depdends on `image` crate now. Only the image encoding part relies on the `image` crate -- Refactored WASM demo use ES6 and `wasm-pack` (Thanks to @Tatrix) -- Label style for X axis and Y axis can be set seperately now using `x\_label\_style` and `y\_label\_style`. (Thanks to @zhiburt) + - Font description is greatly improved, general font family is supported: `serif`, `serif-sans`, `monospace` (Thanks to @Tatrix) + - Tested the font loading on Linux, OSX and Windowns. Make font loading more reliable. + - `BitMapBackend` isn't depdends on `image` crate now. Only the image encoding part relies on the `image` crate + - Refactored WASM demo use ES6 and `wasm-pack` (Thanks to @Tatrix) + - Label style for X axis and Y axis can be set seperately now using `x\_label\_style` and `y\_label\_style`. (Thanks to @zhiburt) ## Plotters 0.2.10 (2019-10-23) -### Improved +- Improved -- Refactored and simplified TTF font cache, use RwLock instead of mutex which may benefit for parallel rendering. (Thanks to @Tatrix) + - Refactored and simplified TTF font cache, use RwLock instead of mutex which may benefit for parallel rendering. (Thanks to @Tatrix) -### Bug Fix +- Bug Fix -- The fast bitmap filling algorithm may overflow the framebuffer and cause segfault + - The fast bitmap filling algorithm may overflow the framebuffer and cause segfault ## Plotters 0.2.9 (2019-10-21) -### Improvement +- Improvement -- Avoid copying image buffer when manipulate the image. (Thanks to @ralfbiedert) -- Bitmap element which allows blit the image to another drawing area. -- Performance improvement: now the bitmap backend is 8 times faster -- Added benchmarks to monitor the performance change + - Avoid copying image buffer when manipulate the image. (Thanks to @ralfbiedert) + - Bitmap element which allows blit the image to another drawing area. + - Performance improvement: now the bitmap backend is 8 times faster + - Added benchmarks to monitor the performance change -### Bug Fix +- Bug Fix -- Performance fix: '?' operator is very slow -- Dynamic Element lifetime bound: Fix a bug that prevents area series draws on non-static lifetime backend + - Performance fix: '?' operator is very slow + - Dynamic Element lifetime bound: Fix a bug that prevents area series draws on non-static lifetime backend ## Plotters 0.2.8 (2019-10-12) -### Added +- Added -- Cairo backend, which supports using Plotters draw a GTK surface. -- Allow secondary axis to be configured with different label style. -- Relative Sizing, now font and size can use relative scale: `(10).percent\_height()` means we want the size is 10% of parent's height. -- Allow the axis overlapping with the plotting area with `ChartBuilder::set\_\label\_area\_overlap`. -- Allow label area is on the top of the drawing area by setting the label area size to negative (Thanks to @serzhiio). -- Allow configure the tick mark size, when the tick mark size is negative the axis becomes inward (Thanks to @serzhiio). + - Cairo backend, which supports using Plotters draw a GTK surface. + - Allow secondary axis to be configured with different label style. + - Relative Sizing, now font and size can use relative scale: `(10).percent\_height()` means we want the size is 10% of parent's height. + - Allow the axis overlapping with the plotting area with `ChartBuilder::set\_\label\_area\_overlap`. + - Allow label area is on the top of the drawing area by setting the label area size to negative (Thanks to @serzhiio). + - Allow configure the tick mark size, when the tick mark size is negative the axis becomes inward (Thanks to @serzhiio). -### Bug Fix +- Bug Fix -- `FontError` from rusttype isn't `Sync` and `Send`. We don't have trait bound to ensure this. (Thanks to @dalance) + - `FontError` from rusttype isn't `Sync` and `Send`. We don't have trait bound to ensure this. (Thanks to @dalance) -### Improvement +- Improvement -- New convenient functions: `disable_mesh` and `disable_axes` + - New convenient functions: `disable_mesh` and `disable_axes` ## Plotters 0.2.7 (2019-10-1) -### Added +- Added -- GIF Support, now bitmap backend is able to render gif animation + - GIF Support, now bitmap backend is able to render gif animation -### Bug Fix +- Bug Fix -- Fixed several polygon filling bugs. -- Completely DateTime coordinate system support + - Fixed several polygon filling bugs. + - Completely DateTime coordinate system support ## Plotters 0.2.6 (2019-09-19) -### Added +- Added -- Allowing axis be placed on top or right by setting `right_y_label_area` and `top_x_label_area` -- Dual-coord system chart support: Now we are able to attach a secondary coord system to the chart using `ChartContext::set_secondary_coord`. And `draw_secondary_axes` to configure the style of secondary axes. Use `draw_secondary axis` to draw series under the secondary coordinate system. -- Add support for `y_label_offset`. Previously only X axis label supports offset attribute. -- New label area size API `set_label_area_size` can be used for all 4 label area -- Added new error bar element -- New axis specification type `PartialAxis` which allows the partially rendered axis. For example, we can define the chart's axis range as `0..1`, but only `0.3..0.7` is rendered on axis. This can be done by `(0.0..1.0).partial_axis(0.3..0.7)` -- Drawing backend now support fill polygon and introduce polygon element -- Area Chart Support + - Allowing axis be placed on top or right by setting `right_y_label_area` and `top_x_label_area` + - Dual-coord system chart support: Now we are able to attach a secondary coord system to the chart using `ChartContext::set_secondary_coord`. And `draw_secondary_axes` to configure the style of secondary axes. Use `draw_secondary axis` to draw series under the secondary coordinate system. + - Add support for `y_label_offset`. Previously only X axis label supports offset attribute. + - New label area size API `set_label_area_size` can be used for all 4 label area + - Added new error bar element + - New axis specification type `PartialAxis` which allows the partially rendered axis. For example, we can define the chart's axis range as `0..1`, but only `0.3..0.7` is rendered on axis. This can be done by `(0.0..1.0).partial_axis(0.3..0.7)` + - Drawing backend now support fill polygon and introduce polygon element + - Area Chart Support -### Improvement +- Improvement -- More examples are included -- Date coordinate now support using monthly or yearly axis. This is useful when plotting some data in monthly or yearly basis. -- Make margin on different side of a chart can be configured separately. -- Better test coverage + - More examples are included + - Date coordinate now support using monthly or yearly axis. This is useful when plotting some data in monthly or yearly basis. + - Make margin on different side of a chart can be configured separately. + - Better test coverage ## Plotters 0.2.5 (2019-09-07) -### Bug Fix +- Bug Fix -- Key points algorithm for numeric coordinate might not respect the constraint + - Key points algorithm for numeric coordinate might not respect the constraint ## Plotters 0.2.4 (2019-09-05) -### Improvement +- Improvement -- Add `i128` and `u128` as coordinate type (Credit to @Geemili) + - Add `i128` and `u128` as coordinate type (Credit to @Geemili) -### Bug Fix +- Bug Fix -- Date coordinate is working for a long time span now + - Date coordinate is working for a long time span now ## Plotters 0.2.3 (2019-08-19) -### Improvement +- Improvement -- Color system now is based on `palette` crate (Credit to @Veykril) + - Color system now is based on `palette` crate (Credit to @Veykril) ## Plotters 0.2.2 (2019-06-25) -### Added +- Added -- More documentation: a Jupyter interactive notebook of Plotters tutorial -- Add more quadrants to the `SeriesLabelPosition` (Credit to @wolfjagger). + - More documentation: a Jupyter interactive notebook of Plotters tutorial + - Add more quadrants to the `SeriesLabelPosition` (Credit to @wolfjagger). -### Improvement +- Improvement -- Histogram improvements, horizontal bar is supported, new creation API which compiler can infer the type -- Supporting split the drawing area with a list of breakpoints using `DrawingArea::split_by_breakpoints` -- Enable SVG support for WASM -- Make the `BitMapBackend` takes an in memory mutable buffer + - Histogram improvements, horizontal bar is supported, new creation API which compiler can infer the type + - Supporting split the drawing area with a list of breakpoints using `DrawingArea::split_by_breakpoints` + - Enable SVG support for WASM + - Make the `BitMapBackend` takes an in memory mutable buffer -### Fix +- Fix -- Rectangle drawing bug when the axis is reversed + - Rectangle drawing bug when the axis is reversed ## Plotters 0.2.1 (2019-06-10) -### Improvement +- Improvement -- Move the sample images and other documentation data out of this repository. + - Move the sample images and other documentation data out of this repository. -### Fix -- Make drawing errors shareable across threads. Otherwise, it causes compile error in some cases. (Thanks to @rkarp) +- Fix + - Make drawing errors shareable across threads. Otherwise, it causes compile error in some cases. (Thanks to @rkarp) ## Plotters 0.2.0 (2019-06-08) -### Added -- Add relative sizing by added function `DrawingArea::relative_to_height` and `DrawingArea::relative_to_width`. -- Added piston backend, now we can render plot on a window and dynamically render the plot +- Added + - Add relative sizing by added function `DrawingArea::relative_to_height` and `DrawingArea::relative_to_width`. + - Added piston backend, now we can render plot on a window and dynamically render the plot -### Improved -- Creating drawing area with `&Rc>`. Previously, the drawing area creation requires take over the drawing backend ownership. But sometimes the drawing backend may have additional options. With new API, this can be done by putting the backend drawing area into smart pointers, thus, the drawing backend is accessible after creates the root drawing area. +- Improved + - Creating drawing area with `&Rc>`. Previously, the drawing area creation requires take over the drawing backend ownership. But sometimes the drawing backend may have additional options. With new API, this can be done by putting the backend drawing area into smart pointers, thus, the drawing backend is accessible after creates the root drawing area. ## Plotters 0.1.14 (2019-06-06) -### Added -- Font is now support rotation transformation. Use `FontDesc::transform` to apply an rotation to transformation. For example, `font.transform(FontTransform::Rotate90)`. -- ChartContext now support drawing axis description. Use `MeshStyle::x_desc` and `MeshStyle::y_desc` to specify the axis description text. -- Add series label support. `ChartContext::draw_series` now returns a struct `SeriesAnno` that collects the additional information for series labeling. `ChartContext::draw_series_labels` are used to actually draw the series label. (See `examples/chart.rs` for detailed examples) -- Mocking drawing backend. -- evcxr Support +- Added + - Font is now support rotation transformation. Use `FontDesc::transform` to apply an rotation to transformation. For example, `font.transform(FontTransform::Rotate90)`. + - ChartContext now support drawing axis description. Use `MeshStyle::x_desc` and `MeshStyle::y_desc` to specify the axis description text. + - Add series label support. `ChartContext::draw_series` now returns a struct `SeriesAnno` that collects the additional information for series labeling. `ChartContext::draw_series_labels` are used to actually draw the series label. (See `examples/chart.rs` for detailed examples) + - Mocking drawing backend. + - evcxr Support -### Improvement -- Unify `OwnedText` and `Text` into `Text`. Previously, `OwnedText` and `Text` are two separate types, one holds a `String` another holds a `&str`. Now `OwnedText` is removed. +- Improvement + - Unify `OwnedText` and `Text` into `Text`. Previously, `OwnedText` and `Text` are two separate types, one holds a `String` another holds a `&str`. Now `OwnedText` is removed. use `Text::new("text".to_string(),...)` for owned text element and `Text::new("text", ...)` for borrowed text. -- Refactor the color representation code, since previously it's heavily relies on the trait object and hard to use -- More test cases + - Refactor the color representation code, since previously it's heavily relies on the trait object and hard to use + - More test cases ## Plotters 0.1.13 (2019-05-31) -### Added -- New abstraction of backend style with `BackendStyle` trait which should be able to extend easier in the future -- Backend support features, now feature options can be used to control which backend should be supported -- Add new trait `IntoDrawingArea`, now we can use `backend.into_drawing_area()` to convert the backend into a raw drawing area -- Now elements support dynamic dispatch, use `element.into_dyn()` to convert the element into a runtime dispatching element - -### Improvement -- Improved the overall code quality -- Documentation polish -- Stabilized APIs -- New conversion traits implementations -- Now transparent color is ignored by SVG, bitmap and HTML Canvas backend - -### Fix -- Changed the open-close pattern to a `present` function which indicates the end of drawing one frame -- Fix the but that `ChartBuilder::title` and `ChartBuilder::margin` cannot be called at the same time && `build_ranged` now returning a result. +- Added + - New abstraction of backend style with `BackendStyle` trait which should be able to extend easier in the future + - Backend support features, now feature options can be used to control which backend should be supported + - Add new trait `IntoDrawingArea`, now we can use `backend.into_drawing_area()` to convert the backend into a raw drawing area + - Now elements support dynamic dispatch, use `element.into_dyn()` to convert the element into a runtime dispatching element + +- Improvement + - Improved the overall code quality + - Documentation polish + - Stabilized APIs + - New conversion traits implementations + - Now transparent color is ignored by SVG, bitmap and HTML Canvas backend + +- Fix + - Changed the open-close pattern to a `present` function which indicates the end of drawing one frame + - Fix the but that `ChartBuilder::title` and `ChartBuilder::margin` cannot be called at the same time && `build_ranged` now returning a result. diff --git a/Cargo.toml b/Cargo.toml index cd387118..1c1f359c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,4 @@ [workspace] members = ["plotters", "plotters-backend", "plotters-bitmap", "plotters-svg"] default-members = ["plotters"] + diff --git a/README.md b/README.md index fa451ddd..b2748cdc 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,7 @@ extern crate plotters; use plotters::prelude::*; let figure = evcxr_figure((640, 480), |root| { - root.fill(&WHITE)?; + root.fill(&WHITE); let mut chart = ChartBuilder::on(&root) .caption("y=x^2", ("Arial", 50).into_font()) .margin(5) diff --git a/doc-template/latest_version b/doc-template/latest_version index 1c09c74e..d15723fb 100644 --- a/doc-template/latest_version +++ b/doc-template/latest_version @@ -1 +1 @@ -0.3.3 +0.3.2 diff --git a/plotters-backend/Cargo.toml b/plotters-backend/Cargo.toml index 9a2ba856..9ae8fe17 100644 --- a/plotters-backend/Cargo.toml +++ b/plotters-backend/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "plotters-backend" -version = "0.3.4" +version = "0.4.0" authors = ["Hao Hou "] edition = "2018" license = "MIT" diff --git a/plotters-backend/src/lib.rs b/plotters-backend/src/lib.rs index d4fd904c..410bf136 100644 --- a/plotters-backend/src/lib.rs +++ b/plotters-backend/src/lib.rs @@ -105,7 +105,7 @@ impl Error for DrawingErrorKind {} /// will use the pixel-based approach to draw other types of low-level shapes. pub trait DrawingBackend: Sized { /// The error type reported by the backend - type ErrorType: Error + Send + Sync; + type ErrorType: Error + Send + Sync + 'static; /// Get the dimension of the drawing backend in pixels fn get_size(&self) -> (u32, u32); @@ -236,12 +236,12 @@ pub trait DrawingBackend: Sized { let width = (max_x - min_x) as i32; let height = (max_y - min_y) as i32; let dx = match style.anchor().h_pos { - HPos::Left => 0, + HPos::Left | HPos::Default => 0, HPos::Right => -width, HPos::Center => -width / 2, }; let dy = match style.anchor().v_pos { - VPos::Top => 0, + VPos::Top | VPos::Default => 0, VPos::Center => -height / 2, VPos::Bottom => -height, }; diff --git a/plotters-backend/src/rasterizer/mod.rs b/plotters-backend/src/rasterizer/mod.rs index d322391e..b475acd2 100644 --- a/plotters-backend/src/rasterizer/mod.rs +++ b/plotters-backend/src/rasterizer/mod.rs @@ -10,7 +10,7 @@ // the question mark operator has a huge performance impact due to LLVM unable to handle it. // So the question is if this trick is still useful, or LLVM is smart enough to handle it since // then. -// +// // -- // Original comment: // diff --git a/plotters-backend/src/text.rs b/plotters-backend/src/text.rs index 16e2c665..4acd931b 100644 --- a/plotters-backend/src/text.rs +++ b/plotters-backend/src/text.rs @@ -60,8 +60,12 @@ impl<'a> From<&'a str> for FontFamily<'a> { /// ``` pub mod text_anchor { /// The horizontal position of the anchor point relative to the text. - #[derive(Clone, Copy)] + #[derive(Default, Clone, Copy)] pub enum HPos { + /// Default value (Left), except chart axes that might provide a different pos. + /// Use another variant to override the default positionning + #[default] + Default, /// Anchor point is on the left side of the text Left, /// Anchor point is on the right side of the text @@ -71,8 +75,12 @@ pub mod text_anchor { } /// The vertical position of the anchor point relative to the text. - #[derive(Clone, Copy)] + #[derive(Default, Clone, Copy)] pub enum VPos { + /// Default value (Top), except chart axes that might provide a different pos + /// Use another variant to override the default positionning + #[default] + Default, /// Anchor point is on the top of the text Top, /// Anchor point is in the vertical center of the text @@ -82,7 +90,7 @@ pub mod text_anchor { } /// The text anchor position. - #[derive(Clone, Copy)] + #[derive(Default, Clone, Copy)] pub struct Pos { /// The horizontal position of the anchor point pub h_pos: HPos, @@ -105,22 +113,6 @@ pub mod text_anchor { pub fn new(h_pos: HPos, v_pos: VPos) -> Self { Pos { h_pos, v_pos } } - - /// Create a default text anchor position (top left). - /// - /// - **returns** The default text anchor position - /// - /// ```rust - /// use plotters_backend::text_anchor::{Pos, HPos, VPos}; - /// - /// let pos = Pos::default(); - /// ``` - pub fn default() -> Self { - Pos { - h_pos: HPos::Left, - v_pos: VPos::Top, - } - } } } @@ -135,6 +127,8 @@ pub enum FontTransform { Rotate180, /// Rotating the text 270 degree clockwise Rotate270, + /// Rotating the text to an arbitrary degree clockwise + RotateAngle(f32), } impl FontTransform { @@ -149,6 +143,12 @@ impl FontTransform { FontTransform::Rotate90 => (-y, x), FontTransform::Rotate180 => (-x, -y), FontTransform::Rotate270 => (y, -x), + FontTransform::RotateAngle(angle) => { + let (x, y) = (x as f32, y as f32); + let (sin, cos) = angle.to_radians().sin_cos(); + let (x, y) = (x * cos - y * sin, x * sin + y * cos); + (x.round() as i32, y.round() as i32) + } } } } @@ -243,3 +243,19 @@ pub trait BackendTextStyle { draw: DrawFunc, ) -> Result, Self::FontError>; } + +#[cfg(test)] +mod tests { + use crate::FontTransform; + + #[test] + fn text_rotation_works() { + assert_eq!(FontTransform::None.transform(1, 0), (1, 0)); + assert_eq!(FontTransform::Rotate90.transform(1, 0), (0, 1)); + assert_eq!(FontTransform::Rotate180.transform(1, 0), (-1, 0)); + assert_eq!(FontTransform::Rotate270.transform(1, 0), (0, -1)); + + assert_eq!(FontTransform::RotateAngle(45f32).transform(10, 0), (7, 7)); + assert_eq!(FontTransform::RotateAngle(45f32).transform(0, 10), (-7, 7)); + } +} diff --git a/plotters-bitmap/Cargo.toml b/plotters-bitmap/Cargo.toml index 6a48bc08..0a904526 100644 --- a/plotters-bitmap/Cargo.toml +++ b/plotters-bitmap/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "plotters-bitmap" -version = "0.3.3" +version = "0.4.0" authors = ["Hao Hou "] edition = "2018" license = "MIT" @@ -18,7 +18,7 @@ gif = { version = "0.11.2", optional = true } path = "../plotters-backend" [target.'cfg(not(target_arch = "wasm32"))'.dependencies.image] -version = "0.24.3" +version = "0.24.2" optional = true default-features = false features = ["jpeg", "png", "bmp"] diff --git a/plotters-bitmap/src/bitmap_pixel/bgrx.rs b/plotters-bitmap/src/bitmap_pixel/bgrx.rs index 16bcd25e..2f90d9d5 100644 --- a/plotters-bitmap/src/bitmap_pixel/bgrx.rs +++ b/plotters-bitmap/src/bitmap_pixel/bgrx.rs @@ -87,7 +87,7 @@ impl PixelFormat for BGRXPixel { let slice = unsafe { std::slice::from_raw_parts_mut(start_ptr, (count - 1) / 2) }; for rp in slice.iter_mut() { let ptr = rp as *mut [u8; 8] as *mut u64; - let d1 = unsafe { *ptr }; + let d1 = unsafe { ptr.read_unaligned() }; let mut h = (d1 >> 8) & M; let mut l = d1 & M; @@ -104,7 +104,7 @@ impl PixelFormat for BGRXPixel { } unsafe { - *ptr = h | l; + ptr.write_unaligned(h | l); } } @@ -196,7 +196,7 @@ impl PixelFormat for BGRXPixel { let d: u64 = std::mem::transmute([ b, g, r, 0, b, g, r, 0, // QW1 ]); - *ptr = d; + ptr.write_unaligned(d); } } diff --git a/plotters-bitmap/src/bitmap_pixel/rgb.rs b/plotters-bitmap/src/bitmap_pixel/rgb.rs index e8b88216..39b3f502 100644 --- a/plotters-bitmap/src/bitmap_pixel/rgb.rs +++ b/plotters-bitmap/src/bitmap_pixel/rgb.rs @@ -67,7 +67,7 @@ impl PixelFormat for RGBPixel { // Since we should always make sure the RGB payload occupies the logic lower bits // thus, this type purning should work for both LE and BE CPUs #[rustfmt::skip] - let (p1, p2, p3): (u64, u64, u64) = unsafe { + let [p1, p2, p3]: [u64; 3] = unsafe { std::mem::transmute([ u16::from(r), u16::from(b), u16::from(g), u16::from(r), // QW1 u16::from(b), u16::from(g), u16::from(r), u16::from(b), // QW2 @@ -76,7 +76,7 @@ impl PixelFormat for RGBPixel { }; #[rustfmt::skip] - let (q1, q2, q3): (u64, u64, u64) = unsafe { + let [q1, q2, q3]: [u64; 3] = unsafe { std::mem::transmute([ u16::from(g), u16::from(r), u16::from(b), u16::from(g), // QW1 u16::from(r), u16::from(b), u16::from(g), u16::from(r), // QW2 @@ -94,8 +94,8 @@ impl PixelFormat for RGBPixel { let start_ptr = &mut dst[start * Self::PIXEL_SIZE] as *mut u8 as *mut [u8; 24]; let slice = unsafe { std::slice::from_raw_parts_mut(start_ptr, (count - 1) / 8) }; for p in slice.iter_mut() { - let ptr = p as *mut [u8; 24] as *mut (u64, u64, u64); - let (d1, d2, d3) = unsafe { *ptr }; + let ptr = p as *mut [u8; 24] as *mut [u64; 3]; + let [d1, d2, d3] = unsafe { ptr.read_unaligned() }; let (mut h1, mut h2, mut h3) = ((d1 >> 8) & M, (d2 >> 8) & M, (d3 >> 8) & M); let (mut l1, mut l2, mut l3) = (d1 & M, d2 & M, d3 & M); @@ -120,7 +120,7 @@ impl PixelFormat for RGBPixel { } unsafe { - *ptr = (h1 | l1, h2 | l2, h3 | l3); + ptr.write_unaligned([h1 | l1, h2 | l2, h3 | l3]); } } @@ -207,14 +207,14 @@ impl PixelFormat for RGBPixel { // TODO: Consider using AVX instructions when possible let ptr = p as *mut [u8; 24] as *mut u64; unsafe { - let (d1, d2, d3): (u64, u64, u64) = std::mem::transmute([ + let [d1, d2, d3]: [u64; 3] = std::mem::transmute([ r, g, b, r, g, b, r, g, // QW1 b, r, g, b, r, g, b, r, // QW2 g, b, r, g, b, r, g, b, // QW3 ]); - *ptr = d1; - *ptr.offset(1) = d2; - *ptr.offset(2) = d3; + ptr.write_unaligned(d1); + ptr.offset(1).write_unaligned(d2); + ptr.offset(2).write_unaligned(d3); } } diff --git a/plotters-svg/Cargo.toml b/plotters-svg/Cargo.toml index 30cd37e6..be179057 100644 --- a/plotters-svg/Cargo.toml +++ b/plotters-svg/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "plotters-svg" -version = "0.3.4" +version = "0.4.0" authors = ["Hao Hou "] edition = "2018" license = "MIT" @@ -14,15 +14,8 @@ readme = "README.md" [dependencies.plotters-backend] path = "../plotters-backend" -[dependencies.image] -version = "0.24.2" -optional = true -default-features = false -features = ["jpeg", "png", "bmp"] - [features] debug = [] -bitmap_encoder = ["image"] [dev-dependencies.plotters] default_features = false diff --git a/plotters-svg/src/svg.rs b/plotters-svg/src/svg.rs index 43bf36a2..93f2c942 100644 --- a/plotters-svg/src/svg.rs +++ b/plotters-svg/src/svg.rs @@ -8,12 +8,12 @@ use plotters_backend::{ FontStyle, FontTransform, }; +use std::fmt::Write as _; use std::fs::File; #[allow(unused_imports)] use std::io::Cursor; use std::io::{BufWriter, Error, Write}; use std::path::Path; -use std::fmt::Write as _; fn make_svg_color(color: BackendColor) -> String { let (r, g, b) = color.rgb; @@ -395,13 +395,13 @@ impl<'a> DrawingBackend for SVGBackend<'a> { let (x0, y0) = pos; let text_anchor = match style.anchor().h_pos { - HPos::Left => "start", + HPos::Left | HPos::Default => "start", HPos::Right => "end", HPos::Center => "middle", }; let dy = match style.anchor().v_pos { - VPos::Top => "0.76em", + VPos::Top | VPos::Default => "0.76em", VPos::Center => "0.5ex", VPos::Bottom => "-0.5ex", }; @@ -459,6 +459,9 @@ impl<'a> DrawingBackend for SVGBackend<'a> { FontTransform::Rotate270 => { attrs.push(("transform", format!("rotate(270, {}, {})", x0, y0))); } + FontTransform::RotateAngle(deg) => { + attrs.push(("transform", format!("rotate({deg}, {x0}, {y0})"))) + } _ => {} } @@ -487,19 +490,18 @@ impl<'a> DrawingBackend for SVGBackend<'a> { (w, h): (u32, u32), src: &'b [u8], ) -> Result<(), DrawingErrorKind> { - use image::codecs::png::PngEncoder; - use image::ImageEncoder; + use image::png::PNGEncoder; let mut data = vec![0; 0]; { let cursor = Cursor::new(&mut data); - let encoder = PngEncoder::new(cursor); + let encoder = PNGEncoder::new(cursor); - let color = image::ColorType::Rgb8; + let color = image::ColorType::RGB(8); - encoder.write_image(src, w, h, color).map_err(|e| { + encoder.encode(src, w, h, color).map_err(|e| { DrawingErrorKind::DrawingError(Error::new( std::io::ErrorKind::Other, format!("Image error: {}", e), diff --git a/plotters/Cargo.toml b/plotters/Cargo.toml index 91f9025d..053af3dd 100644 --- a/plotters/Cargo.toml +++ b/plotters/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "plotters" -version = "0.3.4" +version = "0.4.0" authors = ["Hao Hou "] -edition = "2018" +edition = "2021" license = "MIT" description = "A Rust drawing library focus on data plotting for both WASM and native applications" repository = "https://github.com/plotters-rs/plotters" @@ -14,7 +14,7 @@ exclude = ["doc-template", "plotters-doc-data"] [dependencies] num-traits = "0.2.14" -chrono = { version = "0.4.20", optional = true } +chrono = { version = "0.4.31", optional = true } [dependencies.plotters-backend] path = "../plotters-backend" @@ -25,7 +25,6 @@ optional = true path = "../plotters-bitmap" [dependencies.plotters-svg] -version = "^0.3" optional = true path = "../plotters-svg" @@ -36,7 +35,7 @@ pathfinder_geometry = { version = "0.5.1", optional = true } font-kit = { version = "0.11.0", optional = true } [target.'cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))'.dependencies.image] -version = "0.24.3" +version = "0.24.2" optional = true default-features = false features = ["jpeg", "png", "bmp"] @@ -100,7 +99,6 @@ fontconfig-dlopen = ["font-kit/source-fontconfig-dlopen"] # Misc datetime = ["chrono"] evcxr = ["svg_backend"] -evcxr_bitmap = ["evcxr", "bitmap_backend", "plotters-svg/bitmap_encoder"] deprecated_items = [] # Keep some of the deprecated items for backward compatibility [dev-dependencies] @@ -108,8 +106,8 @@ itertools = "0.10.0" criterion = "0.3.6" rayon = "1.5.1" serde_json = "1.0.82" -serde = "1.0.139" -serde_derive = "1.0.140" +serde = "1.0.138" +serde_derive = "1.0.138" [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] rand = "0.8.3" diff --git a/plotters/examples/console.rs b/plotters/examples/console.rs index feba0956..49fc393b 100644 --- a/plotters/examples/console.rs +++ b/plotters/examples/console.rs @@ -132,12 +132,12 @@ impl DrawingBackend for TextDrawingBackend { let (width, height) = self.estimate_text_size(text, style)?; let (width, height) = (width as i32, height as i32); let dx = match style.anchor().h_pos { - HPos::Left => 0, + HPos::Left | HPos::Default => 0, HPos::Right => -width, HPos::Center => -width / 2, }; let dy = match style.anchor().v_pos { - VPos::Top => 0, + VPos::Top | VPos::Default => 0, VPos::Center => -height / 2, VPos::Bottom => -height, }; @@ -151,10 +151,7 @@ impl DrawingBackend for TextDrawingBackend { fn draw_chart( b: DrawingArea, -) -> Result<(), Box> -where - DB::ErrorType: 'static, -{ +) -> Result<(), Box> { let mut chart = ChartBuilder::on(&b) .margin(1) .caption("Sine and Cosine", ("sans-serif", (10).percent_height())) diff --git a/plotters/examples/pie.rs b/plotters/examples/pie.rs index a950c021..7bd6c3ab 100644 --- a/plotters/examples/pie.rs +++ b/plotters/examples/pie.rs @@ -12,7 +12,7 @@ fn main() -> Result<(), Box> { let radius = 300.0; let sizes = vec![66.0, 33.0]; let _rgba = RGBAColor(0, 50, 255, 1.0); - let colors = vec![RGBColor(0, 50, 255), CYAN]; + let colors = vec![RGBColor(0, 50, 255).to_rgba(), CYAN.to_rgba()]; let labels = vec!["Pizza", "Pacman"]; let mut pie = Pie::new(¢er, &radius, &sizes, &colors, &labels); diff --git a/plotters/examples/slc-temp.rs b/plotters/examples/slc-temp.rs index 9d6e4731..5318cb28 100644 --- a/plotters/examples/slc-temp.rs +++ b/plotters/examples/slc-temp.rs @@ -1,9 +1,16 @@ use plotters::prelude::*; -use chrono::{TimeZone, Utc}; +use chrono::NaiveDate; use std::error::Error; +// it's safe to use unwrap because we use known good values in test cases +macro_rules! create_date { + ($year:expr, $month:expr, $day:expr) => { + NaiveDate::from_ymd_opt($year, $month, $day).unwrap() + }; +} + const OUT_FILE_NAME: &'static str = "plotters-doc-data/slc-temp.png"; fn main() -> Result<(), Box> { let root = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area(); @@ -20,11 +27,11 @@ fn main() -> Result<(), Box> { .set_label_area_size(LabelAreaPosition::Right, 60) .set_label_area_size(LabelAreaPosition::Bottom, 40) .build_cartesian_2d( - (Utc.ymd(2010, 1, 1)..Utc.ymd(2018, 12, 1)).monthly(), + (create_date!(2010, 1, 1)..create_date!(2018, 12, 1)).monthly(), 14.0..104.0, )? .set_secondary_coord( - (Utc.ymd(2010, 1, 1)..Utc.ymd(2018, 12, 1)).monthly(), + (create_date!(2010, 1, 1)..create_date!(2018, 12, 1)).monthly(), -10.0..40.0, ); @@ -42,13 +49,13 @@ fn main() -> Result<(), Box> { .draw()?; chart.draw_series(LineSeries::new( - DATA.iter().map(|(y, m, t)| (Utc.ymd(*y, *m, 1), *t)), + DATA.iter().map(|(y, m, t)| (create_date!(*y, *m, 1), *t)), &BLUE, ))?; chart.draw_series( DATA.iter() - .map(|(y, m, t)| Circle::new((Utc.ymd(*y, *m, 1), *t), 3, BLUE.filled())), + .map(|(y, m, t)| Circle::new((create_date!(*y, *m, 1), *t), 3, BLUE.filled())), )?; // To avoid the IO failure being ignored silently, we manually call the present function diff --git a/plotters/examples/stock.rs b/plotters/examples/stock.rs index 8e9416f6..29939a69 100644 --- a/plotters/examples/stock.rs +++ b/plotters/examples/stock.rs @@ -1,11 +1,9 @@ -use chrono::offset::{Local, TimeZone}; -use chrono::{Date, Duration}; +use chrono::{DateTime, Duration, NaiveDate}; use plotters::prelude::*; -fn parse_time(t: &str) -> Date { - Local - .datetime_from_str(&format!("{} 0:0", t), "%Y-%m-%d %H:%M") +fn parse_time(t: &str) -> NaiveDate { + DateTime::parse_from_str(&format!("{} 0:0", t), "%Y-%m-%d %H:%M") .unwrap() - .date() + .date_naive() } const OUT_FILE_NAME: &'static str = "plotters-doc-data/stock.png"; fn main() -> Result<(), Box> { diff --git a/plotters/src/chart/context/cartesian2d/draw_impl.rs b/plotters/src/chart/context/cartesian2d/draw_impl.rs index 6dafa087..24245a7a 100644 --- a/plotters/src/chart/context/cartesian2d/draw_impl.rs +++ b/plotters/src/chart/context/cartesian2d/draw_impl.rs @@ -119,10 +119,7 @@ impl<'a, DB: DrawingBackend, X: Ranged, Y: Ranged> ChartContext<'a, DB, Cartesia y1 = axis_range.end; } - area.draw(&PathElement::new( - vec![(x0, y0), (x1, y1)], - *axis_style, - ))?; + area.draw(&PathElement::new(vec![(x0, y0), (x1, y1)], *axis_style))?; } Ok(axis_range) @@ -250,6 +247,16 @@ impl<'a, DB: DrawingBackend, X: Ranged, Y: Ranged> ChartContext<'a, DB, Cartesia (cx, cy + label_offset) }; + let h_pos = if matches!(label_style.pos.h_pos, HPos::Default) { + h_pos + } else { + label_style.pos.h_pos + }; + let v_pos = if matches!(label_style.pos.v_pos, VPos::Default) { + v_pos + } else { + label_style.pos.v_pos + }; let label_style = &label_style.pos(Pos::new(h_pos, v_pos)); area.draw_text(t, label_style, (text_x, text_y))?; diff --git a/plotters/src/chart/series.rs b/plotters/src/chart/series.rs index 8c430cbe..4aecf6a1 100644 --- a/plotters/src/chart/series.rs +++ b/plotters/src/chart/series.rs @@ -250,9 +250,7 @@ impl<'a, 'b, DB: DrawingBackend + 'a, CT: CoordTranslate> SeriesLabelStyle<'a, ' continue; } - funcs.push( - draw_func.unwrap_or(&|p: BackendCoord| EmptyElement::at(p).into_dyn()), - ); + funcs.push(draw_func.unwrap_or(&|p: BackendCoord| EmptyElement::at(p).into_dyn())); label_element.push_line(label_text); } diff --git a/plotters/src/coord/ranged1d/types/datetime.rs b/plotters/src/coord/ranged1d/types/datetime.rs index 9b12358c..5476bf33 100644 --- a/plotters/src/coord/ranged1d/types/datetime.rs +++ b/plotters/src/coord/ranged1d/types/datetime.rs @@ -1,15 +1,15 @@ /// The datetime coordinates -use chrono::{Date, DateTime, Datelike, Duration, NaiveDate, NaiveDateTime, TimeZone, Timelike}; +use chrono::{Datelike, Duration, NaiveDate, NaiveDateTime, Timelike}; use std::ops::{Add, Range, Sub}; use crate::coord::ranged1d::{ AsRangedCoord, DefaultFormatting, DiscreteRanged, KeyPointHint, NoDefaultFormatting, Ranged, - ValueFormatter, + ReversibleRanged, ValueFormatter, }; /// The trait that describe some time value. This is the uniformed abstraction that works /// for both Date, DateTime and Duration, etc. -pub trait TimeValue: Eq { +pub trait TimeValue: Eq + Sized { type DateType: Datelike + PartialOrd; /// Returns the date that is no later than the time @@ -20,8 +20,10 @@ pub trait TimeValue: Eq { fn earliest_after_date(date: Self::DateType) -> Self; /// Returns the duration between two time value fn subtract(&self, other: &Self) -> Duration; + /// Add duration to time value + fn add(&self, duration: &Duration) -> Self; /// Instantiate a date type for current time value; - fn ymd(&self, year: i32, month: u32, date: u32) -> Self::DateType; + fn ymd_opt(&self, year: i32, month: u32, date: u32) -> Option; /// Cast current date type into this type fn from_date(date: Self::DateType) -> Self; @@ -46,6 +48,31 @@ pub trait TimeValue: Eq { (f64::from(limit.1 - limit.0) * value_days / total_days) as i32 + limit.0 } + + /// Map pixel to coord spec + fn unmap_coord(point: i32, begin: &Self, end: &Self, limit: (i32, i32)) -> Self { + let total_span = end.subtract(begin); + let offset = (point - limit.0) as i64; + + // Check if nanoseconds fit in i64 + if let Some(total_ns) = total_span.num_nanoseconds() { + let pixel_span = (limit.1 - limit.0) as i64; + let factor = total_ns / pixel_span; + let remainder = total_ns % pixel_span; + if factor == 0 + || i64::MAX / factor > offset.abs() + || (remainder == 0 && i64::MAX / factor >= offset.abs()) + { + let nano_seconds = offset * factor + (remainder * offset) / pixel_span; + return begin.add(&Duration::nanoseconds(nano_seconds)); + } + } + + // Otherwise, use days + let total_days = total_span.num_days() as f64; + let days = (((offset as f64) * total_days) / ((limit.1 - limit.0) as f64)) as i64; + begin.add(&Duration::days(days)) + } } impl TimeValue for NaiveDate { @@ -62,33 +89,12 @@ impl TimeValue for NaiveDate { fn subtract(&self, other: &NaiveDate) -> Duration { *self - *other } - - fn ymd(&self, year: i32, month: u32, date: u32) -> Self::DateType { - NaiveDate::from_ymd(year, month, date) - } - - fn from_date(date: Self::DateType) -> Self { - date - } -} - -impl TimeValue for Date { - type DateType = Date; - fn date_floor(&self) -> Date { - self.clone() - } - fn date_ceil(&self) -> Date { - self.clone() - } - fn earliest_after_date(date: Date) -> Self { - date - } - fn subtract(&self, other: &Date) -> Duration { - self.clone() - other.clone() + fn add(&self, other: &Duration) -> NaiveDate { + *self + *other } - fn ymd(&self, year: i32, month: u32, date: u32) -> Self::DateType { - self.timezone().ymd(year, month, date) + fn ymd_opt(&self, year: i32, month: u32, date: u32) -> Option { + NaiveDate::from_ymd_opt(year, month, date) } fn from_date(date: Self::DateType) -> Self { @@ -96,35 +102,6 @@ impl TimeValue for Date { } } -impl TimeValue for DateTime { - type DateType = Date; - fn date_floor(&self) -> Date { - self.date() - } - fn date_ceil(&self) -> Date { - if self.time().num_seconds_from_midnight() > 0 { - self.date() + Duration::days(1) - } else { - self.date() - } - } - fn earliest_after_date(date: Date) -> DateTime { - date.and_hms(0, 0, 0) - } - - fn subtract(&self, other: &DateTime) -> Duration { - self.clone() - other.clone() - } - - fn ymd(&self, year: i32, month: u32, date: u32) -> Self::DateType { - self.timezone().ymd(year, month, date) - } - - fn from_date(date: Self::DateType) -> Self { - date.and_hms(0, 0, 0) - } -} - impl TimeValue for NaiveDateTime { type DateType = NaiveDate; fn date_floor(&self) -> NaiveDate { @@ -138,19 +115,24 @@ impl TimeValue for NaiveDateTime { } } fn earliest_after_date(date: NaiveDate) -> NaiveDateTime { - date.and_hms(0, 0, 0) + // directly unwrap to avoid nested Options because the op is constant + date.and_hms_opt(0, 0, 0).unwrap() } fn subtract(&self, other: &NaiveDateTime) -> Duration { *self - *other } + fn add(&self, other: &Duration) -> NaiveDateTime { + *self + *other + } - fn ymd(&self, year: i32, month: u32, date: u32) -> Self::DateType { - NaiveDate::from_ymd(year, month, date) + fn ymd_opt(&self, year: i32, month: u32, date: u32) -> Option { + NaiveDate::from_ymd_opt(year, month, date) } fn from_date(date: Self::DateType) -> Self { - date.and_hms(0, 0, 0) + // directly unwrap to avoid nested Options because the op is constant + date.and_hms_opt(0, 0, 0).unwrap() } } @@ -237,11 +219,6 @@ where } } -impl AsRangedCoord for Range> { - type CoordDescType = RangedDate>; - type Value = Date; -} - impl AsRangedCoord for Range { type CoordDescType = RangedDate; type Value = NaiveDate; @@ -294,11 +271,9 @@ impl Monthly { ) -> Vec { let mut ret = vec![]; while end_year > start_year || (end_year == start_year && end_month >= start_month) { - ret.push(T::earliest_after_date(builder.ymd( - start_year, - start_month as u32, - 1, - ))); + if let Some(date) = builder.ymd_opt(start_year, start_month as u32, 1) { + ret.push(T::earliest_after_date(date)); + } start_month += step as i32; if start_month >= 13 { @@ -422,11 +397,12 @@ where let index_from_start_year = index + (self.0.start.date_ceil().month() - 1) as usize; let year = self.0.start.date_ceil().year() + index_from_start_year as i32 / 12; let month = index_from_start_year % 12; - Some(T::earliest_after_date(self.0.start.ymd( - year, - month as u32 + 1, - 1, - ))) + + if let Some(start_ymd) = self.0.start.ymd_opt(year, month as u32 + 1, 1) { + return Some(T::earliest_after_date(start_ymd)); + } + + None } } @@ -464,11 +440,9 @@ fn generate_yearly_keypoints( let mut ret = vec![]; while start_year <= end_year { - ret.push(T::earliest_after_date(builder.ymd( - start_year, - start_month, - 1, - ))); + if let Some(date) = builder.ymd_opt(start_year, start_month, 1) { + ret.push(T::earliest_after_date(date)); + } start_year += freq as i32; } @@ -552,11 +526,14 @@ where fn from_index(&self, index: usize) -> Option { let year = self.0.start.date_ceil().year() + index as i32; - let ret = T::earliest_after_date(self.0.start.ymd(year, 1, 1)); - if ret.date_ceil() <= self.0.start.date_floor() { - return Some(self.0.start.clone()); + if let Some(date) = self.0.start.ymd_opt(year, 1, 1) { + let ret = T::earliest_after_date(date); + if ret.date_ceil() <= self.0.start.date_floor() { + return Some(self.0.start.clone()); + } + return Some(ret); } - Some(ret) + None } } @@ -588,17 +565,6 @@ impl IntoYearly for Range { #[derive(Clone)] pub struct RangedDateTime(DT, DT); -impl AsRangedCoord for Range> { - type CoordDescType = RangedDateTime>; - type Value = DateTime; -} - -impl From>> for RangedDateTime> { - fn from(range: Range>) -> Self { - Self(range.start, range.end) - } -} - impl From> for RangedDateTime { fn from(range: Range) -> Self { Self(range.start, range.end) @@ -663,6 +629,19 @@ where } } +impl
ReversibleRanged for RangedDateTime
+where + DT: Datelike + Timelike + TimeValue + Clone + PartialOrd, + DT: Add, + DT: Sub, + RangedDate: Ranged, +{ + /// Perform the reverse mapping + fn unmap(&self, input: i32, limit: (i32, i32)) -> Option { + Some(TimeValue::unmap_coord(input, &self.0, &self.1, limit)) + } +} + /// The coordinate that for duration of time #[derive(Clone)] pub struct RangedDuration(Duration, Duration); @@ -846,16 +825,30 @@ fn compute_period_per_point(total_ns: u64, max_points: usize, sub_daily: bool) - #[cfg(test)] mod test { use super::*; - use chrono::{TimeZone, Utc}; + + // it's safe to use unwrap in these macros because testcases use good known values + macro_rules! create_date { + ($year:expr, $month:expr, $day:expr) => { + NaiveDate::from_ymd_opt($year, $month, $day).unwrap() + }; + } + macro_rules! create_datetime { + ($year:expr, $month:expr, $day:expr, $hour:expr, $min:expr, $sec:expr) => { + NaiveDate::from_ymd_opt($year, $month, $day) + .unwrap() + .and_hms_opt($hour, $min, $sec) + .unwrap() + }; + } #[test] fn test_date_range_long() { - let range = Utc.ymd(1000, 1, 1)..Utc.ymd(2999, 1, 1); + let range = create_date!(1000, 1, 1)..create_date!(2999, 1, 1); let ranged_coord = Into::>::into(range); - assert_eq!(ranged_coord.map(&Utc.ymd(1000, 8, 10), (0, 100)), 0); - assert_eq!(ranged_coord.map(&Utc.ymd(2999, 8, 10), (0, 100)), 100); + assert_eq!(ranged_coord.map(&create_date!(1000, 8, 10), (0, 100)), 0); + assert_eq!(ranged_coord.map(&create_date!(2999, 8, 10), (0, 100)), 100); let kps = ranged_coord.key_points(23); @@ -878,7 +871,7 @@ mod test { #[test] fn test_date_range_short() { - let range = Utc.ymd(2019, 1, 1)..Utc.ymd(2019, 1, 21); + let range = create_date!(2019, 1, 1)..create_date!(2019, 1, 21); let ranged_coord = Into::>::into(range); let kps = ranged_coord.key_points(4); @@ -921,11 +914,11 @@ mod test { #[test] fn test_yearly_date_range() { use crate::coord::ranged1d::BoldPoints; - let range = Utc.ymd(1000, 8, 5)..Utc.ymd(2999, 1, 1); + let range = create_date!(1000, 8, 5)..create_date!(2999, 1, 1); let ranged_coord = range.yearly(); - assert_eq!(ranged_coord.map(&Utc.ymd(1000, 8, 10), (0, 100)), 0); - assert_eq!(ranged_coord.map(&Utc.ymd(2999, 8, 10), (0, 100)), 100); + assert_eq!(ranged_coord.map(&create_date!(1000, 8, 10), (0, 100)), 0); + assert_eq!(ranged_coord.map(&create_date!(2999, 8, 10), (0, 100)), 100); let kps = ranged_coord.key_points(23); @@ -946,7 +939,7 @@ mod test { assert!(kps.into_iter().all(|x| x.month() == 9 && x.day() == 1)); - let range = Utc.ymd(2019, 8, 5)..Utc.ymd(2020, 1, 1); + let range = create_date!(2019, 8, 5)..create_date!(2020, 1, 1); let ranged_coord = range.yearly(); let kps = ranged_coord.key_points(BoldPoints(23)); assert!(kps.len() == 1); @@ -954,7 +947,7 @@ mod test { #[test] fn test_monthly_date_range() { - let range = Utc.ymd(2019, 8, 5)..Utc.ymd(2020, 9, 1); + let range = create_date!(2019, 8, 5)..create_date!(2020, 9, 1); let ranged_coord = range.monthly(); use crate::coord::ranged1d::BoldPoints; @@ -982,14 +975,14 @@ mod test { #[test] fn test_datetime_long_range() { let coord: RangedDateTime<_> = - (Utc.ymd(1000, 1, 1).and_hms(0, 0, 0)..Utc.ymd(3000, 1, 1).and_hms(0, 0, 0)).into(); + (create_datetime!(1000, 1, 1, 0, 0, 0)..create_datetime!(3000, 1, 1, 0, 0, 0)).into(); assert_eq!( - coord.map(&Utc.ymd(1000, 1, 1).and_hms(0, 0, 0), (0, 100)), + coord.map(&create_datetime!(1000, 1, 1, 0, 0, 0), (0, 100)), 0 ); assert_eq!( - coord.map(&Utc.ymd(3000, 1, 1).and_hms(0, 0, 0), (0, 100)), + coord.map(&create_datetime!(3000, 1, 1, 0, 0, 0), (0, 100)), 100 ); @@ -1015,7 +1008,7 @@ mod test { #[test] fn test_datetime_medium_range() { let coord: RangedDateTime<_> = - (Utc.ymd(2019, 1, 1).and_hms(0, 0, 0)..Utc.ymd(2019, 1, 11).and_hms(0, 0, 0)).into(); + (create_datetime!(2019, 1, 1, 0, 0, 0)..create_datetime!(2019, 1, 11, 0, 0, 0)).into(); let kps = coord.key_points(23); @@ -1039,7 +1032,7 @@ mod test { #[test] fn test_datetime_short_range() { let coord: RangedDateTime<_> = - (Utc.ymd(2019, 1, 1).and_hms(0, 0, 0)..Utc.ymd(2019, 1, 2).and_hms(0, 0, 0)).into(); + (create_datetime!(2019, 1, 1, 0, 0, 0)..create_datetime!(2019, 1, 2, 0, 0, 0)).into(); let kps = coord.key_points(50); @@ -1062,7 +1055,7 @@ mod test { #[test] fn test_datetime_nano_range() { - let start = Utc.ymd(2019, 1, 1).and_hms(0, 0, 0); + let start = create_datetime!(2019, 1, 1, 0, 0, 0); let end = start.clone() + Duration::nanoseconds(100); let coord: RangedDateTime<_> = (start..end).into(); @@ -1136,16 +1129,20 @@ mod test { #[test] fn test_date_discrete() { - let coord: RangedDate> = (Utc.ymd(2019, 1, 1)..Utc.ymd(2019, 12, 31)).into(); + let coord: RangedDate = + (create_date!(2019, 1, 1)..create_date!(2019, 12, 31)).into(); assert_eq!(coord.size(), 365); - assert_eq!(coord.index_of(&Utc.ymd(2019, 2, 28)), Some(31 + 28 - 1)); - assert_eq!(coord.from_index(364), Some(Utc.ymd(2019, 12, 31))); + assert_eq!( + coord.index_of(&create_date!(2019, 2, 28)), + Some(31 + 28 - 1) + ); + assert_eq!(coord.from_index(364), Some(create_date!(2019, 12, 31))); } #[test] fn test_monthly_discrete() { - let coord1 = (Utc.ymd(2019, 1, 10)..Utc.ymd(2019, 12, 31)).monthly(); - let coord2 = (Utc.ymd(2019, 1, 10)..Utc.ymd(2020, 1, 1)).monthly(); + let coord1 = (create_date!(2019, 1, 10)..create_date!(2019, 12, 31)).monthly(); + let coord2 = (create_date!(2019, 1, 10)..create_date!(2020, 1, 1)).monthly(); assert_eq!(coord1.size(), 12); assert_eq!(coord2.size(), 13); @@ -1160,7 +1157,7 @@ mod test { #[test] fn test_yearly_discrete() { - let coord1 = (Utc.ymd(2000, 1, 10)..Utc.ymd(2019, 12, 31)).yearly(); + let coord1 = (create_date!(2000, 1, 10)..create_date!(2019, 12, 31)).yearly(); assert_eq!(coord1.size(), 20); for i in 0..20 { @@ -1168,4 +1165,76 @@ mod test { assert_eq!(coord1.index_of(&coord1.from_index(i).unwrap()).unwrap(), i); } } + + #[test] + fn test_datetime_with_unmap() { + let start_time = create_datetime!(2021, 1, 1, 8, 0, 0); + let end_time = create_datetime!(2023, 1, 1, 8, 0, 0); + let mid = create_datetime!(2022, 1, 1, 8, 0, 0); + let coord: RangedDateTime<_> = (start_time..end_time).into(); + let pos = coord.map(&mid, (1000, 2000)); + assert_eq!(pos, 1500); + let value = coord.unmap(pos, (1000, 2000)); + assert_eq!(value, Some(mid)); + } + + #[test] + fn test_naivedatetime_with_unmap() { + let start_time = create_datetime!(2021, 1, 1, 8, 0, 0); + let end_time = create_datetime!(2023, 1, 1, 8, 0, 0); + let mid = create_datetime!(2022, 1, 1, 8, 0, 0); + let coord: RangedDateTime<_> = (start_time..end_time).into(); + let pos = coord.map(&mid, (1000, 2000)); + assert_eq!(pos, 1500); + let value = coord.unmap(pos, (1000, 2000)); + assert_eq!(value, Some(mid)); + } + + #[test] + fn test_date_with_unmap() { + let start_date = create_date!(2021, 1, 1); + let end_date = create_date!(2023, 1, 1); + let mid = create_date!(2022, 1, 1); + let coord: RangedDate = (start_date..end_date).into(); + let pos = coord.map(&mid, (1000, 2000)); + assert_eq!(pos, 1500); + let value = coord.unmap(pos, (1000, 2000)); + assert_eq!(value, Some(mid)); + } + + #[test] + fn test_naivedate_with_unmap() { + let start_date = create_date!(2021, 1, 1); + let end_date = create_date!(2023, 1, 1); + let mid = create_date!(2022, 1, 1); + let coord: RangedDate = (start_date..end_date).into(); + let pos = coord.map(&mid, (1000, 2000)); + assert_eq!(pos, 1500); + let value = coord.unmap(pos, (1000, 2000)); + assert_eq!(value, Some(mid)); + } + + #[test] + fn test_datetime_unmap_for_nanoseconds() { + let start_time = create_datetime!(2021, 1, 1, 8, 0, 0); + let end_time = start_time + Duration::nanoseconds(1900); + let mid = start_time + Duration::nanoseconds(950); + let coord: RangedDateTime<_> = (start_time..end_time).into(); + let pos = coord.map(&mid, (1000, 2000)); + assert_eq!(pos, 1500); + let value = coord.unmap(pos, (1000, 2000)); + assert_eq!(value, Some(mid)); + } + + #[test] + fn test_datetime_unmap_for_nanoseconds_small_period() { + let start_time = create_datetime!(2021, 1, 1, 8, 0, 0); + let end_time = start_time + Duration::nanoseconds(400); + let coord: RangedDateTime<_> = (start_time..end_time).into(); + let value = coord.unmap(2000, (1000, 2000)); + assert_eq!(value, Some(end_time)); + let mid = start_time + Duration::nanoseconds(200); + let value = coord.unmap(500, (0, 1000)); + assert_eq!(value, Some(mid)); + } } diff --git a/plotters/src/coord/ranged3d/projection.rs b/plotters/src/coord/ranged3d/projection.rs index a9c57c1b..8ad0acc1 100644 --- a/plotters/src/coord/ranged3d/projection.rs +++ b/plotters/src/coord/ranged3d/projection.rs @@ -140,7 +140,7 @@ impl ProjectionMatrix { } /// The helper struct to build a projection matrix -#[derive(Copy, Clone)] +#[derive(Clone)] pub struct ProjectionMatrixBuilder { /// Specifies the yaw of the 3D coordinate system pub yaw: f64, @@ -150,6 +150,7 @@ pub struct ProjectionMatrixBuilder { pub scale: f64, pivot_before: (i32, i32, i32), pivot_after: (i32, i32), + transformation_queue: Vec, } impl Default for ProjectionMatrixBuilder { @@ -160,6 +161,7 @@ impl Default for ProjectionMatrixBuilder { scale: 1.0, pivot_after: (0, 0), pivot_before: (0, 0, 0), + transformation_queue: [].to_vec(), } } } @@ -178,6 +180,12 @@ impl ProjectionMatrixBuilder { self } + /// Adds matrix to list of transformations to apply + pub fn add_transform(&mut self, projection: ProjectionMatrix) -> &mut Self { + self.transformation_queue.push(projection); + self + } + /// Build the matrix based on the configuration pub fn into_matrix(self) -> ProjectionMatrix { let mut ret = if self.pivot_before == (0, 0, 0) { @@ -187,6 +195,10 @@ impl ProjectionMatrixBuilder { ProjectionMatrix::shift(-x as f64, -y as f64, -z as f64) * ProjectionMatrix::default() }; + for transform in self.transformation_queue { + ret = ret * transform; + } + if self.yaw.abs() > 1e-20 { ret = ret * ProjectionMatrix::rotate(0.0, self.yaw, 0.0); } diff --git a/plotters/src/drawing/area.rs b/plotters/src/drawing/area.rs index 2e5c3fe3..9519f378 100644 --- a/plotters/src/drawing/area.rs +++ b/plotters/src/drawing/area.rs @@ -87,7 +87,7 @@ impl Rect { .map(|(a, b)| (*a, *b)) .collect(); - // Justify: this is actually needed. Because we need to return a iterator that have + // Justify: this is actually needed. Because we need to return a iterator that have // static life time, thus we need to copy the value to a buffer and then turn the buffer // into a iterator. #[allow(clippy::needless_collect)] @@ -97,14 +97,12 @@ impl Rect { .map(|(a, b)| (*a, *b)) .collect(); - ysegs - .into_iter() - .flat_map(move |(y0, y1)| { - xsegs - .clone() - .into_iter() - .map(move |(x0, x1)| Self { x0, y0, x1, y1 }) - }) + ysegs.into_iter().flat_map(move |(y0, y1)| { + xsegs + .clone() + .into_iter() + .map(move |(x0, x1)| Self { x0, y0, x1, y1 }) + }) } /// Make the coordinate in the range of the rectangle diff --git a/plotters/src/drawing/backend_impl/mocked.rs b/plotters/src/drawing/backend_impl/mocked.rs index 7569e732..da6bfecc 100644 --- a/plotters/src/drawing/backend_impl/mocked.rs +++ b/plotters/src/drawing/backend_impl/mocked.rs @@ -269,13 +269,6 @@ impl DrawingBackend for MockedBackend { impl Drop for MockedBackend { fn drop(&mut self) { - // `self.drop_check` is typically a testing function; it can panic. - // The current `drop` call may be a part of stack unwinding caused - // by another panic. If so, we should never call it. - if std::thread::panicking() { - return; - } - let mut temp = None; std::mem::swap(&mut temp, &mut self.drop_check); diff --git a/plotters/src/element/basic_shapes_3d.rs b/plotters/src/element/basic_shapes_3d.rs index 97b15e62..fb248996 100644 --- a/plotters/src/element/basic_shapes_3d.rs +++ b/plotters/src/element/basic_shapes_3d.rs @@ -14,25 +14,25 @@ drawing_area.fill(&WHITE).unwrap(); let mut chart_builder = ChartBuilder::on(&drawing_area); let mut chart_context = chart_builder.margin(20).build_cartesian_3d(0.0..3.5, 0.0..2.5, 0.0..1.5).unwrap(); chart_context.configure_axes().x_labels(4).y_labels(3).z_labels(2).draw().unwrap(); -let cubiod = Cubiod::new([(0.,0.,0.), (3.,2.,1.)], BLUE.mix(0.2), BLUE); -chart_context.draw_series(std::iter::once(cubiod)).unwrap(); +let cuboid = Cuboid::new([(0.,0.,0.), (3.,2.,1.)], BLUE.mix(0.2), BLUE); +chart_context.draw_series(std::iter::once(cuboid)).unwrap(); ``` The result is a semi-transparent cuboid with blue edges: ![](https://cdn.jsdelivr.net/gh/facorread/plotters-doc-data@b6703f7/apidoc/cuboid.svg) */ -pub struct Cubiod { +pub struct Cuboid { face_style: ShapeStyle, edge_style: ShapeStyle, vert: [(X, Y, Z); 8], } -impl Cubiod { +impl Cuboid { /** Creates a cuboid. - See [`Cubiod`] for more information and examples. + See [`Cuboid`] for more information and examples. */ #[allow(clippy::redundant_clone)] pub fn new, ES: Into>( @@ -58,7 +58,7 @@ impl Cubiod { } impl<'a, X: 'a, Y: 'a, Z: 'a> PointCollection<'a, (X, Y, Z), BackendCoordAndZ> - for &'a Cubiod + for &'a Cuboid { type Point = &'a (X, Y, Z); type IntoIter = &'a [(X, Y, Z)]; @@ -67,7 +67,7 @@ impl<'a, X: 'a, Y: 'a, Z: 'a> PointCollection<'a, (X, Y, Z), BackendCoordAndZ> } } -impl Drawable for Cubiod { +impl Drawable for Cuboid { fn draw>( &self, points: I, @@ -106,3 +106,7 @@ impl Drawable for Cubiod = Cuboid; diff --git a/plotters/src/element/mod.rs b/plotters/src/element/mod.rs index e2790051..b8296182 100644 --- a/plotters/src/element/mod.rs +++ b/plotters/src/element/mod.rs @@ -272,7 +272,7 @@ impl CoordMapper for BackendCoordOnly { /** Used for 3d coordinate transformations. -See [`Cubiod`] for more information and an example. +See [`Cuboid`] for more information and an example. */ pub struct BackendCoordAndZ; diff --git a/plotters/src/element/pie.rs b/plotters/src/element/pie.rs index 95298345..7994d2f5 100644 --- a/plotters/src/element/pie.rs +++ b/plotters/src/element/pie.rs @@ -1,6 +1,6 @@ use crate::{ element::{Drawable, PointCollection}, - style::{IntoFont, RGBColor, TextStyle, BLACK}, + style::{IntoFont, RGBAColor, TextStyle, BLACK}, }; use plotters_backend::{BackendCoord, DrawingBackend, DrawingErrorKind}; use std::{error::Error, f64::consts::PI, fmt::Display}; @@ -24,7 +24,7 @@ pub struct Pie<'a, Coord, Label: Display> { center: &'a Coord, // cartesian coord radius: &'a f64, sizes: &'a [f64], - colors: &'a [RGBColor], + colors: &'a [RGBAColor], labels: &'a [Label], total: f64, start_radian: f64, @@ -40,7 +40,7 @@ impl<'a, Label: Display> Pie<'a, (i32, i32), Label> { center: &'a (i32, i32), radius: &'a f64, sizes: &'a [f64], - colors: &'a [RGBColor], + colors: &'a [RGBAColor], labels: &'a [Label], ) -> Self { // fold iterator to pre-calculate total from given slice sizes @@ -69,7 +69,7 @@ impl<'a, Label: Display> Pie<'a, (i32, i32), Label> { /// Default is set to start at 0, which is aligned on the x axis. /// ``` /// use plotters::prelude::*; - /// let mut pie = Pie::new(&(50,50), &10.0, &[50.0, 25.25, 20.0, 5.5], &[RED, BLUE, GREEN, WHITE], &["Red", "Blue", "Green", "White"]); + /// let mut pie = Pie::new(&(50,50), &10.0, &[50.0, 25.25, 20.0, 5.5], &[RED.to_rgba(), BLUE.to_rgba(), GREEN.to_rgba(), WHITE.to_rgba()], &["Red", "Blue", "Green", "White"]); /// pie.start_angle(-90.0); // retract to a right angle, so it starts aligned to a vertical Y axis. /// ``` pub fn start_angle(&mut self, start_angle: f64) { @@ -109,18 +109,14 @@ impl<'a, DB: DrawingBackend, Label: Display> Drawable for Pie<'a, (i32, i32) let radian_increment = PI / 180.0 / self.radius.sqrt() * 2.0; let mut perc_labels = Vec::new(); for (index, slice) in self.sizes.iter().enumerate() { - let slice_style = - self.colors - .get(index) - .ok_or_else(|| DrawingErrorKind::FontError(Box::new( - PieError::LengthMismatch, - )))?; + let slice_style = self + .colors + .get(index) + .ok_or_else(|| DrawingErrorKind::FontError(Box::new(PieError::LengthMismatch)))?; let label = self .labels .get(index) - .ok_or_else(|| DrawingErrorKind::FontError(Box::new( - PieError::LengthMismatch, - )))?; + .ok_or_else(|| DrawingErrorKind::FontError(Box::new(PieError::LengthMismatch)))?; // start building wedge line against the previous edge let mut points = vec![*self.center]; let ratio = slice / self.total; diff --git a/plotters/src/evcxr.rs b/plotters/src/evcxr.rs index 8117d35f..e91418ea 100644 --- a/plotters/src/evcxr.rs +++ b/plotters/src/evcxr.rs @@ -1,11 +1,7 @@ use crate::coord::Shift; use crate::drawing::{DrawingArea, IntoDrawingArea}; -use plotters_backend::DrawingBackend; use plotters_svg::SVGBackend; -#[cfg(feature = "evcxr_bitmap")] -use plotters_bitmap::BitMapBackend; - /// The wrapper for the generated SVG pub struct SVGWrapper(String, String); @@ -44,26 +40,3 @@ pub fn evcxr_figure< draw(root).expect("Drawing failure"); SVGWrapper(buffer, "".to_string()) } - -/// Start drawing an evcxr figure -#[cfg(feature = "evcxr_bitmap")] -pub fn evcxr_bitmap_figure< - Draw: FnOnce(DrawingArea) -> Result<(), Box>, ->( - size: (u32, u32), - draw: Draw, -) -> SVGWrapper { - const PIXEL_SIZE : usize = 3; - let mut buf = Vec::new(); - buf.resize((size.0 as usize) * (size.1 as usize) * PIXEL_SIZE, 0); - let root = BitMapBackend::with_buffer(&mut buf, size).into_drawing_area(); - draw(root).expect("Drawing failure"); - let mut buffer = "".to_string(); - { - let mut svg_root = SVGBackend::with_string(&mut buffer, size); - svg_root - .blit_bitmap((0, 0), size, &buf) - .expect("Failure converting to SVG"); - } - SVGWrapper(buffer, "".to_string()) -} diff --git a/plotters/src/lib.rs b/plotters/src/lib.rs index 873288b2..c8be522d 100644 --- a/plotters/src/lib.rs +++ b/plotters/src/lib.rs @@ -102,7 +102,7 @@ including bitmap, vector graph, piston window, GTK/Cairo and WebAssembly.
Real-time Rendering - [code] + [code]
@@ -832,8 +832,10 @@ pub mod prelude { }; // Elements + #[allow(deprecated)] + pub use crate::element::Cubiod; pub use crate::element::{ - Circle, Cross, Cubiod, DynElement, EmptyElement, IntoDynElement, MultiLineText, + Circle, Cross, Cuboid, DynElement, EmptyElement, IntoDynElement, MultiLineText, PathElement, Pie, Pixel, Polygon, Rectangle, Text, TriangleMarker, }; diff --git a/plotters/src/series/line_series.rs b/plotters/src/series/line_series.rs index 2d0cf986..448d029e 100644 --- a/plotters/src/series/line_series.rs +++ b/plotters/src/series/line_series.rs @@ -45,8 +45,7 @@ impl Iterator for LineSeries for RGBAColor { - fn from(rgb: RGBColor) -> Self { - Self(rgb.0, rgb.1, rgb.2, 1.0) - } -} - /// A color in the given palette #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)] pub struct PaletteColor(usize, PhantomData

); diff --git a/plotters/src/test.rs b/plotters/src/test.rs index 2c94f082..59ba2e60 100644 --- a/plotters/src/test.rs +++ b/plotters/src/test.rs @@ -12,11 +12,3 @@ fn regression_test_issue_267() { .draw_line(p1, p2, &RGBColor(0, 0, 0).stroke_width(0)) .unwrap(); } - -#[test] -fn from_trait_impl_rgba_color() { - let rgb = RGBColor(1, 2, 3); - let c = RGBAColor::from(rgb); - - assert_eq!(c.rgb(), rgb.rgb()); -}