Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom themes by deserializing JSON? #23

Open
ynax opened this issue Sep 3, 2023 · 3 comments
Open

Custom themes by deserializing JSON? #23

ynax opened this issue Sep 3, 2023 · 3 comments

Comments

@ynax
Copy link

ynax commented Sep 3, 2023

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....
@bajie-git
Copy link

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
Copy link

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

@LukaOber
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants