Angular echarts图表自适应屏幕指令

关于echarts图表自适应问题

一、引入js文件

1. 在html页面引入angular.min.js文件
2. 在html页面引入echarts.min.js文件 3. 在html页面引入app.js文件 4. 在html页面引入directive.js文件 5. 来源:http://www.bootcdn.cn 6. 引入方式举例:<script src="js/plugins/echarts/echarts.min.js"></script> 7. 备注:src="js/plugins/echarts/echarts.min.js",其中src=""里面对应的是本地echarts.min.js文件所在目录结构;

二、html页面定义容器

1 <div class="row">
2     <div class="col-md-12 col-sm-12 col-xs-12">
3     <!--图形容器-->
4     <div id="previewChart" resize></div>
5     </div>
6 </div>
**注意**:
1. id="previewChart"属性不可缺少,后面会获取容器id,重新绘图;
2. resize 标签不可缺少,angular框架下,这是一个自定义的指令标签,会调用directive.js文件里面名字叫"resize"的指令;

三、directive.js文件添加自定义指令

  • 声明指令模块,自定义resize指令(监听图形容器变化,重新绘图)
 1 angular.module('iManager.directive', []).directive('resize', function ($window) {
 2     return function (scope, element) {
 3         //获取标签的id属性值
 4         var id = element[0].id;
 5         var w = angular.element($window);
 6         scope.getWindowDimensions = function () {
 7             return {
 8                 'h': w.height(),
 9                 'w': w.width()
10             };
11         };
12         scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
13             //获取新窗口的宽度和高度
14             scope.windowHeight = newValue.h;
15             scope.windowWidth = newValue.w;
16             //获取容器,重新绘图
17             var ele = document.getElementById(id);
18             var pieChart = echarts.init(ele);
19             pieChart.resize();
20         }, true);
21         w.bind('resize', function () {
22             //页面脏检查
23             scope.$apply();
24         });
25     }
26 })

 

四、模块声明和依赖注入

1 /** app.js文件 **/
2 var iManager=angular.module("iManager",['iManager.directive'])
3 /*备注:模块声明,模块名:iManager,在[]里面依赖注入相关模块*/

 

 

 

 

转载于:https://www.cnblogs.com/mycnblogs-guoguo/p/9704932.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular Echarts一个使用 Angular 框架与 Echarts 图表库集成的工具。它提供了一种简单而强大的方式来在 Angular 应用创建交互式图表。通过 Angular Echarts,你可以轻松地将各种图表(如折线图、柱状图、饼图等)添加到你的 Angular 组件,并对其进行配置和自定义。 要使用 Angular Echarts,首先需要在你的 Angular 项目安装 echarts 和 ngx-echarts 库。然后,在你的组件引入 ngx-echarts 模块,并使用相应的组件来创建和配置图表。 以下是一个简单的示例,展示如何在 Angular 使用 Echarts: 1. 首先,安装 echarts 和 ngx-echarts: ``` npm install echarts ngx-echarts ``` 2. 在你的组件引入 ngx-echarts 模块: ```typescript import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { NgxEchartsModule } from 'ngx-echarts'; import { AppComponent } from './app.component'; @NgModule({ declarations: [AppComponent], imports: [BrowserModule, NgxEchartsModule.forRoot()], bootstrap: [AppComponent] }) export class AppModule {} ``` 3. 在你的组件模板使用 ngx-echarts 组件来创建图表: ```html <div> <ngx-echarts [options]="chartOptions" [theme]="chartTheme" [loading]="isLoading" (chartInit)="onChartInit($event)"></ngx-echarts> </div> ``` 4. 在你的组件类定义图表的配置和数据: ```typescript import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { chartOptions: any; chartTheme: string; isLoading: boolean; constructor() { this.chartOptions = { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] }, yAxis: { type: 'value' }, series: [{ data: [150, 230, 224, 218, 135, 147, 260], type: 'bar' }] }; this.chartTheme = 'light'; this.isLoading = false; } onChartInit(echartsInstance: any) { // 可以在这里对图表实例进行进一步的配置和操作 console.log('Chart initialized:', echartsInstance); } } ``` 以上示例演示了如何使用 ngx-echartsAngular 创建一个简单的柱状图。你可以根据自己的需求进行配置和扩展,使用不同类型的图表和其他 Echarts 功能。 希望这个简单的示例能帮助你入门 Angular Echarts!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值