ionic2/3實戰-使用Chart.js

前言

安裝Chart.js

  1. 執行cnpm install typings -g,全局安裝Typings
  • 執行typings search chart.js,使用Typings搜索Chart.js

    安裝和使用typings

  • 進入app目錄執行typings install chart.js --source npm

    app安裝chart.js

    執行typings install chart.js --source npm會生成的文件

  • 最后執行cnpm install chart.js安裝chart.js依賴

    安裝chart.js依賴

    執行cnpm install chart.js在node_modules目錄下生成的文件

使用Chart.js

我的contact.html完整代碼如下

<ion-header>
  <ion-navbar>
    <ion-title>
      Contact
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
  <div padding-vertical>
    <canvas #chartBar height="200"></canvas>
  </div>
  <div padding-vertical>
    <canvas #chartLine height="200"></canvas>
  </div>
  <div padding-vertical>
    <canvas #chartPie height="200"></canvas>
  </div>
</ion-content>

我的contact.ts完整代碼如下

import {Component, ViewChild, ElementRef} from '@angular/core';

import {NavController} from 'ionic-angular';
import Chart from 'chart.js'; // 導入chart.js

@Component({
  selector: 'page-contact',
  templateUrl: 'contact.html'
})
export class ContactPage {
  @ViewChild('chartBar') chartBar: ElementRef;
  @ViewChild('chartLine') chartLine: ElementRef;
  @ViewChild('chartPie') chartPie: ElementRef;

  constructor(private navCtrl: NavController) {

  }

  ionViewDidEnter() {
    Chart.Bar(this.chartBar.nativeElement.getContext("2d"), {
      data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
          label: '呵呵',
          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
            }
          }]
        }
      }
    });

    Chart.Line(this.chartLine.nativeElement.getContext("2d"), {
      data: {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [
          {
            label: "哈哈",
            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,
          }
        ]
      }
    });

    Chart.Doughnut(this.chartPie.nativeElement.getContext("2d"), {
      data: {
        labels: [
          "Red",
          "Blue",
          "Yellow"
        ],
        datasets: [
          {
            data: [300, 50, 100],
            backgroundColor: [
              "#FF6384",
              "#36A2EB",
              "#FFCE56"
            ],
            hoverBackgroundColor: [
              "#FF6384",
              "#36A2EB",
              "#FFCE56"
            ]
          }]
      }
    });

  }
}

效果展示

效果展示.gif

其他

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 原文鏈接:http://www.lxweimin.com/p/2a9367afe9e7 1510997059(1)....
    懸筆e絕閱讀 5,538評論 0 0
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,292評論 25 708
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,923評論 18 139
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,241評論 4 61
  • 在李笑來老師音頻版的最后一周,我繼續看專欄、聽專欄,并寫留言,其中連續三天的留言被精選(8月28日、29日、30日...
    楊金社閱讀 192評論 0 0