1

我正在尝试实现以下示例,该示例 基本上是使用 JavaScript 中的 highcharts 库实现的自定义温度计。除了以下代码块外,一切正常。

function(chart) { // on complete chart.renderer.image('a',24, 0, 110, 510).add()}

它没有渲染温度计的背景图像,也没有在代码中引发任何错误。这是此图的 component.ts 文件。

import { TempHumidService } from './../chart-service/temp-humid.service';
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { ChartType } from '../chart';

@Component({
  selector: 'app-temp-humidity',
  templateUrl: './temp-humidity.component.html',
  styleUrls: ['./temp-humidity.component..scss']
})
export class TempHumidityComponent implements OnInit {
  chartType = ChartType;
  humidGraph: object;
  tempGraph: object;
  @Input() filterData: object;
  @Output() moneySaveClick: EventEmitter<any> = new EventEmitter<any>();

  constructor(
    private tempHumidService: TempHumidService
  ) { }

  ngOnInit() {
    this.humidGraph = this.tempHumidService.initHumidGraph();
    this.tempGraph = {
      chart: {
        type: 'column',
        marginBottom: 53
      },
      credits: {
        enabled: false
      },
      title: null,
      legend: {
        enabled: false
      },
      exporting: {
        enabled: false
      },
      yAxis: {
        min: 0,
        max: 100,
        title: null,
        align: 'right'
      },
      xAxis: {
        labels: false
      },
      series: [{
        data: [60],
        color: 'red'
      }],
      function (chart) { // on complete
      chart.renderer.image('http://www.clker.com/cliparts/p/W/e/k/U/d/blank-fundraising-thermometer.svg',24, 0, 110, 510)
        .add();
      },
    }
  }

  onClick(value) {
    this.moneySaveClick.emit(value);
  }

}

这是我的 module.ts 文件

import { Routes, RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DashboardComponent } from './dashboard/dashboard.component';
import { Ng2TableModule } from 'ng2-table/ng2-table';
import { LineGraphComponent } from './line-graph/line-graph.component';
import { ChartsComponent } from './charts/charts.component';
import { HttpClientModule } from '@angular/common/http';

// for using highCharts
import { ChartModule } from 'angular2-highcharts';
import * as highcharts from 'highcharts';
import { HighchartsStatic } from 'angular2-highcharts/dist/HighchartsService';

export function highchartsFactory() {
  const hc = require('highcharts/highstock');
  const hcm = require('highcharts/highcharts-more');
  const sg = require('highcharts/modules/solid-gauge');

  hcm(hc);
  sg(hc);
  return hc;
}

import { JsonpModule } from '@angular/http';

const ROUTES: Routes = [
  { path: '', component: DashboardComponent }
]
@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(ROUTES),
    PaginationModule.forRoot(),
    FormsModule,
    ReactiveFormsModule,
    Ng2TableModule,
    ChartModule,
    ChartModule,
    JsonpModule,
    HttpClientModule
  ],
  exports: [
    RouterModule
  ],
  providers: [
    MoneySavedService,
    TempHumidService,
    GraphApiService,
    {
      provide: HighchartsStatic,
      useFactory: highchartsFactory
    }
  ],
  declarations: [
    DashboardComponent,
    LineGraphComponent,
   ]
})
export class DashboardModule { }

有人可以帮我在使用 highcharts 和 angular4 渲染的图表中显示自定义图像吗?

4

1 回答 1

0

使用 angular2-highcharts 你必须更新图表事件

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule, Component }    from '@angular/core';
import { BrowserModule }          from '@angular/platform-browser';
import { ChartModule }            from 'angular2-highcharts'; 

@Component({
    selector: 'my-app',
    styles: [`
      chart {
        display: block;
      }
    `],
    template: `<chart [options]="options" (load)="onChartload($event)" style="width: 110px; height: 510px; margin: 0 auto"></chart>`
})
class AppComponent {
    constructor() { 
        this.options = {
            chart: {
                type: 'column',
                marginBottom: 72
            },
            credits: {
                enabled: false
            },
            title: '',
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false   
            },
            yAxis: {
                min: 0,
                max: 100,
                title: null,
                align: 'right'
            },
            xAxis: {
                labels: false
            },
            series: [{
                data: [54],
                color: '#c00'
             }]
        };
    }
    options: Object;
    onChartload (e) {
      e.context.renderer.image('http://www.clker.com/cliparts/p/W/e/k/U/d/blank-fundraising-thermometer.svg',24, 0, 110, 510).add();  

    }
}

@NgModule({
  imports:      [BrowserModule, ChartModule.forRoot(require('highcharts'))],
  declarations: [AppComponent],
  bootstrap:    [AppComponent]
})
class AppModule { }


platformBrowserDynamic().bootstrapModule(AppModule);

Plunker演示

于 2017-12-16T13:14:00.767 回答