@@ -47,6 +47,7 @@ import {TimeSeriesPoint} from 'src/app/modules/time-series/models/time-series-po
4747import { parseDate } from 'src/app/utils/date.util' ;
4848import { getColorScheme } from 'src/app/enums/color-scheme.enum' ;
4949import { ChartCommons } from "../../../enums/chart-commons.enum" ;
50+ import { SummaryLabel } from "../models/summary-label.model" ;
5051
5152/**
5253 * Generate line charts with ease and fun 😎
@@ -67,10 +68,9 @@ export class LineChartService {
6768 private _height : number = 550 - this . _margin . top - this . _margin . bottom ;
6869 private _labelGroupHeight : number ;
6970 private _legendGroupTop : number = this . _margin . top + this . _height + 50 ;
70- private _legendGroupLeft : number = this . _margin . left ;
71- private _legendGroupHeight ;
72- private _legendGroupColumnWidth ;
73- private _legendGroupColumns ;
71+ private _legendGroupHeight : number ;
72+ private _legendGroupColumnWidth : number ;
73+ private _legendGroupColumns : number ;
7474 private legendDataMap : Object = { } ;
7575 private brush : BrushBehavior < { } > ;
7676 private focusedLegendEntry : string ;
@@ -118,6 +118,7 @@ this.calculateLegendDimensions();
118118
119119 this . addBrush ( chart , xScale , yScale , data ) ;
120120 this . addLegendsToChart ( chart , incomingData ) ;
121+ this . setSummaryLabel ( chart , incomingData . summaryLabels ) ;
121122 this . addDataLinesToChart ( chart , xScale , yScale , data ) ;
122123
123124 this . bringMouseMarkerToTheFront ( xScale , yScale ) ;
@@ -141,12 +142,14 @@ this.calculateLegendDimensions();
141142
142143 let labelDataMap = { } ;
143144 incomingData . series . forEach ( ( data : EventResultSeriesDTO ) => {
144- let label = data . identifier ;
145+ if ( incomingData . summaryLabels . length > 0 && incomingData . summaryLabels [ 0 ] . key != "measurand" ) {
146+ data . identifier = this . translateMeasurand ( data ) ;
147+ }
145148 let key = this . generateKey ( data ) ;
146149 labelDataMap [ key ] = {
147- text : label ,
150+ text : data . identifier ,
148151 key : key ,
149- show : true ,
152+ show : true
150153 }
151154 } ) ;
152155 this . legendDataMap = labelDataMap ;
@@ -156,27 +159,40 @@ this.calculateLegendDimensions();
156159 * Prepares the incoming data for drawing with D3.js
157160 */
158161 private prepareData ( incomingData : EventResultDataDTO ) : TimeSeries [ ] {
159-
160162 return incomingData . series . map ( ( data : EventResultSeriesDTO ) => {
161163 let lineChartData : TimeSeries = new TimeSeries ( ) ;
164+ if ( incomingData . summaryLabels . length > 0 && incomingData . summaryLabels [ 0 ] . key != "measurand" ) {
165+ data . identifier = this . translateMeasurand ( data ) ;
166+ }
162167 lineChartData . key = this . generateKey ( data ) ;
163168
164169 lineChartData . values = data . data . map ( ( point : EventResultPointDTO ) => {
165170 let lineChartDataPoint : TimeSeriesPoint = new TimeSeriesPoint ( ) ;
166171 lineChartDataPoint . date = parseDate ( point . date ) ;
167172 lineChartDataPoint . value = point . value ;
168- lineChartDataPoint . tooltipText = data . jobGroup + ' | ' + data . measuredEvent + ' : '; // TODO Set exact label text when IT-2793 is implemented
173+ lineChartDataPoint . tooltipText = data . identifier + ' : ' ;
169174 return lineChartDataPoint ;
170175 } ) ;
171176
172177 return lineChartData ;
173178 } ) ;
174179 }
175180
181+ private translateMeasurand ( data : EventResultSeriesDTO ) : string {
182+ let splitLabelList : string [ ] = data . identifier . split ( ' | ' ) ;
183+ let splitLabel : string = this . translationService . instant ( 'frontend.de.iteratec.isr.measurand.' + splitLabelList [ 0 ] ) ;
184+ if ( ! splitLabel . startsWith ( 'frontend.de.iteratec.isr.measurand.' ) ) {
185+ splitLabelList [ 0 ] = splitLabel ;
186+ }
187+ return splitLabelList . join ( ' | ' ) ;
188+ }
189+
176190 private generateKey ( data : EventResultSeriesDTO ) : string {
177- return data . jobGroup
178- + data . measuredEvent
179- + data . data . length ;
191+ let key : string = data . identifier . replace ( / [ ^ _ a - z A - Z 0 - 9 - ] / g, "" ) ;
192+ if ( new RegExp ( '[0-9]' ) . test ( key . charAt ( 0 ) ) ) {
193+ key = key . replace ( / [ 0 - 9 ] / , '_' ) ;
194+ }
195+ return key ;
180196 }
181197
182198 /**
@@ -189,14 +205,69 @@ this.calculateLegendDimensions();
189205 . attr ( 'width' , this . _width + this . _margin . left + this . _margin . right )
190206 . attr ( 'height' , 0 ) ;
191207
208+ svg . append ( 'g' )
209+ . attr ( 'id' , 'header-group' )
210+ . attr ( 'transform' , `translate(${ this . _margin . left } , ${ this . _margin . top - 16 } )` ) ;
211+
212+ let chart = svg . append ( 'g' ) // g = grouping element; group all other stuff into the chart
213+ . attr ( 'id' , 'time-series-chart-drawing-area' )
214+ . attr ( 'transform' , `translate(${ this . _margin . left } , ${ this . _margin . top } )` ) ; // translates the origin to the top left corner (default behavior of D3)
215+
192216 svg . append ( 'g' )
193217 . attr ( 'id' , 'time-series-chart-legend' )
194218 . attr ( 'class' , 'legend-group' )
195- . attr ( 'transform' , `translate(${ this . _legendGroupLeft } , ${ this . _legendGroupTop } )` ) ;
219+ . attr ( 'transform' , `translate(${ this . _margin . left } , ${ this . _legendGroupTop } )` ) ;
196220
197- return svg . append ( 'g' ) // g = grouping element; group all other stuff into the chart
198- . attr ( 'id' , 'time-series-chart-drawing-area' )
199- . attr ( 'transform' , 'translate(' + this . _margin . left + ', ' + this . _margin . top + ')' ) ; // translates the origin to the top left corner (default behavior of D3)
221+ return chart ;
222+ }
223+
224+ private setSummaryLabel ( chart : D3Selection < D3BaseType , { } , D3ContainerElement , { } > , summaryLabels : SummaryLabel [ ] ) : void {
225+ d3Select ( 'g#header-group' ) . selectAll ( '.summary-label-text' ) . remove ( ) ;
226+ if ( summaryLabels . length > 0 ) {
227+ d3Select ( '#header-group' )
228+ . append ( 'g' )
229+ . attr ( 'class' , 'summary-label-text' )
230+ . append ( 'text' )
231+ . attr ( 'id' , 'summary-label-part0' )
232+ . attr ( 'x' , this . _width / 2 )
233+ . attr ( 'text-anchor' , 'middle' )
234+ . attr ( 'fill' , '#555555' ) ;
235+
236+ summaryLabels . forEach ( ( summaryLabel : SummaryLabel , index : number ) => {
237+ this . translationService
238+ . get ( 'frontend.de.iteratec.osm.timeSeries.chart.label.' + summaryLabel . key )
239+ . pipe ( take ( 1 ) )
240+ . subscribe ( ( key : string ) => {
241+ if ( summaryLabel . key == 'measurand' ) {
242+ this . translationService
243+ . get ( 'frontend.de.iteratec.isr.measurand.' + summaryLabel . label )
244+ . pipe ( take ( 1 ) )
245+ . subscribe ( ( label : string ) => {
246+ if ( label . startsWith ( 'frontend.de.iteratec.isr.measurand.' ) ) {
247+ label = summaryLabel . label
248+ }
249+ label = index < summaryLabels . length - 1 ? `${ label } | ` : label ;
250+ this . addSummaryLabel ( key , label , index ) ;
251+ } ) ;
252+ } else {
253+ const label : string = index < summaryLabels . length - 1 ? `${ summaryLabel . label } | ` : summaryLabel . label ;
254+ this . addSummaryLabel ( key , label , index ) ;
255+ }
256+ } ) ;
257+ } ) ;
258+ chart . selectAll ( '.summary-label-text' ) . remove ( ) ;
259+ }
260+ }
261+
262+ private addSummaryLabel ( key : string , label : string , index : number ) : void {
263+ d3Select ( `#summary-label-part${ index } ` )
264+ . append ( 'tspan' )
265+ . attr ( 'id' , `summary-label-part${ index + 1 } ` )
266+ . attr ( 'class' , 'summary-label-key' )
267+ . text ( `${ key } : ` )
268+ . append ( 'tspan' )
269+ . attr ( 'class' , 'summary-label' )
270+ . text ( label ) ;
200271 }
201272
202273 public startResize ( svgElement : ElementRef ) : void {
@@ -538,7 +609,7 @@ this.calculateLegendDimensions();
538609 } )
539610 // fade in
540611 . transition ( ) . duration ( 500 ) . style ( 'opacity' , ( timeSeries : TimeSeries ) => {
541- return ( this . legendDataMap [ timeSeries . key ] . show ) ? '1' : '0.2 ' ;
612+ return ( this . legendDataMap [ timeSeries . key ] . show ) ? '1' : '0.1 ' ;
542613 } ) ;
543614
544615 return resultingSelection ;
0 commit comments