【问题标题】:Highcharts with angular2 and SystemJS Setup带有 angular2 和 SystemJS 设置的 Highcharts
【发布时间】:2016-05-03 18:29:14
【问题描述】:

很难让 ng2-highcharts 和 angular2 很好地协同工作。

我所拥有的是;

import * as Highcharts from 'highcharts';
window['Highcharts'] = Highcharts;  
bootstrap(AppComponent);

SystemJS 配置;

  map: {
    "highcharts": "node_modules/highcharts/highcharts.js",
    "ng2-highcharts": "node_modules/ng2-highcharts",

  }

正如你所看到的,这是一个相当大的技巧,但它是我让它工作的唯一方法 - 当我删除手动窗口分配时,我得到了

ReferenceError: Highcharts is not defined
    at Ng2Highcharts.Object.defineProperty.set

所以我的问题是,肯定有更好的方法吗?有什么想法吗?

我就是这样使用它的;

import { Component, OnInit } from 'angular2/core';
import { Ng2Highcharts } from 'ng2-highcharts/ng2-highcharts';

@Component({
    selector: 'component',
    styleUrls: ['.comp.css'],
    templateUrl: '.comp.html',
    directives: [Ng2Highcharts]
})

谢谢

【问题讨论】:

    标签: highcharts angular systemjs jspm highcharts-ng


    【解决方案1】:

    我稍微修改了几个月前检索到的 ng2-highcharts 的原始实现以克服一些问题(我不得不使用 jQuery - 可能是该软件包的最新版本不再需要任何 twick)。无论如何,这是我使用的指令的代码

    /// <reference path="../../typings/highcharts/highcharts.d.ts" />
    
    declare var jQuery: any;
    
    import {Directive, ElementRef, Input} from 'angular2/core';
    
    @Directive({
        selector: '[ng2-highcharts]'
    })
    export class Ng2Highcharts {
        hostElement: ElementRef;
        chart: HighchartsChartObject;
    
        constructor(ele: ElementRef) {
            this.hostElement = ele;
        }
    
        @Input('ng2-highcharts') set options(opt:HighchartsOptions) {
            if(!opt) {
                console.log('No valid options...');
                console.log(opt);
                return;
            }
            if(opt.series || opt.data) {
                let nativeEl = this.hostElement.nativeElement;
                let jQ = jQuery(nativeEl);
                this.chart = jQ.highcharts(opt);
            } else {
                console.log('No valid options...');
                console.dir(opt);
            }
        }
    }
    

    index.html我有

    System.config({
            packages: {        
              app: {
                format: 'register',
                defaultExtension: 'js'
              },
              ng2Highcharts: {
                format: 'register',
                defaultExtension: 'js'
              },
    .....
    .....
    }})
    

    <script src="./lib/jquery/dist/jquery.js"></script>
    <script src="./lib/highcharts/highstock.js"></script>
    <script src="./lib/highcharts/modules/exporting.js"></script>
    <script src="./lib/highcharts/highcharts-more.js"></script>
    

    在需要使用该指令的组件中,html代码如下所示:

    <div [ng2-highcharts]="chartOptions"></div>
    

    chartOptions 是用这样的代码创建的

    createNewchartOptions() {
            return {
                title: {text: "Performance vs Benchmark (" + this.periodText + ")"},
                rangeSelector: {
                    selected: 4},
                xAxis: {
                    type: 'datetime'
                },
                yAxis: {
                    labels: {
                        formatter: function () {
                            return (this.value > 0 ? ' + ' : '') + this.value + '%';}
                    },
                    plotLines: [{
                        value: 0,
                        width: 2,
                        color: 'silver'}]
                },
                plotOptions: {
                    series: {
                        compare: 'percent'}
                },
                tooltip: {
                    pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
                    valueDecimals: 2}
            };
        }
    

    您需要做的最后一件事是在chartOptions 中设置series(我没有放代码,因为太链接到我的应用程序)。

    希望对你有帮助

    【讨论】:

    • 谢谢,虽然我确信这会正常工作,但仍然感觉像一个黑客攻击!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多