@@ -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,15 +62,17 @@ 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 _legendGroupTop = this . _margin . top + this . _height + 50 ;
69
- private _legendGroupLeft = this . _margin . left ;
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 ;
70
71
private _legendGroupColumnWidth ;
71
72
private _legendGroupColumns ;
72
- private legendDataMap = { } ;
73
- private brush ;
73
+ private legendDataMap : Object = { } ;
74
+ private brush : BrushBehavior < { } > ;
75
+ private focusedLegendEntry : string ;
74
76
75
77
// Map that holds all points clustered by their x-axis values
76
78
private _xAxisCluster : any = { } ;
@@ -98,13 +100,11 @@ export class LineChartService {
98
100
* Draws a line chart for the given data into the given svg
99
101
*/
100
102
public drawLineChart ( incomingData : EventResultDataDTO ) : void {
101
-
102
- let data : TimeSeries [ ] = this . prepareData ( incomingData ) ;
103
-
104
- if ( data . length == 0 ) {
103
+ if ( incomingData . series . length == 0 ) {
105
104
return ;
106
105
}
107
106
107
+ let data : TimeSeries [ ] = this . prepareData ( incomingData ) ;
108
108
let chart : D3Selection < D3BaseType , { } , D3ContainerElement , { } > = d3Select ( 'g#time-series-chart-drawing-area' ) ;
109
109
let xScale : D3ScaleTime < number , number > = this . getXScale ( data ) ;
110
110
let yScale : D3ScaleLinear < number , number > = this . getYScale ( data ) ;
@@ -134,6 +134,10 @@ export class LineChartService {
134
134
* Set the data for the legend after the incoming data is received
135
135
*/
136
136
public setLegendData ( incomingData : EventResultDataDTO ) {
137
+ if ( incomingData . series . length == 0 ) {
138
+ return ;
139
+ }
140
+
137
141
let labelDataMap = { } ;
138
142
incomingData . series . forEach ( ( data : EventResultSeriesDTO ) => {
139
143
let label = data . identifier ;
@@ -784,24 +788,36 @@ export class LineChartService {
784
788
. style ( 'opacity' , 0 )
785
789
. remove ( )
786
790
)
787
- . attr ( "transform" , ( datum , index ) => this . position ( index ) )
791
+ . attr ( "transform" , ( datum , index ) => this . getPosition ( index ) )
788
792
. on ( 'click' , ( datum ) => this . onMouseClick ( datum , incomingData ) ) ;
789
793
}
790
794
791
- private position ( index : number ) : string {
795
+ private getPosition ( index : number ) : string {
792
796
const x = index % this . _legendGroupColumns * this . _legendGroupColumnWidth ;
793
797
const y = Math . floor ( index / this . _legendGroupColumns ) * ChartCommons . LABEL_HEIGHT + 12 ;
794
798
795
799
return "translate(" + x + "," + y + ")" ;
796
800
}
797
801
798
802
private onMouseClick ( labelKey : string , incomingData : EventResultDataDTO ) : void {
799
- if ( d3Event . metaKey ) {
800
- this . legendDataMap [ labelKey ] . show ? this . legendDataMap [ labelKey ] . show = false : this . legendDataMap [ labelKey ] . show = true ;
803
+ if ( d3Event . metaKey || d3Event . ctrlKey ) {
804
+ this . legendDataMap [ labelKey ] . show = ! this . legendDataMap [ labelKey ] . show ;
801
805
} else {
802
- Object . keys ( this . legendDataMap ) . forEach ( ( legend ) => {
803
- this . legendDataMap [ legend ] . show = legend === labelKey ;
804
- } ) ;
806
+ if ( labelKey == this . focusedLegendEntry ) {
807
+ Object . keys ( this . legendDataMap ) . forEach ( ( legend ) => {
808
+ this . legendDataMap [ legend ] . show = true ;
809
+ } ) ;
810
+ this . focusedLegendEntry = "" ;
811
+ } else {
812
+ Object . keys ( this . legendDataMap ) . forEach ( ( legend ) => {
813
+ if ( legend === labelKey ) {
814
+ this . legendDataMap [ legend ] . show = true ;
815
+ this . focusedLegendEntry = legend ;
816
+ } else {
817
+ this . legendDataMap [ legend ] . show = false ;
818
+ }
819
+ } ) ;
820
+ }
805
821
}
806
822
this . drawLineChart ( incomingData ) ;
807
823
}
0 commit comments