Skip to content

Instantly share code, notes, and snippets.

@sangelxyz
Last active November 15, 2024 07:04
Show Gist options
  • Save sangelxyz/7aa34ff7730d5f441918c82936a77347 to your computer and use it in GitHub Desktop.
Save sangelxyz/7aa34ff7730d5f441918c82936a77347 to your computer and use it in GitHub Desktop.
rust echarts graphic
// Add to echarts struct
// graphic: Vec<Graphic>
// add graphic: vec![],
// to chart > new
// add to implementation
pub fn graphic<I>(mut self, graphic: I) -> Self
where
I: IntoIterator<Item = Graphic>,
{
self.graphic.extend(graphic);
self
}
// copy this in to the component/graphic.rs
use serde::{Serialize, Serializer};
#[derive(Serialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum GraphicType {
Image,
// https://echarts.apache.org/en/option.html#graphic.elements-image
Text,
// https://echarts.apache.org/en/option.html#graphic.elements-text
Circle,
// https://echarts.apache.org/en/option.html#graphic.elements-circle
Sector,
// https://echarts.apache.org/en/option.html#graphic.elements-sector
Ring,
// https://echarts.apache.org/en/option.html#graphic.elements-ring
Polygon,
// https://echarts.apache.org/en/option.html#graphic.elements-polygon
Polyline,
// https://echarts.apache.org/en/option.html#graphic.elements-polyline
Rect,
// https://echarts.apache.org/en/option.html#graphic.elements-rect
Line,
// https://echarts.apache.org/en/option.html#graphic.elements-line
BezierCurve,
// https://echarts.apache.org/en/option.html#graphic.elements-bezierCurve
Arc,
// https://echarts.apache.org/en/option.html#graphic.elements-arc
Group,
// https://echarts.apache.org/en/option.html#graphic.elements-group
}
#[derive(Debug)]
pub enum Value {
String(String),
Int(i32),
Float(f32),
}
impl From<String> for Value {
fn from(value: String) -> Self {
Value::String(value)
}
}
impl From<i32> for Value {
fn from(value: i32) -> Self {
Value::Int(value)
}
}
impl From<f32> for Value {
fn from(value: f32) -> Self {
Value::Float(value)
}
}
impl Serialize for Value {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Value::String(s) => serializer.serialize_str(s),
Value::Int(i) => serializer.serialize_i32(*i),
Value::Float(f) => serializer.serialize_f32(*f),
}
}
}
impl From<&str> for Value {
fn from(value: &str) -> Self {
Value::String(value.to_string())
}
}
#[derive(Serialize, Debug)]
pub struct GraphicStyle {
#[serde(skip_serializing_if = "Option::is_none")]
pub fill: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub font: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub stroke: Option<String>,
#[serde(rename = "lineWidth")]
#[serde(skip_serializing_if = "Option::is_none")]
line_width: Option<i32>,
}
impl GraphicStyle {
pub fn new() -> Self {
GraphicStyle {
fill: None,
text: None,
font: None,
stroke: None,
line_width: None,
}
}
pub fn stroke<S: Into<String>>(mut self, stroke: S) -> Self {
self.stroke = Some(stroke.into());
self
}
pub fn line_width<I: Into<i32>>(mut self, width: I) -> Self {
self.line_width = Some(width.into());
self
}
pub fn fill<S: Into<String>>(mut self, fill: S) -> Self {
self.fill = Some(fill.into());
self
}
pub fn text<S: Into<String>>(mut self, text: S) -> Self {
self.text = Some(text.into());
self
}
pub fn font<S: Into<String>>(mut self, font: S) -> Self {
self.font = Some(font.into());
self
}
}
#[derive(Serialize, Debug)]
pub struct Graphic {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub z: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub right: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub left: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bottom: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "type")]
pub type_: Option<GraphicType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub children: Option<Vec<Graphic>>, // TODO: Check if this could be a single item that's not a Vec.
#[serde(skip_serializing_if = "Option::is_none")]
pub shape: Option<GraphicShape>,
#[serde(skip_serializing_if = "Option::is_none")]
pub style: Option<GraphicStyle>,
}
///! Shape, values can be negative.
#[derive(Serialize, Debug)]
pub struct GraphicShape {
#[serde(skip_serializing_if = "Option::is_none")]
pub x1: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub y1: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub x2: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub y2: Option<i32>,
}
impl GraphicShape {
pub fn new() -> Self {
GraphicShape {
x1: None,
y1: None,
x2: None,
y2: None,
}
}
pub fn x1<I: Into<i32>>(mut self, v: I) -> Self {
self.x1 = Some(v.into());
self
}
pub fn x2<I: Into<i32>>(mut self, v: I) -> Self {
self.x2 = Some(v.into());
self
}
pub fn y1<I: Into<i32>>(mut self, v: I) -> Self {
self.x2 = Some(v.into());
self
}
pub fn y2<I: Into<i32>>(mut self, v: I) -> Self {
self.x2 = Some(v.into());
self
}
}
impl Graphic {
pub fn new() -> Self {
Self {
id: None,
z: None,
right: None,
bottom: None,
top: None,
left: None,
type_: None,
children: None,
shape: None,
style: None,
}
}
pub fn shape<F: Into<GraphicShape>>(mut self, shape: F) -> Self {
self.shape = Some(shape.into());
self
}
pub fn style<F: Into<GraphicStyle>>(mut self, style: F) -> Self {
self.style = Some(style.into());
self
}
pub fn type_<S: Into<GraphicType>>(mut self, type_: S) -> Self {
self.type_ = Some(type_.into());
self
}
pub fn id<S: Into<String>>(mut self, id: S) -> Self {
self.id = Some(id.into());
self
}
pub fn z<I: Into<i32>>(mut self, z: I) -> Self {
self.z = Some(z.into());
self
}
pub fn right<S: Into<Value>>(mut self, right: S) -> Self {
self.right = Some(right.into());
self
}
pub fn bottom<S: Into<Value>>(mut self, bottom: S) -> Self {
self.bottom = Some(bottom.into());
self
}
pub fn left<S: Into<Value>>(mut self, left: S) -> Self {
self.left = Some(left.into());
self
}
pub fn top<V: Into<Value>>(mut self, top: V) -> Self {
self.top = Some(top.into());
self
}
pub fn children<C: Into<Graphic>>(mut self, child: C) -> Self {
// If `self.children` is `None`, initialize it with an empty `Vec`
self.children
.get_or_insert_with(Vec::new)
.push(child.into());
self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment