use super::*;
impl DataValue {
pub fn as_number(&self) -> Option<f64> {
match self {
DataValue::Number(value) => Some(*value),
_ => None,
}
}
pub fn to_number(&self) -> Option<f64> {
match self {
DataValue::Number(value) => Some(*value),
DataValue::Text(text) => text.trim().parse::<f64>().ok(),
DataValue::Bool(value) => Some(if *value { 1.0 } else { 0.0 }),
_ => None,
}
}
pub fn as_text(&self) -> Option<&str> {
match self {
DataValue::Text(text) => Some(text.as_str()),
_ => None,
}
}
pub fn as_list(&self) -> Option<&[DataValue]> {
match self {
DataValue::List(values) => Some(values.as_slice()),
_ => None,
}
}
pub fn as_object(&self) -> Option<&[(String, DataValue)]> {
match self {
DataValue::Object(fields) => Some(fields.as_slice()),
_ => None,
}
}
pub fn get(&self, name: &str) -> Option<&DataValue> {
self.as_object()?
.iter()
.find(|(key, _): &&(String, DataValue)| key == name)
.map(|(_, value): &(String, DataValue)| value)
}
pub fn get_mut(&mut self, name: &str) -> Option<&mut DataValue> {
self.as_object_mut()?
.iter_mut()
.find(|(key, _): &&mut (String, DataValue)| key == name)
.map(|(_, value): &mut (String, DataValue)| value)
}
pub fn as_object_mut(&mut self) -> Option<&mut [(String, DataValue)]> {
match self {
DataValue::Object(fields) => Some(fields.as_mut_slice()),
_ => None,
}
}
pub fn is_null(&self) -> bool {
matches!(self, DataValue::Null)
}
}
impl From<f64> for DataValue {
fn from(value: f64) -> Self {
DataValue::Number(value)
}
}
impl From<i32> for DataValue {
fn from(value: i32) -> Self {
DataValue::Number(value as f64)
}
}
impl From<bool> for DataValue {
fn from(value: bool) -> Self {
DataValue::Bool(value)
}
}
impl From<&str> for DataValue {
fn from(value: &str) -> Self {
DataValue::Text(value.to_string())
}
}
impl From<String> for DataValue {
fn from(value: String) -> Self {
DataValue::Text(value)
}
}
impl From<Vec<DataValue>> for DataValue {
fn from(value: Vec<DataValue>) -> Self {
DataValue::List(value)
}
}
impl SeriesType {
pub fn name(&self) -> &'static str {
match self {
SeriesType::Line => SERIES_TYPE_LINE,
SeriesType::Bar => SERIES_TYPE_BAR,
SeriesType::Pie => SERIES_TYPE_PIE,
SeriesType::Scatter => SERIES_TYPE_SCATTER,
SeriesType::EffectScatter => SERIES_TYPE_EFFECT_SCATTER,
SeriesType::Radar => SERIES_TYPE_RADAR,
SeriesType::Gauge => SERIES_TYPE_GAUGE,
SeriesType::Funnel => SERIES_TYPE_FUNNEL,
SeriesType::Heatmap => SERIES_TYPE_HEATMAP,
SeriesType::Graph => SERIES_TYPE_GRAPH,
SeriesType::Treemap => SERIES_TYPE_TREEMAP,
SeriesType::Sunburst => SERIES_TYPE_SUNBURST,
SeriesType::Boxplot => SERIES_TYPE_BOXPLOT,
SeriesType::Candlestick => SERIES_TYPE_CANDLESTICK,
SeriesType::ThemeRiver => SERIES_TYPE_THEME_RIVER,
SeriesType::Sankey => SERIES_TYPE_SANKEY,
SeriesType::Parallel => SERIES_TYPE_PARALLEL,
SeriesType::PictorialBar => SERIES_TYPE_PICTORIAL_BAR,
SeriesType::Lines => SERIES_TYPE_LINES,
SeriesType::Map => SERIES_TYPE_MAP,
SeriesType::Custom => SERIES_TYPE_CUSTOM,
}
}
pub fn parse(name: &str) -> Option<Self> {
Some(match name {
SERIES_TYPE_LINE => SeriesType::Line,
SERIES_TYPE_BAR => SeriesType::Bar,
SERIES_TYPE_PIE => SeriesType::Pie,
SERIES_TYPE_SCATTER => SeriesType::Scatter,
SERIES_TYPE_EFFECT_SCATTER => SeriesType::EffectScatter,
SERIES_TYPE_RADAR => SeriesType::Radar,
SERIES_TYPE_GAUGE => SeriesType::Gauge,
SERIES_TYPE_FUNNEL => SeriesType::Funnel,
SERIES_TYPE_HEATMAP => SeriesType::Heatmap,
SERIES_TYPE_GRAPH => SeriesType::Graph,
SERIES_TYPE_TREEMAP => SeriesType::Treemap,
SERIES_TYPE_SUNBURST => SeriesType::Sunburst,
SERIES_TYPE_BOXPLOT => SeriesType::Boxplot,
SERIES_TYPE_CANDLESTICK => SeriesType::Candlestick,
SERIES_TYPE_THEME_RIVER => SeriesType::ThemeRiver,
SERIES_TYPE_SANKEY => SeriesType::Sankey,
SERIES_TYPE_PARALLEL => SeriesType::Parallel,
SERIES_TYPE_PICTORIAL_BAR => SeriesType::PictorialBar,
SERIES_TYPE_LINES => SeriesType::Lines,
SERIES_TYPE_MAP => SeriesType::Map,
SERIES_TYPE_CUSTOM => SeriesType::Custom,
_ => return None,
})
}
}
impl ChartOption {
pub fn new() -> Self {
Self {
animation: true,
..Self::default()
}
}
pub fn title(mut self, title: TitleOption) -> Self {
self.set_title(Some(title));
self
}
pub fn legend(mut self, legend: LegendOption) -> Self {
self.set_legend(Some(legend));
self
}
pub fn grid(mut self, grid: GridOption) -> Self {
self.set_grid(Some(grid));
self
}
pub fn x_axis(mut self, axis: AxisOption) -> Self {
self.get_mut_x_axis().push(axis);
self
}
pub fn y_axis(mut self, axis: AxisOption) -> Self {
self.get_mut_y_axis().push(axis);
self
}
pub fn polar(mut self, polar: PolarOption) -> Self {
self.get_mut_polar().push(polar);
self
}
pub fn radar(mut self, radar: RadarOption) -> Self {
self.get_mut_radar().push(radar);
self
}
pub fn geo(mut self, geo: GeoOption) -> Self {
self.get_mut_geo().push(geo);
self
}
pub fn calendar(mut self, calendar: CalendarOption) -> Self {
self.get_mut_calendar().push(calendar);
self
}
pub fn dataset(mut self, dataset: DatasetOption) -> Self {
self.set_dataset(Some(dataset));
self
}
pub fn series(mut self, series: SeriesOption) -> Self {
self.get_mut_series().push(series);
self
}
pub fn tooltip(mut self, tooltip: TooltipOption) -> Self {
self.set_tooltip(Some(tooltip));
self
}
pub fn toolbox(mut self, toolbox: ToolboxOption) -> Self {
self.set_toolbox(Some(toolbox));
self
}
pub fn data_zoom(mut self, data_zoom: DataZoomOption) -> Self {
self.get_mut_data_zoom().push(data_zoom);
self
}
pub fn visual_map(mut self, visual_map: VisualMapOption) -> Self {
self.get_mut_visual_map().push(visual_map);
self
}
pub fn graphic(mut self, graphic: GraphicOption) -> Self {
self.get_mut_graphic().push(graphic);
self
}
pub fn brush(mut self, brush: BrushOption) -> Self {
self.set_brush(Some(brush));
self
}
pub fn axis_pointer(mut self, axis_pointer: AxisPointerOption) -> Self {
self.set_axis_pointer(Some(axis_pointer));
self
}
pub fn color(mut self, color: Vec<String>) -> Self {
self.set_color(color);
self
}
pub fn add_color(mut self, color: &str) -> Self {
self.get_mut_color().push(color.to_string());
self
}
pub fn background_color(mut self, color: &str) -> Self {
self.set_background_color(Some(color.to_string()));
self
}
pub fn text_style(mut self, text_style: LabelOption) -> Self {
self.set_text_style(Some(text_style));
self
}
pub fn animation(mut self, animation: bool) -> Self {
self.set_animation(animation);
self
}
pub fn animation_duration(mut self, duration: f64) -> Self {
self.set_animation_duration(Some(duration));
self
}
}
impl SeriesOption {
pub fn line() -> Self {
Self {
kind: SeriesType::Line,
show: true,
..Self::default()
}
}
pub fn bar() -> Self {
Self {
kind: SeriesType::Bar,
show: true,
..Self::default()
}
}
pub fn pie() -> Self {
Self {
kind: SeriesType::Pie,
show: true,
..Self::default()
}
}
pub fn scatter() -> Self {
Self {
kind: SeriesType::Scatter,
show: true,
..Self::default()
}
}
pub fn effect_scatter() -> Self {
Self {
kind: SeriesType::EffectScatter,
show: true,
..Self::default()
}
}
pub fn radar() -> Self {
Self {
kind: SeriesType::Radar,
show: true,
..Self::default()
}
}
pub fn gauge() -> Self {
Self {
kind: SeriesType::Gauge,
show: true,
..Self::default()
}
}
pub fn funnel() -> Self {
Self {
kind: SeriesType::Funnel,
show: true,
..Self::default()
}
}
pub fn heatmap() -> Self {
Self {
kind: SeriesType::Heatmap,
show: true,
..Self::default()
}
}
pub fn graph() -> Self {
Self {
kind: SeriesType::Graph,
show: true,
..Self::default()
}
}
pub fn treemap() -> Self {
Self {
kind: SeriesType::Treemap,
show: true,
..Self::default()
}
}
pub fn sunburst() -> Self {
Self {
kind: SeriesType::Sunburst,
show: true,
..Self::default()
}
}
pub fn boxplot() -> Self {
Self {
kind: SeriesType::Boxplot,
show: true,
..Self::default()
}
}
pub fn candlestick() -> Self {
Self {
kind: SeriesType::Candlestick,
show: true,
..Self::default()
}
}
pub fn theme_river() -> Self {
Self {
kind: SeriesType::ThemeRiver,
show: true,
..Self::default()
}
}
pub fn sankey() -> Self {
Self {
kind: SeriesType::Sankey,
show: true,
..Self::default()
}
}
pub fn parallel() -> Self {
Self {
kind: SeriesType::Parallel,
show: true,
..Self::default()
}
}
pub fn pictorial_bar() -> Self {
Self {
kind: SeriesType::PictorialBar,
show: true,
..Self::default()
}
}
pub fn lines() -> Self {
Self {
kind: SeriesType::Lines,
show: true,
..Self::default()
}
}
pub fn map() -> Self {
Self {
kind: SeriesType::Map,
show: true,
..Self::default()
}
}
pub fn custom() -> Self {
Self {
kind: SeriesType::Custom,
show: true,
..Self::default()
}
}
pub fn name(mut self, name: &str) -> Self {
self.set_name(name.to_string());
self
}
pub fn data(mut self, data: Vec<DataValue>) -> Self {
self.set_data(data);
self
}
pub fn add_data(&mut self, value: DataValue) -> &mut Self {
self.get_mut_data().push(value);
self
}
pub fn stack(mut self, stack: &str) -> Self {
self.set_stack(Some(stack.to_string()));
self
}
pub fn x_axis_index(mut self, index: usize) -> Self {
self.set_x_axis_index(Some(index));
self
}
pub fn y_axis_index(mut self, index: usize) -> Self {
self.set_y_axis_index(Some(index));
self
}
pub fn item_style(mut self, item_style: ItemStyleOption) -> Self {
self.set_item_style(Some(item_style));
self
}
pub fn line_style(mut self, line_style: LineStyleOption) -> Self {
self.set_line_style(Some(line_style));
self
}
pub fn label(mut self, label: LabelOption) -> Self {
self.set_label(Some(label));
self
}
pub fn emphasis(mut self, emphasis: EmphasisOption) -> Self {
self.set_emphasis(Some(emphasis));
self
}
pub fn bar_options(mut self, options: BarOptions) -> Self {
self.set_bar(Some(options));
self
}
pub fn line_options(mut self, options: LineOptions) -> Self {
self.set_line(Some(options));
self
}
pub fn pie_options(mut self, options: PieOptions) -> Self {
self.set_pie(Some(options));
self
}
pub fn scatter_options(mut self, options: ScatterOptions) -> Self {
self.set_scatter(Some(options));
self
}
pub fn radar_options(mut self, options: RadarOptions) -> Self {
self.set_radar(Some(options));
self
}
pub fn gauge_options(mut self, options: GaugeOptions) -> Self {
self.set_gauge(Some(options));
self
}
pub fn funnel_options(mut self, options: FunnelOptions) -> Self {
self.set_funnel(Some(options));
self
}
pub fn heatmap_options(mut self, options: HeatmapOptions) -> Self {
self.set_heatmap(Some(options));
self
}
pub fn is_line_like(&self) -> bool {
matches!(
self.get_kind(),
SeriesType::Line | SeriesType::EffectScatter | SeriesType::ThemeRiver
)
}
pub fn has_area(&self) -> bool {
self.get_line()
.as_ref()
.and_then(|line: &LineOptions| line.area_style.as_ref())
.is_some()
}
}
impl AxisOption {
pub fn category() -> Self {
Self {
axis_type: AxisType::Category,
show: true,
..Self::default()
}
}
pub fn value() -> Self {
Self {
axis_type: AxisType::Value,
show: true,
..Self::default()
}
}
pub fn time() -> Self {
Self {
axis_type: AxisType::Time,
show: true,
..Self::default()
}
}
pub fn log() -> Self {
Self {
axis_type: AxisType::Log,
show: true,
..Self::default()
}
}
pub fn data(mut self, data: Vec<DataValue>) -> Self {
self.set_data(data);
self
}
pub fn name(mut self, name: &str) -> Self {
self.set_name(Some(name.to_string()));
self
}
pub fn axis_label(mut self, label: LabelOption) -> Self {
self.set_axis_label(Some(label));
self
}
pub fn axis_line(mut self, line: AxisLineOption) -> Self {
self.set_axis_line(Some(line));
self
}
pub fn axis_tick(mut self, tick: AxisTickOption) -> Self {
self.set_axis_tick(Some(tick));
self
}
pub fn split_line(mut self, line: SplitLineOption) -> Self {
self.set_split_line(Some(line));
self
}
pub fn min(mut self, min: f64) -> Self {
self.set_min(Some(min));
self
}
pub fn max(mut self, max: f64) -> Self {
self.set_max(Some(max));
self
}
}
impl TitleOption {
pub fn new(text: &str) -> Self {
Self {
text: text.to_string(),
show: true,
..Self::default()
}
}
pub fn subtext(mut self, subtext: &str) -> Self {
self.set_subtext(subtext.to_string());
self
}
}
impl LegendOption {
pub fn new(data: Vec<String>) -> Self {
Self {
data,
show: true,
..Self::default()
}
}
}
impl GridOption {
pub fn new() -> Self {
Self {
show: true,
..Self::default()
}
}
}
impl TooltipOption {
pub fn new() -> Self {
Self {
show: true,
..Self::default()
}
}
pub fn trigger(mut self, trigger: TriggerType) -> Self {
self.set_trigger(trigger);
self
}
}
impl LabelOption {
pub fn new() -> Self {
Self {
show: true,
..Self::default()
}
}
pub fn color(mut self, color: &str) -> Self {
self.set_color(Some(color.to_string()));
self
}
pub fn font_size(mut self, size: f64) -> Self {
self.set_font_size(Some(size));
self
}
pub fn font_family(mut self, family: &str) -> Self {
self.set_font_family(Some(family.to_string()));
self
}
}
impl ItemStyleOption {
pub fn filled(color: &str) -> Self {
Self {
color: Some(color.to_string()),
..Self::default()
}
}
}
impl LineStyleOption {
pub fn new(color: &str, width: f64) -> Self {
Self {
color: Some(color.to_string()),
width: Some(width),
..Self::default()
}
}
}