Skip to content

Commit

Permalink
feat: added tooltip on update strategy (#111)
Browse files Browse the repository at this point in the history
* feat: added tooltip on update strategy
* fix: fixed tooltip's cursor pin
* fix: fixed cursor unpin on setSeries
* chore: updated docs
  • Loading branch information
zefirka authored Jul 24, 2023
1 parent 3598351 commit 41f0c10
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions demo/examples/realtime.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ <h1>Realtime updates</h1>
},
tooltip: {
sum: true,
onUpdate: 'none',
},
series: [
{data: timeline.map(id1), color: 'red', id: '1', interpolation: 'smooth'},
Expand Down
6 changes: 4 additions & 2 deletions docs/en/plugins/tooltip.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ Configuration for this example:
- `tooltip.maxLines: PerScale<number> = 10;` - maximum number of lines per scale in the tooltip
- `tooltip.sum: PerScale<boolean> = false` - show the `Sum` row in the tooltip
- `tooltip.sort?: PerScale<SortFn>` - row comparator
- `tooltip.pinable: boolean` - is tooltip pinable
- ~~`tooltip.pinable: boolean` - is tooltip pinable~~ (DEPRECATED)
- `tooltip.strategy: 'pin' | 'all' | 'none'` - tooltip strategy. `pin` - pin tooltip on click, `all` - pin both on click and drag, `none` - don't pin
- `hideNoData?: PerScale<boolean>` - hide rows if the Y value is equal to `null`
- `tooltip.precision?: PerScale<number>` - decimals count in numbers when formatting in the tooltip
- `tooltip.value: PerScale<ValueFormatter>`- formatter for line values
- `tooltip.onUpdate: 'none' | 'reset'` - how to update tooltip when data is updated. `none` - do nothing, `reset` - reset tooltip state

```ts
type ValueFormatter = (value: string | number | null, precision?: number) => string;
Expand Down Expand Up @@ -92,7 +94,7 @@ Tooltip instance is an EventEmitter, which supports the following events:
- `render` - tooltip is rendered
- `destroy` - tooltip is destroyed

Examplt:
Example:

```ts
const yagr = new Yagr(...);
Expand Down
2 changes: 2 additions & 0 deletions src/YagrCore/mixins/dynamic-updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ function setSeriesImpl(
shouldRecalcData = true;
}

this._batch.fns.push(() => this.plugins?.tooltip?.reset());

if (shouldRecalcData || timeline.length) {
batch.recalc = true;
batch.fns.push(() => {
Expand Down
10 changes: 7 additions & 3 deletions src/YagrCore/plugins/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const DEFAULT_TOOLTIP_OPTIONS = {
xOffset: TOOLTIP_X_OFFSET,
yOffset: TOOLTIP_Y_OFFSET,
virtual: false,
onUpdate: 'reset',
} as const;

export type TooltipPlugin = YagrPlugin<
Expand Down Expand Up @@ -176,6 +177,11 @@ class YagrTooltip {
};

reset = () => {
if (this.opts.onUpdate === 'none') {
this.yagr.plugins.cursor?.pin(false);
return;
}

if (this.state.visible) {
this.hide();
}
Expand Down Expand Up @@ -205,9 +211,7 @@ class YagrTooltip {
this.state.pinned = pinState;
const range = this.state.range || [];

if (range[1] === null || range.length < 2) {
this.yagr.plugins.cursor?.pin(pinState);
}
this.yagr.plugins.cursor?.pin(pinState && (range[1] === null || range.length < 2));

if (this.opts.virtual) {
return this.emit(pinState ? 'pin' : 'unpin');
Expand Down
5 changes: 5 additions & 0 deletions src/YagrCore/plugins/tooltip/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ export interface TooltipOptions {
scales?: PerScale<string>;
/** Is tooltip virtual. Used for custom tooltips, which want to reuse common tooltip */
virtual?: boolean;
/** What to do with tooltip on data change
* - reset : reset tooltip
* - none : do nothing
*/
onUpdate?: 'reset' | 'none';
}

export interface TooltipRow {
Expand Down
1 change: 1 addition & 0 deletions src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface YagrChartProps {
filename: string;
};

onChartInited?: (chart: Yagr, meta: YagrMeta) => void;
/** Fires after chart has drawn */
onChartLoad?: (chart: Yagr, meta: YagrMeta) => void;
/** Fires on every range selection */
Expand Down

0 comments on commit 41f0c10

Please sign in to comment.