使用d3.js

时间:2015-06-22 08:25:46

标签: javascript angularjs svg d3.js chart.js

我第一次尝试用a做的,效果很好。



<button type="button" class="btn btn-default glyphicon glyphicon-arrow-left"></button>
&#13;
&#13;
&#13;

但是,当我尝试对canvas对象执行相同操作时,在这种情况下,chart.js库生成的折线图不起作用。我使用d3.js将图表插入svg。我还在我的angularjs chart.js指令中设置了一个断点(我通过指令使用了chart.js),我注意到该指令根本没有被调用。

带有chart.js画布的foreignObject正在插入到html中,只是没有渲染。

&#13;
&#13;
<foreignObject x="5" y="5">
  <canvas id="line" class="chart chart-line" data="data" labels="labels" legend="true" series="series" click="onClick"></canvas>
</foreignObject>
&#13;
&#13;
&#13;

还有其他人有过类似的问题吗?

1 个答案:

答案 0 :(得分:2)

查看生成的HTML

您需要在foreignObject

上设置高度和宽度
  ...
  .append("xhtml:body")
      .html('<canvas id="line" class="chart chart-line" data="data" labels="labels" legend="true"></canvas>');

并将画布附加到foreignObject

下的body元素
var app = angular.module("app", ["chart.js"]);
app.controller("chartController", ['$scope',
  function($scope) {
    $scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
    $scope.data = [
      [65, 59, 80, 81, 56, 55, 40],
      [28, 48, 40, 19, 86, 27, 90]
    ];
  }
])

d3.select("svg")
  .append("foreignObject")
  .attr("width", 480)
  .attr("height", 500)
  .append("xhtml:body")
  .html('<canvas id="line" class="chart chart-line" data="data" labels="labels"></canvas>');

这是一个工作代码段(请与Chrome核实)

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://rawgit.com/nnnick/Chart.js/master/Chart.min.js"></script>
<link href="https://rawgit.com/jtblin/angular-chart.js/master/dist/angular-chart.css" rel="stylesheet" />
<script src="https://rawgit.com/jtblin/angular-chart.js/master/dist/angular-chart.min.js"></script>

<div ng-app="app">
  <div ng-controller="chartController">
    <svg width="960" height="500"></svg>
  </div>
</div>
npm install