From 1d6264ccabe39e153a82832c20eb46e78e04bb18 Mon Sep 17 00:00:00 2001 From: paleface001 Date: Wed, 18 Mar 2020 23:01:01 +0800 Subject: [PATCH] feat: tooltip formatter Closes #792 --- src/interface/config.ts | 1 + src/plots/area/layer.ts | 26 +++++++++++++++++-- src/plots/bar/layer.ts | 24 +++++++++++++++++- src/plots/bubble/layer.ts | 16 ++++++++++++ src/plots/calendar/layer.ts | 20 ++++++++++++++- src/plots/column/layer.ts | 20 +++++++++++++++ src/plots/density-heatmap/layer.ts | 21 ++++++++++++++++ src/plots/grouped-bar/layer.ts | 14 +++++++++++ src/plots/grouped-column/layer.ts | 14 +++++++++++ src/plots/grouped-rose/layer.ts | 14 +++++++++++ src/plots/heatmap/layer.ts | 28 ++++++++++++++++++--- src/plots/line/layer.ts | 24 +++++++++++++++++- src/plots/pie/layer.ts | 17 +++++++++++++ src/plots/radar/layer.ts | 21 ++++++++++++++++ src/plots/rose/layer.ts | 40 ++++++++++++------------------ src/plots/scatter/layer.ts | 20 ++++++++++++--- src/plots/stacked-area/layer.ts | 14 +++++++++++ src/plots/stacked-bar/layer.ts | 14 +++++++++++ src/plots/stacked-column/layer.ts | 14 +++++++++++ src/plots/stacked-rose/layer.ts | 14 +++++++++++ src/plots/treemap/layer.ts | 3 +++ src/plots/waterfall/layer.ts | 17 +++++++++++++ 22 files changed, 360 insertions(+), 36 deletions(-) diff --git a/src/interface/config.ts b/src/interface/config.ts index aeaa2f81cb..159777bc4f 100644 --- a/src/interface/config.ts +++ b/src/interface/config.ts @@ -158,6 +158,7 @@ export interface Legend { export interface Tooltip { visible?: boolean; + fields?: string[]; shared?: boolean; /** html */ showTitle?: boolean; diff --git a/src/plots/area/layer.ts b/src/plots/area/layer.ts index 98a188801b..80fa142715 100644 --- a/src/plots/area/layer.ts +++ b/src/plots/area/layer.ts @@ -138,7 +138,7 @@ export default class AreaLayer exte protected coord() {} protected addGeometry() { - const props = this.options; + const props: any = this.options; const area = getGeom('area', 'main', { plot: this, }); @@ -147,6 +147,11 @@ export default class AreaLayer exte if (props.label) { this.label(); } + + if (props.tooltip && (props.tooltip.fields || props.tooltip.formatter)) { + this.geometryTooltip(); + } + this.adjustArea(area); this.setConfig('geometry', area); @@ -168,7 +173,7 @@ export default class AreaLayer exte } protected addLine() { - const props = this.options; + const props: any = this.options; const lineConfig = deepMix({}, props.line); if (lineConfig.visible) { const line = getGeom('line', 'guide', { @@ -223,6 +228,23 @@ export default class AreaLayer exte }); } + protected geometryTooltip() { + this.area.tooltip = {}; + const tooltipOptions: any = this.options.tooltip; + if (tooltipOptions.fields) { + this.area.tooltip.fields = tooltipOptions.fields; + } + if (tooltipOptions.formatter) { + this.area.tooltip.callback = tooltipOptions.formatter; + if (!tooltipOptions.fields) { + this.area.tooltip.fields = [this.options.xField, this.options.yField]; + if (this.options.seriesField) { + this.area.tooltip.fields.push(this.options.seriesField); + } + } + } + } + protected parseEvents(eventParser) { super.parseEvents(EventParser); } diff --git a/src/plots/bar/layer.ts b/src/plots/bar/layer.ts index c7cea8f5f8..c15ca49c1d 100644 --- a/src/plots/bar/layer.ts +++ b/src/plots/bar/layer.ts @@ -207,7 +207,7 @@ export default class BaseBarLayer ext protected adjustBar(bar: ElementOption) {} protected addGeometry() { - const props = this.options; + const props: any = this.options; const bar = getGeom('interval', 'main', { positionFields: [props.yField, props.xField], plot: this, @@ -222,6 +222,11 @@ export default class BaseBarLayer ext } this.adjustBar(bar); this.bar = bar; + + if (props.tooltip && (props.tooltip.fields || props.tooltip.formatter)) { + this.geometryTooltip(); + } + this.setConfig('geometry', bar); } @@ -253,6 +258,23 @@ export default class BaseBarLayer ext } } + protected geometryTooltip() { + this.bar.tooltip = {}; + const tooltipOptions: any = this.options.tooltip; + if (tooltipOptions.fields) { + this.bar.tooltip.fields = tooltipOptions.fields; + } + if (tooltipOptions.formatter) { + this.bar.tooltip.callback = tooltipOptions.formatter; + if (!tooltipOptions.fields) { + this.bar.tooltip.fields = [this.options.xField, this.options.yField]; + if (this.options.colorField) { + this.bar.tooltip.fields.push(this.options.colorField); + } + } + } + } + private applyResponsive(stage) { const methods = responsiveMethods[stage]; each(methods, (r) => { diff --git a/src/plots/bubble/layer.ts b/src/plots/bubble/layer.ts index df5ef387b9..f44697c7c1 100644 --- a/src/plots/bubble/layer.ts +++ b/src/plots/bubble/layer.ts @@ -48,6 +48,22 @@ export default class BubbleLayer { } protected addGeometry(): void { - const { valueField, colors } = this.options; + const { valueField, colors, tooltip } = this.options; const polygonConfig: any = { type: 'polygon', position: { @@ -125,9 +125,27 @@ export default class CalendarLayer extends ViewLayer { label: this.extractLabel(), }; + if (tooltip && (tooltip.fields || tooltip.formatter)) { + this.geometryTooltip(polygonConfig); + } + this.setConfig('geometry', polygonConfig); } + protected geometryTooltip(geomConfig) { + geomConfig.tooltip = {}; + const tooltipOptions: any = this.options.tooltip; + if (tooltipOptions.fields) { + geomConfig.tooltip.fields = tooltipOptions.fields; + } + if (tooltipOptions.formatter) { + geomConfig.tooltip.callback = tooltipOptions.formatter; + if (!tooltipOptions.fields) { + geomConfig.tooltip.fields = [WEEK_FIELD, DAY_FIELD]; + } + } + } + private extractLabel() { const props = this.options; const label = props.label; diff --git a/src/plots/column/layer.ts b/src/plots/column/layer.ts index c24543c379..b45c20c4d5 100644 --- a/src/plots/column/layer.ts +++ b/src/plots/column/layer.ts @@ -175,9 +175,29 @@ export default class BaseColumnLayer }, ]; } + + protected geometryTooltip() { + this.bar.tooltip = {}; + const tooltipOptions: any = this.options.tooltip; + if (tooltipOptions.fields) { + this.bar.tooltip.fields = tooltipOptions.fields; + } + if (tooltipOptions.formatter) { + this.bar.tooltip.callback = tooltipOptions.formatter; + if (!tooltipOptions.fields) { + this.bar.tooltip.fields = [this.options.xField, this.options.yField, this.options.groupField]; + } + } + } } registerPlotType('groupedBar', GroupedBarLayer); diff --git a/src/plots/grouped-column/layer.ts b/src/plots/grouped-column/layer.ts index 55e15e8c11..7a83cfa43f 100644 --- a/src/plots/grouped-column/layer.ts +++ b/src/plots/grouped-column/layer.ts @@ -37,6 +37,20 @@ export default class GroupedColumnLayer extends BaseColumnLayer exte } private addLine() { - const props = this.options; + const props: any = this.options; this.line = getGeom('line', 'main', { plot: this, }); @@ -155,6 +155,11 @@ export default class LineLayer exte if (props.label) { this.label(); } + + if (props.tooltip && (props.tooltip.fields || props.tooltip.formatter)) { + this.geometryTooltip(); + } + this.setConfig('geometry', this.line); } @@ -193,6 +198,23 @@ export default class LineLayer exte } } + protected geometryTooltip() { + this.line.tooltip = {}; + const tooltipOptions: any = this.options.tooltip; + if (tooltipOptions.fields) { + this.line.tooltip.fields = tooltipOptions.fields; + } + if (tooltipOptions.formatter) { + this.line.tooltip.callback = tooltipOptions.formatter; + if (!tooltipOptions.fields) { + this.line.tooltip.fields = [this.options.xField, this.options.yField]; + if (this.options.seriesField) { + this.line.tooltip.fields.push(this.options.seriesField); + } + } + } + } + protected animation() { super.animation(); const props = this.options; diff --git a/src/plots/pie/layer.ts b/src/plots/pie/layer.ts index 8d90aef1d4..66dd73ccc0 100644 --- a/src/plots/pie/layer.ts +++ b/src/plots/pie/layer.ts @@ -154,9 +154,26 @@ export default class PieLayer extends if (props.label) { this.label(); } + if (props.tooltip && (props.tooltip.fields || props.tooltip.formatter)) { + this.geometryTooltip(); + } this.setConfig('geometry', pie); } + protected geometryTooltip() { + this.pie.tooltip = {}; + const tooltipOptions: any = this.options.tooltip; + if (tooltipOptions.fields) { + this.pie.tooltip.fields = tooltipOptions.fields; + } + if (tooltipOptions.formatter) { + this.pie.tooltip.callback = tooltipOptions.formatter; + if (!tooltipOptions.fields) { + this.pie.tooltip.fields = [this.options.angleField, this.options.colorField]; + } + } + } + protected animation() { super.animation(); const props = this.options; diff --git a/src/plots/radar/layer.ts b/src/plots/radar/layer.ts index 6898fe83b6..4c43ce3d08 100644 --- a/src/plots/radar/layer.ts +++ b/src/plots/radar/layer.ts @@ -266,6 +266,27 @@ export default class RadarLayer extends ViewLayer { if (props.label) { this.label(); } + if (props.tooltip && (props.tooltip.fields || props.tooltip.formatter)) { + this.geometryTooltip(); + } + } + + protected geometryTooltip() { + const geomConfig = this.line ? this.line : this.area; + geomConfig.tooltip = {}; + const tooltipOptions: any = this.options.tooltip; + if (tooltipOptions.fields) { + geomConfig.tooltip.fields = tooltipOptions.fields; + } + if (tooltipOptions.formatter) { + geomConfig.tooltip.callback = tooltipOptions.formatter; + if (!tooltipOptions.fields) { + geomConfig.tooltip.fields = [this.options.angleField, this.options.radiusField]; + } + if (this.options.seriesField) { + geomConfig.tooltip.fields.push(this.options.seriesField); + } + } } protected label() { diff --git a/src/plots/rose/layer.ts b/src/plots/rose/layer.ts index 48f58679a2..395a13afd1 100644 --- a/src/plots/rose/layer.ts +++ b/src/plots/rose/layer.ts @@ -21,8 +21,6 @@ export interface RoseViewConfig extends ViewConfig { radiusField: string; categoryField: string; colorField?: string; - stackField?: string; - groupField?: string; radius?: number; innerRadius?: number; /** 每个扇形切片的样式 */ @@ -161,7 +159,6 @@ export default class RoseLayer exte protected addGeometry() { const options = this.options; - this.adjustColorFieldMapping(); this.adjustLegendOptions(); const rose = getGeom('interval', 'main', { plot: this, @@ -173,33 +170,28 @@ export default class RoseLayer exte rose.label = this.extractLabel(); rose.adjust = this.adjustRoseAdjust(); this.rose = rose; + if (options.tooltip && (options.tooltip.fields || options.tooltip.formatter)) { + this.geometryTooltip(); + } this.setConfig('geometry', rose); } - protected adjustColorFieldMapping() { - const options = this.options; - if (options.stackField || options.groupField) { - this.options.colorField = null; - } + protected adjustRoseAdjust() { + return; } - protected adjustRoseAdjust() { - /*if (this.options.stackField) { - return [ - { - type: 'stack', - }, - ]; - } else if (this.options.groupField) { - return [ - { - type: 'dodge', - marginRatio: 1, - }, - ]; + protected geometryTooltip() { + this.rose.tooltip = {}; + const tooltipOptions: any = this.options.tooltip; + if (tooltipOptions.fields) { + this.rose.tooltip.fields = tooltipOptions.fields; + } + if (tooltipOptions.formatter) { + this.rose.tooltip.callback = tooltipOptions.formatter; + if (!tooltipOptions.fields) { + this.rose.tooltip.fields = [this.options.radiusField, this.options.categoryField, this.options.colorField]; + } } - return null;*/ - return; } protected animation() { diff --git a/src/plots/scatter/layer.ts b/src/plots/scatter/layer.ts index d036071781..3d8d4c2231 100644 --- a/src/plots/scatter/layer.ts +++ b/src/plots/scatter/layer.ts @@ -217,6 +217,9 @@ export default class ScatterLayer { diff --git a/src/plots/stacked-bar/layer.ts b/src/plots/stacked-bar/layer.ts index b4e38d16d2..7dceb1a614 100644 --- a/src/plots/stacked-bar/layer.ts +++ b/src/plots/stacked-bar/layer.ts @@ -90,6 +90,20 @@ export default class StackedBarLayer { waterfall.style = this._parseStyle(); waterfall.color = this._parseColor(); this.waterfall = waterfall; + if (this.options.tooltip && (this.options.tooltip.fields || this.options.tooltip.formatter)) { + this.geometryTooltip(); + } this.setConfig('geometry', waterfall); } @@ -254,6 +257,20 @@ export default class WaterfallLayer extends ViewLayer { super.parseEvents(EventParser); } + protected geometryTooltip() { + this.waterfall.tooltip = {}; + const tooltipOptions: any = this.options.tooltip; + if (tooltipOptions.fields) { + this.waterfall.tooltip.fields = tooltipOptions.fields; + } + if (tooltipOptions.formatter) { + this.waterfall.tooltip.callback = tooltipOptions.formatter; + if (!tooltipOptions.fields) { + this.waterfall.tooltip.fields = [this.options.xField, VALUE_FIELD]; + } + } + } + /** 牵引线的样式注入到style中 */ private _parseStyle(): IStyleConfig { const style = this.options.waterfallStyle;