question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Do you support angular 2+ (angular 4)?

See original GitHub issue

Hi, I’m trying to implement Chart.js into the angular4 project and there seem to be some type issues.

Expected Behavior

The default chart should be displayed and the implementation should run as planned.

Current Behavior

When fetching the canvas element like described in the examples: const el = document.getElementById(‘myChart’); The error that is thrown looks like this:

ERROR in D:/www/weather-app/src/app/weather-chart/weather-chart.component.ts (23,31): Argument of type 'HTMLElement' is not assignable to parameter of type 'string | string[] | JQuery<HTMLElement> | CanvasRenderingContext2D | HTMLCanvasElement | CanvasRe...'.
  Type 'HTMLElement' is not assignable to type 'HTMLCanvasElement[]'.
    Property 'includes' is missing in type 'HTMLElement'.

When fetching the canvas element like this: const el = document.getElementById('myChart') as HTMLCanvasElement; The error that is thrown looks like this:

client?ffdb:119 D:/www/weather-app/src/app/weather-chart/weather-chart.component.ts (53,15): Argument of type '{ type: string; data: { labels: string[]; datasets: { label: string; data: number[]; backgroundCo...' is not assignable to parameter of type 'ChartConfiguration'.
  Types of property 'options' are incompatible.
    Type '{ scales: { yAxes: { ticks: { beginAtZero: boolean; }; }[]; }; }' is not assignable to type 'ChartOptions'.
      Types of property 'scales' are incompatible.
        Type '{ yAxes: { ticks: { beginAtZero: boolean; }; }[]; }' is not assignable to type 'ChartScales'.
          Types of property 'yAxes' are incompatible.
            Type '{ ticks: { beginAtZero: boolean; }; }[]' is not assignable to type 'ChartYAxe[]'.
              Type '{ ticks: { beginAtZero: boolean; }; }' is not assignable to type 'ChartYAxe'.
                Types of property 'ticks' are incompatible.
                  Type '{ beginAtZero: boolean; }' is not assignable to type 'TickOptions'.
                    Object literal may only specify known properties, and 'beginAtZero' does not exist in type 

Possible Solution

Steps to Reproduce (for bugs)

  1. Install Chart.js
  2. import * as Chart from 'chart.js'; in the desired component
  3. Add the canvas template <canvas id="myChart" width="400" height="400"></canvas>
  4. try to execute the default example
const el = document.getElementById('myChart');
const myChart = new Chart(el, {
      type: 'bar',
      data: {
        labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
        datasets: [{
          label: '# of Votes',
          data: [12, 19, 3, 5, 2, 3],
          backgroundColor: [
            'rgba(255, 99, 132, 0.2)',
            'rgba(54, 162, 235, 0.2)',
            'rgba(255, 206, 86, 0.2)',
            'rgba(75, 192, 192, 0.2)',
            'rgba(153, 102, 255, 0.2)',
            'rgba(255, 159, 64, 0.2)'
          ],
          borderColor: [
            'rgba(255,99,132,1)',
            'rgba(54, 162, 235, 1)',
            'rgba(255, 206, 86, 1)',
            'rgba(75, 192, 192, 1)',
            'rgba(153, 102, 255, 1)',
            'rgba(255, 159, 64, 1)'
          ],
          borderWidth: 1
        }]
      },
      options: {
        scales: {
          yAxes: [{
            ticks: {
              beginAtZero: true
            }
          }]
        }
      }
    });

Context

I’m trying to implement the library…

Environment

  • Chart.js version 2.6.0:
  • Angular v4.3.3
  • Browser name and version: Chrome latest (60)
  • Node v7.7.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
phtncommented, Sep 6, 2018

I was getting the same error and assigned canvas type to any like so:

const canvas: any = document.getElementById('myChart'); const ctx = canvas.getContext('2d');

3reactions
etimbergcommented, Aug 12, 2017

@mnemanja what definitions are you using for the options? Looks like beginAtZero is not included hence the type errors.

I don’t know much about angular, but I think you have to do const el = document.getElementById('myChart') as HTMLCanvasElement otherwise it looks like it’s only giving you HTMLElement

Not sure why it won’t render though. Have you looked at something like https://github.com/valor-software/ng2-charts ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

AngularJS vs. Angular 2 Vs. Angular 4: Key Differences
Angular 4 is compatible with the most recent versions of TypeScript that have powerful type checking and object-oriented features. 3. Expression ...
Read more >
Angular Version List & History – Angular 2,4,5,6,7,8 - Guru99
Difference between Angular 2 and Angular 4 ; Angular two is not backward compatible with Angular JS. Angular four is backward compatible with ......
Read more >
AngularJS vs Angular 2 vs Angular 4 - Intellipaat
This blog on AngularJS vs Angular 2 vs Angular 4, will help you understand the differences between angular versions based on architecture, language, ......
Read more >
Browser support - Angular
Angular supports most recent browsers. ... Safari, 2 most recent major versions ... If you are not using the CLI to create your...
Read more >
Difference between AngularJS and Angular 2 - C# Corner
In Angular 2 only TypeScript 1.8 version was supported, whereas, in Angular 4, it supports TypeScript 2.1 and TypeScript 2.2 compatibility, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found