Hello, I am a newbie to Typescript and I recently encountered an issue that has left me stumped. The problem I am facing involves feeding data to a Dygraph chart which requires data in the format [Date, number, number,...]. However, the API I am using provides multiple series of data in the format [Date, number] instead. Is there an elegant way in type script to convert from [Date, number][] to [Date, number, number,...] without resorting to hard coding like the method below? While it does work, the solution feels quite cumbersome and limits scalability beyond 7 series in the same graph.
If you have any suggestions on how to solve this dilemma in a more efficient manner, your assistance would be greatly appreciated!
export class Datapoint {
date: Date = new Date()
value: number | undefined
}
export class DatapointSerie {
datapoints: Datapoint[] = new Array();
}
export class DygraphData {
maxSeriesCount = 7
data: [Date, number | undefined, ...number[]] = []
constructor(series: DatapointSerie[]) {
let seriesCount = Math.min(series.length, this.maxSeriesCount)
for (let i = 0; i < seriesCount; i++) {
this.fillSerie(series[i], i)
}
}
fillSerie(serie: DatapointSerie, index: number) {
switch (index) {
case 0:
serie.datapoints.forEach((datapoint) => this.data.push([datapoint.date, datapoint.value, ...new Array(this.maxSeriesCount - 1).fill(undefined)]))
break
case 1:
// Similar cases for other indexes can be added here
break
default:
break
}
}
}