Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

TimePoint support for time series

Open anthonysapien opened this issue 2 years ago • 2 comments

Feature Proposal

Time series are so common, it seems like a TimePoint or something similar should be a standard point type with a structure like { date: Date | string, y: number }.

As I understand it, you currently have to define a custom data point type and discover this syntax for the chart data: ChartData<'line', TimePoint[]>.

The chart options don't seem to be aware of the custom data point type. The tooltip label callback doesn't know about the custom data point type, so I had to use a ts-ignore in my code to ignore the issue.

I'm not a full TS or chart.js expert, so I may have missed something, but that's kind of the point. Time series is one of the most common chart types and should be easy to use with TypeScript.

Possible Implementation

Similar to BubblePoint, add a TimePoint type that supports Date objects and parsable date strings.

anthonysapien avatar Dec 11 '23 03:12 anthonysapien

In ChartTypeRegistry: https://github.com/chartjs/Chart.js/blob/84e7238a647623a3dd1f1c699752c4c364a68b1e/src/types/index.d.ts#L3668-L3675 , it defined defaultDataPoint: ScatterDataPoint | number | null

https://github.com/chartjs/Chart.js/blob/84e7238a647623a3dd1f1c699752c4c364a68b1e/src/types/index.d.ts#L237 https://github.com/chartjs/Chart.js/blob/84e7238a647623a3dd1f1c699752c4c364a68b1e/src/types/geometric.d.ts#L10-L13 But ScatterDataPoint should not be defined as Point. The field x in ScatterDataPoint should accept not only number but also string and Date (batter if it also support date object in packages listed in adapters) also. The library do accept Date or luxon.DateTime etc., but the type definition do not allow it.

tlserver avatar Dec 21 '23 09:12 tlserver