Skip to content

Commit

Permalink
feat: support save to png
Browse files Browse the repository at this point in the history
add missing docs
  • Loading branch information
duskmoon314 committed Dec 28, 2024
1 parent 56a6d56 commit 8e5eda3
Show file tree
Hide file tree
Showing 12 changed files with 749 additions and 205 deletions.
66 changes: 58 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ dyn-clone = "1.0.17"
num-traits = "0.2.19"
plotters = "0.3.7"
polars = { version = "0.45.1", features = ["csv", "partition_by"] }
thiserror = "2.0.8"
# thiserror = "1.0.63"
typed-builder = "0.20.0"

# [dev-dependencies]
[dev-dependencies]
# trybuild = "1.0.99"
polars = { version = "0.45.1", features = [
"dtype-u8",
"dtype-u16",
"lazy",
"parquet",
] }
61 changes: 60 additions & 1 deletion src/aes.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
//! Aesthetic mappings
use std::ops::{Add, AddAssign};

use derive_builder::Builder;

pub mod color;

/// Aesthetic mappings
///
/// Aesthetic mappings describe how variables in the data are mapped to visual
/// properties (aesthetics) of geoms. Aesthetic mappings are constructed with
/// the [`aes!`](crate::aes!) marco or [`Aes::builder`] method.
///
/// TODO: some aesthetics are not used as "mapping" but as "constant" values.
#[derive(Clone, Debug, Default, PartialEq, Builder)]
#[builder(default, setter(into, strip_option))]
pub struct Aes {
/// The column name to map to the x-axis
pub x: Option<&'static str>,

/// The column name to map to the y-axis
pub y: Option<&'static str>,

/// The color aesthetic
pub color: Option<color::Color>,

/// The fill aesthetic
pub fill: Option<bool>,

/// The size aesthetic
pub size: Option<i32>,

/// The shape aesthetic
pub shape: Option<u8>,

/// The label aesthetic
pub label: Option<String>,
}

impl Aes {
/// Create a new [`Aes`] object via the builder pattern
pub fn builder() -> AesBuilder {
AesBuilder::default()
}
Expand Down Expand Up @@ -51,10 +68,12 @@ impl Add for Aes {
}

impl AesBuilder {
/// Set the color aesthetic, alias for [`AesBuilder::color`]
pub fn colour<T: Into<color::Color>>(&mut self, colour: T) -> &mut Self {
self.color(colour)
}

/// Set the color aesthetic, alias for [`AesBuilder::color`]
pub fn col<T: Into<color::Color>>(&mut self, col: T) -> &mut Self {
self.color(col)
}
Expand All @@ -63,7 +82,47 @@ impl AesBuilder {
// Trick to hide internal implementation details from the docs
macro_rules! __aes {
($aes: item) => {
/// Construct Aesthetic Mappings
/// # aes!: Construct Aesthetic Mappings
///
/// This macro is used to create an [`Aes`] object in a more concise way
/// like `ggplot2`. It is a wrapper around [`Aes::builder`].
///
/// ## Usage
///
/// ```ignore
/// aes!(
/// x = <X_COLUMN>,
/// y = <Y_COLUMN>,
/// ...
/// )
/// ```
///
/// ### Arguments
///
/// #### `x` and `y`
///
/// The column names to map to the x-axis and y-axis, respectively.
///
/// If they are the first two arguments, they can be passed without the
/// named argument.
///
/// ```
/// # use gongbi::*;
/// let a1 = aes!(x);
/// let a2 = aes!(x, y);
///
/// assert_eq!(a1.x, Some("x"));
/// assert_eq!(a2.x, Some("x"));
/// assert_eq!(a2.y, Some("y"));
/// ```
///
/// #### Other Aesthetics
///
/// Other aesthetics can be set with the named argument.
///
/// - `color`
/// - `size`
/// - `shape`
$aes
};
}
Expand Down
7 changes: 7 additions & 0 deletions src/aes/color.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
//! Color module
//!
//! This module contains predefined colors and utilities to work with colors.
// TODO: remove this when color is fully implemented
#![allow(missing_docs)]

use std::str::FromStr;

use plotters::style::RGBColor;
Expand Down
16 changes: 11 additions & 5 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use std::fmt::Debug;
//! Data module
//!
//! This module defines the `Data` trait and its implementations.
use dyn_clone::DynClone;
use std::fmt::Debug;

pub mod polars;

pub trait Data: Debug + DynClone {
/// # Data trait
///
/// This trait abstracts the data source for the [`Plot`](crate::Plot) struct.
pub trait Data: Debug {
/// Get a column as a vector of f64 values.
fn column_f64(&self, column_name: &str) -> Vec<f64>;

/// Get the minimum and maximum values of a column.
fn column_range_f64(&self, column_name: &str) -> (f64, f64);

/// Get the length of a column.
fn column_len(&self, column_name: &str) -> usize;
}

dyn_clone::clone_trait_object!(Data);
2 changes: 2 additions & 0 deletions src/data/polars.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! `Data` implementation for `polars` DataFrame
use polars::prelude::*;

impl super::Data for DataFrame {
Expand Down
29 changes: 29 additions & 0 deletions src/label.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
//! Label module.
use std::ops::Add;

use derive_builder::Builder;

/// # Label layer
///
/// This layer is used to add important information to the plot.
#[derive(Clone, Debug, Default, PartialEq, Builder)]
#[builder(default, setter(into, strip_option))]
pub struct Label {
/// The caption of the label.
pub caption: Option<String>,

/// The x-axis description.
pub x: Option<String>,

/// The y-axis description.
pub y: Option<String>,
}

impl Label {
/// Create a [`Label`] via the builder pattern.
pub fn builder() -> LabelBuilder {
LabelBuilder::default()
}
}

/// # labs!: Construct a new [`Label`] layer.
///
/// This macro is used to construct a new [`Label`] layer in a more concise like `ggplot2`.
/// It is a wrapper around the [`Label::builder`] method.
///
/// ## Usage
///
/// ```ignore
/// labs!(
/// caption = <Caption>,
/// x = <X>,
/// y = <Y>,
/// )
/// ```
///
/// ### Arguments
///
/// - `caption`: Also known as the title of the plot.
/// - `x`: The x-axis description.
/// - `y`: The y-axis description.
#[macro_export]
macro_rules! labs {
($($arg: ident = $val: expr),* $(,)?) => {
Expand Down
Loading

0 comments on commit 8e5eda3

Please sign in to comment.