如何在angular2中同步多个(highcharts)图表

时间:2017-05-23 10:17:53

标签: angular highcharts angular2-highcharts

每个图表都有一个组件。如何同步这些图表的x轴?

Chartone

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


    @Component({
    selector: 'chartone',
    styles: [`
    :host{
      position: absolute;
      padding-top: 10px;
      padding-right: 10px;
    }
  `],
  template: `
   <chart [options]="options"
         (load)="saveInstance($event.context)">
  </chart>
     `
    })
       export class chartone {
  constructor() {

  this.options = {
            xAxis: {
            },
            yAxis:{
              plotLines: [{
                dashStyle: 'shortdot',
                color: 'green',
                value: '8', 
                width: '1.5',
                zIndex: 2 
              }]
            },

            credits: false,
            chart: {
              backgroundColor: '#e6f2ff',
              borderColor: '#00D490',
              borderWidth: 1,
              type: 'spline',
              width: 700,
              },
            legend: {
              enabled: false
              },
            title: { text : 'Overview'},
            series: [{ color: '#25D366', data: [2,3,5,8,13] },{ color: '#3b5998', data:[4,6,9,11,15]}]

          };

          setInterval(() => this.chart.series[0].addPoint(Math.random() * 10), 6000);
          setInterval(() => this.chart.series[1].addPoint(Math.random() * 10), 6000);

          chart   : Object;
          options : Object;

    }
    saveInstance(chartInstance) {
    this.chart = chartInstance;
    };
}

Charttwo

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


@Component({
  selector: 'charttwo',
  styles: [`
    :host{
      position: absolute;
      padding-top: 10px;
      top: 410px;
      left: 610px;

    }
  `],
  template: `
  <chart [options]="options"
         (load)="saveInstance($event.context)">
  </chart>

  `
})
export class charttwo {
  constructor() {
  this.options = {
            credits: false,
            legend:{
              enabled:false,
            },
            yAxis:{
              plotBands: [{
                color: 'rgba(0,170,0,0.3)',
                from: 4, 
                to: 6, 
              }],
            },
            chart: {
              height: 300,
              backgroundColor: '#e6f2ff',
              borderColor: '#00D490',
              borderWidth: 1,
              type: 'scatter'

          },
            title: { text : 'Average'},
            series: [{ color: '#25D366', data: [2,3,5,8,13] }, {color: '#3b5998',data:[4,6,9,11,15]}]
          };

          setInterval(() => this.chart.series[0].addPoint(Math.random() * 10), 6000);
          setInterval(() => this.chart.series[1].addPoint(Math.random() * 10), 6000);
          chart   : Object;
          options : Object;

    }
    saveInstance(chartInstance) {
    this.chart = chartInstance;
    };
}

最后将有超过2个图表进行同步。 如何同步这些图表的x轴?

编辑:Example

2 个答案:

答案 0 :(得分:3)

我有一个最简单的解决方案,角度2/2+

只需将图表数据合并到同一系列中,然后拆分yAxis即可。这对我有用。以下是我的代码。将您的数据放入系列:数据,它将起作用。

      this.chartOption = {
            chart: {
              type: 'line',           
              zoomType: 'x',
              height: 600,                  

            //Theme of chart
              backgroundColor: "#18252E",
              style: {
                  fontFamily: '\'Unica One\', sans-serif'
              },
              plotBorderColor: '#606063'
            },
            title: {
              text: "",
               style: {
                display: 'none',
                color: '#E0E0E3',
                textTransform: 'uppercase',
                fontSize: '14px'
              }               
            },
            tooltip: {
              shared: true,  
              xDateFormat: '%m/%d/%Y',  
              valueDecimals: 2,
              crosshairs: true,
              backgroundColor: 'rgba(0, 0, 0, 0.85)',
                    style: {
                        color: '#F0F0F0'
                    }
            },          
          xAxis: {
          crosshair: true,
          type: 'datetime',
          dateTimeLabelFormats: {
            year: '%Y'
          }
        },
         yAxis: [{
                title: {
                    text: 'Data1'
                },
                height: 200,
                lineWidth: 2
            }, {
                title: {
                    text: 'Data2'
                },
                top: 300,
                height: 200,
                offset: 0,
                lineWidth: 2
            }],
          legend: {
                align: 'center',
                verticalAlign: 'top',
                layout: 'vertical',
                x: 30,
                y: 0,

               itemStyle: {
                 color: '#ffffff',
                 fontSize: '14px'
               }
          },

          plotOptions: {
              series: {
                  pointStart: 2010,
                  color: '#2B908F'
              }
          },

          series: [{
              name: "Data1",
              data: this.yourData1,
              yAxis : 0,
          },
          {
              name: "Data2",
              data: this.yourData2,
              color: '#90ee7e',
              yAxis : 1
          }]
      };    

Plunker

答案 1 :(得分:1)

你也可以试试

this.chart.addAxis({
    top: this.calculateHeightToDeductAsPercentage(selectedIndicator),
    height: this.getHeightOfIndicator(selectedIndicator),
    id: 'indicator_' + name,
    title: {
        text: '',
    },
    opposite: false,
    offset: 0
});
this.chart.addSeries({
    yAxis: 'indicator_' + name,
    type: 'momentum',
    name: name,
    color: color,
    linkedTo: 'stockPrice',
    params: {
        period: indicatorPeriod
    },
    dataGrouping: {
        enabled: false
    }
});

我以这种方式添加了一个指标。希望这对你也有用。