1

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

夏通

    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;
    };
}

图二

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 轴?

编辑:示例

4

2 回答 2

4

我有一个最简单的 angular 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

于 2017-07-10T06:42:22.273 回答
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
    }
});

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

于 2021-05-20T07:54:39.543 回答