(core) Ensure chart summary is more detailed then linked summary

Summary:
When a summary table is linked, the column used for linking needs
to be included as a group-by column (or the linking can’t work). A
good example is here:
https://templates-s.getgrist.com/doc/investment-research/p/4

This was request by Dmitry here: https://grist.slack.com/archives/C04AYS9JF/p1649400119930389?thread_ts=1649338496.915759&cid=C04AYS9JF (2nd point)

Test Plan: Added new nbrowser test case.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Subscribers: georgegevoian

Differential Revision: https://phab.getgrist.com/D3408
This commit is contained in:
Cyprien P 2022-04-22 18:30:48 +02:00
parent e4d47a2f3c
commit f17e31c023

View File

@ -11,7 +11,7 @@ import {ColumnRec, ViewFieldRec, ViewSectionRec} from 'app/client/models/DocMode
import {reportError} from 'app/client/models/errors';
import {KoSaveableObservable, ObjObservable, setSaveValue} from 'app/client/models/modelUtil';
import {SortedRowSet} from 'app/client/models/rowset';
import {toPageWidget} from 'app/client/ui/PageWidgetPicker';
import {IPageWidget, toPageWidget} from 'app/client/ui/PageWidgetPicker';
import {cssLabel, cssRow, cssSeparator} from 'app/client/ui/RightPanel';
import {cssFieldEntry, cssFieldLabel, IField, VisibleFieldsConfig } from 'app/client/ui/VisibleFieldsConfig';
import {IconName} from 'app/client/ui2018/IconList';
@ -34,6 +34,7 @@ import defaults = require('lodash/defaults');
import defaultsDeep = require('lodash/defaultsDeep');
import isNumber = require('lodash/isNumber');
import sum = require('lodash/sum');
import union = require('lodash/union');
import {Annotations, Config, Data, Datum, ErrorBar, Layout, LayoutAxis, Margin} from 'plotly.js';
@ -882,6 +883,7 @@ export class ChartConfig extends GrainJSDisposable {
const pageWidget = toPageWidget(this._section);
pageWidget.summarize = !this._isSummaryTable();
pageWidget.columns = this._getColumnIds(colIds);
this._ensureValidLinkingIfAny(pageWidget);
const newSection = await this._gristDoc.saveViewSection(this._section, pageWidget);
return ChartConfig._instanceMap.get(newSection)!;
}
@ -889,9 +891,21 @@ export class ChartConfig extends GrainJSDisposable {
private async _setGroupByColumns(groupByCols: string[]) {
const pageWidget = toPageWidget(this._section);
pageWidget.columns = this._getColumnIds(groupByCols);
this._ensureValidLinkingIfAny(pageWidget);
return this._gristDoc.saveViewSection(this._section, pageWidget);
}
// If section is linked to a summary table, makes sure that pageWidget describes a summary table
// that is more detailed than the source summary table. Function mutates `pageWidget`.
private _ensureValidLinkingIfAny(pageWidget: IPageWidget) {
if (!pageWidget.summarize) { return; }
if (!this._section.linkSrcSection().getRowId()) { return; }
const srcPageWidget = toPageWidget(this._section.linkSrcSection());
pageWidget.columns = union(pageWidget.columns, srcPageWidget.columns);
}
// Returns column ids corresponding to each colIds in the selected table (or corresponding summary
// source table, if select table is a summary table).
private _getColumnIds(colIds: string[]) {
const cols = this._isSummaryTable() ?
this._section.table().summarySource().columns().all() :