Chart.js integration


#1

Hello!

I am using Ionic-PWA-toolkit and would like to know how to integrate chart.js into my project?

Leon


#2

#3

Oh thank you!

I was trying this:

import { Component, } from '@stencil/core';
import { Chart } from 'chart.js';

@Component({
  tag: 'app-chart',
  styleUrl: 'app-chart.css'
})
export class AppChart {

  chart : [any];

componentWillLoad () {
  this.chart =  Chart('canvas', {
    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
                }
            }]
        }
    }
});

}

  render() {
    return [
      <ion-header>
        <ion-toolbar color="primary">
          <ion-buttons slot="start">
            <ion-back-button defaultHref="/" />
          </ion-buttons>
          <ion-title>Chart</ion-title>
        </ion-toolbar>
      </ion-header>,

      <ion-content padding color="primary">

        <canvas id="canvas">{ this.chart }</canvas>

      </ion-content>
    ];
  }
}

Following an example in angular but it gives me errors.

The latest is:
Missing Export: src/components/appchart/app-chart.js:1:9
‘Chart’ is not exported by node_modules/chart.js/src/chart.js

I don’t even know if it would work without that error.

The problem is I am using stencil/ionic combo and there is zero tutorials on this and also I lack the knowledge to interpret ionic/angular in stencil.

I will check the tutorial anyway.

Thank you
Leon


#4

Is this error an issue because of this code or is there something wrong with node modules?

Also this is a new error. Previously there were some typescript errors but now that is replaced by this.