@@ -38,6 +38,8 @@ import {
3838 validateInterval ,
3939 validateOffset ,
4040 getLovelace ,
41+ isUsingServerTimezone ,
42+ computeTimezoneDiffWithLocal ,
4143} from './utils' ;
4244import ApexCharts from 'apexcharts' ;
4345import { Ripple } from '@material/mwc-ripple' ;
@@ -80,6 +82,7 @@ import {
8082import parse from 'parse-duration' ;
8183import tinycolor from '@ctrl/tinycolor' ;
8284import { actionHandler } from './action-handler-directive' ;
85+ import { OverrideFrontendLocaleData } from './types-ha' ;
8386
8487/* eslint no-console: 0 */
8588console . info (
@@ -163,6 +166,8 @@ class ChartsCard extends LitElement {
163166
164167 private _yAxisConfig ?: ChartCardYAxis [ ] ;
165168
169+ private _serverTimeOffset = 0 ;
170+
166171 @property ( { attribute : false } ) _lastUpdated : Date = new Date ( ) ;
167172
168173 @property ( { type : Boolean } ) private _warning = false ;
@@ -287,6 +292,7 @@ class ChartsCard extends LitElement {
287292 this . _loaded = false ;
288293 this . _dataLoaded = false ;
289294 this . _updating = false ;
295+ this . _serverTimeOffset = 0 ;
290296 if ( this . _apexBrush ) {
291297 this . _apexBrush . destroy ( ) ;
292298 this . _apexBrush = undefined ;
@@ -753,6 +759,10 @@ class ChartsCard extends LitElement {
753759 private async _initialLoad ( ) {
754760 await this . updateComplete ;
755761
762+ if ( isUsingServerTimezone ( this . _hass ) ) {
763+ this . _serverTimeOffset = computeTimezoneDiffWithLocal ( this . _hass ?. config . time_zone ) ;
764+ }
765+
756766 if ( ! this . _apexChart && this . shadowRoot && this . _config && this . shadowRoot . querySelector ( '#graph' ) ) {
757767 this . _loaded = true ;
758768 const graph = this . shadowRoot . querySelector ( '#graph' ) ;
@@ -826,33 +836,41 @@ class ChartsCard extends LitElement {
826836 const offset = ( this . _seriesOffset [ index ] || 0 ) - ( this . _seriesTimeDelta [ index ] || 0 ) ;
827837 if ( offset ) {
828838 data = offsetData ( graph . history , offset ) ;
839+ } else if ( this . _serverTimeOffset ) {
840+ data = offsetData ( graph . history , this . _serverTimeOffset ) ;
829841 } else {
830842 data = [ ...graph . history ] ;
831843 }
832844 if ( this . _config ?. series [ index ] . type !== 'column' && this . _config ?. series [ index ] . extend_to ) {
833845 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
834846 const lastPoint = data . slice ( - 1 ) [ 0 ] ! ;
835- if ( this . _config ?. series [ index ] . extend_to === 'end' && lastPoint [ 0 ] < end . getTime ( ) ) {
847+ if (
848+ this . _config ?. series [ index ] . extend_to === 'end' &&
849+ lastPoint [ 0 ] < end . getTime ( ) - this . _serverTimeOffset
850+ ) {
836851 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
837- data . push ( [ end . getTime ( ) , lastPoint [ 1 ] ] ) ;
838- } else if ( this . _config ?. series [ index ] . extend_to === 'now' && lastPoint [ 0 ] < now . getTime ( ) ) {
852+ data . push ( [ end . getTime ( ) - this . _serverTimeOffset , lastPoint [ 1 ] ] ) ;
853+ } else if (
854+ this . _config ?. series [ index ] . extend_to === 'now' &&
855+ lastPoint [ 0 ] < now . getTime ( ) - this . _serverTimeOffset
856+ ) {
839857 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
840- data . push ( [ now . getTime ( ) , lastPoint [ 1 ] ] ) ;
858+ data . push ( [ now . getTime ( ) - this . _serverTimeOffset , lastPoint [ 1 ] ] ) ;
841859 }
842860 }
843861 const result = this . _config ?. series [ index ] . invert ? { data : this . _invertData ( data ) } : { data } ;
844862 if ( this . _config ?. series [ index ] . show . in_chart ) graphData . series . push ( result ) ;
845863 if ( this . _config ?. series [ index ] . show . in_brush ) brushData . series . push ( result ) ;
846864 return ;
847865 } ) ;
848- graphData . annotations = this . _computeAnnotations ( start , end , now ) ;
866+ graphData . annotations = this . _computeAnnotations ( start , end , new Date ( now . getTime ( ) - this . _serverTimeOffset ) ) ;
849867 if ( this . _yAxisConfig ) {
850868 graphData . yaxis = this . _computeYAxisAutoMinMax ( start , end ) ;
851869 }
852870 if ( ! this . _apexBrush ) {
853871 graphData . xaxis = {
854- min : start . getTime ( ) ,
855- max : this . _findEndOfChart ( end , false ) ,
872+ min : start . getTime ( ) - this . _serverTimeOffset ,
873+ max : this . _findEndOfChart ( new Date ( end . getTime ( ) - this . _serverTimeOffset ) , false ) ,
856874 } ;
857875 }
858876 } else {
@@ -949,8 +967,8 @@ class ChartsCard extends LitElement {
949967 TIMESERIES_TYPES . includes ( this . _config . chart_type ) ? false : true ,
950968 ) ;
951969 if ( this . _apexBrush ) {
952- const newMin = start . getTime ( ) ;
953- const newMax = this . _findEndOfChart ( end , false ) ;
970+ const newMin = start . getTime ( ) - this . _serverTimeOffset ;
971+ const newMax = this . _findEndOfChart ( new Date ( end . getTime ( ) - this . _serverTimeOffset ) , false ) ;
954972 brushData . xaxis = {
955973 min : newMin ,
956974 max : newMax ,
@@ -1008,6 +1026,7 @@ class ChartsCard extends LitElement {
10081026 ? new Date ( start . getTime ( ) + this . _seriesOffset [ index ] ) . getTime ( )
10091027 : start . getTime ( ) ,
10101028 this . _seriesOffset [ index ] ? new Date ( end . getTime ( ) + this . _seriesOffset [ index ] ) . getTime ( ) : end . getTime ( ) ,
1029+ this . _serverTimeOffset ,
10111030 ) || {
10121031 min : [ 0 , null ] ,
10131032 max : [ 0 , null ] ,
@@ -1444,14 +1463,18 @@ class ChartsCard extends LitElement {
14441463 private _getSpanDates ( ) : { start : Date ; end : Date } {
14451464 let end = new Date ( ) ;
14461465 let start = new Date ( end . getTime ( ) - this . _graphSpan + 1 ) ;
1447- // Span
1466+ const curMoment = moment ( ) ;
1467+ if ( ( this . _hass ?. locale as OverrideFrontendLocaleData ) . time_zone === 'server' ) {
1468+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1469+ curMoment. tz ( this . _hass ! . config . time_zone ) ;
1470+ }
14481471 if ( this . _config ?. span ?. start ) {
14491472 // Just Span
1450- const startM = moment ( ) . startOf ( this . _config . span . start ) ;
1473+ const startM = curMoment . startOf ( this . _config . span . start ) ;
14511474 start = startM . toDate ( ) ;
14521475 end = new Date ( start . getTime ( ) + this . _graphSpan ) ;
14531476 } else if ( this . _config ?. span ?. end ) {
1454- const endM = moment ( ) . endOf ( this . _config . span . end ) ;
1477+ const endM = curMoment . endOf ( this . _config . span . end ) ;
14551478 end = new Date ( endM . toDate ( ) . getTime ( ) + 1 ) ;
14561479 start = new Date ( end . getTime ( ) - this . _graphSpan + 1 ) ;
14571480 }
0 commit comments