@@ -38,7 +38,7 @@ import {
38
38
39
39
import 'd3-transition' ;
40
40
41
- import { brushX as d3BrushX } from 'd3-brush' ;
41
+ import { BrushBehavior , brushX as d3BrushX } from 'd3-brush' ;
42
42
import { EventResultDataDTO } from 'src/app/modules/time-series/models/event-result-data.model' ;
43
43
import { EventResultSeriesDTO } from 'src/app/modules/time-series/models/event-result-series.model' ;
44
44
import { EventResultPointDTO } from 'src/app/modules/time-series/models/event-result-point.model' ;
@@ -62,14 +62,15 @@ export class LineChartService {
62
62
// D3 margin conventions
63
63
// > With this convention, all subsequent code can ignore margins.
64
64
// see: https://bl.ocks.org/mbostock/3019563
65
- private _margin = { top : 40 , right : 70 , bottom : 40 , left : 60 } ;
66
- private _width = 600 - this . _margin . left - this . _margin . right ;
67
- private _height = 550 - this . _margin . top - this . _margin . bottom ;
68
- private _labelGroupHeight ;
69
- private _legendGroupTop = this . _margin . top + this . _height + 50 ;
70
- private _legendGroupLeft = this . _margin . left ;
71
- private legendDataMap = { } ;
72
- private brush ;
65
+ private _margin : any = { top : 40 , right : 70 , bottom : 40 , left : 60 } ;
66
+ private _width : number = 600 - this . _margin . left - this . _margin . right ;
67
+ private _height : number = 550 - this . _margin . top - this . _margin . bottom ;
68
+ private _labelGroupHeight : number ;
69
+ private _legendGroupTop : number = this . _margin . top + this . _height + 50 ;
70
+ private _legendGroupLeft : number = this . _margin . left ;
71
+ private legendDataMap : Object = { } ;
72
+ private brush : BrushBehavior < { } > ;
73
+ private focusedLegendEntry : string ;
73
74
74
75
// Map that holds all points clustered by their x-axis values
75
76
private _xAxisCluster : any = { } ;
@@ -78,7 +79,6 @@ export class LineChartService {
78
79
private _mouseEventCatcher : D3Selection < D3BaseType , { } , D3ContainerElement , { } > ;
79
80
private _markerTooltip : D3Selection < HTMLDivElement , { } , D3ContainerElement , { } > ;
80
81
81
-
82
82
constructor ( private translationService : TranslateService ) {
83
83
}
84
84
@@ -98,13 +98,11 @@ export class LineChartService {
98
98
* Draws a line chart for the given data into the given svg
99
99
*/
100
100
public drawLineChart ( incomingData : EventResultDataDTO ) : void {
101
-
102
- let data : TimeSeries [ ] = this . prepareData ( incomingData ) ;
103
-
104
- if ( data . length == 0 ) {
101
+ if ( incomingData . series . length == 0 ) {
105
102
return ;
106
103
}
107
104
105
+ let data : TimeSeries [ ] = this . prepareData ( incomingData ) ;
108
106
let chart : D3Selection < D3BaseType , { } , D3ContainerElement , { } > = d3Select ( 'g#time-series-chart-drawing-area' ) ;
109
107
let xScale : D3ScaleTime < number , number > = this . getXScale ( data ) ;
110
108
let yScale : D3ScaleLinear < number , number > = this . getYScale ( data ) ;
@@ -134,6 +132,10 @@ export class LineChartService {
134
132
* Set the data for the legend after the incoming data is received
135
133
*/
136
134
public setLegendData ( incomingData : EventResultDataDTO ) {
135
+ if ( incomingData . series . length == 0 ) {
136
+ return ;
137
+ }
138
+
137
139
let labelDataMap = { } ;
138
140
incomingData . series . forEach ( ( data : EventResultSeriesDTO ) => {
139
141
let label = data . identifier ;
@@ -754,11 +756,11 @@ export class LineChartService {
754
756
. style ( 'opacity' , 0 )
755
757
. remove ( )
756
758
)
757
- . attr ( "transform" , ( datum , index ) => this . position ( index ) )
759
+ . attr ( "transform" , ( datum , index ) => this . getPosition ( index ) )
758
760
. on ( 'click' , ( datum ) => this . onMouseClick ( datum , incomingData ) ) ;
759
761
}
760
762
761
- private position ( index : number ) : string {
763
+ private getPosition ( index : number ) : string {
762
764
const columns = 3 ;
763
765
const columnWidth = 550 ;
764
766
const xMargin = 10 ;
@@ -771,12 +773,24 @@ export class LineChartService {
771
773
}
772
774
773
775
private onMouseClick ( labelKey : string , incomingData : EventResultDataDTO ) : void {
774
- if ( d3Event . metaKey ) {
775
- this . legendDataMap [ labelKey ] . show ? this . legendDataMap [ labelKey ] . show = false : this . legendDataMap [ labelKey ] . show = true ;
776
+ if ( d3Event . metaKey || d3Event . ctrlKey ) {
777
+ this . legendDataMap [ labelKey ] . show = ! this . legendDataMap [ labelKey ] . show ;
776
778
} else {
777
- Object . keys ( this . legendDataMap ) . forEach ( ( legend ) => {
778
- this . legendDataMap [ legend ] . show = legend === labelKey ;
779
- } ) ;
779
+ if ( labelKey == this . focusedLegendEntry ) {
780
+ Object . keys ( this . legendDataMap ) . forEach ( ( legend ) => {
781
+ this . legendDataMap [ legend ] . show = true ;
782
+ } ) ;
783
+ this . focusedLegendEntry = "" ;
784
+ } else {
785
+ Object . keys ( this . legendDataMap ) . forEach ( ( legend ) => {
786
+ if ( legend === labelKey ) {
787
+ this . legendDataMap [ legend ] . show = true ;
788
+ this . focusedLegendEntry = legend ;
789
+ } else {
790
+ this . legendDataMap [ legend ] . show = false ;
791
+ }
792
+ } ) ;
793
+ }
780
794
}
781
795
this . drawLineChart ( incomingData ) ;
782
796
}
0 commit comments