如何将Google图表整合为AngularJs指令?

And*_*rin 20 google-visualization angularjs

有一些将Google图表整合为AngularJs指令的示例.

像这样:http://plnkr.co/edit/YzwjuU?p = preview

更新:我想避免在引导整个应用程序之前等待谷歌图表准备就绪(如上例所示):

 google.setOnLoadCallback(function() {
        angular.bootstrap(document.body, ['myApp']);
    });
Run Code Online (Sandbox Code Playgroud)

有没有办法在单个模块中执行此操作而不是在整个应用程序中执行此操作?

更新2:我创建了我的plunker并且它无需等待回调即可工作,但我不确定它是否适用于所有情况.

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

Jes*_*ess 12

以下是AngularJs Google Chart Tools指令的一个很好的例子.

说明

这些相同的指令在plunker本身.

  1. 从github下载ng-google-chart.js并在你的html中添加一个脚本标签.
  2. 创建一个div,如:

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

  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
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*cok 11

正如您已经想到的那样,您可以在html或body标签中初始化角度,而无需等待谷歌图表.

为了确保您在谷歌图表JavaScript代码准备好之前不尝试渲染图表,我将使用指令$ watch在google.setOnLoadCallback的回调函数中设置的新控制器$ scope属性/标志.在$ watch回调中,检查以确保设置了标志,然后进行初始化.