类名 common/document/support/LabelClass.js
import { Zondy } from '../../base'
import { defaultValue } from '../../util'
import { MapGISTextSymbol } from '../../base/symbol'

/**
 * 注记样式,参考示例:<a href='#new-LabelClass'>[创建注记样式对象]</a>
 * @classdesc 注记样式(目前仅支持三维SceneView上要素图层、GeoJSON、wfs等图层动态注记)
 * @class LabelClass
 * @param {Object} options 构造参数
 * @param {MapGISTextSymbol|TextSymbol} [options.symbol] 文本符号样式
 * @param {String} [options.renderMode] 注记渲染方式,1.canvas 2.label 3.ground(测试)
 * @param {Number} [options.groundScale = 20] 渲染模式为ground时,设置像素与实际单位米之间的比例关系。测试接口。假设groundScale为10,20*20px的逻辑范围贴地时会被渲染为200*200m的注记。
 * @param {String} [options.deconflictionStrategy] 注记避让策略,可选1.'static'默认避让策略 2.'none' 无避让策略
 * @param {Number} [options.maxScale] 最大可见范围,单位为米,不支持渲染类型renderMode为ground的模式。
 * @param {Number} [options.minScale] 最小可见范围,单位为米,不支持渲染类型renderMode为ground的模式。
 * @param {Number} [options.labelHeight = 0] 注记高度,单位为米。不支持渲染类型renderMode为ground的模式,此接口属于待废弃接口,后续可能会改动。
 * @param {String} [options.labelPlacement] 布局位置,描述注记和几何之间的关系,此设置会覆盖符号symbol内关于文字布局样式的定义。针对于点类型可选项1.above-left 2.above-center 3.above-right 4.center-left 5.center-center 6.center-right 7.below-left 8.below-center 9.below-right 针对于线类型 1.'on-line' 压线 2.'above-line' 线上 3.'under-line' 线下 针对于区类型1.'parallel' 平行 2.'outside' 区外 3.'bottom' 底部 4.'skeleton' 骨架线
 * @param {String} [options.currentAttributeName] 当前属性字段,属于临时接口,后续可能会扩充字段表达式,拟废弃,推荐使用labelExpression接口
 * @param {String} [options.labelExpression] label字段表达式
 * @param {Boolean} [options.repeatLabel=true] 是否重复注记,限制线几何类型使用
 * @param {Number} [options.repeatLabelDistance =6000] 重复步长,限制线几何类型使用,单位为米
 * @param {Number} [options.repeatStartRate = 0.3] 开始步长比例,限制线几何、区几何类型使用,范围为0~1。0表示步长分段的开始,1表示步长分段的结尾。
 * @param {Number} [options.repeatEndRate = 0.7] 结束步长比例,限制线几何、区几何类型使用,范围为0~1。0表示步长分段的开始,1表示步长分段的结尾。
 * @param {Number} [options.lineLabelSpread = 'auto-spread'] 线注记分布方式  1.'auto-spread' 字母集中,汉字分散 2.'centralization-spread'集中 3.'decentralize-spread' 分散。限制线几何使用。
 * @param {Number} [options.labelPositionAlong = 'middle'] 线文字与线关系,仅集中注记时生效。1.'start' 线头 2.'middle' 线中点 3.'end' 线尾,限制线使用。

 * @param {Number} [options.skeletonLineStartRate=0.4] 骨架线开始步长比例,限制区几何类型使用,范围为0~1。0表示步长分段的开始,1表示步长分段的结尾。
 * @param {Number} [options.skeletonLineEndRate =0.6] 骨架线结束步长比例,限制区几何类型使用,范围为0~1。0表示步长分段的开始,1表示步长分段的结尾。
 *
 * @summary <h5>支持如下方法:</h5>
 * <a href='#toJSON'>[1、转换为json对象]</a><br/>
 * <a href='#clone'>[2、克隆并返回一个新的LabelClass对象]</a><br/>
 * <a href='#fromJSON'>[3、将JSON里的数据导入,并返回一个新的LabelClass对象]</a><br/>
 *
 * @example <caption><h7 id='new-LabelClass'>初始化注记样式</h7></caption>
 * const labelClass = new Zondy.LabelClass({
 *   // 指定文本符号样式
 *   symbol: new Zondy.Symbol.TextSymbol({
 *     // 文字颜色
 *     color: new Color(252, 100, 22, 1),
 *     // 文字样式
 *     font: new Zondy.Font({
 *       // 字体
 *       family: "微软雅黑",
 *       // 文字大小,单位像素
 *       size: 30,
 *       // 文字是否为斜体,正常模式
 *       style: "normal",
 *       // 文字粗细
 *       weight: "normal"
 *     })
 *   })
 * })
 */
