Skip to content

Commit

Permalink
feat: support nil value for bar chart
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Aug 24, 2023
1 parent 818657f commit 0eada91
Show file tree
Hide file tree
Showing 8 changed files with 420 additions and 3 deletions.
4 changes: 3 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
- [x] 支持scatter
- [x] 支持多图合并处理
- [ ] 支持空值处理(中间值为空值)
- [x] 支持candlestick
- [x] 支持candlestick
- [ ] 平均值线、最大最小值标记
- [ ] 柱状图指定单个背景色
137 changes: 137 additions & 0 deletions asset/bar_chart/nil_value.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
140 changes: 140 additions & 0 deletions asset/bar_chart/nil_value_json.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion charts-rs-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,15 @@ pub fn my_default(input: TokenStream) -> TokenStream {
.unwrap_or_else(|| &self.series_colors[0]);
let mut series_labels = vec![];
for (i, p) in series.data.iter().enumerate() {
let value = p.to_owned();
// nil value忽略
if value == NIL_VALUE {
continue;
}
let mut left = unit_width * (i + series.start_index) as f32 + bar_chart_margin;
left += (bar_width + bar_chart_gap) * index as f32;

let y = y_axis_values.get_offset_height(p.to_owned(), max_height);
let y = y_axis_values.get_offset_height(value, max_height);
c1.rect(Rect {
fill: Some(color),
left,
Expand Down
54 changes: 53 additions & 1 deletion src/charts/bar_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ impl BarChart {
#[cfg(test)]
mod tests {
use super::BarChart;
use crate::{Box, LegendCategory, SeriesCategory, THEME_ANT, THEME_DARK, THEME_GRAFANA};
use crate::{
Box, LegendCategory, SeriesCategory, NIL_VALUE, THEME_ANT, THEME_DARK, THEME_GRAFANA,
};
use pretty_assertions::assert_eq;
#[test]
fn bar_chart_basic() {
Expand Down Expand Up @@ -666,4 +668,54 @@ mod tests {
bar_chart.svg().unwrap()
);
}

#[test]
fn bar_chart_nil_value() {
let mut bar_chart = BarChart::new(
vec![
(
"Email",
vec![120.0, NIL_VALUE, 132.0, 101.0, 134.0, 90.0, 230.0],
)
.into(),
(
"Union Ads",
vec![220.0, 182.0, 191.0, NIL_VALUE, 290.0, 330.0, 310.0],
)
.into(),
(
"Direct",
vec![320.0, 332.0, 301.0, 334.0, 390.0, NIL_VALUE, 320.0],
)
.into(),
(
"Search Engine",
vec![NIL_VALUE, 932.0, 901.0, 934.0, 1290.0, 1330.0, 1320.0],
)
.into(),
],
vec![
"Mon".to_string(),
"Tue".to_string(),
"Wed".to_string(),
"Thu".to_string(),
"Fri".to_string(),
"Sat".to_string(),
"Sun".to_string(),
],
);
bar_chart.y_axis_configs[0].axis_width = Some(55.0);
bar_chart.title_text = "Bar Chart".to_string();
bar_chart.legend_margin = Some(Box {
top: 35.0,
bottom: 10.0,
..Default::default()
});
bar_chart.y_axis_configs[0].axis_formatter = Some("{c} ml".to_string());
bar_chart.series_list[0].label_show = true;
assert_eq!(
include_str!("../../asset/bar_chart/nil_value.svg"),
bar_chart.svg().unwrap()
);
}
}
8 changes: 8 additions & 0 deletions src/charts/params.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::NIL_VALUE;

use super::{Align, Box, Color, LegendCategory, Series, SeriesCategory, Theme, YAxisConfig};

pub(crate) fn get_bool_from_value(value: &serde_json::Value, key: &str) -> Option<bool> {
Expand Down Expand Up @@ -42,6 +44,9 @@ pub(crate) fn get_usize_slice_from_value(

pub(crate) fn get_f32_from_value(value: &serde_json::Value, key: &str) -> Option<f32> {
if let Some(value) = value.get(key) {
if value.is_null() {
return Some(NIL_VALUE);
}
if let Some(v) = value.as_f64() {
return Some(v as f32);
}
Expand All @@ -55,6 +60,9 @@ pub(crate) fn get_f32_slice_from_value(value: &serde_json::Value, key: &str) ->
values
.iter()
.map(|item| {
if item.is_null() {
return NIL_VALUE;
}
if let Some(v) = item.as_f64() {
v as f32
} else {
Expand Down
Loading

0 comments on commit 0eada91

Please sign in to comment.