简体   繁体   English

如何将缩放图表与角度2集成

[英]how to integration zoom Chart with angular 2

这是否有可能将Zoom chart与angular 2集成在一起基本的思想是有人做过此事,没有在Internet上找到与此相关的任何线索,这就是为什么我在这里问的任何线索?

1. Description based on angular-seed application 1. 基于angular-seed应用的描述

For Angular2 - here are step by step instructions on how to get ZoomCharts running within the angular-seed application. 对于Angular2-这是有关如何使ZoomCharts在angular-seed应用程序中运行的逐步说明。 Note that they only describe the steps to get a sample running within the seed application, not the steps required to build fully functional component. 请注意,它们仅描述了使样本在种子应用程序中运行的步骤,而没有描述构建功能齐全的组件所需的步骤。

1. Copy zoomcharts.d.ts file into /tools/manual_typings/project/ folder. 1.将zoomcharts.d.ts文件复制到/tools/manual_typings/project/文件夹中。

2. Modify the zoomcharts.d.ts file to support CommonJS/SystemJS module system by adding at the top of it these lines: 2.通过在顶部添加以下几行,修改zoomcharts.d.ts文件以支持CommonJS / SystemJS模块系统:

declare module "ZoomCharts" { export = ZoomCharts; }

3. In the tools\\project.config.ts file add this line into the constructor (of course, using CDN is optional) to register the library with SystemJS loader: 3.在tools\\project.config.ts文件中,将此行添加到构造函数中(当然,使用CDN是可选的)以向SystemJS加载程序注册该库:

this.addPackagesBundles([{ name: "ZoomCharts", path: "https://cdn.zoomcharts-cloud.com/1/latest/zoomcharts.js" }]);

4. Create a new component for the chart, for example, /src/client/app/home/zc-pie-chart.component.ts 4.为图表创建一个新组件,例如/src/client/app/home/zc-pie-chart.component.ts

``` /// ```///

import { Component, OnInit, ViewChild, AfterViewInit, ElementRef } from '@angular/core'; 从'@ angular / core'导入{Component,OnInit,ViewChild,AfterViewInit,ElementRef}; import { PieChart } from "ZoomCharts"; 从“ ZoomCharts”导入{PieChart};

@Component({ moduleId: module.id, selector: "zc-pie-chart", template: "PieChart is being initialized..." }) export class ZcPieChart implements AfterViewInit { @Component({moduleId:module.id,选择器:“ zc-pie-chart”,模板:“ PieChart正在初始化...”})导出类ZcPieChart实现AfterViewInit {

 @ViewChild("container") private container: ElementRef; ngAfterViewInit() { var x = new PieChart({ container: this.container.nativeElement, assetsUrlBase: System.paths["ZoomCharts"] + "/../assets/", data: [{ url: "https://zoomcharts.com/dvsl/data/pie-chart/browsers.json", }] }); } } ``` 

5. Register the component in the module (in this sample case, home.module.ts ): 5.将组件注册到模块中(在本示例中为home.module.ts ):

``` import { ZcPieChart } from './zc-pie-chart.component'; ```从'./zc-pie-chart.component'导入{ZcPieChart};

.. declarations: [..other components.., ZcPieChart], .. ``` ..声明:[..other components ..,ZcPieChart],..```

6. Add it to the view, for example, home.component.html : 6.将其添加到视图中,例如home.component.html

<zc-pie-chart></zc-pie-chart>

2. Integration with Webpack 2. 与Webpack集成

Note: I tested this with Webpack 2.2.1. 注意:我使用Webpack 2.2.1对此进行了测试。

With Webpack you can use simple import ZoomCharts from './zoomcharts/zoomcharts.js'; 使用Webpack,您可以import ZoomCharts from './zoomcharts/zoomcharts.js';使用简单的import ZoomCharts from './zoomcharts/zoomcharts.js'; and it works fine - including bundling of the zoomcharts.js code. 而且效果很好-包括zoomcharts.js代码的捆绑。

Though in such case ZoomCharts will now load dependencies such as moment.js by itself even if they are already included in the webpack bundle. 尽管在这种情况下,ZoomCharts现在将自己加载依赖项(例如moment.js ,即使它们已经包含在webpack捆绑包中。 To enable loading moment.js from the bundle, I used this webpack.config.js file (and using the imports-loader plugin): 为了启用从捆绑包中加载moment.js ,我使用了这个webpack.config.js文件(并使用了imports-loader插件):

```js var path = require('path'); js var path = require('path');

module.exports = { entry: './index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname) }, resolve: { alias: { "moment": path.resolve(__dirname, "zoomcharts", "assets", "moment.js"), "moment-timezone": path.resolve(__dirname, "zoomcharts", "assets", "moment-tz.js"), "zoomcharts": path.resolve(__dirname, "zoomcharts", "zoomcharts.js"), } }, module: { rules: [ { test: path.resolve(__dirname, "zoomcharts", "zoomcharts.js"), loader: { loader: "imports-loader", options: { "moment": "moment", "momentTimezone": "moment-timezone", // workaround that sets window.moment as required by zoomcharts.js "_": ">window.moment=moment;" module.exports = {条目:“ ./ index.js”,输出:{文件名:“ bundle.js”,路径:path.resolve(__ dirname)},解析:{别名:{“时刻”:path.resolve( __dirname,“ zoomcharts”,“资产”,“ moment.js”),“ moment-timezone”:path.resolve(__ dirname,“ zoomcharts”,“ assets”,“ moment-tz.js”),“ zoomcharts”: path.resolve(__ dirname,“ zoomcharts”,“ zoomcharts.js”),}},模块:{规则:[{test:path.resolve(__ dirname,“ zoomcharts”,“ zoomcharts.js”),加载程序:{loader :“ imports-loader”,选项:{“ moment”:“ moment”,“ momentTimezone”:“ moment-timezone”,//解决方法是按照zoomcharts.js的要求设置window.moment“ _”:“>窗口。时刻=时刻;” } } } ], } }; }}}],}}; ``` ```

first install zoomchart dependency 首先安装zoomchart依赖项

npm install --save @dvsl/zoomcharts npm install --save @ dvsl / zoomcharts

Html HTML

<script src="https://cdn.zoomcharts-cloud.com/1/latest/zoomcharts.js"></script>
<div id='demo' style='width:100%; height:300px;'></div>

in ts file create a method 在ts文件中创建方法

import * as zc from '@dvsl/zoomcharts';
import { WindowRef } from './../../WindowRef';
import { Component, OnInit} from '@angular/core';

export class ZoomChartImplementation implements OnInit{

private zc: any = zc;

constructor(private winRef: WindowRef){
winRef.nativeWindow.ZoomChartsLicense = 'INSERT YOUR ZOOM CHART LICENSE HERE';
winRef.nativeWindow.ZoomChartsLicenseKey ='INSERT YOUR ZOOM CHART LICENSE KEY HERE';
}

loadZoomChart(){
const PieChart = this.zc.PieChart;
const t = new PieChart({
  container: document.getElementById('demo'),
  area: { height: 350 },
  data: {
    preloaded: {
      subvalues: [
        {
          id: 'foo', value: 100, subvalues: [
            { id: 'foo-1', value: 50, style: { expandable: false } },
            { id: 'foo-2', value: 50, style: { expandable: false } }
          ]
        },
        { id: 'bar', value: 50, style: { expandable: false } },
        { id: 'baz', value: 30, style: { expandable: false } }
      ]
    }
  }
});

}

}

you can use any chart instead of pie-chart in my example 您可以在我的示例中使用任何图表代替饼图
this.zc.ANY_CHART this.zc.ANY_CHART

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM