Различные Highcharts в одном и том же компоненте (Angular 5) - PullRequest
0 голосов
/ 31 октября 2018

Я пытаюсь отобразить несколько старших графиков в одном и том же компоненте, используя Angular 5. Данные для обоих старших графиков будут разными. Как новичок в angular, кто-нибудь может дать представление о том, как приступить к добавлению еще одного Highchart здесь. Ниже приведена машинопись и файл HTML. Спасибо

app.component.ts

import { Component, ElementRef, ViewChild } from '@angular/core';
import { chart } from 'highcharts';
import * as Highcharts from 'highcharts';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Highcharts + Angular 5';

  @ViewChild('chartTarget') chartTarget: ElementRef;

  chart: Highcharts.ChartObject;

  ngAfterViewInit() {
    const options: Highcharts.Options = {
      chart: {
    type: 'column'
},
title: {
    text: 'Stacked column chart'
},
xAxis: {
    categories: ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
    min: 0,
    title: {
        text: 'Bitbucket Consumption in TBs'
    },
    stackLabels: {
        enabled: true,
        style: {
            fontWeight: 'bold',
        }
    }
},
legend: {
    align: 'right',
    x: -30,
    verticalAlign: 'top',
    y: 25,
    floating: true,
    borderColor: '#CCC',
    borderWidth: 1,
    shadow: false
},
tooltip: {
    headerFormat: '<b>{point.x}</b><br/>',
    pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
},
plotOptions: {
    column: {
        stacking: 'normal',
        dataLabels: {
            enabled: true,
        }
    }
},
series: [{
    name: 'CTC',
    data: [5, 3, 4, 7, 2]
}, {
    name: 'ASW',
    data: [2, 2, 3, 2, 1]
}, {
    name: 'MQS',
    data: [3, 4, 4, 2, 5]
}]
};


this.chart = chart(this.chartTarget.nativeElement, options);
  }
}

app.component.html

<div style="text-align:center">
  <h1>
    Welcome to {{ title }}!
  </h1>
</div>

<div>
  <button (click)="addSeries()">Add Series</button>
</div>
<div #chartTarget>
  chart target
</div>

Как мне добавить еще один div здесь, чтобы здесь можно было использовать другую Highchart?

EDIT: Здесь я попытался включить предложение Эндрю, но по какой-то причине в представлении все еще нет другой видимой диаграммы. https://angular -5h9k9m.stackblitz.io

1 Ответ

0 голосов
/ 31 октября 2018

Вы можете просто добавить новый div, ссылаться на него и т. Д.

app.component.html

<div style="text-align:center">
  <h1>
    Welcome to {{ title }}!
  </h1>
</div>

<div>
  <button (click)="addSeries()">Add Series</button>
</div>
<div #chartTarget>
  chart target
</div>
<div #chartTarget2>
  chart target
</div>

app.component.ts

import { Component, ElementRef, ViewChild, AfterViewInit } from '@angular/core';
import { chart } from 'highcharts';
import * as Highcharts from 'highcharts';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
  title = 'Highcharts + Angular 5';

  @ViewChild('chartTarget') chartTarget: ElementRef;
  @ViewChild('chartTarget2') chartTarget2: ElementRef;

  chart: Highcharts.ChartObject;
  chart2: Highcharts.ChartObject;

  ngAfterViewInit() {
    const options: Highcharts.Options = {
      chart: {
        type: 'column'
      },
      title: {
        text: 'Stacked column chart'
      },
      xAxis: {
        categories: ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
      },
      yAxis: {
        min: 0,
        title: {
          text: 'Bitbucket Consumption in TBs'
        },
        stackLabels: {
          enabled: true,
          style: {
            fontWeight: 'bold',
          }
        }
      },
      legend: {
        align: 'right',
        x: -30,
        verticalAlign: 'top',
        y: 25,
        floating: true,
        borderColor: '#CCC',
        borderWidth: 1,
        shadow: false
      },
      tooltip: {
        headerFormat: '<b>{point.x}</b><br/>',
        pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
      },
      plotOptions: {
        column: {
          stacking: 'normal',
          dataLabels: {
            enabled: true,
          }
        }
      },
      series: [{
        name: 'CTC',
        data: [5, 3, 4, 7, 2]
      }, {
        name: 'ASW',
        data: [2, 2, 3, 2, 1]
      }, {
        name: 'MQS',
        data: [3, 4, 4, 2, 5]
      }]
    };


    this.chart = chart(this.chartTarget.nativeElement, options);
    this.chart2 = chart(this.chartTarget2.nativeElement, options); // maybe use different options
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...