上次我们使用 Wijmo 和 Angular 2 创建了第一个小应用,从零开始构建 Wijmo & Angular 2 小应用, 现在就来看看如何使用 Wijmo & Angular 2 构建一个 FlexChart 。

1.新建我们的项目。

$ mkdir wj-ng2-flexgrid
$ cd wj-ng2-flexgrid

2.配置项目。

我们需要下面 3 个配置文件。

  • package.json 。 用来标记项目需要使用的npm依赖包。
  • tsconfig.json 。 这个是typescript的配置文件,定义了 TypeScript 编译器如何从项目源文件生成 JavaScript 代码。
  • systemjs.config.js 。 为模块加载器SystemJS 提供了该到哪里查找应用模块的信息,并注册了所有必备的依赖包 。(这里使用SystemJS 来配置模块,也可以使用Webpcak,神一般的利器。详情请参考博客园专家级人物 冠军 的博客: http://www.cnblogs.com/haogj/p/5998556.html#3541215)
    如果不太明白配置文件中键值对的意义,可以在底部留言或者上网查询。

->>package.json

{
  "name": "wj-ng2-flexchart","version": "1.0.0","scripts": { "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ","lite": "lite-server","tsc": "tsc","tsc:w": "tsc -w" },"licenses": [ { "type": "MIT","url": "https://github.com/angular/angular.io/blob/master/LICENSE" } ],"dependencies": { "@angular/common": "~2.1.1","@angular/compiler": "~2.1.1","@angular/core": "~2.1.1","@angular/forms": "~2.1.1","@angular/http": "~2.1.1","@angular/platform-browser": "~2.1.1","@angular/platform-browser-dynamic": "~2.1.1","@angular/router": "~3.1.1","@angular/upgrade": "~2.1.1","angular-in-memory-web-api": "~0.1.13","core-js": "^2.4.1","reflect-Metadata": "^0.1.8","rxjs": "5.0.0-beta.12","systemjs": "0.19.39","zone.js": "^0.6.25" },"devDependencies": { "@types/core-js": "^0.9.34","@types/node": "^6.0.45","concurrently": "^3.0.0","lite-server": "^2.2.2","typescript": "^2.0.3" } }

->> tsconfig.json

{
  "compilerOptions": { "target": "es5","module": "commonjs","moduleResolution": "node","sourceMap": true,"emitDecoratorMetadata": true,"experimentalDecorators": true,"removeComments": false,"noImplicitAny": false } }

->> systemjs.config.js

/** * System configuration for Angular samples * Adjust as necessary for your application needs. */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },// map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',// angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js','@angular/common': 'npm:@angular/common/bundles/common.umd.js','@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js','@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js','@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js','@angular/http': 'npm:@angular/http/bundles/http.umd.js','@angular/router': 'npm:@angular/router/bundles/router.umd.js','@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js','@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',// other libraries
      'rxjs':                      'npm:rxjs','angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },// packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',defaultExtension: 'js'
      },rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

安装依赖包。

在当前目录下,运行

$ npm install

所有依赖包会全部下载下来,如果命令行有警告,可以忽略 。这些警告表示包里没有repository field,这些field仅仅用于一些包信息。

如果因为某些原因包无法下载,那可以使用淘宝的镜像 cnpm。这个镜像会每隔10分钟和官方同步一次。

安装结束,会在项目的根目录下多出一个node_modules 文件夹,它实在是太大了 !

现在您需要将 \wijmoEnterprise\Samples\TS\Angular2\FlexGridIntro\FlexGridIntro\node_modules\wijmo 文件夹拷贝到当前项目中的 node_modules 文件夹。这些文件用来将wijmo包包装为 es6 模块。

好了,现在的准备工作已经完成了,您可以开始创建wijmo & Angular 2 的应用了。

3. 创建目录

玩Angular 2,首先我们需要Angular 2的脚手架。

现在来看看我的文件目录,并逐一解释。

