Skip to content

Commit

Permalink
Fix documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
clementwanjau committed May 9, 2024
1 parent f3a3ff4 commit c465216
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 30 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "Rust WASM bindings and chart components for ApexCharts. "
authors = ["Clement Wanjau <clementwanjau@gmail.com>"]
repository = "https://github.com/clementwanjau/apexcharts-rs"
documentation = "https://docs.rs/apexcharts-rs"
version = "0.1.5"
version = "0.1.6"
edition = "2021"
keywords = ["apexcharts", "wasm-charts", "visualization", "yew-charts", "leptos-charts"]
license = "Apache-2.0"
Expand All @@ -16,7 +16,9 @@ crate-type = ["cdylib", "rlib"]

[features]
default = []
### This feature enables the leptos chart components
leptos = ["dep:leptos"]
### This feature enables the yew chart components
yew = ["dep:yew", "dep:gloo"]

[dependencies]
Expand Down
2 changes: 2 additions & 0 deletions src/leptos.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! This module contains the Leptos component for rendering ApexCharts in a Leptos application.
//!
use leptos::*;
use wasm_bindgen::{ JsValue};
use serde_json::Value;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ mod yew;
mod leptos;

pub mod prelude {
//! Re-exports commonly used items.
pub use crate::bindings::ApexChart;
pub use crate::options::{ChartType, ChartSeries, SeriesData, to_jsvalue};
#[cfg(feature = "yew")]
Expand Down
57 changes: 28 additions & 29 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,37 @@ use wasm_bindgen_futures::js_sys;
/// Represents the type of the chart that will be rendered.
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub enum ChartType {
/// Represents an area chart.
Area,
/// Represents a bar chart.
Bar,
/// Represents a line chart.
Line,
Column,
/// Represents a Boxplot chart.
BoxPlot,
/// Represents a candlestick chart.
CandleStick,
/// Represents a range bar chart.
RangeBar,
/// Represents a range area chart.
RangeArea,
/// Represents a heatmap chart.
HeatMap,
/// Represents a treemap chart.
Treemap,
/// Represents a funnel chart.
Funnel,
/// Represents a multi-axis chart.
MultiAxis,
/// Represents a pie chart. The expected type of data is [SeriesData::Radial].
Pie,
/// Represents a donut chart. The expected type of data is [SeriesData::Radial].
Donut,
/// Represents a radar chart.
Radar,
/// Represents a radial bar chart. The expected type of data is [SeriesData::Radial].
RadialBar,
/// Represents a circular gauge chart. The expected type of data is [SeriesData::Radial].
CircularGauge,
}

Expand All @@ -34,7 +49,6 @@ impl Display for ChartType {
ChartType::Area => write!(f, "area"),
ChartType::Bar => write!(f, "bar"),
ChartType::Line => write!(f, "line"),
ChartType::Column => write!(f, "column"),
ChartType::BoxPlot => write!(f, "boxPlot"),
ChartType::CandleStick => write!(f, "candlestick"),
ChartType::RangeBar => write!(f, "rangeBar"),
Expand Down Expand Up @@ -91,18 +105,18 @@ impl Serialize for SeriesData {
}
seq.end()
}
SeriesData::NumericPaired(data) => {
SeriesData::NumericPaired(data) | SeriesData::Timestamped(data) => {
let mut seq = serializer.serialize_seq(Some(data.len()))?;
let data = data.iter().map(|(x, y)| vec![*x, *y]).collect::<Vec<_>>();
for item in data {
seq.serialize_element(&item)?;
}
seq.end()
}
SeriesData::CategoryPaired(data) => {
SeriesData::CategoryPaired(data) | SeriesData::Dated(data) => {
// Serialize the data into a sequence of an object with two properties `x` and `y`. eg `[{x: "Apple", y: 30}, {x: "Banana", y: 40}]`
let mut seq = serializer.serialize_seq(Some(data.len()))?;
let data: Vec<IndexMap<String, serde_json::Value>> = data.iter().map(|(x, y)| {
let data: Vec<IndexMap<String, Value>> = data.iter().map(|(x, y)| {
IndexMap::from_iter(
vec![
("x".to_string(), Value::String(x.to_string())),
Expand All @@ -115,32 +129,9 @@ impl Serialize for SeriesData {
}
seq.end()
}
SeriesData::Timestamped(data) => {
let mut seq = serializer.serialize_seq(Some(data.len()))?;
let data = data.iter().map(|(x, y)| vec![*x, *y]).collect::<Vec<_>>();
for item in data {
seq.serialize_element(&item)?;
}
seq.end()
}
SeriesData::Dated(data) => {
let mut seq = serializer.serialize_seq(Some(data.len()))?;
let data: Vec<IndexMap<String, serde_json::Value>> = data.iter().map(|(x, y)| {
IndexMap::from_iter(
vec![
("x".to_string(), Value::String(x.to_string())),
("y".to_string(), Value::Number(serde_json::Number::from(*y)))
]
)
}).collect::<Vec<_>>();
for item in data {
seq.serialize_element(&item)?;
}
seq.end()
},
SeriesData::Radial(data) => {
let mut seq = serializer.serialize_seq(Some(data.len()))?;
let data: Vec<IndexMap<String, serde_json::Value>> = data.iter().map(|(x, y)| {
let data: Vec<IndexMap<String, Value>> = data.iter().map(|(x, y)| {
IndexMap::from_iter(
vec![
("x".to_string(), Value::String(x.to_string())),
Expand All @@ -166,11 +157,18 @@ impl Serialize for SeriesData {
///
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ChartSeries {
/// The name of the series. This is used to identify the series in the chart. It is also displayed in the legend.
pub name: String,
/// The data that will be rendered in the chart. Different types of charts require different types of data.
pub data: SeriesData,
/// The color of the series. This is used to set the color of the series in the chart.
pub color: String,
/// The type of the series. This is used to set the type of the series in the chart. Note that this
/// overrides the type of the chart provided in the `ApexChartComponent` component. Usually, you don't need to set this.
#[serde(skip_serializing_if = "Option::is_none")]
pub r#type: Option<ChartType>,
/// The z-index of the series. This is used to set the z-index of the series in the chart. It is used to determine
/// the order in which the series are rendered in the chart. The series with the highest z-index is rendered on top.
#[serde(skip_serializing_if = "Option::is_none")]
pub z_index: Option<i32>,
}
Expand All @@ -190,6 +188,7 @@ impl From<ChartSeries> for JsValue {
}
}

/// A helper function to convert a vector of items into a JsValue.
pub fn to_jsvalue<T: Into<JsValue>>(vec: Vec<T>) -> JsValue {
let array = js_sys::Array::new();
for item in vec {
Expand Down
9 changes: 9 additions & 0 deletions src/yew.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! This module contains the Yew component for rendering ApexCharts in a Yew application.
use wasm_bindgen::JsValue;
use gloo::timers::callback::Timeout;
use serde_json::Value;
Expand Down Expand Up @@ -70,14 +72,21 @@ pub enum ApexChartComponentMsg {

#[derive(Clone, Debug, Properties, PartialEq)]
pub struct ApexChartComponentProps {
/// The type of the chart.
#[prop_or(ChartType::Line)]
pub r#type: ChartType,
/// The options for the chart in json. These options are used to customize the chart. Refer to the
/// [ApexCharts documentation](https://apexcharts.com/docs/options/) for more information.
#[prop_or("".into())]
pub options: String,
/// The unique id of the chart. This is used to identify the chart in the DOM.
pub id: String,
/// The series to be rendered in the chart. This is used to set the data that will be rendered in the chart.
pub series: Vec<ChartSeries>,
/// The width of the chart. This is used to set the width of the chart.
#[prop_or("100%".to_string())]
pub width: String,
/// The height of the chart. This is used to set the height of the chart.
#[prop_or("auto".to_string())]
pub height: String,
}
Expand Down

0 comments on commit c465216

Please sign in to comment.