【问题标题】:Unable to display the chart using Ionic3/2无法使用 Ionic3/2 显示图表
【发布时间】:2018-02-11 04:59:58
【问题描述】:

您好,我尝试使用 High 图表显示静态数据,但出现错误 13,我放置了这个

<div id="container" style="display: block;" ></div>

在 html 文件中,下面是我用来显示图表的数据

import * as HighCharts from 'highcharts';

ionViewDidLoad(){
    var myChart = HighCharts.chart('container', {
      chart: {
        type: 'bar'
      },
      title: {
        text: 'Fruit Consumption'
      },
      xAxis: {
        categories: ['Apples', 'Bananas', 'Oranges']
      },
      yAxis: {
        title: {
          text: 'Fruit eaten'
        }
      },
      series: [{
        name: 'Jane',
        data: [1, 0, 4]
      }, {
        name: 'John',
        data: [5, 7, 3]
      }]
    });
  }

有没有更好的 ionic 图表库来显示 chart.js 以外的图表

【问题讨论】:

  • 你用 ng2-highcharts 试过了吗

标签: angular ionic-framework charts highcharts ionic2


【解决方案1】:

有一个名为 angular2-highcharts

的 angular2 包
import { ChartModule } from 'angular2-highcharts';

和选项,

class AppComponent {
    constructor() {

        this.options = {
            chart: {
                type: 'bar'
            },
            title: {
                text: 'Fruit Consumption'
            },
            xAxis: {
                categories: ['Apples', 'Bananas', 'Oranges']
            },
            yAxis: {
                title: {
                    text: 'Fruit eaten'
                }
            },
            series: [{
                name: 'Jane',
                data: [1, 0, 4]
            }, {
                name: 'John',
                data: [5, 7, 3]
            }]
        };
    }
    options: Object;
}

DEMO

【讨论】:

  • 适用于 Angular 5? & 离子/角度的最佳图表库是什么
  • 哪个图表库更好?
  • 带角度的你可以使用 ng2-charts 或 highcharts
  • 对答案的帮助
  • 我在应用程序 module.ts 中收到一个错误,说 ChartModule.forRoot(require('highcharts'))' 找不到名称要求
【解决方案2】:

查看官方highcharts#typescript

stackblitz演示

import { Component, ViewChild, ElementRef } from '@angular/core';
import { NavController } from 'ionic-angular';
import  Highcharts from 'highcharts';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage{
 @ViewChild("container", { read: ElementRef }) container: ElementRef;
  constructor(public navCtrl: NavController) {

  }
  ionViewDidLoad() {
    Highcharts.chart(this.container.nativeElement, {
      chart: {
        type: 'bar'
      },
      title: {
        text: 'Fruit Consumption'
      },
      xAxis: {
        categories: ['Apples', 'Bananas', 'Oranges']
      },
      yAxis: {
        title: {
          text: 'Fruit eaten'
        }
      },
      series: [{
        name: 'Jane',
        data: [1, 0, 4]
      }, {
        name: 'John',
        data: [5, 7, 3]
      }]
    })
  }

}

【讨论】:

  • 我尝试了您的解决方案,但我得到以下错误 ERROR TypeError: Cannot read property 'nativeElement' of undefined
  • 检查这一行@ViewChild("container", { read: ElementRef }) container: ElementRef;,错误可能在这里。请在错误所在的位置添加您的工作演示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 2018-05-08
  • 1970-01-01
  • 2020-10-31
相关资源
最近更新 更多