mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-22 05:52:37 -06:00
#4389, #4390: drag a selected chart's data outline to change its range A selected chart now outlines its data as Excel does: series names in red, category labels in purple and the values as one blue block. The values block has a handle on each corner. Dragging one previews the new block, and the release points the chart at it as one undo step. Esc cancels. The new engine command chart.setValuesRange does the move. Each series keeps its formatting, a wider block adds series and a narrower one drops them, and names, category labels and bubble sizes that line up with the block follow it. PivotCharts and series that are not one block are refused. #4389: chart data drag review fixes - Category labels are compared as blocks, so a reference written with a leading = still counts as the same labels. - A resize keeps the orientation the series were read in, so a chart shrunk to one cell per series stays resizable. - The preview stays while edge scrolling moves the chart off screen. - A values corner hidden under frozen panes is not a handle. - The release takes the block from the pointer's last position, even when it moved in the release frame. - Esc cancels the drag with Ctrl or Cmd held too. Each fix has a test that fails without it. #4389: read a corner drag from the grabbed corner's own cell A values corner lies on a cell boundary, which belongs to the next row and column, so a press on the bottom-right corner of A1:A5 read as B6: releasing where the press was, or dragging straight down the edge of column A, changed the other axis too. The drag now records the offset from the press point to the centre of the corner's cell and reads the pointer through it, so the press point stays in its cell and each whole cell of movement moves the corner by one. #4389: snap a dragged chart data edge to the nearest gridline Reading the pointer from the middle of the grabbed corner's cell let the grabbed row's height decide when the block moved: with a tall row 5 and a short row 6, the pointer reached the bottom of row 6 while the block stayed at A1:A5. Each edge the corner moves now snaps to the gridline nearest the pointer, so every row and column changes over at its own middle. A release where the press was still changes nothing, since the corner lies on its own gridlines, and an edge dropped on its fixed edge keeps one row or column. #4389: drop a chart data drag when the chart's data changes under it A command run between frames (the control channel, a macro) could change the chart's values block while the button was held, and the release then put the dragged block over that change. The drag now checks, from the session, that the chart still plots the block it started from: a move drops a stale drag, and the release skips it.
178 lines
7.5 KiB
Rust
178 lines
7.5 KiB
Rust
//! A chart's data as blocks on the sheet, and `chart.setValuesRange`, which dragging a corner of a
|
|
//! selected chart's values outline runs (#4389, #4390).
|
|
|
|
use gridcraft_core::RangeRef;
|
|
use gridcraft_model::{Chart, Color};
|
|
use serde_json::json;
|
|
|
|
use super::s;
|
|
use crate::Session;
|
|
use crate::chart_data::{ChartData, chart_data};
|
|
|
|
fn r(a: &str) -> RangeRef {
|
|
RangeRef::parse(a).unwrap()
|
|
}
|
|
|
|
fn chart(s: &Session) -> Chart {
|
|
s.doc().unwrap().wb.active().unwrap().charts[0].clone()
|
|
}
|
|
|
|
fn data(s: &Session) -> Option<ChartData> {
|
|
chart_data(&s.doc().unwrap().wb, 0, &chart(s))
|
|
}
|
|
|
|
/// Points each plotted series has, as the chart resolves them.
|
|
fn points(s: &Session) -> Vec<usize> {
|
|
gridcraft_chart::resolve(&s.doc().unwrap().wb, 0, &chart(s)).series.iter().map(|se| se.values.len()).collect()
|
|
}
|
|
|
|
/// 10..80 down A1:A8, charted from A1:A5 only.
|
|
fn single_column() -> Session {
|
|
let mut s = s();
|
|
let values: Vec<_> = (1..=8).map(|i| json!([i * 10])).collect();
|
|
s.execute("range.setValues", json!({"range": "A1", "values": values})).unwrap();
|
|
s.execute("selection.set", json!({"range": "A1:A5"})).unwrap();
|
|
s.execute("insert.chart", json!({"type": "column"})).unwrap();
|
|
s
|
|
}
|
|
|
|
/// Region names across row 1, quarters down column A, charted from A1:C4.
|
|
fn table() -> Session {
|
|
let mut s = s();
|
|
s.execute(
|
|
"range.setValues",
|
|
json!({"range": "A1", "values": [
|
|
["", "North", "South", "East"],
|
|
["Q1", 10, 15, 12],
|
|
["Q2", 20, 25, 22],
|
|
["Q3", 30, 35, 32],
|
|
["Q4", 40, 45, 42],
|
|
["Q5", 50, 55, 52],
|
|
]}),
|
|
)
|
|
.unwrap();
|
|
s.execute("selection.set", json!({"range": "A1:C4"})).unwrap();
|
|
s.execute("insert.chart", json!({"type": "column"})).unwrap();
|
|
s
|
|
}
|
|
|
|
#[test]
|
|
fn a_plain_column_chart_is_one_values_block() {
|
|
let s = single_column();
|
|
assert_eq!(data(&s), Some(ChartData { sheet: 0, values: r("A1:A5"), by_rows: false, categories: None, names: None }));
|
|
}
|
|
|
|
#[test]
|
|
fn a_table_chart_has_names_categories_and_values_blocks() {
|
|
let s = table();
|
|
assert_eq!(data(&s), Some(ChartData { sheet: 0, values: r("B2:C4"), by_rows: false, categories: Some(r("A2:A4")), names: Some(r("B1:C1")) }));
|
|
}
|
|
|
|
#[test]
|
|
fn dragging_the_values_down_plots_the_new_rows_and_undo_puts_them_back() {
|
|
let mut s = single_column();
|
|
assert_eq!(points(&s), [5]);
|
|
let out = s.execute("chart.setValuesRange", json!({"range": "A1:A8"})).unwrap();
|
|
assert_eq!(out["values"], "Sheet1!A1:A8");
|
|
assert_eq!(chart(&s).series[0].values, "Sheet1!$A$1:$A$8");
|
|
assert_eq!(points(&s), [8]);
|
|
assert_eq!(data(&s).unwrap().values, r("A1:A8"));
|
|
s.execute("edit.undo", json!({})).unwrap();
|
|
assert_eq!(points(&s), [5]);
|
|
assert_eq!(chart(&s).series[0].values, "Sheet1!$A$1:$A$5");
|
|
}
|
|
|
|
#[test]
|
|
fn a_wider_block_adds_series_and_existing_series_keep_their_formatting() {
|
|
let mut s = table();
|
|
s.execute("chart.formatSelection", json!({"series": 1, "color": "#C00000"})).unwrap();
|
|
let south = chart(&s).series[1].color;
|
|
assert!(south.is_some());
|
|
s.execute("chart.setValuesRange", json!({"range": "B2:D6"})).unwrap();
|
|
let c = chart(&s);
|
|
assert_eq!(c.series.len(), 3);
|
|
assert_eq!(c.series[1].color, south, "South keeps its colour");
|
|
assert_eq!(c.series[2].color, None::<Color>, "the new series takes the automatic colour");
|
|
let names: Vec<_> = c.series.iter().map(|se| se.name.clone().unwrap()).collect();
|
|
assert_eq!(names, ["Sheet1!$B$1", "Sheet1!$C$1", "Sheet1!$D$1"], "names follow along the header row");
|
|
assert!(c.series.iter().all(|se| se.categories.as_deref() == Some("Sheet1!$A$2:$A$6")), "labels span the new points");
|
|
assert_eq!(c.series[2].values, "Sheet1!$D$2:$D$6");
|
|
assert_eq!(c.source.as_deref(), Some("Sheet1!A1:D6"), "Switch Row/Column sees the moved blocks");
|
|
assert_eq!(points(&s), [5, 5, 5]);
|
|
assert_eq!(data(&s), Some(ChartData { sheet: 0, values: r("B2:D6"), by_rows: false, categories: Some(r("A2:A6")), names: Some(r("B1:D1")) }));
|
|
// A narrower block drops the last series.
|
|
s.execute("chart.setValuesRange", json!({"range": "B2:B3"})).unwrap();
|
|
let c = chart(&s);
|
|
assert_eq!(c.series.len(), 1);
|
|
assert_eq!(c.series[0].categories.as_deref(), Some("Sheet1!$A$2:$A$3"));
|
|
s.execute("chart.switchRowColumn", json!({})).unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn series_in_rows_grow_across_the_columns() {
|
|
let mut s = s();
|
|
s.execute("range.setValues", json!({"range": "A1", "values": [[1, 2, 3, 4, 5, 6, 7], [7, 6, 5, 4, 3, 2, 1]]})).unwrap();
|
|
s.execute("selection.set", json!({"range": "A1:E2"})).unwrap();
|
|
s.execute("insert.chart", json!({"type": "line"})).unwrap();
|
|
assert_eq!(data(&s).map(|d| (d.values, d.by_rows)), Some((r("A1:E2"), true)));
|
|
s.execute("chart.setValuesRange", json!({"range": "A1:G2"})).unwrap();
|
|
assert_eq!(points(&s), [7, 7]);
|
|
assert_eq!(chart(&s).series[1].values, "Sheet1!$A$2:$G$2");
|
|
}
|
|
|
|
/// Changes the first chart on the first sheet in place, as a file can leave it.
|
|
fn edit_chart(s: &mut Session, f: impl FnOnce(&mut Chart)) {
|
|
let wb = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb);
|
|
f(&mut wb.sheet_mut(0).unwrap().charts[0]);
|
|
}
|
|
|
|
#[test]
|
|
fn category_labels_written_with_and_without_equals_are_one_block() {
|
|
let mut s = table();
|
|
edit_chart(&mut s, |c| c.series[1].categories = Some("=Sheet1!$A$2:$A$4".into()));
|
|
assert_eq!(data(&s).unwrap().categories, Some(r("A2:A4")));
|
|
s.execute("chart.setValuesRange", json!({"range": "B2:C6"})).unwrap();
|
|
assert!(chart(&s).series.iter().all(|se| se.categories.as_deref() == Some("Sheet1!$A$2:$A$6")), "both series' labels follow");
|
|
}
|
|
|
|
#[test]
|
|
fn series_read_in_rows_stay_resizable_after_shrinking_to_one_cell_each() {
|
|
let mut s = s();
|
|
s.execute("range.setValues", json!({"range": "A1", "values": [[1, 2, 3, 4, 5], [5, 4, 3, 2, 1]]})).unwrap();
|
|
s.execute("selection.set", json!({"range": "A1:E2"})).unwrap();
|
|
s.execute("insert.chart", json!({"type": "line"})).unwrap();
|
|
// A file can carry series in rows without saying so; the layout is read from the series.
|
|
edit_chart(&mut s, |c| c.by_rows = false);
|
|
assert_eq!(data(&s).map(|d| d.by_rows), Some(true));
|
|
s.execute("chart.setValuesRange", json!({"range": "A1:A2"})).unwrap();
|
|
assert!(chart(&s).by_rows);
|
|
assert_eq!(data(&s).map(|d| (d.values, d.by_rows)), Some((r("A1:A2"), true)), "still one block, still in rows");
|
|
s.execute("chart.setValuesRange", json!({"range": "A1:C2"})).unwrap();
|
|
assert_eq!(points(&s), [3, 3]);
|
|
}
|
|
|
|
#[test]
|
|
fn set_values_range_refuses_what_it_cannot_do() {
|
|
let mut s = table();
|
|
let before = chart(&s);
|
|
for (params, why) in [
|
|
(json!({}), "missing `range`"),
|
|
(json!({"range": "not a range"}), "not a range"),
|
|
(json!({"range": "A1:Z100000"}), "too large"),
|
|
(json!({"range": "A1:JA2"}), "too large"),
|
|
(json!({"range": "Sheet2!B2:C4"}), ""),
|
|
(json!({"chart": 999, "range": "B2:C4"}), "chart"),
|
|
] {
|
|
let err = s.execute("chart.setValuesRange", params.clone()).unwrap_err();
|
|
assert!(err.to_string().contains(why), "{params}: {err}");
|
|
assert_eq!(chart(&s), before, "{params} left the chart as it was");
|
|
}
|
|
// Series with a gap between them are not one block.
|
|
s.execute("selection.set", json!({"range": "A1:D4"})).unwrap();
|
|
s.execute("chart.selectData", json!({"range": "A1:D4"})).unwrap();
|
|
s.execute("chart.selectData", json!({"removeSeries": [1]})).unwrap();
|
|
assert_eq!(data(&s), None);
|
|
let err = s.execute("chart.setValuesRange", json!({"range": "B2:C4"})).unwrap_err();
|
|
assert!(err.to_string().contains("not one block"), "{err}");
|
|
}
|