└─ wj-ng2-flexchart/ ······························· 项目所在目录
   ├─ node_modules/ ······························· 项目依赖包
   ├─ app/ ········································ 应用程序子目录
   │  ├─ components/ ······························ 组件目录
   │  │  ├─ app.component.html ···················· 根组件app.Component模板
   │  │  └─ app.conponent.ts ······················ 根组件app.Component
   │  ├─ services/ ································ 服务目录
   │  │  └─ data.service.ts ······················· 数据服务 data.Service
   │  ├─ app.module.ts ···························· 根模块app.module
   │  └─ main.ts ·································· Angular 引导文件
   ├─ scripts/ ···································· 外部js 目录
   │  ├─ deFinition/ ······························ wijmo 模块定义目录
   │  └─ vendor/ ·································· wijmo 脚本目录
   ├─ styles/ ····································· 样式目录
   ├─ index.html ·································· 应用宿主页面
   ├─ package.json ································ npm 依赖列表
   ├─ systemjs.config.js ·························· systemJS 配置
   ├─ tsconfig.js ································· TypeScript 配置
   └─ readme.md ··································· 程序说明

这看起来似乎比较复杂,但是却很有条理。

4. 编写宿主页面

在宿主页面中,除了Angular 2中必须的组件,还需要引入Wijmo js脚本。

<html>
<head>
    <Meta charset="UTF-8">
    <title>使用 Angular 2 来创建FlexGrid控件</title>
    <!--angular 2 模块开始 -->
    <!--用于填充旧版浏览器-->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-Metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <!--systemjs 配置开始-->
    <script src="systemjs.config.js"></script>

    <!--wijmo 模块开始-->
    <script src="scripts/vendor/wijmo.min.js"></script>
    <script src="scripts/vendor/wijmo.chart.min.js"></script>
    <link rel="stylesheet" href="styles/wijmo.min.css">
    <script src="scripts/vendor/wijmo.angular2.min.js"></script>
    <!--mine-->
    <script> System.import('./app/main').catch(function(err){ console.error(err); }); </script>
</head>
<body>
    <!--申明根组件-->
    <app-cmp>
        Loading ...
    </app-cmp>
</body>
</html>

5. 编写数据服务

这个页面定义完毕,现在来编写一个数据服务。这个数据服务需要被注入到组件中,因此需要引入一个元标记 Injectable 。

data.Service 返回一些国家相关信息的随机数据。

'use strict'

import { Injectable } from '@angular/core';

@Injectable()
export class DataService {
    getData(countries: string[]): any[] {
        var data = [];
        for (let i = 0; i < countries.length; i++) {
            data.push({
                country: countries[i],downloads: Math.round(Math.random() * 20000),sales: Math.random() * 10000,expenses: Math.random() * 5000
            });
        }
        return data;
    };
}

6. 编写根组件和模块

现在我们编写应用的第一个组件:根组件 app.component,也是这个程序唯一的组件。

import { Component,Inject } from '@angular/core';
import { DataService } from '../services/data.service';

@Component({
    selector: 'app-cmp',templateUrl: 'app/components/app.component.html',})
export class AppComponent {
    protected dataSvc: DataService;
    countries = 'US,Germany,UK,Japan,Italy,Greece'.split(',');
    data: { country: string,downloads: number,sales: number,expenses: number }[];

    constructor( @Inject(DataService) dataSvc: DataService) {
        this.dataSvc = dataSvc;
        this.data = this.dataSvc.getData(this.countries);
    }
}

在这个组件中,需要引入两个元标记。Component,Inject ,还需要注入定义的数据服务data.Service。

在组件app.component.html模板中,

<div>
    <wj-flex-chart [itemsSource]="data" [bindingX]="'country'">
        <wj-flex-chart-series [name]="'Sales'" [binding]="'sales'"></wj-flex-chart-series>
        <wj-flex-chart-series [name]="'Expenses'" [binding]="'expenses'"></wj-flex-chart-series>
        <wj-flex-chart-series [name]="'Downloads'" [binding]="'downloads'"></wj-flex-chart-series>
    </wj-flex-chart>
