21

Google チャートを AngularJs ディレクティブとして統合する例がいくつかあります。

このように: http://plnkr.co/edit/YzwjuU?p=preview

更新: アプリケーション全体をブートストラップする前に、Google チャートの準備が整うのを待つのを避けたい (上記の例に示すように):

 google.setOnLoadCallback(function() {
        angular.bootstrap(document.body, ['myApp']);
    });

アプリ全体ではなく、単一のモジュールでこれを行う方法はありますか?

更新 2: plunker を作成しましたが、コールバックを待たずに動作しますが、すべての場合に動作するかどうかはわかりません。

http://plnkr.co/edit/7UUfcq4dD7nd4MylB4ni

4

4 に答える 4

12

これは、 AngularJs の Google Chart Tools ディレクティブの動作の良い例です。

指示

これらの同じ指示はプランカー自体にあります。

  1. github から ng-google-chart.js をダウンロードし、HTML にスクリプト タグを追加します。
  2. 次のような div を作成します。

    <div google-chart chart="chart" style="{{chart.cssStyle}}"> </div>

  3. 次のように「googlechart」をモジュールに追加します。

    angular.module('myApp',[ 'googlechart', ...

  4. 次の$scope.chartように入力します。
{
  "type": "ColumnChart",
  "cssStyle": "height:200px; width:300px;",
  "data": {
    "cols": [
      {
        "id": "month",
        "label": "Month",
        "type": "string",
        "p": {}
      },
      {
        "id": "laptop-id",
        "label": "Laptop",
        "type": "number",
        "p": {}
      },
      {
        "id": "desktop-id",
        "label": "Desktop",
        "type": "number",
        "p": {}
      },
      {
        "id": "server-id",
        "label": "Server",
        "type": "number",
        "p": {}
      },
      {
        "id": "cost-id",
        "label": "Shipping",
        "type": "number"
      }
    ],
    "rows": [
      {
        "c": [
          {
            "v": "January"
          },
          {
            "v": 19,
            "f": "42 items"
          },
          {
            "v": 12,
            "f": "Ony 12 items"
          },
          {
            "v": 7,
            "f": "7 servers"
          },
          {
            "v": 4
          }
        ]
      },
      {
        "c": [
          {
            "v": "February"
          },
          {
            "v": 13
          },
          {
            "v": 1,
            "f": "1 unit (Out of stock this month)"
          },
          {
            "v": 12
          },
          {
            "v": 2
          }
        ]
      },
      {
        "c": [
          {
            "v": "March"
          },
          {
            "v": 24
          },
          {
            "v": 0
          },
          {
            "v": 11
          },
          {
            "v": 6
          }
        ]
      }
    ]
  },
  "options": {
    "title": "Sales per month",
    "isStacked": "true",
    "fill": 20,
    "displayExactValues": true,
    "vAxis": {
      "title": "Sales unit",
      "gridlines": {
        "count": 6
      }
    },
    "hAxis": {
      "title": "Date"
    }
  },
  "formatters": {},
  "displayed": true
}
于 2014-09-03T13:07:26.570 に答える
11

すでにわかっているように、Google チャートを待たずに、html または body タグで angular を初期化できます。

Google チャート JavaScript コードの準備が整う前にチャートをレンダリングしようとしないようにするために、ディレクティブ $watch に、google.setOnLoadCallback のコールバック関数内で設定する新しいコントローラーの $scope プロパティ/フラグを設定します。$watch コールバック内で、フラグが設定されていることを確認してから、初期化を行います。

于 2013-01-19T23:09:52.413 に答える