Skip to content

Commit 4111ae9

Browse files
authored
Merge pull request #287 from iteratec/fix/legendMultiSelect
Selecting entries in legend
2 parents 14b7ce8 + 9127192 commit 4111ae9

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

frontend/src/app/modules/time-series/services/line-chart.service.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838

3939
import 'd3-transition';
4040

41-
import {brushX as d3BrushX} from 'd3-brush';
41+
import {BrushBehavior, brushX as d3BrushX} from 'd3-brush';
4242
import {EventResultDataDTO} from 'src/app/modules/time-series/models/event-result-data.model';
4343
import {EventResultSeriesDTO} from 'src/app/modules/time-series/models/event-result-series.model';
4444
import {EventResultPointDTO} from 'src/app/modules/time-series/models/event-result-point.model';
@@ -62,14 +62,15 @@ export class LineChartService {
6262
// D3 margin conventions
6363
// > With this convention, all subsequent code can ignore margins.
6464
// 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;
7374

7475
// Map that holds all points clustered by their x-axis values
7576
private _xAxisCluster: any = {};
@@ -78,7 +79,6 @@ export class LineChartService {
7879
private _mouseEventCatcher: D3Selection<D3BaseType, {}, D3ContainerElement, {}>;
7980
private _markerTooltip: D3Selection<HTMLDivElement, {}, D3ContainerElement, {}>;
8081

81-
8282
constructor(private translationService: TranslateService) {
8383
}
8484

@@ -98,13 +98,11 @@ export class LineChartService {
9898
* Draws a line chart for the given data into the given svg
9999
*/
100100
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) {
105102
return;
106103
}
107104

105+
let data: TimeSeries[] = this.prepareData(incomingData);
108106
let chart: D3Selection<D3BaseType, {}, D3ContainerElement, {}> = d3Select('g#time-series-chart-drawing-area');
109107
let xScale: D3ScaleTime<number, number> = this.getXScale(data);
110108
let yScale: D3ScaleLinear<number, number> = this.getYScale(data);
@@ -134,6 +132,10 @@ export class LineChartService {
134132
* Set the data for the legend after the incoming data is received
135133
*/
136134
public setLegendData(incomingData: EventResultDataDTO) {
135+
if (incomingData.series.length == 0) {
136+
return;
137+
}
138+
137139
let labelDataMap = {};
138140
incomingData.series.forEach((data: EventResultSeriesDTO) => {
139141
let label = data.identifier;
@@ -754,11 +756,11 @@ export class LineChartService {
754756
.style('opacity', 0)
755757
.remove()
756758
)
757-
.attr("transform", (datum, index) => this.position(index))
759+
.attr("transform", (datum, index) => this.getPosition(index))
758760
.on('click', (datum) => this.onMouseClick(datum, incomingData));
759761
}
760762

761-
private position(index: number): string {
763+
private getPosition(index: number): string {
762764
const columns = 3;
763765
const columnWidth = 550;
764766
const xMargin = 10;
@@ -771,12 +773,24 @@ export class LineChartService {
771773
}
772774

773775
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;
776778
} 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+
}
780794
}
781795
this.drawLineChart(incomingData);
782796
}

0 commit comments

Comments
 (0)