Skip to content

Commit

Permalink
Support saving image as PNG from the HTML page
Browse files Browse the repository at this point in the history
  • Loading branch information
yuankunzhang committed Jul 24, 2023
1 parent 15ff677 commit 18c136f
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 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.2.3"
version = "0.2.4"
edition = "2021"
authors = ["Yuankun Zhang <[email protected]>"]
homepage = "https://github.com/yuankunzhang/echarts-rs"
Expand Down
2 changes: 1 addition & 1 deletion charming/src/asset/charts.html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{{#each gep_maps}}
echarts.registerMap('{{ this.name }}', {{{ this.opt }}});
{{/each}}
var chart = echarts.init(document.getElementById('{{ chart_id }}'){{#if theme}}, '{{ theme }}'{{/if}});
var chart = echarts.init(document.getElementById('{{ chart_id }}'), {{#if theme}}'{{ theme }}'{{else}}null{{/if}}, { renderer: '{{ canvas_type }}' });
var option = {{{ chart_option }}};
chart.setOption(option);
</script>
Expand Down
7 changes: 7 additions & 0 deletions charming/src/component/toolbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,11 @@ impl Toolbox {
self.bottom = Some(bottom.into());
self
}

pub fn save_as_image_type(&self) -> Option<&SaveAsImageType> {
self.feature
.as_ref()
.and_then(|f| f.save_as_image.as_ref())
.and_then(|s| s.type_.as_ref())
}
}
10 changes: 8 additions & 2 deletions charming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ pub use renderer::*;

use component::{
AngleAxis, Aria, Axis, Axis3D, DataZoom, GeoMap, Grid, Grid3D, Legend, ParallelAxis,
ParallelCoordinate, PolarCoordinate, RadarCoordinate, RadiusAxis, SingleAxis, Title, Toolbox,
VisualMap,
ParallelCoordinate, PolarCoordinate, RadarCoordinate, RadiusAxis, SaveAsImageType, SingleAxis,
Title, Toolbox, VisualMap,
};
use datatype::Dataset;
use element::{process_raw_strings, AxisPointer, Color, MarkLine, Tooltip};
Expand Down Expand Up @@ -502,6 +502,12 @@ impl Chart {
self.geo_maps.push(map.into());
self
}

pub fn save_as_image_type(&self) -> Option<&SaveAsImageType> {
self.toolbox
.as_ref()
.and_then(|toolbox| toolbox.save_as_image_type())
}
}

impl ToString for Chart {
Expand Down
7 changes: 6 additions & 1 deletion charming/src/renderer/html_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use handlebars::Handlebars;

use crate::{theme::Theme, Chart, EchartsError};
use crate::{component::SaveAsImageType, theme::Theme, Chart, EchartsError};

pub struct HtmlRenderer {
title: String,
Expand All @@ -27,6 +27,10 @@ impl HtmlRenderer {
pub fn render(&self, chart: &Chart) -> Result<String, EchartsError> {
let template = include_str!("../asset/charts.html.hbs");
let (theme, theme_source) = self.theme.to_str();
let canvas_type = match chart.save_as_image_type() {
Some(&SaveAsImageType::Svg) => "svg".to_string(),
_ => "canvas".to_string(),
};
let data = Handlebars::new()
.render_template(
template,
Expand All @@ -37,6 +41,7 @@ impl HtmlRenderer {
"width": self.width,
"height": self.height,
"chart_id": "chart",
"canvas_type": canvas_type,
"chart_option": chart.to_string(),
}),
)
Expand Down
7 changes: 6 additions & 1 deletion gallery/src/aria/default_decal.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use charming::{
component::{Aria, Axis, Decal},
component::{Aria, Axis, Decal, Feature, SaveAsImage, SaveAsImageType, Toolbox},
element::AxisType,
series::Bar,
Chart,
};

pub fn chart() -> Chart {
Chart::new()
.toolbox(
Toolbox::new().feature(
Feature::new().save_as_image(SaveAsImage::new().type_(SaveAsImageType::Svg)),
),
)
.x_axis(
Axis::new()
.type_(AxisType::Category)
Expand Down

0 comments on commit 18c136f

Please sign in to comment.