class LabelClass {
  constructor(options) {
    options = defaultValue(options, {})
    /**
     * 注记符号样式
     * @member {MapGISTextSymbol|TextSymbol} LabelClass.prototype.symbol
     */
    this.symbol = new MapGISTextSymbol(options.symbol)
    /**
     * 注记渲染方式,1.canvas 2.label 3.ground(测试)
     * @member {String} LabelClass.prototype.renderMode
     */
    this.renderMode = defaultValue(options.renderMode, 'canvas')
    /**
     * 避让策略,可选1.'static'默认避让策略 2.'none' 无避让策略
     * @member {String} LabelClass.prototype.deconflictionStrategy
     */
    this.deconflictionStrategy = defaultValue(
      options.deconflictionStrategy,
      'static'
    )
    /**
     * 最大可见范围,单位为米
     * @member {Number} LabelClass.prototype.maxScale
     */
    this.maxScale = options.maxScale
    /**
     * 最小可见范围,单位为米
     * @member {Number} LabelClass.prototype.minScale
     */
    this.minScale = options.minScale
    /**
     * 注记高度,单位为米
     * @member {Number} LabelClass.prototype.labelHeight
     */
    this.labelHeight = defaultValue(options.labelHeight, 0)
    /**
     * 布局位置,描述注记和几何之间的关系。针对于点类型可选项1.above-left 2.above-center 3.above-right 4.center-left 5.center-center 6.center-right 7.below-left 8.below-center 9.below-right 针对于线类型 1.'on-line' 压线 2.'above-line' 线上 3.'under-line' 线下 针对于区类型1.'parallel' 平行 2.'outside' 区外 3.'bottom' 底部 4.'skeleton' 骨架线
     * @member {String} LabelClass.prototype.labelPlacement
     */
    this.labelPlacement = options.labelPlacement
    /**
     * 当前属性字段,属于临时接口,后续可能会扩充字段表达式,后续拟废弃接口
     * @member {String} LabelClass.prototype.currentAttributeName
     */
    this.currentAttributeName = options.currentAttributeName
    /**
     * label字段表达式
     * @member {String} LabelClass.prototype.labelExpression
     */
    this.labelExpression = options.labelExpression
    /**
     * 是否重复注记,限制线几何类型使用
     * @member {Boolean} LabelClass.prototype.repeatLabel
     */
    this.repeatLabel = defaultValue(options.repeatLabel, true)
    /**
     * 重复步长,限制线几何类型使用,单位为米
     * @member {Number} LabelClass.prototype.repeatLabelDistance
     */
    this.repeatLabelDistance = defaultValue(options.repeatLabelDistance, 6000)
    /**
     * 开始步长比例,限制线几何、区几何类型使用,范围为0~1。0表示步长分段的开始,1表示步长分段的结尾。
     * @member {Number} LabelClass.prototype.repeatStartRate
     */
    this.repeatStartRate = defaultValue(options.repeatStartRate, 0.3)
    /**
     * 结束步长比例,限制线几何、区几何类型使用,范围为0~1。0表示步长分段的开始,1表示步长分段的结尾。
     * @member {Number} LabelClass.prototype.repeatEndRate
     */
    this.repeatEndRate = defaultValue(options.repeatEndRate, 0.7)
    /**
     * 线注记分布方式  1.'auto-spread' 字母集中,汉字分散 2.'centralization-spread'集中 3.'decentralize-spread' 分散。限制线几何使用。
     * @member {String} LabelClass.prototype.lineLabelSpread
     */
    this.lineLabelSpread = defaultValue(options.lineLabelSpread, 'auto-spread')
    /**
     * 线文字与线关系,仅集中注记时生效。1.'start' 线头 2.'middle' 线中点 3.'end' 线尾,限制线使用。
     * @member {String} LabelClass.prototype.labelPositionAlong
     */
    this.labelPositionAlong = defaultValue(options.labelPositionAlong, 'middle')
    /**
     * 骨架线开始步长比例,限制区几何类型使用,范围为0~1。0表示步长分段的开始,1表示步长分段的结尾。
     * @member {String} LabelClass.prototype.skeletonLineStartRate
     */
    this.skeletonLineStartRate = defaultValue(
      options.skeletonLineStartRate,
      0.4
    )
    /**
     * 骨架线结束步长比例,限制区几何类型使用,范围为0~1。0表示步长分段的开始,1表示步长分段的结尾。
     * @member {String} LabelClass.prototype.skeletonLineEndRate
     */
    this.skeletonLineEndRate = defaultValue(options.skeletonLineEndRate, 0.6)
    /**
     * 渲染模式为ground时,设置像素与实际单位米之间的比例关系。测试接口。假设groundScale为10,20*20px的逻辑范围贴地时会被渲染为200*200m的注记。
     * @member {Number} LabelClass.prototype.groundScale
     */
    this.groundScale = defaultValue(options.groundScale, 20)
  }

  /**
   * 转换为json对象<a id='toJSON'></a>
   * @return {Object} json对象
   */
  toJSON() {
    return {
      symbol: this.symbol.toJSON(),
      renderMode: this.renderMode,
      deconflictionStrategy: this.deconflictionStrategy,
      maxScale: this.maxScale,
      minScale: this.minScale,
      labelHeight: this.labelHeight,
      labelPlacement: this.labelPlacement,
      currentAttributeName: this.currentAttributeName,
      labelExpression: this.labelExpression,
      repeatLabel: this.repeatLabel,
      repeatLabelDistance: this.repeatLabelDistance,
      repeatStartRate: this.repeatStartRate,
      repeatEndRate: this.repeatEndRate,
      lineLabelSpread: this.lineLabelSpread,
      labelPositionAlong: this.labelPositionAlong,
      skeletonLineStartRate: this.skeletonLineStartRate,
      skeletonLineEndRate: this.skeletonLineEndRate,
      groundScale: this.groundScale
    }
  }

  /**
   * 克隆并返回一个新的LabelClass对象<a id='clone'></a>
   * @return {LabelClass} 新的LabelClass对象
   */
  clone() {
    return new LabelClass(this.toJSON())
  }

  /**
   * 将JSON里的数据导入,并返回一个新的LabelClass对象<a id='fromJSON'></a>
   * @param {Object} [json] 新的LabelClass对象
   * @return {LabelClass} 新的LabelClass对象
   */
  static fromJSON(json) {
    json = defaultValue(json, {})
    return new LabelClass(json)
  }
}

Zondy.LabelClass = LabelClass
export default LabelClass
构造函数
成员变量
方法
事件