<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="../lib/reset.css"> <script src="../lib/angular-1.7.0/angular.min.js"></script> <script src="../lib/echarts/echarts.js"></script> </head> <body ng-app="app" ng-controller="myCtrl"> <div e-chart ec-data="option" style=‘width: 500px; height: 300px; float:left‘></div> <script> var app=angular.module(‘app‘,[]); app.controller(‘myCtrl‘,function($scope){ $scope.option={ title: { text: ‘第一个 ECharts 实例‘ }, tooltip: {}, legend: { data:[‘销量‘] }, xAxis: { data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"] }, yAxis: {}, series: [{ name: ‘销量‘, type: ‘bar‘, data: [5, 20, 36, 10, 10, 20] }] } }); app.directive("eChart", function($http, $window) { function link($scope, element, attrs) { //初始化图表 var myChart = echarts.init(element[0]); //监控option数据变化 $scope.$watch( attrs["ecData"], function() { var option = $scope.$eval(attrs.ecData); if (angular.isObject(option)) { //绘制图表 myChart.setOption(option); } }, true ); $scope.getDom = function() { return { height: element[0].offsetHeight, width: element[0].offsetWidth }; }; //监控图表宽高变化,响应式 $scope.$watch( $scope.getDom, function() { // resize echarts图表 myChart.resize(); }, true ); } return { //A 作为属性使用 restrict: "A", link: link }; }); </script> </body> </html>
原文:https://www.cnblogs.com/SpringAndMoon/p/13491379.html