|
1 |
| -import { Injectable } from '@angular/core'; |
2 |
| -import {TimeSeries} from "../models/time-series.model"; |
| 1 | +import {Injectable} from '@angular/core'; |
| 2 | +import {TimeSeries} from '../models/time-series.model'; |
3 | 3 | import {
|
4 | 4 | scaleLinear as d3ScaleLinear,
|
5 | 5 | ScaleLinear as D3ScaleLinear,
|
6 | 6 | scaleTime as d3ScaleTime,
|
7 | 7 | ScaleTime as D3ScaleTime
|
8 |
| -} from "d3-scale"; |
9 |
| -import {TimeSeriesPoint} from "../models/time-series-point.model"; |
10 |
| -import {max as d3Max, min as d3Min} from "d3-array"; |
| 8 | +} from 'd3-scale'; |
| 9 | +import {TimeSeriesPoint} from '../models/time-series-point.model'; |
| 10 | +import {max as d3Max, min as d3Min} from 'd3-array'; |
11 | 11 |
|
12 | 12 | @Injectable({
|
13 | 13 | providedIn: 'root'
|
14 | 14 | })
|
15 | 15 | export class LineChartScaleService {
|
16 | 16 |
|
17 |
| - constructor() { } |
18 |
| - |
19 |
| - private getDate(data: TimeSeries[], f: Function): Date { |
20 |
| - return f(data, (dataItem: TimeSeries) => { |
21 |
| - return f(dataItem.values, (point: TimeSeriesPoint) => { |
22 |
| - return point.date; |
23 |
| - }); |
24 |
| - }); |
25 |
| - }; |
| 17 | + constructor() { |
| 18 | + } |
26 | 19 |
|
27 |
| - public getMinDate(data: TimeSeries[]): Date { |
| 20 | + public getMinDate(data: TimeSeries[][]): Date { |
28 | 21 | return this.getDate(data, d3Min);
|
29 |
| - }; |
30 |
| - |
31 |
| - public getMaxDate(data: TimeSeries[]): Date { |
32 |
| - return this.getDate(data, d3Max); |
33 |
| - }; |
34 |
| - |
35 |
| - private getMaxValue(data: TimeSeries[]): number { |
36 |
| - return d3Max(data, (dataItem: TimeSeries) => { |
37 |
| - return d3Max(dataItem.values, (point: TimeSeriesPoint) => { |
38 |
| - return point.value; |
39 |
| - }); |
40 |
| - }); |
41 | 22 | }
|
42 | 23 |
|
43 |
| - private getMaxValueInTime(data: TimeSeries[], minDate: Date, maxDate: Date): number { |
44 |
| - return d3Max(data, (dataSeries: TimeSeries) => { |
45 |
| - const valuesInRange = dataSeries.values.filter(value => value.date <= maxDate && value.date >= minDate); |
46 |
| - return d3Max(valuesInRange, (point: TimeSeriesPoint) => { |
47 |
| - return point.value; |
48 |
| - }); |
49 |
| - }); |
| 24 | + public getMaxDate(data: TimeSeries[][]): Date { |
| 25 | + return this.getDate(data, d3Max); |
50 | 26 | }
|
51 | 27 |
|
52 | 28 | /**
|
53 | 29 | * Determine the xScale for the given data
|
54 | 30 | */
|
55 |
| - public getXScale(data: TimeSeries[], width: number): D3ScaleTime<number, number> { |
| 31 | + public getXScale(data: TimeSeries[][], width: number): D3ScaleTime<number, number> { |
56 | 32 | return d3ScaleTime() // Define a scale for the X-Axis
|
57 | 33 | .range([0, width]) // Display the X-Axis over the complete width
|
58 | 34 | .domain([this.getMinDate(data), this.getMaxDate(data)]);
|
59 | 35 | }
|
60 | 36 |
|
61 | 37 | /**
|
62 |
| - * Determine the yScale for the given data |
| 38 | + * Determine the yScales for the given data |
63 | 39 | */
|
64 |
| - public getYScale(data: TimeSeries[], height: number): D3ScaleLinear<number, number> { |
65 |
| - return d3ScaleLinear() // Linear scale for the numbers on the Y-Axis |
66 |
| - .range([height, 0]) // Display the Y-Axis over the complete height - origin is top left corner, so height comes first |
67 |
| - .domain([0, this.getMaxValue(data)]) |
68 |
| - .nice(); |
| 40 | + public getYScales(dataList: TimeSeries[][], height: number): D3ScaleLinear<number, number>[] { |
| 41 | + const yScales: D3ScaleLinear<number, number>[] = []; |
| 42 | + dataList.forEach((data: TimeSeries[]) => { |
| 43 | + const yScale: D3ScaleLinear<number, number> = d3ScaleLinear() // Linear scale for the numbers on the Y-Axis |
| 44 | + .range([height, 0]) // Display the Y-Axis over the complete height - origin is top left corner, so height comes first |
| 45 | + .domain([0, this.getMaxValue(data)]) |
| 46 | + .nice(); |
| 47 | + yScales.push(yScale); |
| 48 | + }); |
| 49 | + |
| 50 | + return yScales; |
69 | 51 | }
|
70 | 52 |
|
71 | 53 | /**
|
72 |
| - * Determine the yScale for the given data in time range |
| 54 | + * Determine the yScales for the given data in time range |
73 | 55 | */
|
74 |
| - public getYScaleInRange(data: TimeSeries[], minDate: Date, maxDate: Date, height: number): D3ScaleLinear<number, number> { |
75 |
| - return d3ScaleLinear() // Linear scale for the numbers on the Y-Axis |
76 |
| - .range([height, 0]) // Display the Y-Axis over the complete height - origin is top left corner, so height comes first |
77 |
| - .domain([0, this.getMaxValueInTime(data, minDate, maxDate)]) |
78 |
| - .nice(); |
| 56 | + public getYScalesInTimeRange(dataList: TimeSeries[][], minDate: Date, maxDate: Date, height: number): D3ScaleLinear<number, number>[] { |
| 57 | + const yScales: D3ScaleLinear<number, number>[] = []; |
| 58 | + dataList.forEach((data: TimeSeries[]) => { |
| 59 | + const yScale: D3ScaleLinear<number, number> = d3ScaleLinear() // Linear scale for the numbers on the Y-Axis |
| 60 | + .range([height, 0]) // Display the Y-Axis over the complete height - origin is top left corner, so height comes first |
| 61 | + .domain([0, this.getMaxValueInTime(data, minDate, maxDate)]) |
| 62 | + .nice(); |
| 63 | + yScales.push(yScale); |
| 64 | + }); |
| 65 | + |
| 66 | + return yScales; |
| 67 | + } |
| 68 | + |
| 69 | + private getDate(dataList: TimeSeries[][], f: Function): Date { |
| 70 | + return f(dataList, (data: TimeSeries[]) => { |
| 71 | + return f(data, (dataItem: TimeSeries) => { |
| 72 | + return f(dataItem.values, (point: TimeSeriesPoint) => { |
| 73 | + return point.date; |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + private getMaxValue(data: TimeSeries[]): number { |
| 80 | + return d3Max(data, (dataItem: TimeSeries) => { |
| 81 | + return d3Max(dataItem.values, (point: TimeSeriesPoint) => { |
| 82 | + return point.value; |
| 83 | + }); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + private getMaxValueInTime(data: TimeSeries[], minDate: Date, maxDate: Date): number { |
| 88 | + return d3Max(data, (dataSeries: TimeSeries) => { |
| 89 | + const valuesInRange = dataSeries.values.filter(value => value.date <= maxDate && value.date >= minDate); |
| 90 | + return d3Max(valuesInRange, (point: TimeSeriesPoint) => { |
| 91 | + return point.value; |
| 92 | + }); |
| 93 | + }); |
79 | 94 | }
|
80 | 95 | }
|
0 commit comments