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

Mixed chart example in documentaion has a configuration that doesnt match the ChartConfiguration Type #10382

Closed
1 of 3 tasks
dhertzb opened this issue May 26, 2022 · 1 comment · Fixed by #10387
Closed
1 of 3 tasks
Labels
type: documentation type: types Typescript type changes
Milestone

Comments

@dhertzb
Copy link

dhertzb commented May 26, 2022

Documentation Is:

  • Missing or needed?
  • Confusing
  • Not sure?

Please Explain in Detail...

First example here doesnt work in charts.js@3.8.0

const mixedChart = new Chart(ctx, {
    data: {
        datasets: [{
            type: 'bar',
            label: 'Bar Dataset',
            data: [10, 20, 30, 40]
        }, {
            type: 'line',
            label: 'Line Dataset',
            data: [50, 50, 50, 50],
        }],
        labels: ['January', 'February', 'March', 'April']
    },
    options: options
});

When I try to emulate that in my chart

return new Chart(ctx, {
      data: {
        datasets: [
          {
            type: 'bar',
            borderColor: 'yellow',
            data: this.getChartDataForUserPointer(),
            backgroundColor: 'yellow',
          },
          {
            type: 'line',
            borderColor: '#7655e9',
            data: this.getGeneralChartDataByCategory(),
            backgroundColor: this.getGradient(ctx),
            fill: true,
            pointRadius: 0,
          },
        ],
      }
    });
  }

I get the follow error:

Argument of type '{ data: { datasets: ({ type: "bar"; borderColor: string; data: ICordinates[]; backgroundColor: string; } | { type: "line"; borderColor: string; data: ICordinates[]; backgroundColor: any; fill: true; pointRadius: number; })[]; }; options: { ...; }; }' is not assignable to parameter of type 'ChartConfiguration<"bar" | "line", ICordinates[], unknown>'.
  Property 'type' is missing in type '{ data: { datasets: ({ type: "bar"; borderColor: string; data: ICordinates[]; backgroundColor: string; } | { type: "line"; borderColor: string; data: ICordinates[]; backgroundColor: any; fill: true; pointRadius: number; })[]; }; options: { ...; }; }' but required in type 'ChartConfiguration<"bar" | "line", ICordinates[], unknown>'.ts(2345)

I also checked the ChartConfiguration Interface and the TType is really mandatory, so I have to check the old commits in file documentation to get a way to work.

image

So when I change to this, It works!

 return new Chart(ctx, {
      type: 'bar',
      data: {
        datasets: [
          {
            borderColor: 'yellow',
            data: this.getChartDataForUserPointer(),
            backgroundColor: 'yellow',
          },
          {
            type: 'line',
            borderColor: '#7655e9',
            data: this.getGeneralChartDataByCategory(),
            backgroundColor: this.getGradient(ctx),
            fill: true,
            pointRadius: 0,
          },
        ],
      },
    });
  }

Your Proposal for Changes

Revert to old example!

Example

No response

@LeeLenaleee
Copy link
Collaborator

LeeLenaleee commented May 27, 2022

Technically it is not necesarry to provide a type. As you can see in the linked fiddle if you provide a type to every dataset it also works.
But it is required to give either all the datasets their own type or a default type otherwise the chart wont render.

Not sure if you can go in depth enough that typescript will check if all datasets have a type or if at least a default type is available

https://jsfiddle.net/0k2L7q65/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation type: types Typescript type changes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants