From e4882dd9496efa6ee960b12c7b0a814589b1e204 Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Thu, 25 Feb 2021 17:50:35 -0800 Subject: [PATCH 01/18] New HAnnotations chart primitive --- .../chart-primitives/h-annotations.hbs | 17 ++++++++ .../chart-primitives/h-annotations.js | 40 +++++++++++++++++++ .../chart-primitives/v-annotations.hbs | 2 +- ui/app/styles/charts/chart-annotation.scss | 30 +++++++++++++- 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 ui/app/components/chart-primitives/h-annotations.hbs create mode 100644 ui/app/components/chart-primitives/h-annotations.js diff --git a/ui/app/components/chart-primitives/h-annotations.hbs b/ui/app/components/chart-primitives/h-annotations.hbs new file mode 100644 index 000000000..feefe542b --- /dev/null +++ b/ui/app/components/chart-primitives/h-annotations.hbs @@ -0,0 +1,17 @@ +
+ {{#each this.processed key=@key as |annotation|}} +
+ +
+
+ {{/each}} +
diff --git a/ui/app/components/chart-primitives/h-annotations.js b/ui/app/components/chart-primitives/h-annotations.js new file mode 100644 index 000000000..293cb667e --- /dev/null +++ b/ui/app/components/chart-primitives/h-annotations.js @@ -0,0 +1,40 @@ +import Component from '@glimmer/component'; +import { htmlSafe } from '@ember/template'; +import { action } from '@ember/object'; +import styleString from 'nomad-ui/utils/properties/glimmer-style-string'; + +export default class ChartPrimitiveVAnnotations extends Component { + @styleString + get chartAnnotationsStyle() { + return { + width: this.args.width, + left: this.args.left, + }; + } + + get processed() { + const { scale, prop, annotations, format, labelProp } = this.args; + + if (!annotations || !annotations.length) return null; + + let sortedAnnotations = annotations.sortBy(prop).reverse(); + + return sortedAnnotations.map(annotation => { + const y = scale(annotation[prop]); + const x = 0; + const formattedY = format()(annotation[prop]); + + return { + annotation, + style: htmlSafe(`transform:translate(${x}px,${y}px)`), + label: annotation[labelProp], + a11yLabel: `${annotation[labelProp]} at ${formattedY}`, + }; + }); + } + + @action + selectAnnotation(annotation) { + if (this.args.annotationClick) this.args.annotationClick(annotation); + } +} diff --git a/ui/app/components/chart-primitives/v-annotations.hbs b/ui/app/components/chart-primitives/v-annotations.hbs index f7b13a1d1..1a2a80399 100644 --- a/ui/app/components/chart-primitives/v-annotations.hbs +++ b/ui/app/components/chart-primitives/v-annotations.hbs @@ -1,6 +1,6 @@
{{#each this.processed key=@key as |annotation|}} -
+
@@ -217,6 +257,12 @@ export let VerticalAnnotations = () => { @annotations={{this.annotations}} @annotationClick={{action (mut this.activeAnnotation)}} @activeAnnotation={{this.activeAnnotation}} /> + +
  • + {{datum.formattedX}} + {{datum.formattedY}} +
  • +
    {{/if}} @@ -334,6 +380,14 @@ export let StepLine = () => { <:svg as |c|> + <:after as |c|> + +
  • + {{datum.formattedX}} + {{datum.formattedY}} +
  • +
    +

    {{this.activeAnnotation.info}}

    {{/if}} From 53f9b47c72b0e35f846b88006d2bd0168eabb84a Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Tue, 9 Mar 2021 16:46:49 -0800 Subject: [PATCH 18/18] Move complex annotation hbs logic into JS As @backspace pointed out, we're processing a bunch of other stuff anyway, so might as well process the active state there too where it's more likely to be expected. --- ui/app/components/chart-primitives/h-annotations.hbs | 5 +---- ui/app/components/chart-primitives/h-annotations.js | 11 ++++++++++- ui/app/components/chart-primitives/v-annotations.hbs | 5 +---- ui/app/components/chart-primitives/v-annotations.js | 12 +++++++++++- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/ui/app/components/chart-primitives/h-annotations.hbs b/ui/app/components/chart-primitives/h-annotations.hbs index feefe542b..78999e2b4 100644 --- a/ui/app/components/chart-primitives/h-annotations.hbs +++ b/ui/app/components/chart-primitives/h-annotations.hbs @@ -4,10 +4,7 @@ diff --git a/ui/app/components/chart-primitives/h-annotations.js b/ui/app/components/chart-primitives/h-annotations.js index 293cb667e..e189fc23c 100644 --- a/ui/app/components/chart-primitives/h-annotations.js +++ b/ui/app/components/chart-primitives/h-annotations.js @@ -1,6 +1,6 @@ import Component from '@glimmer/component'; import { htmlSafe } from '@ember/template'; -import { action } from '@ember/object'; +import { action, get } from '@ember/object'; import styleString from 'nomad-ui/utils/properties/glimmer-style-string'; export default class ChartPrimitiveVAnnotations extends Component { @@ -29,10 +29,19 @@ export default class ChartPrimitiveVAnnotations extends Component { style: htmlSafe(`transform:translate(${x}px,${y}px)`), label: annotation[labelProp], a11yLabel: `${annotation[labelProp]} at ${formattedY}`, + isActive: this.annotationIsActive(annotation), }; }); } + annotationIsActive(annotation) { + const { key, activeAnnotation } = this.args; + if (!activeAnnotation) return false; + + if (key) return get(annotation, key) === get(activeAnnotation, key); + return annotation === activeAnnotation; + } + @action selectAnnotation(annotation) { if (this.args.annotationClick) this.args.annotationClick(annotation); diff --git a/ui/app/components/chart-primitives/v-annotations.hbs b/ui/app/components/chart-primitives/v-annotations.hbs index 1a2a80399..7b50ec745 100644 --- a/ui/app/components/chart-primitives/v-annotations.hbs +++ b/ui/app/components/chart-primitives/v-annotations.hbs @@ -4,10 +4,7 @@ diff --git a/ui/app/components/chart-primitives/v-annotations.js b/ui/app/components/chart-primitives/v-annotations.js index f05481778..a49c0f268 100644 --- a/ui/app/components/chart-primitives/v-annotations.js +++ b/ui/app/components/chart-primitives/v-annotations.js @@ -1,6 +1,6 @@ import Component from '@glimmer/component'; import { htmlSafe } from '@ember/template'; -import { action } from '@ember/object'; +import { action, get } from '@ember/object'; import styleString from 'nomad-ui/utils/properties/glimmer-style-string'; const iconFor = { @@ -51,10 +51,20 @@ export default class ChartPrimitiveVAnnotations extends Component { iconClass: iconClassFor[annotation.type], staggerClass: prevHigh ? 'is-staggered' : '', label: `${annotation.type} event at ${formattedX}`, + isActive: this.annotationIsActive(annotation), }; }); } + annotationIsActive(annotation) { + const { key, activeAnnotation } = this.args; + console.log(key, activeAnnotation, annotation); + if (!activeAnnotation) return false; + + if (key) return get(annotation, key) === get(activeAnnotation, key); + return annotation === activeAnnotation; + } + @action selectAnnotation(annotation) { if (this.args.annotationClick) this.args.annotationClick(annotation);