angularjs通过指令方式引用echarts

  1. index.html引入
  <script src="lib/echarts//echarts.js"></script>
  <script src="lib/echarts/walden.js"></script>
  1. directive.js 指令含饼图,柱图,折线图
.directive('pieCharts', function($parse, $interval){
  'use strict';
  return {
    restrict: 'AE',
    // replace: true,
    scope: {
      options: '=',
    },
    template: '<div>这是饼图</div>',
    controller: function ($scope) {
    },
    link: function(scope, element, attrs) {
      let chart = element.find('div')[0];
      let parent = element[0];
      //    console.log(parent.clientHeight+":"+parent.clientWidth);
      chart.style.width = parent.clientWidth + 'px';
      chart.style.height = parent.clientHeight + 'px';

      let myChart = echarts.init(element[0], 'walden');
      window.addEventListener('resize', function () {
        myChart.resize();//监测图表自适应
      });

      scope.$watch('options', function (n, o) {
        if (typeof (n) === 'object') {
          myChart.setOption(scope.options);
        }
      }, true);

    }
  };
})

.directive('barCharts', function () {
  'use strict';
  return {
    restrict: 'AE',
    scope: {
      // source: '=',
      options: '=',
      parentwidth: '='
    },
    template: '<div>这是柱图</div>',
    controller: function ($scope) {
    },
    link: function (scope, element, attr) {
      // console.log(scope.source);
      let chart = element.find('div')[0];
      let parent = element[0];
      //    console.log(parent.clientHeight+":"+parent.clientWidth);
      if (scope.parentwidth) {
        parent.style.width = scope.parentwidth;
      }
      chart.style.width = parent.clientWidth + 'px';
      chart.style.height = parent.clientHeight + 'px';

      let myChart = echarts.init(chart, 'walden');
      window.addEventListener('resize', function () {
        myChart.resize();//监测图表自适应
      });

      scope.$watch('options', function (n, o) {
        if (typeof (n) === 'object') {
          myChart.setOption(scope.options);
        }
      }, true);
    }
  };
})

.directive('lineCharts', function($parse, $interval){
  'use strict';
  return {
    restrict: 'AE',
    // replace: true,
    scope: {
      options: '=',
      parentwidth: '='
    },
    template: '<div>这是折线图</div>',
    controller: function ($scope) {
    },
    link: function(scope, element, attrs) {
      let chart = element.find('div')[0];
      let parent = element[0];
      //    console.log(parent.clientHeight+":"+parent.clientWidth);
      if (scope.parentwidth) {
        parent.style.width = scope.parentwidth;
      }
      chart.style.width = parent.clientWidth + 'px';
      chart.style.height = parent.clientHeight + 'px';

      let myChart = echarts.init(element[0], 'walden');
      window.addEventListener('resize', function () {
        myChart.resize();//监测图表自适应
      });

      scope.$watch('options', function (n, o) {
        if (typeof (n) === 'object') {
          myChart.setOption(scope.options);
        }
      }, true);
    }
  };
});
  1. demo.js 添加配置

monthOptions: {
  // color: ['#a3d4ff'],
  tooltip : {
    trigger: 'axis'
  },
  xAxis : [
    {
      type : 'category',
      data: _.map(sourceMonth, item => {
        return `${item}月`;
      })
    }
  ],
  yAxis : [
    {
      type : 'value'
    }
  ],
  series : [
    {
      name:'按月查看',
      type:'bar',
      label: {
        normal: {
          formatter: '{c}人',
          show: true,
          position: 'top'
        }
      },
      data:['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
    }
  ]
};

teamOptions: {
  title : {
    text: '当前在场人数:0',
    x:'center',
    y: 'bottom'
  },
  tooltip : {
    trigger: 'item',
    formatter: '{c}人 ({d}%) <br/> {b}',
    confine: true,
  },
  minAngle: 5,              //最小的扇区角度(0 ~ 360),用于防止某个值过小导致扇区太小影响交互
  avoidLabelOverlap: true,   //是否启用防止标签重叠策略
  series : [
    {
      name: '按班组',
      type: 'pie',
      radius: ['25%', '45%'],
      label: {
        normal: {
          // formatter: '{c}人 ({d}%) \n {b}',
          formatter(v) {
            console.log(v.value);
            let name = v.name;
            let text = v.value + '人(' + v.percent + '%)' + '\n' 
            if (name.length <= 8) {
              name = name;
            } else {
              name = `${name.slice(0, 8)}...`
            }
            return text + name;
          },
          textStyle: {
            fontSize: 8
          }
        },
        confine: true
      },
      data:[]
    }
  ]
},

options: {
  // color: ['#2f4554', '#d48265', '#61a0a8'],
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    data:['实发总薪资','在线发放薪资', '台账小计薪资'],
    y: 'bottom'
  },
  xAxis: {
    type: 'category',
    data:  _.map(sourceMonth, item => {
      return `${item}月`;
    })
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name:'实发总薪资',
      type:'line',
      data:['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
    },
    {
      name:'在线发放薪资',
      type:'line',
      data:['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
    },
    {
      name:'台账小计薪资',
      type:'line',
      data:['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
    }
  ]
},

4.demo.html 中使用指令可滚动

<div style="overflow: scroll">
   <div bar-charts options="attendance.monthOptions" parentwidth="'600px'" style="width: 100%;height:300px;overflow: scroll"></div>
</div>
<div ng-show="crumbs[0].active">
  <div pie-charts options="inNumber.teamOptions" style="width: 100%;height:300px;"></div>
</div>
<div style="overflow: scroll">
  <div line-charts options="detail.options" parentwidth="'600px'" style="width: 100%;height:300px;overflow: scroll"></div>
</div>

遇到的问题:
let parent = element[‘context’]; 需要引入jquery 1.x版本可正常显示echarts,但是会导致移动端其他组件的滑动事件失效
改为 let parent = element[0];不需要依赖jquery

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值