Skip to content

Commit

Permalink
Merge branch 'yuankunzhang:main' into fix_ssr_with_many_points
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaOber authored Oct 6, 2024
2 parents d77a3a9 + 9356afb commit 6dc200b
Show file tree
Hide file tree
Showing 14 changed files with 458 additions and 124 deletions.
488 changes: 391 additions & 97 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions charming/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "charming"
description = "A visualization library for Rust"
version = "0.3.1"
version = "0.4.0"
edition = "2021"
authors = ["Yuankun Zhang <[email protected]>"]
repository = "https://github.com/yuankunzhang/charming"
Expand All @@ -12,14 +12,14 @@ license = "MIT/Apache-2.0"
readme = "../README.md"

[dependencies]
deno_core = { version = "0.232", optional = true }
deno_core = { version = "0.311", optional = true }
handlebars = { version = "4.3", optional = true }
image = { version = "0.24", optional = true }
resvg = { version = "0.36", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = {version = "0.6", optional = true }
serde_json = "1.0"
serde_v8 = { version = "0.141", optional = true }
serde_v8 = { version = "0.220", optional = true }
wasm-bindgen = { version = "0.2", optional = true }

[dependencies.web-sys]
Expand Down
10 changes: 10 additions & 0 deletions charming/src/datatype/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl From<f64> for NumericValue {
#[serde(untagged)]
pub enum CompositeValue {
Number(NumericValue),
OptionalNumber(Option<NumericValue>),
String(String),
Array(Vec<CompositeValue>),
}
Expand All @@ -48,6 +49,15 @@ where
}
}

impl<N> From<Option<N>> for CompositeValue
where
N: Into<NumericValue>,
{
fn from(n: Option<N>) -> Self {
CompositeValue::OptionalNumber(n.map(Into::into))
}
}

impl From<&str> for CompositeValue {
fn from(s: &str) -> Self {
CompositeValue::String(s.to_string())
Expand Down
2 changes: 1 addition & 1 deletion charming/src/element/border_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "snake_case")]
Expand Down
2 changes: 1 addition & 1 deletion charming/src/element/color.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::ser::{SerializeStruct, Serializer};
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Serialize)]
#[serde(rename_all = "snake_case")]
Expand Down
2 changes: 1 addition & 1 deletion charming/src/element/item_style.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Serialize, Deserialize};
use super::{border_type::BorderType, color::Color};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
Expand Down
2 changes: 1 addition & 1 deletion charming/src/element/text_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct TextStyle {

#[serde(skip_serializing_if = "Option::is_none")]
align: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
padding: Option<[f64; 4]>,
}
Expand Down
11 changes: 7 additions & 4 deletions charming/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(clippy::to_string_trait_impl)]
#![cfg_attr(docsrs, feature(doc_cfg))]
/*!
Charming is a powerful and versatile chart rendering library for Rust that
Expand Down Expand Up @@ -516,9 +515,13 @@ impl Chart {
}
}

impl ToString for Chart {
fn to_string(&self) -> String {
process_raw_strings(serde_json::to_string_pretty(self).unwrap().as_str())
impl std::fmt::Display for Chart {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
process_raw_strings(&serde_json::to_string_pretty(self).unwrap())
)
}
}

Expand Down
8 changes: 3 additions & 5 deletions charming/src/renderer/image_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ impl ImageRenderer {
runtime
.execute_script(
"[runtime.js]",
include_str!("../asset/runtime.js").to_string().into(),
include_str!("../asset/runtime.js").to_string(),
)
.unwrap();
runtime
.execute_script(
"[echarts.js]",
include_str!("../asset/echarts-5.4.2.min.js")
.to_string()
.into(),
include_str!("../asset/echarts-5.4.2.min.js").to_string(),
)
.unwrap();

Expand Down Expand Up @@ -89,7 +87,7 @@ impl ImageRenderer {
}),
)
.expect("Failed to render template");
let result = self.js_runtime.execute_script("[anon]", code.into());
let result = self.js_runtime.execute_script("[anon]", code);

match result {
Ok(global) => {
Expand Down
9 changes: 9 additions & 0 deletions charming/src/series/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ pub struct Line {
#[serde(skip_serializing_if = "Option::is_none")]
smooth: Option<f64>,

#[serde(skip_serializing_if = "Option::is_none")]
connect_nulls: Option<bool>,

#[serde(skip_serializing_if = "Option::is_none")]
mark_point: Option<MarkPoint>,

Expand Down Expand Up @@ -101,6 +104,7 @@ impl Line {
item_style: None,
emphasis: None,
smooth: None,
connect_nulls: None,
mark_point: None,
mark_line: None,
mark_area: None,
Expand Down Expand Up @@ -179,6 +183,11 @@ impl Line {
self
}

pub fn connect_nulls(mut self, connect_nulls: bool) -> Self {
self.connect_nulls = Some(connect_nulls);
self
}

pub fn mark_point<M: Into<MarkPoint>>(mut self, mark_point: M) -> Self {
self.mark_point = Some(mark_point.into());
self
Expand Down
11 changes: 7 additions & 4 deletions charming/src/series/sankey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};

use crate::{
datatype::CompositeValue,
element::{Emphasis, Label, LineStyle, Orient, ItemStyle},
element::{Emphasis, ItemStyle, Label, LineStyle, Orient},
};

#[derive(Serialize)]
Expand All @@ -29,7 +29,10 @@ pub struct SankeyNode {
}

impl SankeyNode {
pub fn new<S>(name: S) -> Self where S: Into<String> {
pub fn new<S>(name: S) -> Self
where
S: Into<String>,
{
Self {
name: name.into(),
value: None,
Expand All @@ -54,8 +57,8 @@ impl SankeyNode {
}

impl<S> From<S> for SankeyNode
where
S: Into<String>,
where
S: Into<String>,
{
fn from(name: S) -> Self {
SankeyNode {
Expand Down
2 changes: 2 additions & 0 deletions gallery/src/gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use charming_gallery::CHARTS;

#[tokio::main]
async fn main() {
println!("Access the plots at: http://127.0.0.1:5555");

let app = Router::new()
.route("/", get(index))
.route("/:type/:name", get(render));
Expand Down
23 changes: 21 additions & 2 deletions gallery/src/line/distribution_of_electricity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,28 @@ pub fn chart() -> Chart {
Line::new()
.name("Electricity")
.smooth(0.5)
.connect_nulls(true)
.data(vec![
300, 280, 250, 260, 270, 300, 550, 500, 400, 390, 380, 390, 400, 500, 600, 750,
800, 700, 600, 400,
Some(300),
Some(280),
Some(250),
None,
Some(270),
Some(300),
Some(550),
Some(500),
Some(400),
Some(390),
Some(380),
Some(390),
Some(400),
Some(500),
Some(600),
Some(750),
Some(800),
Some(700),
Some(600),
Some(400),
])
.mark_area(
MarkArea::new()
Expand Down
6 changes: 1 addition & 5 deletions gallery/src/line/line_gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ pub fn chart() -> Chart {
.max((data_list.len() - 1) as f64),
)
.x_axis(Axis::new().data(data_list.to_vec()))
.x_axis(
Axis::new()
.data(data_list.to_vec())
.grid_index(1),
)
.x_axis(Axis::new().data(data_list.to_vec()).grid_index(1))
.y_axis(Axis::new())
.y_axis(Axis::new().grid_index(1))
.grid(Grid::new().bottom("60%"))
Expand Down

0 comments on commit 6dc200b

Please sign in to comment.