yuankunzhang / charming

A visualization library for Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom themes by deserializing JSON?

ynax opened this issue · comments

It would be nice to be able to use custom themes defined in a JSON or .toml file by deserialzing these into a custom theme struct.

A possible way to achieve this is by creating a CustomTheme struct with fields corresponding to the settings, while using preexisting structs from the crate. As those struct already has most of (if not all) of the needed fields, e.g the Line struct has line_style which is needed. The problem however is that the Line struct also has fields which are not required, nor wanted (?), for a theme struct - i.e the type_ (?) and the data field.

Is this something that is in the works? If wanted I could try to implement this, however, then there would be need for some guidance what needs to be done.

A small example of what I'm getting at..

use serde::{Serialize, Deserialize};

use charming::element::Color;
use charming::series::Line;

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CustomTheme {
    #[serde(skip_serializing_if = "Option::is_none")]
    color: Option<Vec<Color>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    line: Option<Line>,
    // and then more... 
}

let theme: &str = r#"
{
    "color": ["\#ff0000", "\#00ff00", "\#0000ff"],
    "line": {
         "lineStyle"  : {
               "width": 3
          },
          "symbolSize": 8
     }
}
"#;

let my_custom_theme: CustomTheme = serde_json::from_str(theme).unwrap();

// Then set theme....

I have a similar need, the received parameter is a json string, I can not directly Deserialize json to struct Chart because it does not support Deserialize, if the direct json string is rendered as a parameter, or all classes implement deserialize

At present, it is my own fork to add this method
image