Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct ChartOptions types for a Line chart with Time scale #9085

Closed
2 of 3 tasks
j-a-h-i-r opened this issue May 13, 2021 · 12 comments
Closed
2 of 3 tasks

Correct ChartOptions types for a Line chart with Time scale #9085

j-a-h-i-r opened this issue May 13, 2021 · 12 comments

Comments

@j-a-h-i-r
Copy link

Documentation Is:

  • Missing or needed
  • Confusing
  • Not Sure?

Please Explain in Detail...

I want to get the correct types for a Line chart which has Time scale. From documentation I found that I can pass "line" to the ChartOptions type.

const chartOptions = ChartOptions<'line'> = {
    scales: {
      x: {
        time: {
          unit: 'day'
        }
      }
    }
  }

But the problem is I can't find any way to specify that I want to use the Time scale. As a result when I try to do the following

this.chartOptions.scales.x.time.unit = 'hour';

VSCode shows that,

Property 'time' does not exist on type '_DeepPartialObject<{ type: "linear"; } ...........

How can I use the proper types for Time scale on a Line chart so that I get the correct type checks? I could not find any documentation for this anywhere so any pointers is appreciated.

Your Proposal for Changes

Example

@etimberg
Copy link
Member

You need to add type: 'time' to the axis definition, ie

const chartOptions = ChartOptions<'line'> = {
    scales: {
      x: {
        type: 'time',
        time: {
          unit: 'day'
        }
      }
    }
}

@etimberg
Copy link
Member

This is mentioned in the docs in https://www.chartjs.org/docs/latest/axes/

@j-a-h-i-r
Copy link
Author

Thanks for the snippet. I was unaware of the type: 'time' property. But even after adding it, VSCode is still saying that Property 'time' does not exist on type '_DeepPartialObject< .... when I do this :( ,

this.chartOptions.scales.x.time.unit = 'hour';

@etimberg etimberg added the type: types Typescript type changes label May 13, 2021
@etimberg
Copy link
Member

I tried to reproduce this issue but could not. #9091

@ghost
Copy link

ghost commented May 14, 2021

After adding this (time) correct format with Chart.js v3 , I am getting Error: "time" is not a registered scale. Any suggestions ?

@LeeLenaleee
Copy link
Collaborator

Chart.js is treeshakable so you will have to import and register the timescale, https://www.chartjs.org/docs/master/getting-started/integration.html#bundlers-webpack-rollup-etc

@etimberg
Copy link
Member

Closing as stale since there is no test case

@JBaczuk
Copy link

JBaczuk commented Jan 24, 2022

I had the same error as @mycodedig, to get it working I had to register the TimeScale object on the chart:

import { TimeScale, ... } from 'chart.js';
Chart.register(TimeScale, ...);

Then, I got this error:

Type 'string' is not assignable to type '"timeseries" | undefined'.

So, I added as any (not ideal):

type: 'time' as any,

If there is a more correct way, I'd love to know.

@JBaczuk
Copy link

JBaczuk commented Jan 25, 2022

I just realized, the type definition is in react-chartjs-2 so it is probably related to that library.

UPDATE: Apparently, react-chartjs-2 uses this library for types.

react-chartjs-2 reuse types from Chart.js - reactchartjs/react-chartjs-2#1009 (comment)

@JBaczuk
Copy link

JBaczuk commented Jan 31, 2022

Update:

Once I added as const to the following lines, the errors are gone.

minUnit: 'minute' as const,

and

source: 'data' as const,

The confusing part is this error gave no indication that those other 2 lines needed additional type casting:

Types of property 'type' are incompatible.
              Type '"time"' is not assignable to type '"timeseries" | undefined'.ts(2322)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants