|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import {TimeSeries} from "../models/time-series.model"; |
| 3 | +import { |
| 4 | + scaleLinear as d3ScaleLinear, |
| 5 | + ScaleLinear as D3ScaleLinear, |
| 6 | + scaleTime as d3ScaleTime, |
| 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"; |
| 11 | + |
| 12 | +@Injectable({ |
| 13 | + providedIn: 'root' |
| 14 | +}) |
| 15 | +export class LineChartScaleService { |
| 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 | + }; |
| 26 | + |
| 27 | + public getMinDate(data: TimeSeries[]): Date { |
| 28 | + 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 | + } |
| 42 | + |
| 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 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Determine the xScale for the given data |
| 54 | + */ |
| 55 | + public getXScale(data: TimeSeries[], width: number): D3ScaleTime<number, number> { |
| 56 | + return d3ScaleTime() // Define a scale for the X-Axis |
| 57 | + .range([0, width]) // Display the X-Axis over the complete width |
| 58 | + .domain([this.getMinDate(data), this.getMaxDate(data)]); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Determine the yScale for the given data |
| 63 | + */ |
| 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(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Determine the yScale for the given data in time range |
| 73 | + */ |
| 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(); |
| 79 | + } |
| 80 | +} |
0 commit comments