Skip to content

Commit 8a23f68

Browse files
authored
Merge pull request #288 from iteratec/feature/legendDynamicPosition
Feature/legend dynamic position
2 parents 4111ae9 + f786981 commit 8a23f68

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

frontend/src/app/enums/chart-commons.enum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ export enum ChartCommons {
66
CHART_HEADER_HEIGHT = 40,
77
COLOR_PREVIEW_SIZE = 10,
88
COLOR_PREVIEW_MARGIN = 5,
9-
LABEL_HEIGHT = 30
9+
LABEL_HEIGHT = 18
1010
}

frontend/src/app/modules/time-series/components/time-series-line-chart/time-series-line-chart.component.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,11 @@ osm-time-series-line-chart {
5959

6060
}
6161
}
62+
63+
.legend-entry {
64+
cursor: pointer;
65+
}
66+
67+
.legend-text {
68+
font-size: 12px;
69+
}

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ export class LineChartService {
6868
private _labelGroupHeight: number;
6969
private _legendGroupTop: number = this._margin.top + this._height + 50;
7070
private _legendGroupLeft: number = this._margin.left;
71+
private _legendGroupHeight;
72+
private _legendGroupColumnWidth;
73+
private _legendGroupColumns;
7174
private legendDataMap: Object = {};
7275
private brush: BrushBehavior<{}>;
7376
private focusedLegendEntry: string;
@@ -106,9 +109,9 @@ export class LineChartService {
106109
let chart: D3Selection<D3BaseType, {}, D3ContainerElement, {}> = d3Select('g#time-series-chart-drawing-area');
107110
let xScale: D3ScaleTime<number, number> = this.getXScale(data);
108111
let yScale: D3ScaleLinear<number, number> = this.getYScale(data);
109-
this._labelGroupHeight = data.length * ChartCommons.LABEL_HEIGHT;
112+
this.calculateLegendDimensions();
110113
d3Select('osm-time-series-line-chart').transition().duration(500).style('visibility', 'visible');
111-
d3Select('svg#time-series-chart').transition().duration(500).attr('height', this._height + this._labelGroupHeight + this._margin.top + this._margin.bottom);
114+
d3Select('svg#time-series-chart').transition().duration(500).attr('height', this._height + this._legendGroupHeight + this._margin.top + this._margin.bottom);
112115
d3Select('.x-axis').transition().call(this.updateXAxis, xScale);
113116
d3Select('.y-axis').transition().call(this.updateYAxis, yScale, this._width, this._margin);
114117
this.brush = d3BrushX().extent([[0, 0], [this._width, this._height]]);
@@ -187,6 +190,7 @@ export class LineChartService {
187190
.attr('height', 0);
188191

189192
svg.append('g')
193+
.attr('id', 'time-series-chart-legend')
190194
.attr('class', 'legend-group')
191195
.attr('transform', `translate(${this._legendGroupLeft}, ${this._legendGroupTop})`);
192196

@@ -245,6 +249,34 @@ export class LineChartService {
245249
.nice();
246250
}
247251

252+
private calculateLegendDimensions(): void {
253+
let maximumLabelWidth: number = 1;
254+
let labels = Object.keys(this.legendDataMap);
255+
256+
d3Select('g#time-series-chart-legend')
257+
.append('g')
258+
.attr('id', 'renderToCalculateMaxWidth')
259+
.selectAll('.renderToCalculateMaxWidth')
260+
.data(labels)
261+
.enter()
262+
.append('text')
263+
.attr('class', 'legend-text')
264+
.text(datum => this.legendDataMap[datum].text)
265+
.each((datum, index, groups) => {
266+
Array.from(groups).forEach((text) => {
267+
if (text) {
268+
maximumLabelWidth = Math.max(maximumLabelWidth, text.getBoundingClientRect().width)
269+
}
270+
});
271+
});
272+
273+
d3Select('g#renderToCalculateMaxWidth').remove();
274+
275+
this._legendGroupColumnWidth = maximumLabelWidth + ChartCommons.COLOR_PREVIEW_SIZE + 30;
276+
this._legendGroupColumns = Math.floor(this._width / this._legendGroupColumnWidth);
277+
this._legendGroupHeight = Math.ceil(labels.length / this._legendGroupColumns) * ChartCommons.LABEL_HEIGHT + 30;
278+
}
279+
248280
private getMaxValue(data: TimeSeries[]): number {
249281
return d3Max(data, (dataItem: TimeSeries) => {
250282
return d3Max(dataItem.values, (point: TimeSeriesPoint) => {
@@ -761,13 +793,8 @@ export class LineChartService {
761793
}
762794

763795
private getPosition(index: number): string {
764-
const columns = 3;
765-
const columnWidth = 550;
766-
const xMargin = 10;
767-
const yMargin = 10;
768-
769-
const x = index % columns * columnWidth + xMargin;
770-
const y = Math.floor(index / columns) * ChartCommons.LABEL_HEIGHT + yMargin;
796+
const x = index % this._legendGroupColumns * this._legendGroupColumnWidth;
797+
const y = Math.floor(index / this._legendGroupColumns) * ChartCommons.LABEL_HEIGHT + 12;
771798

772799
return "translate(" + x + "," + y + ")";
773800
}

0 commit comments

Comments
 (0)