首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取Highmaps缩放级别,是否有Highmaps缩放事件?

如何获取Highmaps缩放级别,是否有Highmaps缩放事件?
EN

Stack Overflow用户
提问于 2019-04-24 23:03:29
回答 1查看 581关注 0票数 0

是否有Highmaps缩放事件?

另外,如何获取地图的当前缩放级别?

设置缩放级别很容易,但我需要得到它。

我查看了API文档,但似乎没有缩放事件或getZoomLevel函数。

(这是网站允许的知识分享,我将回答我自己的问题。StackOverflow建议的javascript标签)

https://api.highcharts.com/highmaps/chart.events

显然,如果有人有相关信息,请自己回答:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-24 23:03:29

这是我的代码。它是用Angular编写的,但它没有使用任何Angular包装器,它直接使用Highcharts /Highmap v7.1.1,所以如果需要的话,它应该很容易转换成普通的。

代码的相关部分是loadredraw Highmap事件,它们用于计算缩放级别。它们也用来代替缩放事件(因为没有缩放事件)。lodash去抖动用于防止setZoomLevel函数被过频繁地调用。

代码语言:javascript
复制
import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'
import { getRandomNumber } from '@utils/number-utils'
import { debounce, merge } from 'lodash'

declare const Highcharts: any

@Component({
  selector: 'app-highmaps',
  templateUrl: './highmaps.component.html',
  styleUrls: ['./highmaps.component.scss']
})

export class HighmapsComponent implements OnInit, OnDestroy {

  @Input() height: number
  @Input() width: number
  @Input() options: any

  @Output() highmapsLoad = new EventEmitter<any>()
  @Output() highmapsRedraw = new EventEmitter<any>()
  @Output() highmapsMouseOver = new EventEmitter<any>()
  @Output() highmapsMouseOut = new EventEmitter<any>()
  @Output() highmapsZoom = new EventEmitter<any>()

  ext0: any
  highmapsContainerId: string
  highmapsLoadInterval: any
  map: any
  zoomLevel: number

  constructor() {
    this.onHighmapsLoad = this.onHighmapsLoad.bind(this)
    this.onHighmapsRedraw = this.onHighmapsRedraw.bind(this)
    this.onHighmapsMouseOver = this.onHighmapsMouseOver.bind(this)
    this.onHighmapsMouseOut = this.onHighmapsMouseOut.bind(this)
    this.onHighmapsZoom = debounce(this.onHighmapsZoom.bind(this), 100, {leading: false, trailing: true})
  }

  ngOnInit() {
    this.highmapsContainerId = 'highmaps-container' + getRandomNumber()
    this.options = merge({}, this.options, {
      chart: {
        events: {
          load: this.onHighmapsLoad,
          redraw: this.onHighmapsRedraw
        }
      },
      credits: {
        enabled: false
      },
      plotOptions: {
        series: {
          point: {
            events: {
              mouseOver: this.onHighmapsMouseOver,
              mouseOut: this.onHighmapsMouseOut
            }
          }
        }
      }
    })

    setTimeout(() => {
      this.map = Highcharts.mapChart(this.highmapsContainerId, this.options)
    })
  }

  ngOnDestroy() {
    clearInterval(this.highmapsLoadInterval)
  }

  private onHighmapsLoad(evt) {
    this.highmapsLoadInterval = setInterval(() => {
      if (this.map && this.map.axes) {
        this.highmapsLoad.emit(this.getEmitObj(evt))
        this.ext0 = this.map.axes[0].getExtremes()
        clearInterval(this.highmapsLoadInterval)
      }
    }, 50)
  }

  private onHighmapsRedraw(evt) {
    this.highmapsRedraw.emit(this.getEmitObj(evt))
    this.onHighmapsZoom(evt)
  }

  private onHighmapsMouseOver(evt) {
    this.highmapsMouseOver.emit(this.getEmitObj(evt))
  }

  private onHighmapsMouseOut(evt) {
    this.highmapsMouseOut.emit(this.getEmitObj(evt))
  }

  private onHighmapsZoom(evt) {
    const ext1 = this.map.axes[0].getExtremes()
    const zoom = (ext1.max - ext1.min) / (this.ext0.max - this.ext0.min)
    const temp = Number((zoom).toFixed(3))
    if (this.zoomLevel !== temp) {
      const emitObj = this.getEmitObj(evt)
      this.zoomLevel = temp
      emitObj.zoomLevel = this.zoomLevel
      this.highmapsZoom.emit(emitObj)
    }
  }

  private getEmitObj(evt): any {
    return {evt, map: this.map}
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55833188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档