javascript - 如何在 Ionic 应用程序中使用 Google Charts

标签 javascript angularjs ionic-framework google-visualization

我正在尝试在 Ionic 应用程序中使用 Google 图表。但是,在我的浏览器中测试该应用程序时,我只得到一个空白页面。我在这里做错了什么?

我的 app.js 文件如下所示:

angular.module('testGC', ['ionic', 'googlechart', 'googlechart-docs'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
    });
    })

.controller("GenericChartCtrl", function ($scope) {
    $scope.chartObject = {};

    $scope.chartObject.type = "BarChart";

    $scope.chartObject.data = {"cols": [
        {id: "t", label: "Topping", type: "string"},
        {id: "s", label: "Slices", type: "number"}
    ], "rows": [
        {c: [
            {v: "Mushrooms"},
            {v: 3},
        ]},
        {c: $scope.onions},
        {c: [
            {v: "Olives"},
            {v: 31}
        ]},
        {c: [
            {v: "Zucchini"},
            {v: 1},
        ]},
        {c: [
            {v: "Pepperoni"},
            {v: 2},
        ]}
    ]};

    $scope.chartObject.options = {
        'title': 'How Much Pizza I Ate Last Night'
    };
    });

我的 index.html 文件如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
        <title></title>

        <link href="lib/ionic/css/ionic.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">

        <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
        <link href="css/ionic.app.css" rel="stylesheet">
        -->

        <!-- ionic/angularjs js -->
        <script src="lib/ionic/js/ionic.bundle.js"></script>

        <!-- cordova script (this will be a 404 during development) -->
        <script src="cordova.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-google-chart/0.1.0/ng-google-chart.min.js" type="text/javascript"></script>


        <!-- your app's js -->
        <script src="js/app.js"></script>

    </head>
    <body>

        <ion-pane>
            <ion-header-bar class="bar-stable">
                <h1 class="title">Ionic Blank Starter</h1>
            </ion-header-bar>
            <ion-content ng-controller="GenericChartCtrl">
                <div google-chart chart1="sampleData" style="width: 100%;height:100%;border: 1px solid green;padding-bottom: 44px" class="has-footer">
                </div>
            </ion-content>
        </ion-pane>
    </body>
</html>

最佳答案

由于以下原因,图表未呈现:

  • ng-app 指令丢失
  • 提供的图表数据 ($scope.chartObject.data) 包含 未定义的行数据(参见 $scope.onions)
  • chart div 元素的声明中,无效指令 chart1 已指定且未定义 sampleData 已绑定(bind)

修改index.html

<body ng-app="chartApp">
    <ion-pane>
        <ion-header-bar class="bar-stable">
            <h1 class="title">Ionic Blank Starter</h1>
        </ion-header-bar>
        <ion-content ng-controller="GenericChartCtrl">
            <div google-chart chart="chartObject" class="chart-container">
            </div>
        </ion-content>
    </ion-pane>
</body>

修改后的app.js

angular.module('chartApp', ['ionic', 'googlechart'])

.controller("GenericChartCtrl", function ($scope) {
    $scope.chartObject = {};

    $scope.chartObject.type = "BarChart";

    $scope.chartObject.data = {
        "cols": [
            { id: "t", label: "Topping", type: "string" },
            { id: "s", label: "Slices", type: "number" }
        ], "rows": [
            {
                c: [
                   { v: "Mushrooms" },
                   { v: 3 },
                ]
            },
            //{ c: $scope.onions },
            {
                c: [
                   { v: "Olives" },
                   { v: 31 }
                ]
            },
            {
                c: [
                   { v: "Zucchini" },
                   { v: 1 },
                ]
            },
            {
                c: [
                   { v: "Pepperoni" },
                   { v: 2 },
                ]
            }
        ]
    };

    $scope.chartObject.options = {
        'title': 'How Much Pizza I Ate Last Night'
    };
});

Demo:Plunker

关于javascript - 如何在 Ionic 应用程序中使用 Google Charts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34571069/

相关文章:

javascript - 如何使用 Ember ArrayController 获取对象集合?

android - 难以在 Android 设备上显示跨平台应用程序的滚动条

javascript - AngularJS 父子循环分页

javascript - PinesNotify 不适用于 jQuery UI

javascript - 为什么对齐元素不沿交叉轴居中?

javascript - Angular JS : can we set resolve or promise on controller which did not have route defined?

android - ionic 1、 ionic 和 ionic 3 之间有什么区别? ionic 2 和 ionic3 是一样的吗?

vue.js - 为什么要加一个: infront of a Vue component attribute?

angularjs - Uncaught Error : [$injector:modulerr] http://errors. angularjs.org/1.2.14/$injector/modulerr

javascript - 如何从 CSS 获取颜色值