0

我需要将图表缩放到init事件的指定时间段。

      "listeners": [{
        "event": "zoomed",
        "method": this.calulateMetrics
      },{
        "event": "dataUpdated",
        "method": this.showPeriod
      }, {
        "event": "init",
        "method": this.showPeriod
      },{
        "event": "rendered",
        "method": this.showPeriod
      }],


  showPeriod(event) {
    // Retrieve the data
    let dates = this.$store.getters.getDates
    let startDate = dates[0]
    let endDate = (dates[dates.length - 1])
    endDate.setHours(22)
    try {
      this.chart.zoom(startDate, endDate)
      console.log("Last date", endDate)
    } catch(err) {
      console.log("Error",(err.message))
    }
  },

虽然我无法捕捉到任何错误,但期间保持不变。除了init我尝试过rendereddataUpdated但它也没有帮助我。

但是当我触发对外部事件的调用时,<v-btn v-on:click="showPeriod">Button</v-btn>它工作正常。

整个选项文件如下:

   this.chart = window.AmCharts.makeChart("chartdiv", {
      "path": AmCharts_path,
      "type": "stock",
      "theme": "light",

      "dataSets": this.$store.state.portfoliosData.performance.map(function (port, idx) {
        return {
          "title": port.name,
          "fieldMappings": [{
            "fromField": "value",
            "toField": "value"
          }],
          "dataProvider": port.data,
          "compared": (idx === 0 ? false : true),
          "categoryField": "date"
        }
      }),

      "panels": [{
        "title": "Value",
        "percentHeight": 70,
        "stockGraphs": [
          {
            "id": "g1",
            "valueField": "value",
            "comparable": true,
            "compareField": "value",
            "balloonFunction": this.ballonRender,
            "compareGraphBalloonFunction": this.ballonRender
          }]
      }],

      "chartScrollbarSettings": {
        "graph": "g1",
        "markPeriodChange": true
      },

      "categoryAxis": {
        "parseDates": true,
      },

      "categoryAxesSettings": {
        "groupToPeriods": ["DD"]
      },

      "balloon": {
          "fixedPosition": true,
          "maxWidth": 10000,
          "offsetY": 1,
      },

      "dataDateFormat": "YYYY-MM-DD",

      "chartCursorSettings": {
        "valueBalloonsEnabled": true,
        "categoryBalloonEnabled": true,
        "selectWithoutZooming": true,
        "showNextAvailable": true,
        "categoryBalloonAlpha": 0.8,
        "bulletsEnabled": true,
        "bulletSize": 10,
        "valueZoomable":true,
        "usePeriod": "DD",
        "categoryBalloonDateFormats": [
            {period:'fff',format:'JJ:NN:SS'},
            {period:'ss',format:'JJ:NN:SS'},
            {period:'mm',format:'JJ:NN'},
            {period:'hh',format:'JJ:NN'},
            {period:'DD',format:'EEEE, MMM DD'},
            {period:'WW',format:'EEEE, MMM DD'},
            {period:'MM',format:'MMM'},
            {period:'YYYY',format:'YYYY'}
          ]
      },

       "periodSelector": {
         "position": "bottom"
      },
    });
  },

复制链接可以在这里找到

4

1 回答 1

2

主要问题是 AmCharts 的侦听器回调不了解您的组件范围。this在回调中指的是 AmCharts 的回调范围,而不是您的组件。chart每个 AmCharts 侦听器都在事件的属性中提供对自身的引用(init例如,请参阅 ),因此将所有this.chart行更改为event.chart回调中的行将解决该部分问题。如果您需要对组件本身的引用,请在您的renderGraph()方法中向图表对象添加一个自定义属性,然后通过以下方式引用它event.chart

  renderGraph() {
    // ...
    this.chart = window.AmCharts.makeChart("chartdiv", {
      // ...
      componentRef: this,
      // ...
    })
  }

  //example using initPeriod:
  initPeriod(event) { //event parameter contains {type: "init", chart: stockChartReference}
    console.log("init period")
    let dates = event.chart.componentRef.$store.getters.getDates
    let startDate = dates[0]
    let endDate = (dates[dates.length - 1])
    endDate.setHours(22)
    try {
      event.chart.zoom(startDate, endDate)
      console.log("Last date", endDate)
    } catch(err) {
      console.log("Error",(err.message))
    }
  },

您的zoomed侦听器引用了一个未定义的方法,称为calulateMetrics. 我看到一个calculateMetrics在 amstore 中调用的方法,但我不确定这是否是您的意思,并且将其更改为该方法似乎对我不起作用,所以我在添加适当的方法时请您听从。添加缺少的方法以及确保您像其他回调一样正确引用图表对象将解决您的问题。

zoomed除缺少方法外的所有内容的演示: https ://codesandbox.io/s/5wpopzwlnp

于 2018-08-01T11:14:48.787 回答