Chart.js integration

Hello!

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

Leon

1 Like

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

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.

You can implement Chart-JS by flowing step-

  1. Go to your project directory
  2. Install chart-js by command npm install chart.js --save
  3. Import ‘chart.js’ where you want to use on .ts file
import {Component, OnInit, ViewChild} from '@angular/core';
import { Chart } from 'chart.js';

@Component({
    selector: 'app-your-component-name',
    templateUrl: './your-component-name.component.html',
    styleUrls: ['./your-component-name.component.scss'],
})
export class YourComponentName implements OnInit {
    lineChart: any;
    @ViewChild('lineCanvas') lineCanvas;

    constructor() {}
    ngOnInit() {
        this.yourCustomFunctionName();
    }

    public yourCustomFunctionName() {
        this.lineChart = new Chart(this.lineCanvas.nativeElement, {

            type: 'line',
            data: {
                labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
                datasets: [
                    {
                        label: 'My First dataset',
                        fill: false,
                        lineTension: 0.1,
                        backgroundColor: 'rgba(75,192,192,0.4)',
                        borderColor: 'rgba(75,192,192,1)',
                        borderCapStyle: 'butt',
                        borderDash: [],
                        borderDashOffset: 0.0,
                        borderJoinStyle: 'miter',
                        pointBorderColor: 'rgba(75,192,192,1)',
                        pointBackgroundColor: '#fff',
                        pointBorderWidth: 1,
                        pointHoverRadius: 5,
                        pointHoverBackgroundColor: 'rgba(75,192,192,1)',
                        pointHoverBorderColor: 'rgba(220,220,220,1)',
                        pointHoverBorderWidth: 2,
                        pointRadius: 1,
                        pointHitRadius: 10,
                        data: [65, 59, 80, 81, 56, 55, 40],
                        spanGaps: false,
                    }
                ]
            }
        });
    }

}

  1. Now view page you just use it.
<canvas #lineCanvas></canvas>

‘Happy Coding’

7 Likes