2022-07-04 14:14:55 +00:00
|
|
|
import BaseView from 'app/client/components/BaseView';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {GristDoc} from 'app/client/components/GristDoc';
|
2021-11-22 08:45:48 +00:00
|
|
|
import {consolidateValues, formatPercent, sortByXValues, splitValuesByIndex,
|
|
|
|
uniqXValues} from 'app/client/lib/chartUtil';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {Delay} from 'app/client/lib/Delay';
|
|
|
|
import {Disposable} from 'app/client/lib/dispose';
|
|
|
|
import {fromKoSave} from 'app/client/lib/fromKoSave';
|
|
|
|
import {loadPlotly, PlotlyType} from 'app/client/lib/imports';
|
2022-07-04 14:14:55 +00:00
|
|
|
import DataTableModel from 'app/client/models/DataTableModel';
|
2022-01-17 16:08:43 +00:00
|
|
|
import {ColumnRec, ViewFieldRec, ViewSectionRec} from 'app/client/models/DocModel';
|
2020-10-12 20:00:56 +00:00
|
|
|
import {reportError} from 'app/client/models/errors';
|
2022-02-24 08:33:22 +00:00
|
|
|
import {KoSaveableObservable, ObjObservable, setSaveValue} from 'app/client/models/modelUtil';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {SortedRowSet} from 'app/client/models/rowset';
|
2022-04-22 16:30:48 +00:00
|
|
|
import {IPageWidget, toPageWidget} from 'app/client/ui/PageWidgetPicker';
|
2022-08-08 13:32:50 +00:00
|
|
|
import {cssLabel, cssRow, cssSeparator} from 'app/client/ui/RightPanelStyles';
|
2021-09-15 08:51:18 +00:00
|
|
|
import {cssFieldEntry, cssFieldLabel, IField, VisibleFieldsConfig } from 'app/client/ui/VisibleFieldsConfig';
|
2022-04-07 10:10:58 +00:00
|
|
|
import {IconName} from 'app/client/ui2018/IconList';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {squareCheckbox} from 'app/client/ui2018/checkbox';
|
2022-09-06 01:51:57 +00:00
|
|
|
import {theme, vars} from 'app/client/ui2018/cssVars';
|
2021-09-15 08:51:18 +00:00
|
|
|
import {cssDragger} from 'app/client/ui2018/draggableList';
|
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
2022-04-07 10:10:58 +00:00
|
|
|
import {IOptionFull, linkSelect, menu, menuItem, menuText, select} from 'app/client/ui2018/menus';
|
2022-02-08 15:23:14 +00:00
|
|
|
import {nativeCompare, unwrap} from 'app/common/gutil';
|
2022-03-18 09:57:54 +00:00
|
|
|
import {Sort} from 'app/common/SortSpec';
|
2021-11-22 08:45:48 +00:00
|
|
|
import {BaseFormatter} from 'app/common/ValueFormatter';
|
2021-09-30 07:41:36 +00:00
|
|
|
import {decodeObject} from 'app/plugin/objtypes';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {Events as BackboneEvents} from 'backbone';
|
2022-01-17 16:08:43 +00:00
|
|
|
import {Computed, dom, DomContents, DomElementArg, fromKo, Disposable as GrainJSDisposable,
|
2022-02-08 15:23:14 +00:00
|
|
|
IDisposable, IOption, makeTestId, Observable, styled, UseCB} from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as ko from 'knockout';
|
2021-11-22 08:45:48 +00:00
|
|
|
import clamp = require('lodash/clamp');
|
2020-10-02 15:10:00 +00:00
|
|
|
import debounce = require('lodash/debounce');
|
|
|
|
import defaults = require('lodash/defaults');
|
|
|
|
import defaultsDeep = require('lodash/defaultsDeep');
|
2021-11-22 08:45:48 +00:00
|
|
|
import isNumber = require('lodash/isNumber');
|
2022-09-06 01:51:57 +00:00
|
|
|
import merge = require('lodash/merge');
|
2021-11-22 08:45:48 +00:00
|
|
|
import sum = require('lodash/sum');
|
2022-04-22 16:30:48 +00:00
|
|
|
import union = require('lodash/union');
|
2022-08-02 14:54:54 +00:00
|
|
|
import type {Annotations, Config, Datum, ErrorBar, Layout, LayoutAxis, Margin,
|
|
|
|
PlotData as PlotlyPlotData} from 'plotly.js';
|
2022-10-28 16:11:08 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
2021-11-22 08:45:48 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
let Plotly: PlotlyType;
|
|
|
|
|
|
|
|
// When charting multiple series based on user data, limit the number of series given to plotly.
|
|
|
|
const MAX_SERIES_IN_CHART = 100;
|
2021-11-22 08:45:48 +00:00
|
|
|
const DONUT_DEFAULT_HOLE_SIZE = 0.75;
|
|
|
|
const DONUT_DEFAULT_TEXT_SIZE = 24;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
const testId = makeTestId('test-chart-');
|
|
|
|
|
2022-12-09 15:46:03 +00:00
|
|
|
const t = makeT('ChartView');
|
2022-10-28 16:11:08 +00:00
|
|
|
|
2021-11-22 08:45:48 +00:00
|
|
|
function isPieLike(chartType: string) {
|
|
|
|
return ['pie', 'donut'].includes(chartType);
|
|
|
|
}
|
|
|
|
|
2022-10-12 15:33:36 +00:00
|
|
|
function firstFieldIsLabels(chartType: string) {
|
|
|
|
return ['pie', 'donut', 'kaplan_meier', 'scatter'].includes(chartType);
|
|
|
|
}
|
|
|
|
|
2022-01-17 16:08:43 +00:00
|
|
|
export function isNumericOnly(chartType: string) {
|
|
|
|
return ['bar', 'pie', 'donut', 'kaplan_meier', 'line', 'area', 'scatter'].includes(chartType);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the type of the visibleCol if col is of type `Ref`, otherwise returns the type of col.
|
|
|
|
function visibleColType(col: ColumnRec, use: UseCB = unwrap) {
|
|
|
|
const colType = use(col.pureType);
|
|
|
|
const isRef = colType === 'Ref';
|
|
|
|
return isRef ? use(use(col.visibleColModel).type) : colType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if col is one of 'Numeric', 'Int', 'Any'.
|
|
|
|
export function isNumericLike(col: ColumnRec, use: UseCB = unwrap) {
|
|
|
|
const colType = visibleColType(col, use);
|
|
|
|
return ['Numeric', 'Int', 'Any'].includes(colType);
|
|
|
|
}
|
|
|
|
|
2021-11-22 08:45:48 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
interface ChartOptions {
|
|
|
|
multiseries?: boolean;
|
|
|
|
lineConnectGaps?: boolean;
|
|
|
|
lineMarkers?: boolean;
|
2022-03-18 09:57:54 +00:00
|
|
|
stacked?: boolean;
|
2020-10-02 15:10:00 +00:00
|
|
|
invertYAxis?: boolean;
|
|
|
|
logYAxis?: boolean;
|
|
|
|
// If "symmetric", one series after each Y series gives the length of the error bars around it. If
|
|
|
|
// "separate", two series after each Y series give the length of the error bars above and below it.
|
|
|
|
errorBars?: 'symmetric' | 'separate';
|
2021-11-22 08:45:48 +00:00
|
|
|
donutHoleSize?: number;
|
|
|
|
showTotal?: boolean;
|
|
|
|
textSize?: number;
|
2022-02-24 08:33:22 +00:00
|
|
|
isXAxisUndefined?: boolean;
|
2022-03-18 10:30:12 +00:00
|
|
|
orientation?: 'v'|'h';
|
2022-04-07 10:10:58 +00:00
|
|
|
aggregate?: boolean;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tslint:disable:no-console
|
|
|
|
|
|
|
|
// We use plotly's Datum to describe the type of values in cells. Cells may not match this
|
|
|
|
// perfectly, but it's helpful for type-checking anyway.
|
|
|
|
type RowPropGetter = (rowId: number) => Datum;
|
|
|
|
|
|
|
|
// We convert Grist data to a list of Series first, from which we then construct Plotly traces.
|
|
|
|
interface Series {
|
|
|
|
label: string; // Corresponds to the column name.
|
|
|
|
group?: Datum; // The group value, when grouped.
|
|
|
|
values: Datum[];
|
2022-03-18 09:57:54 +00:00
|
|
|
isInSortSpec?: boolean; // Whether this series is present in sort spec for this chart.
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSeriesName(series: Series, haveMultiple: boolean) {
|
2022-01-11 10:19:34 +00:00
|
|
|
if (series.group === undefined) {
|
2020-10-02 15:10:00 +00:00
|
|
|
return series.label;
|
2022-01-11 10:19:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Let's show [Blank] instead of leaving the name empty for that series. There is a possibility
|
|
|
|
// to confuse user between a blank cell and a cell holding the `[Blank]` value. But that is rare
|
|
|
|
// enough, and confusion can easily be removed by the chart creator by editing blank cells
|
|
|
|
// directly in the the table to put something more meaningful instead.
|
|
|
|
const groupName = series.group === '' ? '[Blank]' : series.group;
|
|
|
|
if (haveMultiple) {
|
|
|
|
return `${groupName} \u2022 ${series.label}`; // the unicode character is "black circle"
|
2020-10-02 15:10:00 +00:00
|
|
|
} else {
|
2022-01-11 10:19:34 +00:00
|
|
|
return String(groupName);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 14:54:54 +00:00
|
|
|
type Data = Partial<PlotlyPlotData>;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// The output of a ChartFunc. Normally it just returns one or more Data[] series, but sometimes it
|
|
|
|
// includes layout information: e.g. a "Scatter Plot" returns a Layout with axis labels.
|
|
|
|
interface PlotData {
|
|
|
|
data: Data[];
|
|
|
|
layout?: Partial<Layout>;
|
|
|
|
config?: Partial<Config>;
|
|
|
|
}
|
|
|
|
|
2021-10-14 15:21:46 +00:00
|
|
|
// Data options to pass to chart functions.
|
2021-11-22 08:45:48 +00:00
|
|
|
interface DataOptions extends Data {
|
2021-10-14 15:21:46 +00:00
|
|
|
|
|
|
|
// Allows to set the pie sort option (see: https://plotly.com/javascript/reference/pie/#pie-sort).
|
|
|
|
// Supports pie charts only.
|
|
|
|
sort?: boolean;
|
2021-11-22 08:45:48 +00:00
|
|
|
|
|
|
|
// Formatter to be used for the total inside donut charts.
|
|
|
|
totalFormatter?: BaseFormatter;
|
2021-10-14 15:21:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Convert a list of Series into a set of Plotly traces.
|
2021-10-14 15:21:46 +00:00
|
|
|
type ChartFunc = (series: Series[], options: ChartOptions, dataOptions?: DataOptions) => PlotData;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Helper for converting numeric Date/DateTime values (seconds since Epoch) to JS Date objects for
|
|
|
|
// use with plotly.
|
|
|
|
function dateGetter(getter: RowPropGetter): RowPropGetter {
|
|
|
|
return (r: number) => {
|
|
|
|
// 0's will turn into nulls, and non-numbers will turn into NaNs and then nulls. This prevents
|
|
|
|
// Plotly from including 1970-01-01 onto X axis, which usually makes the plot useless.
|
|
|
|
const val = (getter(r) as number) * 1000;
|
|
|
|
// Plotly recommends using strings for dates rather than Date objects or timestamps. They are
|
|
|
|
// interpreted more consistently. See https://github.com/plotly/plotly.js/issues/1532#issuecomment-290420534.
|
|
|
|
return val ? new Date(val).toISOString() : null;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-30 07:41:36 +00:00
|
|
|
|
|
|
|
// List of column types whose values are encoded has list, ie: ['L', 'foo', ...]. Such values
|
|
|
|
// require special treatment to show correctly in charts.
|
|
|
|
const LIST_TYPES = ['ChoiceList', 'RefList'];
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* ChartView component displays created charts.
|
|
|
|
*/
|
|
|
|
export class ChartView extends Disposable {
|
|
|
|
public viewPane: Element;
|
|
|
|
|
|
|
|
// These elements are defined in BaseView, from which we inherit with some hackery.
|
|
|
|
protected viewSection: ViewSectionRec;
|
|
|
|
protected sortedRows: SortedRowSet;
|
|
|
|
protected tableModel: DataTableModel;
|
2021-11-22 08:45:48 +00:00
|
|
|
protected gristDoc: GristDoc;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
private _chartType: ko.Observable<string>;
|
|
|
|
private _options: ObjObservable<any>;
|
|
|
|
private _chartDom: HTMLElement;
|
|
|
|
private _update: () => void;
|
2021-02-25 15:07:05 +00:00
|
|
|
private _resize: () => void;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-11-22 08:45:48 +00:00
|
|
|
private _formatterComp: ko.Computed<BaseFormatter|undefined>;
|
|
|
|
|
2021-12-20 11:21:27 +00:00
|
|
|
// peek section's sort spec
|
|
|
|
private get _sortSpec() { return this.viewSection.activeSortSpec.peek(); }
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
public create(gristDoc: GristDoc, viewSectionModel: ViewSectionRec) {
|
|
|
|
BaseView.call(this as any, gristDoc, viewSectionModel);
|
|
|
|
|
|
|
|
this._chartDom = this.autoDispose(this.buildDom());
|
|
|
|
|
2021-02-25 15:07:05 +00:00
|
|
|
this._resize = this.autoDispose(Delay.untilAnimationFrame(this._resizeChart, this));
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Note that .viewPane is used by ViewLayout to insert the actual DOM into the document.
|
|
|
|
this.viewPane = this._chartDom;
|
|
|
|
|
|
|
|
this._chartType = this.viewSection.chartTypeDef;
|
|
|
|
this._options = this.viewSection.optionsObj;
|
|
|
|
|
2021-11-22 08:45:48 +00:00
|
|
|
// Computed that returns the formatter of the first series. This is useful to format the total
|
|
|
|
// within a donut chart.
|
|
|
|
this._formatterComp = this.autoDispose(ko.computed(() => {
|
|
|
|
const field = this.viewSection.viewFields().at(1);
|
2021-12-15 22:31:53 +00:00
|
|
|
return field?.visibleColFormatter();
|
2021-11-22 08:45:48 +00:00
|
|
|
}));
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
this._update = debounce(() => this._updateView(), 0);
|
|
|
|
|
2022-01-17 16:08:43 +00:00
|
|
|
let subs: IDisposable[] = [];
|
2020-10-02 15:10:00 +00:00
|
|
|
this.autoDispose(this._chartType.subscribe(this._update));
|
|
|
|
this.autoDispose(this._options.subscribe(this._update));
|
2022-01-17 16:08:43 +00:00
|
|
|
this.autoDispose(this.viewSection.viewFields().subscribe((viewFields: ViewFieldRec[]) => {
|
|
|
|
this._update();
|
|
|
|
subs.forEach((sub) => sub.dispose());
|
|
|
|
subs = [
|
|
|
|
...viewFields.map((field) => field.displayColModel.peek().type.subscribe(this._update)),
|
|
|
|
...viewFields.map((field) => field.visibleColModel.peek().type.subscribe(this._update)),
|
|
|
|
];
|
|
|
|
}));
|
2020-10-02 15:10:00 +00:00
|
|
|
this.listenTo(this.sortedRows, 'rowNotify', this._update);
|
|
|
|
this.autoDispose(this.sortedRows.getKoArray().subscribe(this._update));
|
2021-11-22 08:45:48 +00:00
|
|
|
this.autoDispose(this._formatterComp.subscribe(this._update));
|
2023-02-24 11:12:55 +00:00
|
|
|
this.autoDispose(this.gristDoc.docPageModel.appModel.currentTheme.addListener(() => this._update()));
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 20:00:56 +00:00
|
|
|
public prepareToPrint(onOff: boolean) {
|
|
|
|
Plotly.relayout(this._chartDom, {}).catch(reportError);
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
protected onTableLoaded() {
|
|
|
|
(BaseView.prototype as any).onTableLoaded.call(this);
|
|
|
|
this._update();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected onResize() {
|
2021-02-25 15:07:05 +00:00
|
|
|
this._resize();
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected buildDom() {
|
|
|
|
return dom('div.chart_container', testId('container'));
|
|
|
|
}
|
|
|
|
|
|
|
|
private listenTo(...args: any[]): void { /* replaced by Backbone */ }
|
|
|
|
|
|
|
|
private async _updateView() {
|
|
|
|
if (this.isDisposed()) { return; }
|
|
|
|
|
|
|
|
const chartFunc = chartTypes[this._chartType()];
|
|
|
|
if (typeof chartFunc !== 'function') {
|
|
|
|
console.warn("Unknown trace type %s", this._chartType());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const fields: ViewFieldRec[] = this.viewSection.viewFields().all();
|
|
|
|
const rowIds: number[] = this.sortedRows.getKoArray().peek() as number[];
|
2022-01-11 10:19:34 +00:00
|
|
|
const startIndexForYAxis = this._options.prop('multiseries').peek() ? 2 : 1;
|
2022-01-17 16:08:43 +00:00
|
|
|
let series: Series[] = fields
|
|
|
|
.filter((field, i) => i < startIndexForYAxis || this._isCompatibleSeries(field.column.peek()))
|
|
|
|
.map((field) => {
|
|
|
|
// Use the colId of the displayCol, which may be different in case of Reference columns.
|
|
|
|
const colId: string = field.displayColModel.peek().colId.peek();
|
|
|
|
const getter = this.tableModel.tableData.getRowPropFunc(colId) as RowPropGetter;
|
|
|
|
const pureType = field.displayColModel().pureType();
|
|
|
|
const fullGetter = (pureType === 'Date' || pureType === 'DateTime') ? dateGetter(getter) : getter;
|
|
|
|
return {
|
|
|
|
label: field.label(),
|
|
|
|
values: rowIds.map(fullGetter),
|
2022-03-18 09:57:54 +00:00
|
|
|
isInSortSpec: Boolean(Sort.findCol(this._sortSpec, field.colRef.peek())),
|
2022-01-17 16:08:43 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-09-30 07:41:36 +00:00
|
|
|
for (let i = 0; i < series.length; ++i) {
|
|
|
|
if (i < fields.length && LIST_TYPES.includes(fields[i].column.peek().pureType.peek())) {
|
|
|
|
if (i < startIndexForYAxis) {
|
|
|
|
// For x-axis and group column data, split series we should split records.
|
|
|
|
series = splitValuesByIndex(series, i);
|
|
|
|
} else {
|
|
|
|
// For all y-axis, it's not sure what would be a sensible representation for choice list,
|
|
|
|
// simply stringify choice list values seems reasonable.
|
|
|
|
series[i].values = series[i].values.map((v) => String(decodeObject(v as any)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 15:21:46 +00:00
|
|
|
const dataOptions: DataOptions = {};
|
2020-10-02 15:10:00 +00:00
|
|
|
const options: ChartOptions = this._options.peek() || {};
|
|
|
|
let plotData: PlotData = {data: []};
|
|
|
|
|
2021-12-20 11:21:27 +00:00
|
|
|
if (isPieLike(this._chartType.peek())) {
|
|
|
|
|
|
|
|
// Plotly's pie charts have a sort option that is enabled by default. Let's turn it off.
|
2021-10-14 15:21:46 +00:00
|
|
|
dataOptions.sort = false;
|
2021-12-20 11:21:27 +00:00
|
|
|
|
|
|
|
// This line is for labels to stay in order when value changes, which can happen when using
|
|
|
|
// charts with linked list.
|
|
|
|
sortByXValues(series);
|
2021-10-14 15:21:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 08:45:48 +00:00
|
|
|
if (this._chartType.peek() === 'donut') {
|
|
|
|
dataOptions.totalFormatter = this._formatterComp.peek();
|
|
|
|
}
|
|
|
|
|
2022-02-23 12:19:10 +00:00
|
|
|
if (!options.multiseries && series.length) {
|
2021-10-14 15:21:46 +00:00
|
|
|
plotData = chartFunc(series, options, dataOptions);
|
2020-10-02 15:10:00 +00:00
|
|
|
} else if (series.length > 1) {
|
|
|
|
// We need to group all series by the first column.
|
2022-03-18 09:57:54 +00:00
|
|
|
// Sort series alphabetically only if user has not defined a sort on this chart.
|
|
|
|
const shouldSort = !series[0].isInSortSpec;
|
|
|
|
const nseries = groupSeries(series[0].values, series.slice(1), shouldSort);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// This will be in the order in which nseries Map was created; concat() flattens the arrays.
|
2021-10-21 08:02:51 +00:00
|
|
|
const xvalues = Array.from(new Set(series[1].values));
|
2020-10-02 15:10:00 +00:00
|
|
|
for (const gSeries of nseries.values()) {
|
2021-10-21 08:02:51 +00:00
|
|
|
|
|
|
|
// All series have partial list of values, ie: if some may have Q1, Q2, Q3, Q4 as x values
|
|
|
|
// some others might only have Q1. This causes inconsistent result in regard of the order
|
|
|
|
// bars will be displayed by plotly (for bar charts). This eventually result in bars not
|
|
|
|
// following the sorting order. This line fixes that issue by consolidating all series to
|
|
|
|
// have at least on entry of each x values.
|
|
|
|
if (this._chartType.peek() === 'bar') {
|
2021-12-20 11:21:27 +00:00
|
|
|
if (this._sortSpec?.length) { consolidateValues(gSeries, xvalues); }
|
2021-10-21 08:02:51 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 15:21:46 +00:00
|
|
|
const part = chartFunc(gSeries, options, dataOptions);
|
2020-10-02 15:10:00 +00:00
|
|
|
part.data = plotData.data.concat(part.data);
|
|
|
|
plotData = part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Plotly = Plotly || await loadPlotly();
|
|
|
|
|
|
|
|
// Loading plotly is asynchronous and it may happen that the chart view had been disposed in the
|
|
|
|
// meantime and cause error later. So let's check again.
|
|
|
|
if (this.isDisposed()) { return; }
|
|
|
|
|
2022-09-06 01:51:57 +00:00
|
|
|
const layout: Partial<Layout> = defaultsDeep(plotData.layout, this._getPlotlyLayout(options));
|
2020-10-02 15:10:00 +00:00
|
|
|
const config: Partial<Config> = {...plotData.config, displayModeBar: false};
|
|
|
|
// react() can be used in place of newPlot(), and is faster when updating an existing plot.
|
|
|
|
await Plotly.react(this._chartDom, plotData.data, layout, config);
|
|
|
|
this._resizeChart();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _resizeChart() {
|
2021-09-22 10:58:43 +00:00
|
|
|
if (this.isDisposed() || !Plotly || !this._chartDom.parentNode) { return; }
|
2020-10-02 15:10:00 +00:00
|
|
|
Plotly.Plots.resize(this._chartDom);
|
|
|
|
}
|
2022-01-17 16:08:43 +00:00
|
|
|
|
|
|
|
private _isCompatibleSeries(col: ColumnRec) {
|
|
|
|
return isNumericOnly(this._chartType.peek()) ? isNumericLike(col) : true;
|
|
|
|
}
|
2022-09-06 01:51:57 +00:00
|
|
|
|
|
|
|
private _getPlotlyLayout(options: ChartOptions): Partial<Layout> {
|
|
|
|
// Note that each call to getPlotlyLayout() creates a new layout object. We are intentionally
|
|
|
|
// avoiding reuse because Plotly caches too many layout calculations when the object is reused.
|
|
|
|
const yaxis: Partial<LayoutAxis> = {automargin: true, title: {standoff: 0}};
|
|
|
|
const xaxis: Partial<LayoutAxis> = {automargin: true, title: {standoff: 0}};
|
|
|
|
if (options.logYAxis) { yaxis.type = 'log'; }
|
|
|
|
if (options.invertYAxis) { yaxis.autorange = 'reversed'; }
|
|
|
|
const layout = {
|
|
|
|
// Margins include labels, titles, legend, and may get auto-expanded beyond this.
|
|
|
|
margin: {
|
|
|
|
l: 50,
|
|
|
|
r: 50,
|
|
|
|
b: 40, // Space below chart which includes x-axis labels
|
|
|
|
t: 30, // Space above the chart (doesn't include any text)
|
|
|
|
pad: 4
|
|
|
|
} as Margin,
|
|
|
|
yaxis,
|
|
|
|
xaxis,
|
|
|
|
...(options.stacked ? {barmode: 'relative'} : {}),
|
|
|
|
};
|
|
|
|
return merge(layout, this._getPlotlyTheme());
|
|
|
|
}
|
|
|
|
|
|
|
|
private _getPlotlyTheme(): Partial<Layout> {
|
|
|
|
const appModel = this.gristDoc.docPageModel.appModel;
|
|
|
|
const {colors} = appModel.currentTheme.get();
|
|
|
|
return {
|
|
|
|
paper_bgcolor: colors['chart-bg'],
|
|
|
|
plot_bgcolor: colors['chart-bg'],
|
|
|
|
xaxis: {
|
|
|
|
color: colors['chart-x-axis'],
|
|
|
|
},
|
|
|
|
yaxis: {
|
|
|
|
color: colors['chart-y-axis'],
|
|
|
|
},
|
|
|
|
font: {
|
|
|
|
color: colors['chart-fg'],
|
|
|
|
},
|
|
|
|
legend: {
|
|
|
|
bgcolor: colors['chart-legend-bg'],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Group the given array of series by a column of group values. The groupColumn and each of the
|
|
|
|
* series should be arrays of the same length.
|
|
|
|
*
|
|
|
|
* For example, if groupColumn has CompanyID, and valueSeries contains [Date, Employees, Revenues]
|
|
|
|
* (each an array of values), then returns a map mapping each CompanyID to the array [Date,
|
|
|
|
* Employees, Revenue], each value of which is itself an array of values for that CompanyID.
|
|
|
|
*/
|
2022-03-18 09:57:54 +00:00
|
|
|
function groupSeries<T extends Datum>(groupColumn: T[], valueSeries: Series[], sort: boolean): Map<T, Series[]> {
|
2020-10-02 15:10:00 +00:00
|
|
|
const nseries = new Map<T, Series[]>();
|
|
|
|
|
|
|
|
// Limit the number if group values so as to limit the total number of series we pass into
|
|
|
|
// Plotly. Too many series are impossible to make sense of anyway, and can hang the browser.
|
|
|
|
// TODO: When not all data is shown, we should probably show some indicator, similar to when
|
|
|
|
// OnDemand data is truncated.
|
|
|
|
const maxGroups = Math.floor(MAX_SERIES_IN_CHART / valueSeries.length);
|
2022-03-18 09:57:54 +00:00
|
|
|
let groupValues: T[] = [...new Set(groupColumn)];
|
|
|
|
if (sort) {
|
|
|
|
groupValues.sort();
|
|
|
|
}
|
|
|
|
groupValues = groupValues.slice(0, maxGroups);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Set up empty lists for each group.
|
|
|
|
for (const group of groupValues) {
|
|
|
|
nseries.set(group, valueSeries.map((s: Series) => ({
|
|
|
|
label: s.label,
|
|
|
|
group,
|
|
|
|
values: []
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now fill up the lists.
|
|
|
|
for (let row = 0; row < groupColumn.length; row++) {
|
|
|
|
const group = groupColumn[row];
|
|
|
|
const series: Series[]|undefined = nseries.get(group);
|
|
|
|
if (series) {
|
|
|
|
for (let i = 0; i < valueSeries.length; i++) {
|
|
|
|
series[i].values.push(valueSeries[i].values[row]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nseries;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If errorBars are requested, removes error bar series from the 'series' list, adding instead a
|
|
|
|
// mapping from each main Y series to the corresponding plotly ErrorBar object.
|
|
|
|
function extractErrorBars(series: Series[], options: ChartOptions): Map<Series, ErrorBar> {
|
|
|
|
const result = new Map<Series, ErrorBar>();
|
|
|
|
if (options.errorBars) {
|
|
|
|
// We assume that series is of the form [X, Y1, Y1-bar, Y2, Y2-bar, ...] (if "symmetric") or
|
|
|
|
// [X, Y1, Y1-below, Y1-above, Y2, Y2-below, Y2-above, ...] (if "separate").
|
|
|
|
for (let i = 1; i < series.length; i++) {
|
|
|
|
result.set(series[i], {
|
|
|
|
type: 'data',
|
|
|
|
symmetric: (options.errorBars === 'symmetric'),
|
|
|
|
array: series[i + 1] && series[i + 1].values,
|
|
|
|
arrayminus: (options.errorBars === 'separate' ? series[i + 2] && series[i + 2].values : undefined),
|
|
|
|
thickness: 1,
|
|
|
|
width: 3,
|
|
|
|
});
|
|
|
|
series.splice(i + 1, (options.errorBars === 'symmetric' ? 1 : 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getting an ES6 class to work with old-style multiple base classes takes a little hacking.
|
|
|
|
defaults(ChartView.prototype, BaseView.prototype);
|
|
|
|
Object.assign(ChartView.prototype, BackboneEvents);
|
|
|
|
|
|
|
|
/**
|
2021-09-15 08:51:18 +00:00
|
|
|
* The grainjs component for side-pane configuration options for a Chart section.
|
2020-10-02 15:10:00 +00:00
|
|
|
*/
|
2021-09-15 08:51:18 +00:00
|
|
|
export class ChartConfig extends GrainJSDisposable {
|
|
|
|
|
2022-04-07 10:10:58 +00:00
|
|
|
private static _instanceMap = new WeakMap<ViewSectionRec, ChartConfig>();
|
|
|
|
|
2021-09-15 08:51:18 +00:00
|
|
|
// helper to build the draggable field list
|
2022-02-04 10:21:43 +00:00
|
|
|
private _configFieldsHelper = VisibleFieldsConfig.create(this, this._gristDoc, this._section);
|
2021-09-15 08:51:18 +00:00
|
|
|
|
2022-02-24 08:33:22 +00:00
|
|
|
// The index for the x-axis in the list visible fields. Could be eigther 0 or 1 or -1 depending on
|
|
|
|
// whether multiseries and isXAxisUndefined are set.
|
2021-09-15 08:51:18 +00:00
|
|
|
private _xAxisFieldIndex = Computed.create(
|
2022-02-24 08:33:22 +00:00
|
|
|
this,
|
|
|
|
fromKo(this._optionsObj.prop('multiseries')),
|
|
|
|
fromKo(this._optionsObj.prop('isXAxisUndefined')), (_use, multiseries, isUndefined) => (
|
|
|
|
isUndefined ? -1 : (multiseries ? 1 : 0)
|
2021-09-15 08:51:18 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2022-04-07 10:10:58 +00:00
|
|
|
// The colId of the grouping column, or "" if multiseries is disabled or there are no viewFields,
|
2021-12-29 20:05:30 +00:00
|
|
|
// for example during section removal.
|
2022-04-07 10:10:58 +00:00
|
|
|
private _groupDataColId: Computed<string> = Computed.create(this, (use) => {
|
2021-09-15 08:51:18 +00:00
|
|
|
const multiseries = use(this._optionsObj.prop('multiseries'));
|
|
|
|
const viewFields = use(use(this._section.viewFields).getObservable());
|
2022-04-07 10:10:58 +00:00
|
|
|
if (!multiseries || viewFields.length === 0) { return ""; }
|
|
|
|
return use(use(viewFields[0].column).colId);
|
2021-09-15 08:51:18 +00:00
|
|
|
})
|
|
|
|
.onWrite((colId) => this._setGroupDataColumn(colId));
|
|
|
|
|
|
|
|
// Updating the group data column involves several changes of the list of view fields which could
|
|
|
|
// leave the x-axis field index momentarily point to the wrong column. The freeze x axis
|
|
|
|
// observable is part of a hack to fix this issue.
|
|
|
|
private _freezeXAxis = Observable.create(this, false);
|
|
|
|
|
2021-09-16 08:33:56 +00:00
|
|
|
private _freezeYAxis = Observable.create(this, false);
|
|
|
|
|
2022-04-07 10:10:58 +00:00
|
|
|
// The colId of the x-axis, or "" is x axis is undefined.
|
|
|
|
private _xAxis: Computed<string> = Computed.create(
|
2021-09-15 08:51:18 +00:00
|
|
|
this, this._xAxisFieldIndex, this._freezeXAxis, (use, i, freeze) => {
|
|
|
|
if (freeze) { return this._xAxis.get(); }
|
|
|
|
const viewFields = use(use(this._section.viewFields).getObservable());
|
2022-02-24 08:33:22 +00:00
|
|
|
if (-1 < i && i < viewFields.length) {
|
2022-04-07 10:10:58 +00:00
|
|
|
return use(use(viewFields[i].column).colId);
|
2021-09-15 08:51:18 +00:00
|
|
|
}
|
2022-04-07 10:10:58 +00:00
|
|
|
return "";
|
2021-09-15 08:51:18 +00:00
|
|
|
})
|
|
|
|
.onWrite((colId) => this._setXAxis(colId));
|
|
|
|
|
2022-04-27 11:59:19 +00:00
|
|
|
// Whether value is aggregated or not
|
|
|
|
private _isValueAggregated = Computed.create(this, (use) => this._isSummaryTable(use))
|
|
|
|
.onWrite((val) => this._setAggregation(val));
|
|
|
|
|
2022-04-07 10:10:58 +00:00
|
|
|
// Columns options
|
|
|
|
private _columnsOptions: Computed<Array<IOptionFull<string>>> = Computed.create(
|
|
|
|
this, this._freezeXAxis, (use, freeze) => {
|
|
|
|
if (freeze) { return this._columnsOptions.get(); }
|
2022-04-27 11:59:19 +00:00
|
|
|
const columns = use(this._isValueAggregated) ?
|
2022-04-07 10:10:58 +00:00
|
|
|
this._getSummarySourceColumns(use) :
|
|
|
|
this._getColumns(use);
|
|
|
|
return columns
|
2022-02-24 08:33:22 +00:00
|
|
|
// filter out hidden column (ie: manualsort ...)
|
2022-04-07 10:10:58 +00:00
|
|
|
.filter((col) => !col.isHiddenCol.peek())
|
|
|
|
.map((col) => ({
|
|
|
|
value: col.colId(), label: col.label.peek(), icon: 'FieldColumn' as IconName,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// The list of available columns for the group data picker.
|
|
|
|
private _groupDataOptions = Computed.create<Array<IOption<string>>>(this, (use) => [
|
|
|
|
{value: "", label: 'Pick a column'},
|
|
|
|
...use(this._columnsOptions)
|
2021-09-15 08:51:18 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Force checking/unchecking of the group data checkbox option.
|
|
|
|
private _groupDataForce = Observable.create(null, false);
|
|
|
|
|
|
|
|
// State for the group data option checkbox. True, if a group data column is set or if the user
|
|
|
|
// forced it. False otherwise.
|
|
|
|
private _groupData = Computed.create(
|
2022-04-07 10:10:58 +00:00
|
|
|
this, this._groupDataColId, this._groupDataForce, (_use, colId, force) => {
|
|
|
|
if (colId) { return true; }
|
2021-09-15 08:51:18 +00:00
|
|
|
return force;
|
|
|
|
}).onWrite((val) => {
|
|
|
|
if (val === false) {
|
2022-04-07 10:10:58 +00:00
|
|
|
this._groupDataColId.set("");
|
2021-09-15 08:51:18 +00:00
|
|
|
}
|
|
|
|
this._groupDataForce.set(val);
|
|
|
|
});
|
|
|
|
|
2021-09-17 09:32:12 +00:00
|
|
|
// The label to show for the first field in the axis configurator.
|
|
|
|
private _firstFieldLabel = Computed.create(this, fromKo(this._section.chartTypeDef), (
|
2022-10-12 15:33:36 +00:00
|
|
|
(_use, chartType) => firstFieldIsLabels(chartType) ? 'LABEL' : 'X-AXIS'
|
2021-09-17 09:32:12 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
// A computed that returns `this._section.chartTypeDef` and that takes care of removing the group
|
|
|
|
// data option when type is switched to 'pie'.
|
|
|
|
private _chartType = Computed.create(this, (use) => use(this._section.chartTypeDef))
|
|
|
|
.onWrite((val) => {
|
|
|
|
return this._gristDoc.docData.bundleActions('switched chart type', async () => {
|
|
|
|
await this._section.chartTypeDef.saveOnly(val);
|
|
|
|
// When switching chart type to 'pie' makes sure to remove the group data option.
|
2021-11-22 08:45:48 +00:00
|
|
|
if (isPieLike(val)) {
|
2022-04-07 10:10:58 +00:00
|
|
|
await this._setGroupDataColumn("");
|
2021-09-17 09:32:12 +00:00
|
|
|
this._groupDataForce.set(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-09-15 08:51:18 +00:00
|
|
|
constructor(private _gristDoc: GristDoc, private _section: ViewSectionRec) {
|
|
|
|
super();
|
2022-04-07 10:10:58 +00:00
|
|
|
ChartConfig._instanceMap.set(_section, this);
|
2021-09-15 08:51:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private get _optionsObj() { return this._section.optionsObj; }
|
|
|
|
|
2021-12-05 05:14:38 +00:00
|
|
|
public buildDom(): DomContents {
|
2021-09-15 08:51:18 +00:00
|
|
|
|
|
|
|
if (this._section.parentKey() !== 'chart') { return null; }
|
|
|
|
|
|
|
|
return [
|
|
|
|
cssRow(
|
2021-09-17 09:32:12 +00:00
|
|
|
select(this._chartType, [
|
2021-09-15 08:51:18 +00:00
|
|
|
{value: 'bar', label: 'Bar Chart', icon: 'ChartBar' },
|
|
|
|
{value: 'pie', label: 'Pie Chart', icon: 'ChartPie' },
|
2021-11-22 08:45:48 +00:00
|
|
|
{value: 'donut', label: 'Donut Chart', icon: 'ChartDonut' },
|
2021-09-15 08:51:18 +00:00
|
|
|
{value: 'area', label: 'Area Chart', icon: 'ChartArea' },
|
|
|
|
{value: 'line', label: 'Line Chart', icon: 'ChartLine' },
|
|
|
|
{value: 'scatter', label: 'Scatter Plot', icon: 'ChartLine' },
|
|
|
|
{value: 'kaplan_meier', label: 'Kaplan-Meier Plot', icon: 'ChartKaplan'},
|
|
|
|
]),
|
|
|
|
testId("type"),
|
|
|
|
),
|
2021-11-22 08:45:48 +00:00
|
|
|
dom.maybe((use) => !isPieLike(use(this._section.chartTypeDef)), () => [
|
2021-09-15 08:51:18 +00:00
|
|
|
// These options don't make much sense for a pie chart.
|
2022-04-07 10:10:58 +00:00
|
|
|
cssCheckboxRowObs('Split series', this._groupData),
|
2021-09-15 08:51:18 +00:00
|
|
|
cssCheckboxRow('Invert Y-axis', this._optionsObj.prop('invertYAxis')),
|
2022-03-18 10:30:12 +00:00
|
|
|
cssRow(
|
|
|
|
cssRowLabel('Orientation'),
|
|
|
|
dom('div', linkSelect(fromKoSave(this._optionsObj.prop('orientation')), [
|
|
|
|
{value: 'v', label: 'Vertical'},
|
|
|
|
{value: 'h', label: 'Horizontal'}
|
|
|
|
], {defaultLabel: 'Vertical'})),
|
|
|
|
testId('orientation'),
|
|
|
|
),
|
2021-09-15 08:51:18 +00:00
|
|
|
cssCheckboxRow('Log scale Y-axis', this._optionsObj.prop('logYAxis')),
|
|
|
|
]),
|
2021-12-05 05:14:38 +00:00
|
|
|
dom.maybeOwned((use) => use(this._section.chartTypeDef) === 'donut', (owner) => [
|
2021-11-22 08:45:48 +00:00
|
|
|
cssSlideRow(
|
|
|
|
'Hole Size',
|
|
|
|
Computed.create(owner, (use) => use(this._optionsObj.prop('donutHoleSize')) ?? DONUT_DEFAULT_HOLE_SIZE),
|
|
|
|
(val: number) => this._optionsObj.prop('donutHoleSize').saveOnly(val),
|
|
|
|
testId('option')
|
|
|
|
),
|
|
|
|
cssCheckboxRow('Show Total', this._optionsObj.prop('showTotal')),
|
|
|
|
dom.maybe(this._optionsObj.prop('showTotal'), () => (
|
|
|
|
cssNumberWithSpinnerRow(
|
|
|
|
'Text Size',
|
|
|
|
Computed.create(owner, (use) => use(this._optionsObj.prop('textSize')) ?? DONUT_DEFAULT_TEXT_SIZE),
|
|
|
|
(val: number) => this._optionsObj.prop('textSize').saveOnly(val),
|
|
|
|
testId('option')
|
|
|
|
)
|
|
|
|
))
|
|
|
|
]),
|
2021-09-15 08:51:18 +00:00
|
|
|
dom.maybe((use) => use(this._section.chartTypeDef) === 'line', () => [
|
|
|
|
cssCheckboxRow('Connect gaps', this._optionsObj.prop('lineConnectGaps')),
|
|
|
|
cssCheckboxRow('Show markers', this._optionsObj.prop('lineMarkers')),
|
|
|
|
]),
|
|
|
|
dom.maybe((use) => ['line', 'bar'].includes(use(this._section.chartTypeDef)), () => [
|
2022-03-18 09:57:54 +00:00
|
|
|
cssCheckboxRow('Stack series', this._optionsObj.prop('stacked')),
|
2021-09-15 08:51:18 +00:00
|
|
|
cssRow(
|
|
|
|
cssRowLabel('Error bars'),
|
|
|
|
dom('div', linkSelect(fromKoSave(this._optionsObj.prop('errorBars')), [
|
|
|
|
{value: '', label: 'None'},
|
|
|
|
{value: 'symmetric', label: 'Symmetric'},
|
|
|
|
{value: 'separate', label: 'Above+Below'},
|
|
|
|
], {defaultLabel: 'None'})),
|
|
|
|
testId('error-bars'),
|
|
|
|
),
|
|
|
|
dom.domComputed(this._optionsObj.prop('errorBars'), (value: ChartOptions["errorBars"]) =>
|
2022-12-06 13:40:02 +00:00
|
|
|
value === 'symmetric' ? cssRowHelp(t("Each Y series is followed by a series for the length of error bars.")) :
|
2023-01-03 16:45:14 +00:00
|
|
|
value === 'separate' ? cssRowHelp(t("Each Y series is followed by two series, for " +
|
|
|
|
"top and bottom error bars."))
|
|
|
|
: null
|
2021-09-15 08:51:18 +00:00
|
|
|
),
|
|
|
|
]),
|
|
|
|
|
|
|
|
cssSeparator(),
|
|
|
|
|
|
|
|
dom.maybe(this._groupData, () => [
|
2022-04-07 10:10:58 +00:00
|
|
|
cssLabel('Split Series'),
|
2021-09-15 08:51:18 +00:00
|
|
|
cssRow(
|
|
|
|
select(this._groupDataColId, this._groupDataOptions),
|
|
|
|
testId('group-by-column'),
|
|
|
|
),
|
2022-12-06 13:40:02 +00:00
|
|
|
cssHintRow(t("Create separate series for each value of the selected column.")),
|
2020-10-02 15:10:00 +00:00
|
|
|
]),
|
2021-09-15 08:51:18 +00:00
|
|
|
|
|
|
|
// TODO: user should select x axis before widget reach page
|
2021-09-17 09:32:12 +00:00
|
|
|
cssLabel(dom.text(this._firstFieldLabel), testId('first-field-label')),
|
2021-09-15 08:51:18 +00:00
|
|
|
cssRow(
|
|
|
|
select(
|
2022-04-07 10:10:58 +00:00
|
|
|
this._xAxis, this._columnsOptions,
|
2022-12-06 13:40:02 +00:00
|
|
|
{ defaultLabel: t("Pick a column") }
|
2021-09-15 08:51:18 +00:00
|
|
|
),
|
|
|
|
testId('x-axis'),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
2022-04-07 10:10:58 +00:00
|
|
|
cssCheckboxRowObs('Aggregate values', this._isValueAggregated),
|
2021-09-15 08:51:18 +00:00
|
|
|
|
|
|
|
cssLabel('SERIES'),
|
2021-09-16 08:33:56 +00:00
|
|
|
this._buildYAxis(),
|
2021-09-15 08:51:18 +00:00
|
|
|
cssRow(
|
|
|
|
cssAddYAxis(
|
|
|
|
cssAddIcon('Plus'), 'Add Series',
|
2022-01-17 16:08:43 +00:00
|
|
|
menu(() => {
|
|
|
|
const hiddenColumns = this._section.hiddenColumns.peek();
|
|
|
|
const filterFunc = this._isCompatibleSeries.bind(this);
|
|
|
|
const nonNumericCount = hiddenColumns.filter((col) => !filterFunc(col)).length;
|
|
|
|
return [
|
|
|
|
...hiddenColumns
|
|
|
|
.filter((col) => filterFunc(col))
|
|
|
|
.map((col) => menuItem(
|
|
|
|
() => this._configFieldsHelper.addField(col),
|
|
|
|
col.label.peek(),
|
|
|
|
)),
|
|
|
|
nonNumericCount ? menuText(
|
|
|
|
`${nonNumericCount} ` + (
|
|
|
|
nonNumericCount > 1 ?
|
|
|
|
`non-numeric columns are not shown` :
|
|
|
|
`non-numeric column is not shown`
|
|
|
|
),
|
|
|
|
testId('yseries-picker-message'),
|
|
|
|
) : null,
|
|
|
|
];
|
|
|
|
}),
|
2021-09-15 08:51:18 +00:00
|
|
|
testId('add-y-axis'),
|
|
|
|
)
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
2021-09-15 08:51:18 +00:00
|
|
|
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-04-07 10:10:58 +00:00
|
|
|
private async _setXAxis(colId: string) {
|
2021-09-15 08:51:18 +00:00
|
|
|
const optionsObj = this._section.optionsObj;
|
2022-04-07 10:10:58 +00:00
|
|
|
const findColumn = () => this._getColumns().find((c) => c.colId() === colId);
|
2021-09-15 08:51:18 +00:00
|
|
|
const viewFields = this._section.viewFields.peek();
|
|
|
|
|
|
|
|
await this._gristDoc.docData.bundleActions('selected new x-axis', async () => {
|
2021-09-16 08:33:56 +00:00
|
|
|
this._freezeYAxis.set(true);
|
2022-04-07 10:10:58 +00:00
|
|
|
this._freezeXAxis.set(true);
|
2021-09-16 08:33:56 +00:00
|
|
|
try {
|
2022-02-24 08:33:22 +00:00
|
|
|
|
2021-09-16 08:33:56 +00:00
|
|
|
// first remove the current field
|
2022-02-24 08:33:22 +00:00
|
|
|
if (this._xAxisFieldIndex.get() !== -1 && this._xAxisFieldIndex.get() < viewFields.peek().length) {
|
2021-09-16 08:33:56 +00:00
|
|
|
await this._configFieldsHelper.removeField(viewFields.peek()[this._xAxisFieldIndex.get()]);
|
|
|
|
}
|
2021-09-15 08:51:18 +00:00
|
|
|
|
2022-02-24 08:33:22 +00:00
|
|
|
// if x axis was undefined, set option to false
|
|
|
|
await setSaveValue(this._optionsObj.prop('isXAxisUndefined'), false);
|
|
|
|
|
2022-04-08 13:47:08 +00:00
|
|
|
// if new field was used to split series, disable multiseries
|
2022-04-07 10:10:58 +00:00
|
|
|
const fieldIndex = viewFields.peek().findIndex((f) => f.column.peek().colId() === colId);
|
2021-09-16 08:33:56 +00:00
|
|
|
if (fieldIndex === 0 && optionsObj.prop('multiseries').peek()) {
|
|
|
|
await optionsObj.prop('multiseries').setAndSave(false);
|
|
|
|
return;
|
|
|
|
}
|
2021-09-15 08:51:18 +00:00
|
|
|
|
2022-04-08 13:47:08 +00:00
|
|
|
// if values aggregation is 'on' update the grouped by columns before findColumn()
|
|
|
|
// call. This will make sure that colId is not missing from the summary table's columns (as
|
|
|
|
// could happen if it were a non-numeric for instance).
|
2022-04-27 11:59:19 +00:00
|
|
|
if (this._isValueAggregated.get()) {
|
2022-04-08 13:47:08 +00:00
|
|
|
const splitColId = this._groupDataColId.get();
|
|
|
|
const cols = splitColId === colId ? [colId] : [splitColId, colId];
|
|
|
|
await this._setGroupByColumns(cols);
|
|
|
|
}
|
|
|
|
|
2022-02-24 08:33:22 +00:00
|
|
|
// if the new column for the x axis is already visible, make it the first visible column,
|
|
|
|
// else add it as the first visible field. The field will be first because it will be
|
|
|
|
// inserted before current xAxis column (which is already first (or second if we have
|
|
|
|
// multi-series option checked))
|
2021-09-16 08:33:56 +00:00
|
|
|
const xAxisField = viewFields.peek()[this._xAxisFieldIndex.get()];
|
|
|
|
if (fieldIndex > -1) {
|
|
|
|
await this._configFieldsHelper.changeFieldPosition(viewFields.peek()[fieldIndex], xAxisField);
|
|
|
|
} else {
|
2022-04-07 10:10:58 +00:00
|
|
|
const col = findColumn();
|
|
|
|
if (col) {
|
|
|
|
await this._configFieldsHelper.addField(col, xAxisField);
|
|
|
|
}
|
2021-09-16 08:33:56 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this._freezeYAxis.set(false);
|
2022-04-07 10:10:58 +00:00
|
|
|
this._freezeXAxis.set(false);
|
2021-09-15 08:51:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-07 10:10:58 +00:00
|
|
|
private async _setGroupDataColumn(colId: string) {
|
2021-09-15 08:51:18 +00:00
|
|
|
const viewFields = this._section.viewFields.peek().peek();
|
|
|
|
|
2022-12-06 13:40:02 +00:00
|
|
|
await this._gristDoc.docData.bundleActions(t("selected new group data columns"), async () => {
|
2021-09-15 08:51:18 +00:00
|
|
|
this._freezeXAxis.set(true);
|
2021-09-16 08:33:56 +00:00
|
|
|
this._freezeYAxis.set(true);
|
2021-09-15 08:51:18 +00:00
|
|
|
try {
|
2022-04-07 10:10:58 +00:00
|
|
|
|
2021-09-15 08:51:18 +00:00
|
|
|
// if grouping was already set, first remove the current field
|
2022-04-07 10:10:58 +00:00
|
|
|
if (this._groupDataColId.get()) {
|
2021-09-15 08:51:18 +00:00
|
|
|
await this._configFieldsHelper.removeField(viewFields[0]);
|
|
|
|
}
|
|
|
|
|
2022-04-07 10:10:58 +00:00
|
|
|
// if values aggregation is 'on' update the grouped by columns first. This will make sure
|
|
|
|
// that colId is not missing from the summary table's columns (as could happen if it were a
|
|
|
|
// non-numeric for instance).
|
2022-04-27 11:59:19 +00:00
|
|
|
if (this._isValueAggregated.get()) {
|
2022-04-07 10:10:58 +00:00
|
|
|
const xAxisColId = this._xAxis.get();
|
2022-04-08 13:47:08 +00:00
|
|
|
const cols = xAxisColId === colId ? [colId] : [colId, xAxisColId];
|
2022-04-07 10:10:58 +00:00
|
|
|
await this._setGroupByColumns(cols);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (colId) {
|
|
|
|
const col = this._getColumns().find((c) => c.colId() === colId)!;
|
|
|
|
const field = viewFields.find((f) => f.column.peek().colId() === colId);
|
2021-09-15 08:51:18 +00:00
|
|
|
|
|
|
|
// if new field is already visible, moves the fields to the first place else add the field to the first
|
|
|
|
// place
|
|
|
|
if (field) {
|
|
|
|
await this._configFieldsHelper.changeFieldPosition(field, viewFields[0]);
|
|
|
|
} else {
|
|
|
|
await this._configFieldsHelper.addField(col, viewFields[0]);
|
|
|
|
}
|
2022-02-24 08:33:22 +00:00
|
|
|
|
|
|
|
// if this column is used as xAxis, set the xAxis to undefined (show Pick a column label)
|
|
|
|
if (colId === this._xAxis.get()) {
|
|
|
|
await this._optionsObj.prop('isXAxisUndefined').setAndSave(true);
|
|
|
|
}
|
2021-09-15 08:51:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 10:10:58 +00:00
|
|
|
await this._optionsObj.prop('multiseries').setAndSave(Boolean(colId));
|
|
|
|
|
2021-09-15 08:51:18 +00:00
|
|
|
} finally {
|
|
|
|
this._freezeXAxis.set(false);
|
2021-09-16 08:33:56 +00:00
|
|
|
this._freezeYAxis.set(false);
|
2021-09-15 08:51:18 +00:00
|
|
|
}
|
2021-09-17 09:32:12 +00:00
|
|
|
}, {nestInActiveBundle: true});
|
2021-09-15 08:51:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 10:10:58 +00:00
|
|
|
private _getColumns(use: UseCB = unwrap) {
|
|
|
|
const table = use(this._section.table);
|
|
|
|
return use(use(table.columns).getObservable());
|
|
|
|
}
|
|
|
|
|
|
|
|
private _getSummarySourceColumns(use: UseCB = unwrap) {
|
|
|
|
let table = use(this._section.table);
|
|
|
|
table = use(table.summarySource);
|
|
|
|
return use(use(table.columns).getObservable());
|
|
|
|
}
|
|
|
|
|
2021-09-15 08:51:18 +00:00
|
|
|
private _buildField(col: IField) {
|
|
|
|
return cssFieldEntry(
|
|
|
|
cssFieldLabel(dom.text(col.label)),
|
|
|
|
cssRemoveIcon(
|
|
|
|
'Remove',
|
|
|
|
dom.on('click', () => this._configFieldsHelper.removeField(col)),
|
|
|
|
testId('ref-select-remove'),
|
|
|
|
),
|
|
|
|
testId('y-axis'),
|
|
|
|
);
|
|
|
|
}
|
2021-09-16 08:33:56 +00:00
|
|
|
|
2022-01-17 16:08:43 +00:00
|
|
|
private _buildYAxis(): DomContents {
|
2021-09-16 08:33:56 +00:00
|
|
|
|
|
|
|
// The y-axis are all visible fields that comes after the x-axis and maybe the group data
|
|
|
|
// column. Hence the draggable list of y-axis needs to skip either one or two visible fields.
|
2022-02-24 08:33:22 +00:00
|
|
|
const skipFirst = Computed.create(this,
|
|
|
|
fromKo(this._optionsObj.prop('multiseries')),
|
|
|
|
fromKo(this._optionsObj.prop('isXAxisUndefined')),
|
|
|
|
(_use, multiseries, isUndefined) => (
|
|
|
|
(isUndefined ? 0 : 1) + (multiseries ? 1 : 0)
|
2021-09-16 08:33:56 +00:00
|
|
|
));
|
|
|
|
|
2022-01-17 16:08:43 +00:00
|
|
|
return dom.domComputed((use) => {
|
|
|
|
const filterFunc = (field: ViewFieldRec) => this._isCompatibleSeries(use(field.column), use);
|
|
|
|
return this._configFieldsHelper.buildVisibleFieldsConfigHelper({
|
|
|
|
itemCreateFunc: (field) => this._buildField(field),
|
|
|
|
draggableOptions: {
|
|
|
|
removeButton: false,
|
|
|
|
drag_indicator: cssDragger,
|
|
|
|
}, skipFirst, freeze: this._freezeYAxis, filterFunc
|
|
|
|
});
|
2021-09-16 08:33:56 +00:00
|
|
|
});
|
|
|
|
}
|
2022-01-17 16:08:43 +00:00
|
|
|
|
|
|
|
private _isCompatibleSeries(col: ColumnRec, use: UseCB = unwrap) {
|
|
|
|
return isNumericOnly(use(this._chartType)) ? isNumericLike(col, use) : true;
|
|
|
|
}
|
|
|
|
|
2022-04-07 10:10:58 +00:00
|
|
|
private async _setAggregation(val: boolean) {
|
|
|
|
try {
|
|
|
|
this._freezeXAxis.set(true);
|
2022-12-06 13:40:02 +00:00
|
|
|
await this._gristDoc.docData.bundleActions(t("Toggle chart aggregation"), async () => {
|
2022-04-07 10:10:58 +00:00
|
|
|
if (val) {
|
|
|
|
await this._doAggregation();
|
|
|
|
} else {
|
|
|
|
await this._undoAggregation();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
if (!this.isDisposed()) {
|
|
|
|
this._freezeXAxis.set(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the aggregation: if not a summary table, turns into one; else update groupby columns to
|
|
|
|
// match the X-Axis and Split-series columns.
|
|
|
|
private async _doAggregation(): Promise<void> {
|
|
|
|
if (!this._isSummaryTable()) {
|
|
|
|
await this._toggleSummaryTable();
|
|
|
|
} else {
|
|
|
|
await this._setGroupByColumns([this._xAxis.get(), this._groupDataColId.get()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Undo the aggregation.
|
|
|
|
private async _undoAggregation() {
|
|
|
|
if (this._isSummaryTable()) {
|
|
|
|
await this._toggleSummaryTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _isSummaryTable(use: UseCB = unwrap) {
|
|
|
|
return Boolean(use(use(this._section.table).summarySourceTable));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Toggle whether section table is a summary table. Must use with care: this function calls
|
|
|
|
// `this.dispose()` as a side effect. Conveniently returns the ChartConfig instance of the new
|
|
|
|
// view section that replaces the old one.
|
|
|
|
private async _toggleSummaryTable(): Promise<ChartConfig> {
|
|
|
|
const colIds = [this._xAxis.get(), this._groupDataColId.get()];
|
|
|
|
const pageWidget = toPageWidget(this._section);
|
|
|
|
pageWidget.summarize = !this._isSummaryTable();
|
|
|
|
pageWidget.columns = this._getColumnIds(colIds);
|
2022-04-22 16:30:48 +00:00
|
|
|
this._ensureValidLinkingIfAny(pageWidget);
|
2022-04-07 10:10:58 +00:00
|
|
|
const newSection = await this._gristDoc.saveViewSection(this._section, pageWidget);
|
|
|
|
return ChartConfig._instanceMap.get(newSection)!;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _setGroupByColumns(groupByCols: string[]) {
|
|
|
|
const pageWidget = toPageWidget(this._section);
|
|
|
|
pageWidget.columns = this._getColumnIds(groupByCols);
|
2022-04-22 16:30:48 +00:00
|
|
|
this._ensureValidLinkingIfAny(pageWidget);
|
2022-04-07 10:10:58 +00:00
|
|
|
return this._gristDoc.saveViewSection(this._section, pageWidget);
|
|
|
|
}
|
|
|
|
|
2022-04-22 16:30:48 +00:00
|
|
|
// 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).
|
2022-04-07 10:10:58 +00:00
|
|
|
private _getColumnIds(colIds: string[]) {
|
|
|
|
const cols = this._isSummaryTable() ?
|
|
|
|
this._section.table().summarySource().columns().all() :
|
|
|
|
this._section.table().columns().all();
|
|
|
|
const columns = colIds
|
|
|
|
.map((colId) => colId && cols.find(c => c.colId() === colId))
|
|
|
|
.filter((col): col is ColumnRec => Boolean(col))
|
|
|
|
.map(col => col.id());
|
|
|
|
return columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 08:45:48 +00:00
|
|
|
// Row for a numeric option. User can change value using spinners or directly using keyboard. In
|
|
|
|
// case of invalid values, the field reverts to the saved one.
|
|
|
|
function cssNumberWithSpinnerRow(label: string, value: Computed<number>, save: (val: number) => Promise<void>,
|
|
|
|
...args: DomElementArg[]) {
|
|
|
|
const minValue = 1;
|
|
|
|
let input: HTMLInputElement;
|
|
|
|
|
|
|
|
// Set the input's value to the value that's saved on the server.
|
|
|
|
function reset() {
|
|
|
|
input.value = value.get() + "px";
|
|
|
|
}
|
|
|
|
|
|
|
|
async function onChange(val: string, func: (val: number) => number = (v) => v) {
|
|
|
|
let fvalue = parseFloat(val);
|
|
|
|
if (isFinite(fvalue)) {
|
|
|
|
fvalue = clamp(func(fvalue), minValue, Infinity);
|
|
|
|
await save(fvalue);
|
|
|
|
}
|
|
|
|
// Reset is needed if value were not a valid number.
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
return cssRow(
|
|
|
|
cssRowLabel(label),
|
|
|
|
cssNumberWithSpinner(
|
|
|
|
input = cssNumberInput(
|
|
|
|
{type: 'text'},
|
|
|
|
dom.prop('value', (use) => use(value) + "px"),
|
|
|
|
dom.on('change', (_ev, el) => onChange(el.value)),
|
|
|
|
dom.onKeyDown({
|
|
|
|
ArrowDown: (_ev, el) => onChange(el.value, (val) => val - 1),
|
|
|
|
ArrowUp: (_ev, el) => onChange(el.value, (val) => val + 1),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
|
|
|
|
// We add spinners as overlay in order to support showing the unit 'px' next to the value.
|
|
|
|
cssSpinners(
|
|
|
|
'input',
|
|
|
|
{type: 'number', step: '1', min: String(minValue)},
|
|
|
|
dom.prop('value', value),
|
|
|
|
dom.on('change', (_ev, el) => onChange(el.value)),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
...args
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Row for a numeric option that leaves between 0 and 1. User can change value using a slider, or
|
|
|
|
// spinners or by directly using keyboard. Value is shown as percent. If user enter an invalid
|
|
|
|
// value, field reverts to the saved value.
|
|
|
|
function cssSlideRow(label: string, value: Computed<number>, save: (val: number) => Promise<void>,
|
|
|
|
...args: DomElementArg[]) {
|
|
|
|
let input: HTMLInputElement;
|
|
|
|
|
|
|
|
// Set the input's value to the value that's saved on the server.
|
|
|
|
function reset() {
|
|
|
|
input.value = formatPercent(value.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
async function onChange(val: string, func: (val: number) => number = (v) => v) {
|
|
|
|
let fvalue = parseFloat(val);
|
|
|
|
if (isFinite(fvalue)) {
|
|
|
|
fvalue = clamp(func(fvalue), 0, 99) / 100;
|
|
|
|
await save(fvalue);
|
|
|
|
}
|
|
|
|
// Reset is needed if value were not a valid number.
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
return cssRow(
|
|
|
|
cssRowLabel(label),
|
|
|
|
cssRangeInput(
|
|
|
|
{type: 'range', min: "0", max: "1", step: "0.01"},
|
|
|
|
dom.prop('value', value),
|
|
|
|
dom.on('change', (_ev, el) => save(Number(el.value)))
|
|
|
|
),
|
|
|
|
cssNumberWithSpinner(
|
|
|
|
input = cssNumberInput(
|
|
|
|
{type: 'text'},
|
|
|
|
dom.prop('value', (use) => formatPercent(use(value))),
|
|
|
|
dom.on('change', (_ev, el) => onChange(el.value)),
|
|
|
|
dom.onKeyDown({
|
|
|
|
ArrowDown: (_ev, el) => onChange(el.value, (val) => val - 1),
|
|
|
|
ArrowUp: (_ev, el) => onChange(el.value, (val) => val + 1),
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
|
|
|
|
// We add spinners as overlay in order to support showing the unit '%' next to the value.
|
|
|
|
cssSpinners(
|
|
|
|
'input',
|
|
|
|
{type: 'number', step: '0.01', min: '0', max: '0.99'},
|
|
|
|
dom.prop('value', value),
|
|
|
|
dom.on('change', (_ev, el) => save(Number(el.value))),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
...args
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
function cssCheckboxRow(label: string, value: KoSaveableObservable<unknown>, ...args: DomElementArg[]) {
|
2021-09-15 08:51:18 +00:00
|
|
|
return cssCheckboxRowObs(label, fromKoSave(value), ...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
function cssCheckboxRowObs(label: string, value: Observable<boolean>, ...args: DomElementArg[]) {
|
2020-10-02 15:10:00 +00:00
|
|
|
return dom('label', cssRow.cls(''),
|
2021-09-15 08:51:18 +00:00
|
|
|
cssRowLabel(label),
|
|
|
|
squareCheckbox(value, ...args),
|
2020-10-02 15:10:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-22 08:45:48 +00:00
|
|
|
function basicPlot(series: Series[], options: ChartOptions, dataOptions: Data): PlotData {
|
2020-10-02 15:10:00 +00:00
|
|
|
trimNonNumericData(series);
|
|
|
|
const errorBars = extractErrorBars(series, options);
|
2021-09-01 09:41:30 +00:00
|
|
|
|
|
|
|
if (dataOptions.type === 'bar') {
|
|
|
|
// Plotly has weirdness when redundant values shows up on the x-axis: the values that shows
|
|
|
|
// up on hover is different than the value on the y-axis. It seems that one is the sum of all
|
|
|
|
// values with same x-axis value, while the other is the last of them. To fix this, we force
|
|
|
|
// unique values for the x-axis.
|
2022-01-03 09:14:06 +00:00
|
|
|
uniqXValues(series);
|
2021-09-01 09:41:30 +00:00
|
|
|
}
|
2022-03-18 10:30:12 +00:00
|
|
|
const [axis1, axis2] = options.orientation === 'h' ? ['y', 'x'] : ['x', 'y'];
|
2021-09-01 09:41:30 +00:00
|
|
|
|
2022-03-18 09:57:54 +00:00
|
|
|
const dataSeries = series.slice(1).map((line: Series): Data => ({
|
|
|
|
name: getSeriesName(line, series.length > 2),
|
2022-05-19 18:56:06 +00:00
|
|
|
[axis1]: replaceEmptyLabels(series[0].values),
|
2022-03-18 10:30:12 +00:00
|
|
|
[axis2]: line.values,
|
|
|
|
[`error_${axis2}`]: errorBars.get(line),
|
|
|
|
orientation: options.orientation,
|
2022-03-18 09:57:54 +00:00
|
|
|
...dataOptions,
|
|
|
|
stackgroup: makeRelativeStackGroup(dataOptions.stackgroup, line.values),
|
|
|
|
}));
|
|
|
|
|
|
|
|
// When stacking, stackgroup will be non-empty (an arbitrary value, set to "A" for line-charts).
|
|
|
|
// We further separate positive series from negative ones, by changing stackgroup to a different
|
|
|
|
// value ("-A") for series which look probably negative. This keeps positive ones above the
|
|
|
|
// x-axis, and negative ones below, as for barmode=relative (which only applies to bar charts).
|
|
|
|
function makeRelativeStackGroup(stackgroup: string|undefined, values: Datum[]) {
|
|
|
|
if (!stackgroup) { return stackgroup; }
|
|
|
|
const firstNonZero = values.find(v => v && (v > 0 || v < 0));
|
|
|
|
const isNegative = firstNonZero && firstNonZero < 0;
|
|
|
|
return isNegative ? "-" + stackgroup : stackgroup;
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
return {
|
2022-03-18 09:57:54 +00:00
|
|
|
data: dataSeries,
|
2020-10-02 15:10:00 +00:00
|
|
|
layout: {
|
2022-08-02 14:54:54 +00:00
|
|
|
[`${axis1}axis`]: {title: series.length > 0 ? {text: series[0].label}: {}},
|
2020-10-02 15:10:00 +00:00
|
|
|
// Include yaxis title for a single y-value series only (2 series total);
|
|
|
|
// If there are fewer than 2 total series, there is no y-series to display.
|
|
|
|
// If there are multiple y-series, a legend will be included instead, and the yaxis title
|
|
|
|
// is less meaningful, so omit it.
|
2022-08-02 14:54:54 +00:00
|
|
|
[`${axis2}axis`]: {title: series.length === 2 ? {text: series[1].label} : {}},
|
2020-10-02 15:10:00 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Most chart types take a list of series and then use the first series for the X-axis, and each
|
|
|
|
// subsequent series for their Y-axis values, allowing for multiple lines on the same plot.
|
|
|
|
// Each series should have the form {label, values}.
|
|
|
|
export const chartTypes: {[name: string]: ChartFunc} = {
|
|
|
|
// TODO There is a lot of code duplication across chart types. Some refactoring is in order.
|
|
|
|
bar(series: Series[], options: ChartOptions): PlotData {
|
|
|
|
return basicPlot(series, options, {type: 'bar'});
|
|
|
|
},
|
|
|
|
line(series: Series[], options: ChartOptions): PlotData {
|
|
|
|
sortByXValues(series);
|
|
|
|
return basicPlot(series, options, {
|
|
|
|
type: 'scatter',
|
|
|
|
connectgaps: options.lineConnectGaps,
|
|
|
|
mode: options.lineMarkers ? 'lines+markers' : 'lines',
|
2022-03-18 09:57:54 +00:00
|
|
|
stackgroup: (options.stacked ? "A" : ""),
|
2020-10-02 15:10:00 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
area(series: Series[], options: ChartOptions): PlotData {
|
|
|
|
sortByXValues(series);
|
|
|
|
return basicPlot(series, options, {
|
|
|
|
type: 'scatter',
|
|
|
|
fill: 'tozeroy',
|
|
|
|
line: {shape: 'spline'},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
scatter(series: Series[], options: ChartOptions): PlotData {
|
|
|
|
return basicPlot(series.slice(1), options, {
|
|
|
|
type: 'scatter',
|
|
|
|
mode: 'text+markers',
|
|
|
|
text: series[0].values as string[],
|
|
|
|
textposition: "bottom center",
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-10-14 15:21:46 +00:00
|
|
|
pie(series: Series[], _options: ChartOptions, dataOptions: DataOptions = {}): PlotData {
|
2020-10-02 15:10:00 +00:00
|
|
|
let line: Series;
|
|
|
|
if (series.length === 0) {
|
|
|
|
return {data: []};
|
|
|
|
}
|
|
|
|
if (series.length > 1) {
|
|
|
|
trimNonNumericData(series);
|
|
|
|
line = series[1];
|
|
|
|
} else {
|
|
|
|
// When there is only one series of labels, simply count their occurrences.
|
|
|
|
line = {label: 'Count', values: series[0].values.map(() => 1)};
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
data: [{
|
|
|
|
type: 'pie',
|
|
|
|
name: getSeriesName(line, false),
|
|
|
|
// nulls cause JS errors when pie charts resize, so replace with blanks.
|
|
|
|
// (a falsy value would cause plotly to show its index, like "2" which is more confusing).
|
2022-05-19 18:56:06 +00:00
|
|
|
labels: replaceEmptyLabels(series[0].values),
|
2020-10-02 15:10:00 +00:00
|
|
|
values: line.values,
|
2021-10-14 15:21:46 +00:00
|
|
|
...dataOptions,
|
2020-10-02 15:10:00 +00:00
|
|
|
}]
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2021-11-22 08:45:48 +00:00
|
|
|
|
|
|
|
donut(series: Series[], options: ChartOptions, dataOptions: DataOptions = {}): PlotData {
|
|
|
|
const hole = isNumber(options.donutHoleSize) ? options.donutHoleSize : DONUT_DEFAULT_HOLE_SIZE;
|
|
|
|
const annotations: Array<Partial<Annotations>> = [];
|
|
|
|
const plotData: PlotData = chartTypes.pie(series, options, {...dataOptions, hole});
|
|
|
|
|
|
|
|
function format(val: number) {
|
|
|
|
if (dataOptions.totalFormatter) {
|
2022-01-13 10:04:56 +00:00
|
|
|
return dataOptions.totalFormatter.formatAny(val);
|
2021-11-22 08:45:48 +00:00
|
|
|
}
|
|
|
|
return String(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.showTotal) {
|
|
|
|
annotations.push({
|
|
|
|
text: format(
|
|
|
|
series.length > 1 ?
|
|
|
|
sum(series[1].values.filter(isNumber)) :
|
|
|
|
plotData.data[0].labels!.length,
|
|
|
|
),
|
|
|
|
showarrow: false,
|
|
|
|
font: {
|
|
|
|
size: options.textSize ?? DONUT_DEFAULT_TEXT_SIZE,
|
|
|
|
}
|
|
|
|
} as any);
|
|
|
|
}
|
|
|
|
return defaultsDeep(
|
|
|
|
plotData,
|
|
|
|
{layout: {annotations}}
|
|
|
|
);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
kaplan_meier(series: Series[]): PlotData {
|
|
|
|
// For this plot, the first series names the category of each point, and the second the
|
|
|
|
// survival time for that point. We turn that into as many series as there are categories.
|
|
|
|
if (series.length < 2) { return {data: []}; }
|
|
|
|
const newSeries = groupIntoSeries(series[0].values, series[1].values);
|
|
|
|
return {
|
|
|
|
data: newSeries.map((line: Series): Data => {
|
|
|
|
const points = kaplanMeierPlot(line.values as number[]);
|
|
|
|
return {
|
|
|
|
type: 'scatter',
|
|
|
|
mode: 'lines',
|
|
|
|
line: {shape: 'hv'},
|
|
|
|
name: getSeriesName(line, false),
|
|
|
|
x: points.map(p => p.x),
|
|
|
|
y: points.map(p => p.y),
|
|
|
|
} as Data;
|
|
|
|
})
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assumes a list of series of the form [xValues, yValues1, yValues2, ...]. Remove from all series
|
|
|
|
* those points for which all of the y-values are non-numeric (e.g. null or a string).
|
|
|
|
*/
|
|
|
|
function trimNonNumericData(series: Series[]): void {
|
|
|
|
const values = series.slice(1).map((s) => s.values);
|
|
|
|
for (const s of series) {
|
|
|
|
s.values = s.values.filter((_, i) => values.some(v => typeof v[i] === 'number'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:56:06 +00:00
|
|
|
/**
|
|
|
|
* Replace empty values with "-", which is relevant for labels in Pie Charts and for X-axis in
|
|
|
|
* other chart types.
|
|
|
|
*
|
|
|
|
* In pie charts, nulls cause JS errors. In other types, nulls in X-axis cause that point to be
|
|
|
|
* omitted (but still affect the Y scale, causing confusion). Replace with "-" rather than blank
|
|
|
|
* because plotly replaces falsy values by their index (eg "2") in Pie charts, which is confusing.
|
|
|
|
*/
|
|
|
|
function replaceEmptyLabels(values: Datum[]): Datum[] {
|
|
|
|
return values.map(v => (v == null || v === "") ? "-" : v);
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Given two parallel arrays, returns an array of series of the form
|
|
|
|
// {label: category, values: array-of-values}
|
|
|
|
function groupIntoSeries(categoryList: Datum[], valueList: Datum[]): Series[] {
|
|
|
|
const groups = new Map();
|
|
|
|
for (const [i, cat] of categoryList.entries()) {
|
|
|
|
if (!groups.has(cat)) { groups.set(cat, []); }
|
|
|
|
groups.get(cat).push(valueList[i]);
|
|
|
|
}
|
|
|
|
return Array.from(groups, ([label, values]) => ({label, values}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a list of survivalValues, returns a list of {x, y} pairs for the kaplanMeier plot.
|
|
|
|
function kaplanMeierPlot(survivalValues: number[]): Array<{x: number, y: number}> {
|
|
|
|
// First get a distribution of survivalValue -> count.
|
|
|
|
const dist = new Map<number, number>();
|
|
|
|
for (const v of survivalValues) {
|
|
|
|
dist.set(v, (dist.get(v) || 0) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the distinct values.
|
|
|
|
const distinctValues = Array.from(dist.keys());
|
|
|
|
distinctValues.sort(nativeCompare);
|
|
|
|
|
|
|
|
// Now generate plot values, with 'x' for survivalValue and 'y' the number of surviving points.
|
|
|
|
let y = survivalValues.length;
|
|
|
|
const points = [{x: 0, y}];
|
|
|
|
for (const x of distinctValues) {
|
|
|
|
y -= dist.get(x)!;
|
|
|
|
points.push({x, y});
|
|
|
|
}
|
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
2021-09-15 08:51:18 +00:00
|
|
|
|
|
|
|
const cssRowLabel = styled('div', `
|
2020-10-02 15:10:00 +00:00
|
|
|
flex: 1 0 0px;
|
|
|
|
margin-right: 8px;
|
|
|
|
|
|
|
|
font-weight: initial; /* negate bootstrap */
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.text};
|
2020-10-02 15:10:00 +00:00
|
|
|
overflow: hidden;
|
|
|
|
text-overflow: ellipsis;
|
2021-09-15 08:51:18 +00:00
|
|
|
user-select: none;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssRowHelp = styled(cssRow, `
|
|
|
|
font-size: ${vars.smallFontSize};
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.lightText};
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
2021-09-15 08:51:18 +00:00
|
|
|
|
|
|
|
const cssAddIcon = styled(icon, `
|
|
|
|
margin-right: 4px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssAddYAxis = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
cursor: pointer;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.controlFg};
|
|
|
|
--icon-color: ${theme.controlFg};
|
2021-09-15 08:51:18 +00:00
|
|
|
|
|
|
|
&:not(:first-child) {
|
|
|
|
margin-top: 8px;
|
|
|
|
}
|
|
|
|
&:hover, &:focus, &:active {
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.controlHoverFg};
|
|
|
|
--icon-color: ${theme.controlHoverFg};
|
2021-09-15 08:51:18 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssRemoveIcon = styled(icon, `
|
|
|
|
display: none;
|
|
|
|
cursor: pointer;
|
|
|
|
flex: none;
|
|
|
|
margin-left: 8px;
|
|
|
|
.${cssFieldEntry.className}:hover & {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssHintRow = styled('div', `
|
|
|
|
margin: -4px 16px 8px 16px;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.lightText};
|
2021-09-15 08:51:18 +00:00
|
|
|
`);
|
2021-11-22 08:45:48 +00:00
|
|
|
|
|
|
|
const cssRangeInput = styled('input', `
|
|
|
|
input& {
|
|
|
|
width: 82px;
|
|
|
|
margin-right: 4px;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssNumberWithSpinner = styled('div', `
|
|
|
|
position: relative;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssNumberInput = styled('input', `
|
|
|
|
width: 55px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
|
|
const cssSpinners = styled('input', `
|
|
|
|
width: 19px;
|
|
|
|
position: absolute;
|
|
|
|
top: 2px;
|
|
|
|
right: 1px;
|
|
|
|
border: none;
|
|
|
|
outline: none;
|
|
|
|
appearance: none;
|
|
|
|
-moz-appearance: none;
|
|
|
|
visibility: hidden;
|
|
|
|
|
|
|
|
.${cssNumberWithSpinner.className}:hover & {
|
|
|
|
visibility: visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* needed for chrome to show spinners, indeed the cursor could be outside of spinners' input
|
|
|
|
element */
|
|
|
|
&[type=number]::-webkit-inner-spin-button {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
`);
|