|
1 | 1 | import 'array-flat-polyfill'; |
2 | 2 | import { LitElement, html, customElement, property, TemplateResult, CSSResult, PropertyValues } from 'lit-element'; |
3 | 3 | import { ClassInfo, classMap } from 'lit-html/directives/class-map'; |
4 | | -import { ChartCardConfig, ChartCardSeriesConfig, EntityCachePoints, EntityEntryCache } from './types'; |
| 4 | +import { ChartCardConfig, ChartCardSeriesConfig, EntityCachePoints, EntityEntryCache, HistoryPoint } from './types'; |
5 | 5 | import { getLovelace, HomeAssistant } from 'custom-card-helpers'; |
6 | 6 | import localForage from 'localforage'; |
7 | 7 | import * as pjson from '../package.json'; |
@@ -307,6 +307,7 @@ class ChartsCard extends LitElement { |
307 | 307 | const defColors = this._config?.color_list || DEFAULT_COLORS; |
308 | 308 | if (this._config) { |
309 | 309 | this._graphs = this._config.series.map((serie, index) => { |
| 310 | + serie.index = index; |
310 | 311 | if (!this._headerColors[index]) { |
311 | 312 | this._headerColors[index] = defColors[index % defColors.length]; |
312 | 313 | } |
@@ -540,28 +541,8 @@ class ChartsCard extends LitElement { |
540 | 541 | min: start.getTime(), |
541 | 542 | max: this._findEndOfChart(end), |
542 | 543 | }, |
| 544 | + annotations: this._computeAnnotations(start, end), |
543 | 545 | }; |
544 | | - if (this._config.now?.show) { |
545 | | - const color = computeColor(this._config.now.color || 'var(--primary-color)'); |
546 | | - const textColor = computeTextColor(color); |
547 | | - graphData.annotations = { |
548 | | - xaxis: [ |
549 | | - { |
550 | | - x: new Date().getTime(), |
551 | | - strokeDashArray: 3, |
552 | | - label: { |
553 | | - text: this._config.now.label, |
554 | | - borderColor: color, |
555 | | - style: { |
556 | | - color: textColor, |
557 | | - background: color, |
558 | | - }, |
559 | | - }, |
560 | | - borderColor: color, |
561 | | - }, |
562 | | - ], |
563 | | - }; |
564 | | - } |
565 | 546 | } else { |
566 | 547 | // No timeline charts |
567 | 548 | graphData = { |
@@ -633,6 +614,87 @@ class ChartsCard extends LitElement { |
633 | 614 | this._updating = false; |
634 | 615 | } |
635 | 616 |
|
| 617 | + private _computeAnnotations(start: Date, end: Date) { |
| 618 | + return { |
| 619 | + ...this._computeMinMaxPointsAnnotations(start, end), |
| 620 | + ...this._computeNowAnnotation(), |
| 621 | + }; |
| 622 | + } |
| 623 | + |
| 624 | + private _computeMinMaxPointsAnnotations(start: Date, end: Date) { |
| 625 | + return { |
| 626 | + points: this._config?.series_in_graph.flatMap((serie, index) => { |
| 627 | + if (serie.show.extremas) { |
| 628 | + const { min, max } = this._graphs?.[serie.index]?.minMaxWithTimestamp(start.getTime(), end.getTime()) || { |
| 629 | + min: [0, null], |
| 630 | + max: [0, null], |
| 631 | + }; |
| 632 | + const bgColor = computeColor(this._colors[index]); |
| 633 | + const txtColor = computeTextColor(bgColor); |
| 634 | + if (!min[0] || !max[0]) return []; |
| 635 | + return [ |
| 636 | + this._getPointAnnotationStyle(min, bgColor, txtColor, serie, index), |
| 637 | + this._getPointAnnotationStyle(max, bgColor, txtColor, serie, index), |
| 638 | + ]; |
| 639 | + } else { |
| 640 | + return []; |
| 641 | + } |
| 642 | + }), |
| 643 | + }; |
| 644 | + } |
| 645 | + |
| 646 | + private _getPointAnnotationStyle( |
| 647 | + value: HistoryPoint, |
| 648 | + bgColor: string, |
| 649 | + txtColor: string, |
| 650 | + serie: ChartCardSeriesConfig, |
| 651 | + index: number, |
| 652 | + ) { |
| 653 | + return { |
| 654 | + x: value[0], |
| 655 | + y: value[1], |
| 656 | + seriesIndex: index, |
| 657 | + marker: { |
| 658 | + strokeColor: bgColor, |
| 659 | + fillColor: 'var(--card-background-color)', |
| 660 | + }, |
| 661 | + label: { |
| 662 | + text: truncateFloat(value[1], serie.float_precision)?.toString(), |
| 663 | + borderColor: 'var(--card-background-color)', |
| 664 | + borderWidth: 2, |
| 665 | + style: { |
| 666 | + background: bgColor, |
| 667 | + color: txtColor, |
| 668 | + }, |
| 669 | + }, |
| 670 | + }; |
| 671 | + } |
| 672 | + |
| 673 | + private _computeNowAnnotation() { |
| 674 | + if (this._config?.now?.show) { |
| 675 | + const color = computeColor(this._config.now.color || 'var(--primary-color)'); |
| 676 | + const textColor = computeTextColor(color); |
| 677 | + return { |
| 678 | + xaxis: [ |
| 679 | + { |
| 680 | + x: new Date().getTime(), |
| 681 | + strokeDashArray: 3, |
| 682 | + label: { |
| 683 | + text: this._config.now.label, |
| 684 | + borderColor: color, |
| 685 | + style: { |
| 686 | + color: textColor, |
| 687 | + background: color, |
| 688 | + }, |
| 689 | + }, |
| 690 | + borderColor: color, |
| 691 | + }, |
| 692 | + ], |
| 693 | + }; |
| 694 | + } |
| 695 | + return {}; |
| 696 | + } |
| 697 | + |
636 | 698 | private _computeChartColors(): (string | (({ value }) => string))[] { |
637 | 699 | const defaultColors: (string | (({ value }) => string))[] = computeColorsWithAlpha( |
638 | 700 | this._colors, |
|
0 commit comments