Skip to content

Commit 960bc77

Browse files
committed
#327 Legend fix
1 parent 7431ae8 commit 960bc77

File tree

1 file changed

+49
-44
lines changed

1 file changed

+49
-44
lines changed

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

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export class LineChartService {
262262
};
263263

264264
const createContextMenu = () => {
265-
if(!this._contextMenuBackground) {
265+
if (!this._contextMenuBackground) {
266266
this._contextMenuBackground = d3Select('body')
267267
.selectAll('.d3-context-menu-background')
268268
.data([1])
@@ -276,7 +276,7 @@ export class LineChartService {
276276
}, false);
277277
}
278278

279-
if(!this._contextMenu) {
279+
if (!this._contextMenu) {
280280
this._contextMenu = d3Select('body')
281281
.selectAll('.d3-context-menu')
282282
.data([1])
@@ -730,14 +730,18 @@ export class LineChartService {
730730
// Find series with only one dot in range
731731
const seriesWithOneDot = data
732732
.map((d: TimeSeries, index: number) => {
733-
return {key: d.key, values: d.values.filter((point) => point.date <= maxDate && point.date >= minDate), index: index}
733+
return {
734+
key: d.key,
735+
values: d.values.filter((point) => point.date <= maxDate && point.date >= minDate),
736+
index: index
737+
}
734738
})
735739
.filter((d: TimeSeries) => {
736740
return d.values.length === 1;
737741
});
738742

739743
// If there is no series with one dot, end the function
740-
if(seriesWithOneDot.length === 0) {
744+
if (seriesWithOneDot.length === 0) {
741745
return;
742746
}
743747

@@ -1198,48 +1202,49 @@ export class LineChartService {
11981202
};
11991203

12001204
return (chart: D3Selection<D3BaseType, {}, D3ContainerElement, {}>, incomingData: EventResultDataDTO): void => {
1201-
chart.selectAll('.legend-entry').remove();
1202-
const legendEntry = d3Select('.legend-group').selectAll('.legend-entry').data(Object.keys(this.legendDataMap));
1203-
legendEntry.join(
1204-
enter => {
1205-
const legendElement = enter
1206-
.append('g')
1207-
.attr('class', 'legend-entry');
1208-
legendElement
1209-
.append('rect')
1210-
.attr('class', 'legend-rect')
1211-
.attr('height', ChartCommons.COLOR_PREVIEW_SIZE)
1212-
.attr('width', ChartCommons.COLOR_PREVIEW_SIZE)
1213-
.attr("rx", 2)
1214-
.attr("ry", 2)
1215-
.attr('fill', (key: string, index: number) => {
1216-
return getColorScheme()[incomingData.series.length - index - 1]
1217-
});
1218-
legendElement
1219-
.append('text')
1220-
.attr('class', 'legend-text')
1221-
.attr('x', 10 + 5)
1222-
.attr('y', ChartCommons.COLOR_PREVIEW_SIZE)
1223-
.text(datum => this.legendDataMap[datum].text);
1224-
return legendElement;
1225-
},
1226-
update => {
1227-
update
1205+
const legendGroup = d3Select('.legend-group');
1206+
1207+
// Remove old legend elements
1208+
legendGroup.selectAll('.legend-entry').remove();
1209+
1210+
legendGroup
1211+
.selectAll()
1212+
.data(Object.keys(this.legendDataMap))
1213+
.join(
1214+
enter => {
1215+
const legendElement = enter
1216+
.append('g')
1217+
.attr('class', 'legend-entry')
1218+
.style('opacity', (datum) => {
1219+
return (this.legendDataMap[datum].show) ? 1 : 0.2;
1220+
});
1221+
legendElement
1222+
.append('rect')
1223+
.attr('class', 'legend-rect')
1224+
.attr('height', ChartCommons.COLOR_PREVIEW_SIZE)
1225+
.attr('width', ChartCommons.COLOR_PREVIEW_SIZE)
1226+
.attr("rx", 2)
1227+
.attr("ry", 2)
1228+
.attr('fill', (key: string, index: number) => {
1229+
return getColorScheme()[incomingData.series.length - index - 1]
1230+
});
1231+
legendElement
1232+
.append('text')
1233+
.attr('class', 'legend-text')
1234+
.attr('x', 10 + 5)
1235+
.attr('y', ChartCommons.COLOR_PREVIEW_SIZE)
1236+
.text(datum => this.legendDataMap[datum].text);
1237+
return legendElement;
1238+
},
1239+
update => update,
1240+
exit => exit
12281241
.transition()
12291242
.duration(ChartCommons.TRANSITION_DURATION)
1230-
.style('opacity', (datum) => {
1231-
return (this.legendDataMap[datum].show) ? 1 : 0.2;
1232-
});
1233-
return update;
1234-
},
1235-
exit => exit
1236-
.transition()
1237-
.duration(ChartCommons.TRANSITION_DURATION)
1238-
.style('opacity', 0)
1239-
.remove()
1240-
)
1241-
.attr("transform", (datum, index) => getPosition(index))
1242-
.on('click', (datum) => onLegendClick(datum, incomingData));
1243+
.style('opacity', 0)
1244+
.remove()
1245+
)
1246+
.attr("transform", (_, index: number) => getPosition(index))
1247+
.on('click', (datum: string) => onLegendClick(datum, incomingData));
12431248
};
12441249
})();
12451250
}

0 commit comments

Comments
 (0)