在Angular 4应用程序中使用Morris Js插件

在Angular 4应用程序中使用Morris Js插件,angular,typescript,morris.js,Angular,Typescript,Morris.js,我想在我的angular 4项目中使用Morris Js插件? 我知道如何在angular 2,4中使用jQuery? 但我不知道如何将Morris chart集成到我的NG4应用程序中。 如果您有这方面的经验,请给我帮助。我使用与使用jQuery相同的方法解决此问题。 请看下面的代码 declare var $: any; declare var Morris: any; ... ngOnInit() { Morris.Bar({ element: 'morris-bar',

我想在我的angular 4项目中使用Morris Js插件? 我知道如何在angular 2,4中使用jQuery? 但我不知道如何将Morris chart集成到我的NG4应用程序中。
如果您有这方面的经验,请给我帮助。

我使用与使用jQuery相同的方法解决此问题。 请看下面的代码

declare var $: any;
declare var Morris: any;
...
ngOnInit() {
   Morris.Bar({
      element: 'morris-bar',
      data: [
        {
          x: '2017 R1',
          y: 3,
          z: 2,
          a: 3,
        },
   ......
   xkey: 'X',
   kyes: ['y', 'z', 'a'],
   ...
   });
}

我使用与jQuery相同的方法来解决这个问题。 请看下面的代码

declare var $: any;
declare var Morris: any;
...
ngOnInit() {
   Morris.Bar({
      element: 'morris-bar',
      data: [
        {
          x: '2017 R1',
          y: 3,
          z: 2,
          a: 3,
        },
   ......
   xkey: 'X',
   kyes: ['y', 'z', 'a'],
   ...
   });
}
  • 应安装Raphael.js(npm install Raphael),然后在脚本中将其添加到angular.json中。例如:

    “脚本”:[“/node\u modules/raphael/raphael.min.js”]

  • Install morris npm Install morris.js06(该fork不需要jQuery,这对于Angular4+web应用程序更好)

  • 在src/app/directives/morris chart中创建指令morris chart.ts和接口morris chart.interface.ts(以下提供了源代码)

  • 在src/app/app.module.ts中,添加
    从“/directions/morris-chart/morris-chart”导入{MorrisChartDirective}
    声明[]中的
    指令

  • 在src/app/app.component.ts中,添加
    import'morris.js06/morris.js'

    公共数据; 公共选择; 构造函数(){ ... } 恩戈尼尼特(){ this.datas=[ {xkey:'2018',值:20}, {xkey:'2019',值:10} ] 此选项={ xkey:“xkey”, ykeys:['value'], 标签:['value'] }; }

  • 在src/app/app.component.html中,添加

  • 就这些^^

    受此启发,这里提供了一个指令和一个界面,可以轻松地将Morris图表与Angular4结合使用+

    莫里斯图表.ts

    import { Directive, AfterViewInit, OnInit, OnDestroy, Input, ElementRef, NgZone, OnChanges, SimpleChanges, Output, EventEmitter } from '@angular/core';
    
    import { ChartOptions, ChartDatas } from './morris-chart.interface';
    
    @Directive({
        selector: '[mk-morris-js]'
    })
    export class MorrisChartDirective implements OnInit, AfterViewInit, OnChanges, OnDestroy {
        private window: any = window;
        private _options: ChartOptions;
    
        public chartInstance: any;
    
        @Input() type = 'Line';
        @Input() options: ChartOptions;
        @Input() data: ChartDatas;
        @Output() clickChart = new EventEmitter();
    
        /*
        * [constructor description]
        * @method constructor
        * @param  {ElementRef} private elementRef [description]
        * @param  {NgZone}     private ngZone     [description]
        */
        constructor(
            private elementRef: ElementRef,
            private ngZone: NgZone
        ) {}
    
        /*
        * [ngOnInit description]
        * @method ngOnInit
        */
        ngOnInit() {
            this._options = Object.assign({}, this.options);
            this._options.element = this.elementRef.nativeElement;
            this._options.data = this.data;
        }
    
        /*
        * [ngAfterViewInit description]
        * @method ngAfterViewInit
        */
        ngAfterViewInit() {
            if(!this.window.Morris) {
                throw new Error('Please include node_modules/morris.js/morris.js');
            } else {
                this.ngZone.runOutsideAngular(() => {
                    this.chartInstance = new this.window.Morris[this.type](this._options);
                    let my_this = this;
                    this.chartInstance.on('click', function(i, row) { 
                        my_this.clickChart.emit({ event, i, row });
                    });
                });
            }
        }
    
        /*
        * [ngOnChanges description]
        * @method ngOnChanges
        * @param  {SimpleChanges} changes [description]
        */
        ngOnChanges(changes: SimpleChanges) {
            if((changes.data && !changes.data.firstChange) || (changes.options && !changes.options.firstChange)) {
                Object.assign(this.chartInstance.options, this.options);
                this.chartInstance.setData(this.data);
            }
        }
    
        /*
        * [ngOnDestroy description]
        * @method ngOnDestroy
        */
        ngOnDestroy() {
            if (this.chartInstance.el.empty instanceof Function) {
                this.chartInstance.el.empty();
            }
        }
    }
    
    export interface ChartData {
      label: string;
      value: number;
    }
    
    export interface ChartDatas {
        [key: string]: ChartData;
    }
    
    export interface ChartOptions {
      element: Element;
      data: ChartDatas;
      resize?: boolean;
    }
    
    
    export interface ChartDonutOptions extends ChartOptions {
      colors?: Array<string>;
      formater?: (y, data) => string;
      resize?: boolean;
    }
    
    export interface ChartAreaBarLineOptions {
      xkey: string,
      ykeys: Array<string>;
      labels: Array<string>;
      hideHover?: boolean|string;
      hoverCallback?: (index, options, content, row) => void;
      axes?: boolean;
      grid?: boolean;
      gridTextColor?: string;
      gridTextSize?: number;
      gridTextFamily?: string;
      gridTextWeight?: string;
      fillOpacity?: number;
    }
    
    export interface ChartAreaOptions extends ChartAreaBarLineOptions {
      behaveLikeLine?: boolean;
    }
    
    export interface ChartBarOptions extends ChartAreaBarLineOptions {
      barColors?: Array<string>;
      stacked?: boolean;
    }
    
    export interface ChartLineOptions extends ChartAreaBarLineOptions {
      lineColors?: Array<string>;
      lineWidth?: number;
      pointSize?: number;
      pointFillColors?: string;
      pointStrokeColors?: string;
      ymax?: string|number;
      ymin?: string|number;
      smooth?: boolean;
      postUnits?: string;
      preUnits?: string;
      dateFormat?: (timestamp: number) => string;
      xLabels?: string;
      xLabelFormat?: (date: Date) => string;
      xLabelAngle?: number;
      yLabelFormat?: (label: string|number) => string;
      goals?: Array<number>;
      goalStrokeWidth?: number;
      goalLineColors?: string;
      events?: Array<number>;
      eventStrokeWidth?: number;
      eventLineColors?: Array<string>;
      continuousLine?: boolean;
    }
    
    morris chart.interface.ts

    import { Directive, AfterViewInit, OnInit, OnDestroy, Input, ElementRef, NgZone, OnChanges, SimpleChanges, Output, EventEmitter } from '@angular/core';
    
    import { ChartOptions, ChartDatas } from './morris-chart.interface';
    
    @Directive({
        selector: '[mk-morris-js]'
    })
    export class MorrisChartDirective implements OnInit, AfterViewInit, OnChanges, OnDestroy {
        private window: any = window;
        private _options: ChartOptions;
    
        public chartInstance: any;
    
        @Input() type = 'Line';
        @Input() options: ChartOptions;
        @Input() data: ChartDatas;
        @Output() clickChart = new EventEmitter();
    
        /*
        * [constructor description]
        * @method constructor
        * @param  {ElementRef} private elementRef [description]
        * @param  {NgZone}     private ngZone     [description]
        */
        constructor(
            private elementRef: ElementRef,
            private ngZone: NgZone
        ) {}
    
        /*
        * [ngOnInit description]
        * @method ngOnInit
        */
        ngOnInit() {
            this._options = Object.assign({}, this.options);
            this._options.element = this.elementRef.nativeElement;
            this._options.data = this.data;
        }
    
        /*
        * [ngAfterViewInit description]
        * @method ngAfterViewInit
        */
        ngAfterViewInit() {
            if(!this.window.Morris) {
                throw new Error('Please include node_modules/morris.js/morris.js');
            } else {
                this.ngZone.runOutsideAngular(() => {
                    this.chartInstance = new this.window.Morris[this.type](this._options);
                    let my_this = this;
                    this.chartInstance.on('click', function(i, row) { 
                        my_this.clickChart.emit({ event, i, row });
                    });
                });
            }
        }
    
        /*
        * [ngOnChanges description]
        * @method ngOnChanges
        * @param  {SimpleChanges} changes [description]
        */
        ngOnChanges(changes: SimpleChanges) {
            if((changes.data && !changes.data.firstChange) || (changes.options && !changes.options.firstChange)) {
                Object.assign(this.chartInstance.options, this.options);
                this.chartInstance.setData(this.data);
            }
        }
    
        /*
        * [ngOnDestroy description]
        * @method ngOnDestroy
        */
        ngOnDestroy() {
            if (this.chartInstance.el.empty instanceof Function) {
                this.chartInstance.el.empty();
            }
        }
    }
    
    export interface ChartData {
      label: string;
      value: number;
    }
    
    export interface ChartDatas {
        [key: string]: ChartData;
    }
    
    export interface ChartOptions {
      element: Element;
      data: ChartDatas;
      resize?: boolean;
    }
    
    
    export interface ChartDonutOptions extends ChartOptions {
      colors?: Array<string>;
      formater?: (y, data) => string;
      resize?: boolean;
    }
    
    export interface ChartAreaBarLineOptions {
      xkey: string,
      ykeys: Array<string>;
      labels: Array<string>;
      hideHover?: boolean|string;
      hoverCallback?: (index, options, content, row) => void;
      axes?: boolean;
      grid?: boolean;
      gridTextColor?: string;
      gridTextSize?: number;
      gridTextFamily?: string;
      gridTextWeight?: string;
      fillOpacity?: number;
    }
    
    export interface ChartAreaOptions extends ChartAreaBarLineOptions {
      behaveLikeLine?: boolean;
    }
    
    export interface ChartBarOptions extends ChartAreaBarLineOptions {
      barColors?: Array<string>;
      stacked?: boolean;
    }
    
    export interface ChartLineOptions extends ChartAreaBarLineOptions {
      lineColors?: Array<string>;
      lineWidth?: number;
      pointSize?: number;
      pointFillColors?: string;
      pointStrokeColors?: string;
      ymax?: string|number;
      ymin?: string|number;
      smooth?: boolean;
      postUnits?: string;
      preUnits?: string;
      dateFormat?: (timestamp: number) => string;
      xLabels?: string;
      xLabelFormat?: (date: Date) => string;
      xLabelAngle?: number;
      yLabelFormat?: (label: string|number) => string;
      goals?: Array<number>;
      goalStrokeWidth?: number;
      goalLineColors?: string;
      events?: Array<number>;
      eventStrokeWidth?: number;
      eventLineColors?: Array<string>;
      continuousLine?: boolean;
    }
    
    导出接口图表数据{
    标签:字符串;
    值:数字;
    }
    导出接口图表数据{
    [键:字符串]:图表数据;
    }
    导出接口图表选项{
    元素:元素;
    数据:图表数据;
    调整大小?:布尔值;
    }
    导出接口图表自动选项扩展图表选项{
    颜色?:数组;
    格式化程序?:(y,数据)=>字符串;
    调整大小?:布尔值;
    }
    导出接口图表AreaBarLineOptions{
    xkey:string,
    ykeys:数组;
    标签:数组;
    隐藏?:布尔|字符串;
    hoverCallback?:(索引、选项、内容、行)=>void;
    轴?:布尔值;
    网格?:布尔;
    gridTextColor?:字符串;
    gridTextSize?:数字;
    gridTextFamily?:字符串;
    gridTextWeight?:字符串;
    fillOpacity?:数字;
    }
    导出界面ChartAreaOptions扩展ChartAreaBarLineOptions{
    behaveLikeLine?:布尔值;
    }
    导出接口ChartBarOptions扩展ChartAreaBarLineOptions{
    barColors?:数组;
    堆叠?:布尔值;
    }
    导出界面ChartLineOptions扩展ChartAreaBarLineOptions{
    lineColors?:数组;
    线宽?:数字;
    点大小?:数字;
    PointFillColor?:字符串;
    pointStrokeColors?:字符串;
    ymax?:字符串|编号;
    ymin?:字符串|编号;
    平滑?:布尔值;
    姿势?:字符串;
    preUnits?:字符串;
    dateFormat?:(时间戳:数字)=>字符串;
    xLabels?:字符串;
    xLabelFormat?:(日期:date)=>字符串;
    xLabelAngle?:编号;
    yLabelFormat?:(标签:字符串|编号)=>字符串;
    目标?:数组;
    goalStrokeWidth?:编号;
    目标颜色?:字符串;
    事件?:数组;
    eventStrokeWidth?:编号;
    eventLineColors?:数组;
    连续线?:布尔;
    }
    
  • 应安装Raphael.js(npm install Raphael),然后在脚本中将其添加到angular.json中。例如:

    “脚本”:[“/node\u modules/raphael/raphael.min.js”]

  • Install morris npm Install morris.js06(该fork不需要jQuery,这对于Angular4+web应用程序更好)

  • 在src/app/directives/morris chart中创建指令morris chart.ts和接口morris chart.interface.ts(以下提供了源代码)

  • 在src/app/app.module.ts中,添加
    从“/directions/morris-chart/morris-chart”导入{MorrisChartDirective}
    声明[]中的
    指令

  • 在src/app/app.component.ts中,添加
    import'morris.js06/morris.js'

    公共数据; 公共选择; 构造函数(){ ... } 恩戈尼尼特(){ this.datas=[ {xkey:'2018',值:20}, {xkey:'2019',值:10} ] 此选项={ xkey:“xkey”, ykeys:['value'], 标签:['value'] }; }

  • 在src/app/app.component.html中,添加

  • 就这些^^

    受此启发,这里提供了一个指令和一个界面,可以轻松地将Morris图表与Angular4结合使用+

    莫里斯图表.ts

    import { Directive, AfterViewInit, OnInit, OnDestroy, Input, ElementRef, NgZone, OnChanges, SimpleChanges, Output, EventEmitter } from '@angular/core';
    
    import { ChartOptions, ChartDatas } from './morris-chart.interface';
    
    @Directive({
        selector: '[mk-morris-js]'
    })
    export class MorrisChartDirective implements OnInit, AfterViewInit, OnChanges, OnDestroy {
        private window: any = window;
        private _options: ChartOptions;
    
        public chartInstance: any;
    
        @Input() type = 'Line';
        @Input() options: ChartOptions;
        @Input() data: ChartDatas;
        @Output() clickChart = new EventEmitter();
    
        /*
        * [constructor description]
        * @method constructor
        * @param  {ElementRef} private elementRef [description]
        * @param  {NgZone}     private ngZone     [description]
        */
        constructor(
            private elementRef: ElementRef,
            private ngZone: NgZone
        ) {}
    
        /*
        * [ngOnInit description]
        * @method ngOnInit
        */
        ngOnInit() {
            this._options = Object.assign({}, this.options);
            this._options.element = this.elementRef.nativeElement;
            this._options.data = this.data;
        }
    
        /*
        * [ngAfterViewInit description]
        * @method ngAfterViewInit
        */
        ngAfterViewInit() {
            if(!this.window.Morris) {
                throw new Error('Please include node_modules/morris.js/morris.js');
            } else {
                this.ngZone.runOutsideAngular(() => {
                    this.chartInstance = new this.window.Morris[this.type](this._options);
                    let my_this = this;
                    this.chartInstance.on('click', function(i, row) { 
                        my_this.clickChart.emit({ event, i, row });
                    });
                });
            }
        }
    
        /*
        * [ngOnChanges description]
        * @method ngOnChanges
        * @param  {SimpleChanges} changes [description]
        */
        ngOnChanges(changes: SimpleChanges) {
            if((changes.data && !changes.data.firstChange) || (changes.options && !changes.options.firstChange)) {
                Object.assign(this.chartInstance.options, this.options);
                this.chartInstance.setData(this.data);
            }
        }
    
        /*
        * [ngOnDestroy description]
        * @method ngOnDestroy
        */
        ngOnDestroy() {
            if (this.chartInstance.el.empty instanceof Function) {
                this.chartInstance.el.empty();
            }
        }
    }
    
    export interface ChartData {
      label: string;
      value: number;
    }
    
    export interface ChartDatas {
        [key: string]: ChartData;
    }
    
    export interface ChartOptions {
      element: Element;
      data: ChartDatas;
      resize?: boolean;
    }
    
    
    export interface ChartDonutOptions extends ChartOptions {
      colors?: Array<string>;
      formater?: (y, data) => string;
      resize?: boolean;
    }
    
    export interface ChartAreaBarLineOptions {
      xkey: string,
      ykeys: Array<string>;
      labels: Array<string>;
      hideHover?: boolean|string;
      hoverCallback?: (index, options, content, row) => void;
      axes?: boolean;
      grid?: boolean;
      gridTextColor?: string;
      gridTextSize?: number;
      gridTextFamily?: string;
      gridTextWeight?: string;
      fillOpacity?: number;
    }
    
    export interface ChartAreaOptions extends ChartAreaBarLineOptions {
      behaveLikeLine?: boolean;
    }
    
    export interface ChartBarOptions extends ChartAreaBarLineOptions {
      barColors?: Array<string>;
      stacked?: boolean;
    }
    
    export interface ChartLineOptions extends ChartAreaBarLineOptions {
      lineColors?: Array<string>;
      lineWidth?: number;
      pointSize?: number;
      pointFillColors?: string;
      pointStrokeColors?: string;
      ymax?: string|number;
      ymin?: string|number;
      smooth?: boolean;
      postUnits?: string;
      preUnits?: string;
      dateFormat?: (timestamp: number) => string;
      xLabels?: string;
      xLabelFormat?: (date: Date) => string;
      xLabelAngle?: number;
      yLabelFormat?: (label: string|number) => string;
      goals?: Array<number>;
      goalStrokeWidth?: number;
      goalLineColors?: string;
      events?: Array<number>;
      eventStrokeWidth?: number;
      eventLineColors?: Array<string>;
      continuousLine?: boolean;
    }
    
    morris chart.interface.ts

    import { Directive, AfterViewInit, OnInit, OnDestroy, Input, ElementRef, NgZone, OnChanges, SimpleChanges, Output, EventEmitter } from '@angular/core';
    
    import { ChartOptions, ChartDatas } from './morris-chart.interface';
    
    @Directive({
        selector: '[mk-morris-js]'
    })
    export class MorrisChartDirective implements OnInit, AfterViewInit, OnChanges, OnDestroy {
        private window: any = window;
        private _options: ChartOptions;
    
        public chartInstance: any;
    
        @Input() type = 'Line';
        @Input() options: ChartOptions;
        @Input() data: ChartDatas;
        @Output() clickChart = new EventEmitter();
    
        /*
        * [constructor description]
        * @method constructor
        * @param  {ElementRef} private elementRef [description]
        * @param  {NgZone}     private ngZone     [description]
        */
        constructor(
            private elementRef: ElementRef,
            private ngZone: NgZone
        ) {}
    
        /*
        * [ngOnInit description]
        * @method ngOnInit
        */
        ngOnInit() {
            this._options = Object.assign({}, this.options);
            this._options.element = this.elementRef.nativeElement;
            this._options.data = this.data;
        }
    
        /*
        * [ngAfterViewInit description]
        * @method ngAfterViewInit
        */
        ngAfterViewInit() {
            if(!this.window.Morris) {
                throw new Error('Please include node_modules/morris.js/morris.js');
            } else {
                this.ngZone.runOutsideAngular(() => {
                    this.chartInstance = new this.window.Morris[this.type](this._options);
                    let my_this = this;
                    this.chartInstance.on('click', function(i, row) { 
                        my_this.clickChart.emit({ event, i, row });
                    });
                });
            }
        }
    
        /*
        * [ngOnChanges description]
        * @method ngOnChanges
        * @param  {SimpleChanges} changes [description]
        */
        ngOnChanges(changes: SimpleChanges) {
            if((changes.data && !changes.data.firstChange) || (changes.options && !changes.options.firstChange)) {
                Object.assign(this.chartInstance.options, this.options);
                this.chartInstance.setData(this.data);
            }
        }
    
        /*
        * [ngOnDestroy description]
        * @method ngOnDestroy
        */
        ngOnDestroy() {
            if (this.chartInstance.el.empty instanceof Function) {
                this.chartInstance.el.empty();
            }
        }
    }
    
    export interface ChartData {
      label: string;
      value: number;
    }
    
    export interface ChartDatas {
        [key: string]: ChartData;
    }
    
    export interface ChartOptions {
      element: Element;
      data: ChartDatas;
      resize?: boolean;
    }
    
    
    export interface ChartDonutOptions extends ChartOptions {
      colors?: Array<string>;
      formater?: (y, data) => string;
      resize?: boolean;
    }
    
    export interface ChartAreaBarLineOptions {
      xkey: string,
      ykeys: Array<string>;
      labels: Array<string>;
      hideHover?: boolean|string;
      hoverCallback?: (index, options, content, row) => void;
      axes?: boolean;
      grid?: boolean;
      gridTextColor?: string;
      gridTextSize?: number;
      gridTextFamily?: string;
      gridTextWeight?: string;
      fillOpacity?: number;
    }
    
    export interface ChartAreaOptions extends ChartAreaBarLineOptions {
      behaveLikeLine?: boolean;
    }
    
    export interface ChartBarOptions extends ChartAreaBarLineOptions {
      barColors?: Array<string>;
      stacked?: boolean;
    }
    
    export interface ChartLineOptions extends ChartAreaBarLineOptions {
      lineColors?: Array<string>;
      lineWidth?: number;
      pointSize?: number;
      pointFillColors?: string;
      pointStrokeColors?: string;
      ymax?: string|number;
      ymin?: string|number;
      smooth?: boolean;
      postUnits?: string;
      preUnits?: string;
      dateFormat?: (timestamp: number) => string;
      xLabels?: string;
      xLabelFormat?: (date: Date) => string;
      xLabelAngle?: number;
      yLabelFormat?: (label: string|number) => string;
      goals?: Array<number>;
      goalStrokeWidth?: number;
      goalLineColors?: string;
      events?: Array<number>;
      eventStrokeWidth?: number;
      eventLineColors?: Array<string>;
      continuousLine?: boolean;
    }
    
    导出接口图表数据{
    标签:字符串;
    值:数字;
    }
    导出接口图表数据{
    [键:字符串]:图表数据;
    }
    导出接口图表选项{
    元素:元素;
    数据:图表数据;
    调整大小?:布尔值;
    }
    导出接口图表自动选项扩展图表选项{
    颜色?:数组;
    格式化程序?:(y,数据)=>字符串;
    调整大小?:布尔值;
    }
    导出接口图表AreaBarLineOptions{
    xkey:string,
    ykeys:数组;
    标签:数组;
    隐藏?:布尔|字符串;
    hoverCallback?:(索引、选项、内容、行)=>void;
    轴?:布尔值;
    网格?:布尔;
    gridTextColor?:字符串;
    gridTextSize?:数字;
    gridTextFamily?:字符串;
    gridTextWeight?:字符串;
    fillOpacity?:数字;
    }
    导出界面ChartAreaOptions扩展ChartAreaBarLineOptions{
    behaveLikeLine?:布尔值;
    }
    导出接口ChartBarOptions扩展ChartAreaBarLineOptions{
    barColors?:数组;
    堆叠?:布尔值;
    }
    导出界面ChartLineOptions扩展ChartAreaBarLineOptions{
    lineColors?:数组;
    线宽?:数字;
    点大小?:数字;
    PointFillColor?:字符串;
    pointStrokeColors?:字符串;
    ymax?:字符串|编号;
    ymin?:字符串|编号;
    平滑?:布尔值;
    姿势?:stri