Skip to content

Commit f6010a2

Browse files
jqnatividadclaude
andcommitted
fix: accept non-numeric span values in Violin
Type the violin `span` field as `NumOrStringCollection` instead of `Vec<f64>`. Plotly.js types `span` as an `info_array` of `any`, so date/category axis values must be allowed for manual KDE spans on non-numeric value axes. Numeric spans continue to work unchanged; add a test covering both numeric and date-string spans. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 6f9054f commit f6010a2

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

plotly/src/traces/violin.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
Dim, HoverInfo, Label, LegendGroupTitle, Line, Marker, Orientation, PlotType, Visible,
1313
XAxisId, YAxisId,
1414
},
15-
private::NumOrString,
15+
private::{NumOrString, NumOrStringCollection},
1616
Trace,
1717
};
1818

@@ -232,7 +232,7 @@ where
232232
#[serde(rename = "scalemode")]
233233
scale_mode: Option<ScaleMode>,
234234
side: Option<ViolinSide>,
235-
span: Option<Vec<f64>>,
235+
span: Option<NumOrStringCollection>,
236236
#[serde(rename = "spanmode")]
237237
span_mode: Option<SpanMode>,
238238
#[serde(rename = "quartilemethod")]
@@ -305,6 +305,22 @@ mod tests {
305305
assert_eq!(to_value(SpanMode::Manual).unwrap(), json!("manual"));
306306
}
307307

308+
#[test]
309+
fn serialize_span_numeric_and_dates() {
310+
// Numeric span (typical case).
311+
let numeric = Violin::new(vec![1, 2, 3]).span(vec![0., 10.]);
312+
assert_eq!(to_value(numeric).unwrap()["span"], json!([0.0, 10.0]));
313+
314+
// Date/string span for date-valued axes (plotly.js types `span` as `any`).
315+
let dated = Violin::new(vec![1, 2, 3])
316+
.span_mode(SpanMode::Manual)
317+
.span(vec!["2020-01-01", "2020-12-31"]);
318+
assert_eq!(
319+
to_value(dated).unwrap()["span"],
320+
json!(["2020-01-01", "2020-12-31"])
321+
);
322+
}
323+
308324
#[test]
309325
fn serialize_violin_side() {
310326
assert_eq!(to_value(ViolinSide::Both).unwrap(), json!("both"));

0 commit comments

Comments
 (0)