charming icon indicating copy to clipboard operation
charming copied to clipboard

Custom themes by deserializing JSON?

Open ynax opened this issue 1 year ago • 2 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....

ynax avatar Sep 03 '23 16:09 ynax

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

bajie-git avatar Oct 25 '23 08:10 bajie-git

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

bajie-git avatar Oct 25 '23 09:10 bajie-git

Answering this thread for anyone who stumbles upon this in the future. You can use (echarts theme builder)[https://echarts.apache.org/en/theme-builder.html] to create a custom theme and use it with charming::theme::Custom. This library does not support creating a theme from json or toml at this time.

LukaOber avatar Oct 31 '24 08:10 LukaOber