mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-09 12:00:01 -06:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2afabb4f7e | ||
|
|
a2737739cc |
+37
-11
@@ -11,6 +11,10 @@ use crate::{ChartData, Rgba, SeriesData, TrendlineData};
|
|||||||
pub(crate) const MAX_POINTS: usize = 100_000;
|
pub(crate) const MAX_POINTS: usize = 100_000;
|
||||||
/// Cap on series per chart.
|
/// Cap on series per chart.
|
||||||
pub(crate) const MAX_SERIES: usize = 255;
|
pub(crate) const MAX_SERIES: usize = 255;
|
||||||
|
/// Cap on the points of all series of one chart together (values, X values and bubble sizes):
|
||||||
|
/// [`MAX_SERIES`] × [`MAX_POINTS`] would let a few short whole-column references retain and
|
||||||
|
/// draw tens of millions of points. Series past it get only what is left, or no points.
|
||||||
|
pub(crate) const MAX_CHART_POINTS: usize = 1_000_000;
|
||||||
/// Cap on trendlines drawn per series.
|
/// Cap on trendlines drawn per series.
|
||||||
pub(crate) const MAX_TRENDLINES: usize = 16;
|
pub(crate) const MAX_TRENDLINES: usize = 16;
|
||||||
|
|
||||||
@@ -77,10 +81,15 @@ fn union_parts(f: &str) -> Option<Vec<&str>> {
|
|||||||
/// Evaluates a series formula to a flat list of values (row-major), capped. A union of
|
/// Evaluates a series formula to a flat list of values (row-major), capped. A union of
|
||||||
/// references (`(A1:A3,A5)`, as PivotCharts use) is read part by part.
|
/// references (`(A1:A3,A5)`, as PivotCharts use) is read part by part.
|
||||||
fn eval_list(wb: &Workbook, sheet: usize, formula: &str) -> Vec<Value> {
|
fn eval_list(wb: &Workbook, sheet: usize, formula: &str) -> Vec<Value> {
|
||||||
eval_list_in(wb, sheet, formula, 0)
|
eval_list_in(wb, sheet, formula, 0, MAX_POINTS)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn eval_list_in(wb: &Workbook, sheet: usize, formula: &str, depth: u32) -> Vec<Value> {
|
/// [`eval_list`] keeping at most `cap` values (and never more than [`MAX_POINTS`]).
|
||||||
|
fn eval_list_capped(wb: &Workbook, sheet: usize, formula: &str, cap: usize) -> Vec<Value> {
|
||||||
|
eval_list_in(wb, sheet, formula, 0, cap.min(MAX_POINTS))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn eval_list_in(wb: &Workbook, sheet: usize, formula: &str, depth: u32, cap: usize) -> Vec<Value> {
|
||||||
let f = formula.trim();
|
let f = formula.trim();
|
||||||
if f.is_empty() {
|
if f.is_empty() {
|
||||||
return vec![];
|
return vec![];
|
||||||
@@ -90,16 +99,19 @@ fn eval_list_in(wb: &Workbook, sheet: usize, formula: &str, depth: u32) -> Vec<V
|
|||||||
{
|
{
|
||||||
let mut out = Vec::new();
|
let mut out = Vec::new();
|
||||||
for p in parts {
|
for p in parts {
|
||||||
if out.len() >= MAX_POINTS {
|
if out.len() >= cap {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let rest = MAX_POINTS - out.len();
|
let rest = cap - out.len();
|
||||||
out.extend(eval_list_in(wb, sheet, p, depth + 1).into_iter().take(rest));
|
out.extend(eval_list_in(wb, sheet, p, depth + 1, rest).into_iter().take(rest));
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
if cap == 0 {
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
match evaluate(wb, sheet, CellRef::default(), f) {
|
match evaluate(wb, sheet, CellRef::default(), f) {
|
||||||
Value::Array(a) => a.iter().take(MAX_POINTS).cloned().collect(),
|
Value::Array(a) => a.iter().take(cap).cloned().collect(),
|
||||||
v => vec![v],
|
v => vec![v],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -111,8 +123,11 @@ fn to_num(v: &Value) -> Option<f64> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn nums(wb: &Workbook, sheet: usize, formula: &str) -> Vec<Option<f64>> {
|
/// A series formula's numbers, at most `cap` of them; `cap` is reduced by what was taken.
|
||||||
eval_list(wb, sheet, formula).iter().map(to_num).collect()
|
fn nums(wb: &Workbook, sheet: usize, formula: &str, cap: &mut usize) -> Vec<Option<f64>> {
|
||||||
|
let out: Vec<Option<f64>> = eval_list_capped(wb, sheet, formula, *cap).iter().map(to_num).collect();
|
||||||
|
*cap -= out.len().min(*cap);
|
||||||
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Number format code of the first cell of a reference ("General" when not a reference).
|
/// Number format code of the first cell of a reference ("General" when not a reference).
|
||||||
@@ -228,15 +243,26 @@ pub fn resolve(wb: &Workbook, sheet: usize, chart: &Chart) -> ChartData {
|
|||||||
let mut series = Vec::new();
|
let mut series = Vec::new();
|
||||||
let mut categories: Option<Vec<String>> = None;
|
let mut categories: Option<Vec<String>> = None;
|
||||||
let mut category_levels = Vec::new();
|
let mut category_levels = Vec::new();
|
||||||
|
// One point budget for the whole chart, not one per series.
|
||||||
|
let mut budget = MAX_CHART_POINTS;
|
||||||
for (i, s) in chart.series.iter().take(MAX_SERIES).enumerate() {
|
for (i, s) in chart.series.iter().take(MAX_SERIES).enumerate() {
|
||||||
let values = nums(wb, sheet, &s.values);
|
let values = nums(wb, sheet, &s.values, &mut budget);
|
||||||
let name = s.name.as_deref().map(|n| series_name(wb, sheet, n)).filter(|n| !n.is_empty()).unwrap_or_else(|| format!("Series{}", i + 1));
|
let name = s.name.as_deref().map(|n| series_name(wb, sheet, n)).filter(|n| !n.is_empty()).unwrap_or_else(|| format!("Series{}", i + 1));
|
||||||
let color =
|
let color =
|
||||||
s.color.as_ref().and_then(|c| c.resolve(&wb.theme)).map(|[r, g, b]| [r, g, b, 0xFF]).unwrap_or_else(|| series_color(&wb.theme, i));
|
s.color.as_ref().and_then(|c| c.resolve(&wb.theme)).map(|[r, g, b]| [r, g, b, 0xFF]).unwrap_or_else(|| series_color(&wb.theme, i));
|
||||||
let kind = effective_kind(chart.kind, s.kind, i);
|
let kind = effective_kind(chart.kind, s.kind, i);
|
||||||
let x = match (&s.categories, numeric_x || matches!(kind, ChartKind::Scatter | ChartKind::ScatterLines | ChartKind::Bubble)) {
|
let x = match (&s.categories, numeric_x || matches!(kind, ChartKind::Scatter | ChartKind::ScatterLines | ChartKind::Bubble)) {
|
||||||
// X values with text in them (labels) are ignored: the points are numbered 1..n.
|
// X values with text in them (labels) are ignored: the points are numbered 1..n.
|
||||||
(Some(f), true) => Some(eval_list(wb, sheet, f)).filter(|xs| !xs.iter().any(Value::is_text)).map(|xs| xs.iter().map(to_num).collect()),
|
(Some(f), true) => {
|
||||||
|
let xs = eval_list_capped(wb, sheet, f, budget);
|
||||||
|
// Only X values that are kept are charged to the budget.
|
||||||
|
if xs.iter().any(Value::is_text) {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
budget -= xs.len().min(budget);
|
||||||
|
Some(xs.iter().map(to_num).collect())
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
if categories.is_none()
|
if categories.is_none()
|
||||||
@@ -248,7 +274,7 @@ pub fn resolve(wb: &Workbook, sheet: usize, chart: &Chart) -> ChartData {
|
|||||||
category_levels = outer;
|
category_levels = outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let sizes = s.bubble_sizes.as_deref().map(|f| nums(wb, sheet, f));
|
let sizes = s.bubble_sizes.as_deref().map(|f| nums(wb, sheet, f, &mut budget));
|
||||||
let trendlines = s
|
let trendlines = s
|
||||||
.trendlines
|
.trendlines
|
||||||
.iter()
|
.iter()
|
||||||
|
|||||||
@@ -1258,3 +1258,41 @@ fn box_whisker_draws_each_series_quartile_method() {
|
|||||||
// The boxes span 3.5 and 2.5 data units on the same value axis.
|
// The boxes span 3.5 and 2.5 data units on the same value axis.
|
||||||
assert!((heights[0] / heights[1] - 1.4).abs() < 0.001);
|
assert!((heights[0] / heights[1] - 1.4).abs() < 0.001);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn series_share_one_point_budget() {
|
||||||
|
// Many series over the same long column each get a share of one chart-wide budget, not
|
||||||
|
// `MAX_POINTS` apiece.
|
||||||
|
let mut wb = Workbook::new();
|
||||||
|
let s = wb.sheet_mut(0).unwrap();
|
||||||
|
s.set_value(CellRef::new(0, 0), Value::Number(1.0));
|
||||||
|
s.set_value(CellRef::new(59_999, 0), Value::Number(2.0));
|
||||||
|
let mut c = chart(ChartKind::ColumnClustered);
|
||||||
|
c.series = (0..40).map(|_| Series { values: "Sheet1!$A$1:$A$60000".into(), ..Default::default() }).collect();
|
||||||
|
let d = resolve(&wb, 0, &c);
|
||||||
|
assert_eq!(d.series.len(), 40);
|
||||||
|
assert_eq!(d.series[0].values.len(), 60_000);
|
||||||
|
let total: usize = d.series.iter().map(|s| s.values.len()).sum();
|
||||||
|
assert_eq!(total, crate::resolve::MAX_CHART_POINTS);
|
||||||
|
assert!(d.series.last().is_some_and(|s| s.values.is_empty()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rejected_text_x_values_are_not_charged_to_the_point_budget() {
|
||||||
|
// A text X range is ignored (the points are numbered), so repeating it in many series must not
|
||||||
|
// use up the chart's budget and leave the later series without values.
|
||||||
|
let mut wb = Workbook::new();
|
||||||
|
let s = wb.sheet_mut(0).unwrap();
|
||||||
|
for r in 0..100 {
|
||||||
|
s.set_value(CellRef::new(r, 0), Value::Number(f64::from(r)));
|
||||||
|
}
|
||||||
|
s.set_value(CellRef::new(0, 1), Value::text("label"));
|
||||||
|
s.set_value(CellRef::new(59_999, 1), Value::Number(2.0));
|
||||||
|
let mut c = chart(ChartKind::Scatter);
|
||||||
|
c.series = (0..40)
|
||||||
|
.map(|_| Series { values: "Sheet1!$A$1:$A$100".into(), categories: Some("Sheet1!$B$1:$B$60000".into()), ..Default::default() })
|
||||||
|
.collect();
|
||||||
|
let d = resolve(&wb, 0, &c);
|
||||||
|
assert_eq!(d.series.len(), 40);
|
||||||
|
assert!(d.series.iter().all(|s| s.values.len() == 100 && s.x.is_none()));
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,10 @@ use crate::io::esc;
|
|||||||
/// Widest or tallest chart written; larger anchors are clamped.
|
/// Widest or tallest chart written; larger anchors are clamped.
|
||||||
const MAX_SIDE: f32 = 16_384.0;
|
const MAX_SIDE: f32 = 16_384.0;
|
||||||
|
|
||||||
|
/// Most bytes of SVG written for the charts of one web page together. A chart past what is
|
||||||
|
/// left is left out of the page rather than grown without bound.
|
||||||
|
pub(crate) const MAX_PAGE_SVG_BYTES: usize = 64 * 1024 * 1024;
|
||||||
|
|
||||||
struct HelveticaMeasure;
|
struct HelveticaMeasure;
|
||||||
|
|
||||||
impl gridcraft_chart::Measure for HelveticaMeasure {
|
impl gridcraft_chart::Measure for HelveticaMeasure {
|
||||||
@@ -18,24 +22,34 @@ impl gridcraft_chart::Measure for HelveticaMeasure {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `chart` of sheet `sheet` as an `<svg>` element placed by `style`, or `None` for a degenerate size.
|
/// `chart` of sheet `sheet` as an `<svg>` element placed by `style`, or `None` for a degenerate
|
||||||
pub(crate) fn chart_svg(wb: &Workbook, sheet: usize, chart: &Chart, style: &str) -> Option<String> {
|
/// size or an element longer than `budget` bytes.
|
||||||
|
pub(crate) fn chart_svg(wb: &Workbook, sheet: usize, chart: &Chart, style: &str, budget: usize) -> Option<String> {
|
||||||
let (w, h) = (chart.anchor.width.min(MAX_SIDE), chart.anchor.height.min(MAX_SIDE));
|
let (w, h) = (chart.anchor.width.min(MAX_SIDE), chart.anchor.height.min(MAX_SIDE));
|
||||||
if !(w >= 1.0 && h >= 1.0) {
|
if !(w >= 1.0 && h >= 1.0) {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let data = gridcraft_chart::resolve(wb, sheet, chart);
|
let data = gridcraft_chart::resolve(wb, sheet, chart);
|
||||||
let prims = gridcraft_chart::render(chart, &data, w, h, &HelveticaMeasure);
|
let prims = gridcraft_chart::render(chart, &data, w, h, &HelveticaMeasure);
|
||||||
Some(prims_svg(&prims, w, h, style))
|
prims_svg_within(&prims, w, h, style, budget)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
fn prims_svg(prims: &[Prim], w: f32, h: f32, style: &str) -> String {
|
fn prims_svg(prims: &[Prim], w: f32, h: f32, style: &str) -> String {
|
||||||
|
prims_svg_within(prims, w, h, style, usize::MAX).unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The `<svg>` element, or `None` as soon as it grows past `budget` bytes.
|
||||||
|
fn prims_svg_within(prims: &[Prim], w: f32, h: f32, style: &str, budget: usize) -> Option<String> {
|
||||||
let mut out = String::new();
|
let mut out = String::new();
|
||||||
let _ = write!(
|
let _ = write!(
|
||||||
out,
|
out,
|
||||||
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{w}\" height=\"{h}\" viewBox=\"0 0 {w} {h}\" font-family=\"Helvetica,Arial,sans-serif\" style=\"{style}\">"
|
"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{w}\" height=\"{h}\" viewBox=\"0 0 {w} {h}\" font-family=\"Helvetica,Arial,sans-serif\" style=\"{style}\">"
|
||||||
);
|
);
|
||||||
for p in prims {
|
for p in prims {
|
||||||
|
if out.len() > budget {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
match p {
|
match p {
|
||||||
Prim::Rect { x, y, w, h, fill, stroke, radius } => {
|
Prim::Rect { x, y, w, h, fill, stroke, radius } => {
|
||||||
let _ = write!(out, "<rect x=\"{}\" y=\"{}\" width=\"{}\" height=\"{}\"", num(*x), num(*y), num(*w), num(*h));
|
let _ = write!(out, "<rect x=\"{}\" y=\"{}\" width=\"{}\" height=\"{}\"", num(*x), num(*y), num(*w), num(*h));
|
||||||
@@ -92,7 +106,7 @@ fn prims_svg(prims: &[Prim], w: f32, h: f32, style: &str) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
out.push_str("</svg>");
|
out.push_str("</svg>");
|
||||||
out
|
(out.len() <= budget).then_some(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fill and stroke attributes of an open shape element; the caller closes it.
|
/// Fill and stroke attributes of an open shape element; the caller closes it.
|
||||||
@@ -149,6 +163,17 @@ fn num(v: f32) -> String {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn an_svg_past_its_byte_budget_is_left_out() {
|
||||||
|
let rect = Prim::Rect { x: 1.0, y: 2.0, w: 3.0, h: 4.0, fill: Some([255, 0, 0, 255]), stroke: None, radius: 0.0 };
|
||||||
|
let prims = vec![rect; 10_000];
|
||||||
|
let full = prims_svg(&prims, 100.0, 50.0, "");
|
||||||
|
assert!(full.len() > 100_000);
|
||||||
|
assert_eq!(prims_svg_within(&prims, 100.0, 50.0, "", full.len()), Some(full.clone()));
|
||||||
|
assert_eq!(prims_svg_within(&prims, 100.0, 50.0, "", full.len() - 1), None);
|
||||||
|
assert_eq!(prims_svg_within(&prims, 100.0, 50.0, "", 1_000), None);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn every_primitive_becomes_an_svg_element() {
|
fn every_primitive_becomes_an_svg_element() {
|
||||||
let red = [255, 0, 0, 255];
|
let red = [255, 0, 0, 255];
|
||||||
|
|||||||
@@ -736,8 +736,10 @@ pub fn to_html(wb: &Workbook, sheet: usize) -> Result<String> {
|
|||||||
place(&im.anchor)
|
place(&im.anchor)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
let mut svg_budget = crate::chart_svg::MAX_PAGE_SVG_BYTES;
|
||||||
for ch in &sh.charts {
|
for ch in &sh.charts {
|
||||||
if let Some(svg) = crate::chart_svg::chart_svg(wb, sheet, ch, &place(&ch.anchor)) {
|
if let Some(svg) = crate::chart_svg::chart_svg(wb, sheet, ch, &place(&ch.anchor), svg_budget) {
|
||||||
|
svg_budget -= svg.len();
|
||||||
out.push_str(&svg);
|
out.push_str(&svg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user