</div>

在这里,仅仅需要引入一个 wj-flex-grid 标记,就可以创建一个 flexgrid控件了,wj-flex-grid 组件是作为一个子组件存在的,在app.module 模块中注入。
通过wj-flex-chart-series 来申明系列,通过bindling 来指定绑定的字段。
itemsSource 绑定一个数据源,这个itemsSource是flexgrid已经封装完成的属性。在 flexgrid 内部是通过 @Input 来完成的。

在根模块中将组件注入

import {   NgModule } from '@angular/core';
import { browserModule } from '@angular/platform-browser';
import { WjGridModule } from 'wijmo/wijmo.angular2.grid';
import { AppComponent } from './components/app.component';
import { DataService } from './services/data.service';
@NgModule({
    imports: [ WjGridModule,browserModule],declarations: [AppComponent],providers:[DataService],bootstrap: [AppComponent],})
export class AppModule { }

在这里,需要将引用的所有的组件和模块都要注入进来。

7. 引导和启动引用

最后是引导程序 main.ts

import { platformbrowserDynamic } from '@angular/platform-browser-dynamic';
import {enableProdMode} from '@angular/core';
import { AppModule } from './app.module';
enableProdMode();
platformbrowserDynamic().bootstrapModule(AppModule);

在根目录下,运行

$ npm start

这时,程序会自动打开默认浏览器并渲染页面。

start 命令是执行定义在 package.json 文件中的scripts命令。 会将ts代码编译为原生js,并且会启动一个静态服务器。 这个服务器会检测文件的变化,当发现文件改动,那么会自动编译ts代码。

http://images2015.cnblogs.com/blog/857465/201611/857465-20161109111636561-530904138.png” alt=”效果截图” title=”” />

使用Wijmo & Angular 2 创建 FlexChart的更多相关文章

  1. 利用Node实现HTML5离线存储的方法

    这篇文章主要介绍了利用Node实现HTML5离线存储的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  2. canvas中普通动效与粒子动效的实现代码示例

    canvas用于在网页上绘制图像、动画,可以将其理解为画布,在这个画布上构建想要的效果。本文详细的介绍了粒子特效,和普通动效进行对比,非常具有实用价值,需要的朋友可以参考下

  3. H5混合开发app如何升级的方法

    本篇文章主要介绍了H5混合开发app如何升级的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  4. canvas学习和滤镜实现代码

    这篇文章主要介绍了canvas学习和滤镜实现代码,利用 canvas,前端人员可以很轻松地、进行图像处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. localStorage的过期时间设置的方法详解

    这篇文章主要介绍了localStorage的过期时间设置的方法详解的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  6. 详解HTML5 data-* 自定义属性

    这篇文章主要介绍了详解HTML5 data-* 自定义属性的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  7. HTML5的postMessage的使用手册

    HTML5提出了一个新的用来跨域传值的方法,即postMessage,这篇文章主要介绍了HTML5的postMessage的使用手册的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  8. 教你使用Canvas处理图片的方法

    本篇文章主要介绍了教你使用Canvas处理图片的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  9. ios – 使用带有NodeJs HTTPS的certificates.cer

    我为IOS推送通知生成了一个.cer文件,我希望将它与NodeJSHTTPS模块一起使用.我发现HTTPS模块的唯一例子是使用.pem和.sfx文件,而不是.cer:有解决方案吗解决方法.cer文件可以使用两种不同的格式进行编码:PEM和DER.如果您的文件使用PEM格式编码,您可以像使用任何其他.pem文件一样使用它(有关详细信息,请参见Node.jsdocumentation):如果您的文件使

  10. ios – Swift语言:如何调用SecRandomCopyBytes

    从Objective-C,我可以这样做:在Swift中尝试这个时,我有以下内容:但我得到这个编译器错误:data.mutableBytes参数被拒绝,因为类型不匹配,但我无法弄清楚如何强制参数.解决方法这似乎有效:

