From 8f3a9bdfbb551b8c361fd8db72f51cd008e3990b Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Sat, 8 Oct 2022 14:21:17 +0200 Subject: [PATCH] Feature/refactor animation configuration (#1337) * Refactor animation configuration * Update changelog --- CHANGELOG.md | 4 + .../admin-market-data-detail.component.html | 1 + .../components/home-market/home-market.html | 1 + .../home-overview/home-overview.html | 1 + .../position-detail-dialog.html | 1 + .../line-chart.component.stories.ts | 3 +- .../lib/line-chart/line-chart.component.ts | 77 ++++++++++--------- 7 files changed, 51 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2ff6e59c..7da86cd56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Added + +- Supported a progressive line animation in the line chart component + ### Changed - Moved the benchmark comparator from experimental to general availability diff --git a/apps/client/src/app/components/admin-market-data-detail/admin-market-data-detail.component.html b/apps/client/src/app/components/admin-market-data-detail/admin-market-data-detail.component.html index c3d905be0..f7204d02f 100644 --- a/apps/client/src/app/components/admin-market-data-detail/admin-market-data-detail.component.html +++ b/apps/client/src/app/components/admin-market-data-detail/admin-market-data-detail.component.html @@ -2,6 +2,7 @@ { @@ -164,51 +166,29 @@ export class LineChartComponent implements AfterViewInit, OnChanges, OnDestroy { ] }; - const animationDuration = 1000; - - const delayBetweenPoints = animationDuration / labels.length; - const animation = { - x: { - type: 'number', - easing: 'linear', - duration: delayBetweenPoints, - from: NaN, - delay(ctx) { - if (ctx.type !== 'data' || ctx.xStarted) { - return 0; - } - ctx.xStarted = true; - return ctx.index * delayBetweenPoints; - } - }, - y: { - type: 'number', - easing: 'linear', - duration: delayBetweenPoints, - from: 0, - delay(ctx) { - if (ctx.type !== 'data' || ctx.yStarted) { - return 0; - } - ctx.yStarted = true; - return ctx.index * delayBetweenPoints; - } - } - }; - if (this.chartCanvas) { if (this.chart) { this.chart.data = data; this.chart.options.plugins.tooltip = ( this.getTooltipPluginConfiguration() ); - this.chart.options.animation = this.isAnimated && animation; + this.chart.options.animation = + this.isAnimated && + { + x: this.getAnimationConfigurationForAxis({ labels, axis: 'x' }), + y: this.getAnimationConfigurationForAxis({ labels, axis: 'y' }) + }; this.chart.update(); } else { this.chart = new Chart(this.chartCanvas.nativeElement, { data, options: { - animation: this.isAnimated && animation, + animation: + this.isAnimated && + { + x: this.getAnimationConfigurationForAxis({ labels, axis: 'x' }), + y: this.getAnimationConfigurationForAxis({ labels, axis: 'y' }) + }, aspectRatio: 16 / 9, elements: { point: { @@ -291,6 +271,31 @@ export class LineChartComponent implements AfterViewInit, OnChanges, OnDestroy { this.isLoading = false; } + private getAnimationConfigurationForAxis({ + axis, + labels + }: { + axis: 'x' | 'y'; + labels: string[]; + }) { + const delayBetweenPoints = this.ANIMATION_DURATION / labels.length; + + return { + delay(context) { + if (context.type !== 'data' || context[`${axis}Started`]) { + return 0; + } + + context[`${axis}Started`] = true; + return context.index * delayBetweenPoints; + }, + duration: delayBetweenPoints, + easing: 'linear', + from: NaN, + type: 'number' + }; + } + private getTooltipPluginConfiguration() { return { ...getTooltipOptions({