随机推荐

  1. Angular2 innerHtml删除样式

    我正在使用innerHtml并在我的cms中设置html,响应似乎没问题,如果我这样打印:{{poi.content}}它给了我正确的内容:``但是当我使用[innerHtml]=“poi.content”时,它会给我这个html:当我使用[innerHtml]时,有谁知道为什么它会剥离我的样式Angular2清理动态添加的HTML,样式,……

  2. 为Angular根组件/模块指定@Input()参数

    我有3个根组件,由根AppModule引导.你如何为其中一个组件指定@input()参数?也不由AppModalComponent获取:它是未定义的.据我所知,你不能将@input()传递给bootstraped组件.但您可以使用其他方法来做到这一点–将值作为属性传递.index.html:app.component.ts:

  3. angular-ui-bootstrap – 如何为angular ui-bootstrap tabs指令指定href参数

    我正在使用角度ui-bootstrap库,但我不知道如何为每个选项卡指定自定义href.在角度ui-bootstrap文档中,指定了一个可选参数select(),但我不知道如何使用它来自定义每个选项卡的链接另一种重新定义问题的方法是如何使用带有角度ui-bootstrap选项卡的路由我希望现在还不算太晚,但我今天遇到了同样的问题.你可以通过以下方式实现:1)在控制器中定义选项卡href:2)声明一个函数来改变控制器中的散列:3)使用以下标记:我不确定这是否是最好的方法,我很乐意听取别人的意见.

  4. 离子框架 – 标签内部的ng-click不起作用

    >为什么标签标签内的按钮不起作用?>但是标签外的按钮(登陆)工作正常,为什么?>请帮我解决这个问题.我需要在点击时做出回复按钮workingdemo解决方案就是不要为物品使用标签.而只是使用divHTML

  5. Angular 2:将值传递给路由数据解析

    我正在尝试编写一个DataResolver服务,允许Angular2路由器在初始化组件之前预加载数据.解析器需要调用不同的API端点来获取适合于正在加载的路由的数据.我正在构建一个通用解析器,而不是为我的许多组件中的每个组件设置一个解析器.因此,我想在路由定义中传递指向正确端点的自定义输入.例如,考虑以下路线:app.routes.ts在第一个实例中,解析器需要调用/path/to/resourc

  6. angularjs – 解释ngModel管道,解析器,格式化程序,viewChangeListeners和$watchers的顺序

    换句话说:如果在模型更新之前触发了“ng-change”,我可以理解,但是我很难理解在更新模型之后以及在完成填充更改之前触发函数绑定属性.如果您读到这里:祝贺并感谢您的耐心等待!

  7. 角度5模板形式检测形式有效性状态的变化

    为了拥有一个可以监听其包含的表单的有效性状态的变化的组件并执行某些组件的方法,是reactiveforms的方法吗?

  8. Angular 2 CSV文件下载

    我在springboot应用程序中有我的后端,从那里我返回一个.csv文件WheniamhittingtheURLinbrowsercsvfileisgettingdownloaded.现在我试图从我的角度2应用程序中点击此URL,代码是这样的:零件:服务:我正在下载文件,但它像ActuallyitshouldbeBook.csv请指导我缺少的东西.有一种解决方法,但您需要创建一个页面上的元

  9. angularjs – Angular UI-Grid:过滤后如何获取总项数

    提前致谢:)你应该避免使用jQuery并与API进行交互.首先需要在网格创建事件中保存对API的引用.您应该已经知道总行数.您可以使用以下命令获取可见/已过滤行数:要么您可以使用以下命令获取所选行的数量:

  10. angularjs – 迁移gulp进程以包含typescript

    或者我应该使用tsc作为我的主要构建工具,让它解决依赖关系,创建映射文件并制作捆绑包?

返回
顶部