legend.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * DevExtreme (viz/vector_map/legend.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var extend = require("../../core/utils/extend").extend;
  11. var each = require("../../core/utils/iterator").each;
  12. var _extend = extend;
  13. var _each = each;
  14. var legendModule = require("../components/legend");
  15. var _BaseLegend = legendModule.Legend;
  16. var unknownSource = {
  17. category: "UNKNOWN",
  18. name: "UNKNOWN"
  19. };
  20. function buildData(partition, values, field) {
  21. var i;
  22. var ii = values.length;
  23. var list = [];
  24. var item;
  25. for (i = 0; i < ii; ++i) {
  26. list[i] = item = {
  27. start: partition[i],
  28. end: partition[i + 1],
  29. index: i
  30. };
  31. item[field] = values[i];
  32. item.states = {
  33. normal: {
  34. fill: item.color
  35. }
  36. };
  37. item.visible = true
  38. }
  39. return list
  40. }
  41. var Legend = function(parameters) {
  42. var that = this;
  43. that._params = parameters;
  44. that._root = parameters.renderer.g().attr({
  45. "class": "dxm-legend"
  46. }).linkOn(parameters.container, {
  47. name: "legend",
  48. after: "legend-base"
  49. }).enableLinks().linkAppend();
  50. parameters.layoutControl.addItem(that);
  51. _BaseLegend.call(that, {
  52. renderer: parameters.renderer,
  53. group: that._root,
  54. backgroundClass: null,
  55. itemsGroupClass: null,
  56. textField: "text",
  57. getFormatObject: function(data) {
  58. return data
  59. }
  60. });
  61. that._onDataChanged = function(data) {
  62. that._updateData(data)
  63. }
  64. };
  65. Legend.prototype = _extend(require("../../core/utils/object").clone(_BaseLegend.prototype), {
  66. constructor: Legend,
  67. dispose: function() {
  68. var that = this;
  69. that._params.layoutControl.removeItem(that);
  70. that._unbindData();
  71. that._root.linkRemove().linkOff();
  72. that._params = that._root = that._onDataChanged = null;
  73. return _BaseLegend.prototype.dispose.apply(that, arguments)
  74. },
  75. resize: function(size) {
  76. this._params.notifyDirty();
  77. if (null === size) {
  78. this.erase()
  79. } else {
  80. this.draw(size.width, size.height)
  81. }
  82. this._params.notifyReady()
  83. },
  84. locate: _BaseLegend.prototype.shift,
  85. _updateData: function(data) {
  86. this._options.defaultColor = data && data.defaultColor;
  87. this.update(data ? buildData(data.partition, data.values, this._dataName) : [], this._options, this._params.themeManager.theme("legend").title);
  88. this.updateLayout()
  89. },
  90. _unbindData: function() {
  91. if (this._dataCategory) {
  92. this._params.dataExchanger.unbind(this._dataCategory, this._dataName, this._onDataChanged)
  93. }
  94. },
  95. _bindData: function(arg) {
  96. this._params.dataExchanger.bind(this._dataCategory = arg.category, this._dataName = arg.name, this._onDataChanged)
  97. },
  98. setOptions: function(options) {
  99. var that = this;
  100. that.update(that._data, options, this._params.themeManager.theme("legend").title);
  101. that._unbindData();
  102. var source = options.source;
  103. that._bindData(source ? {
  104. category: source.layer,
  105. name: source.grouping
  106. } : unknownSource);
  107. that.updateLayout();
  108. return that
  109. }
  110. });
  111. function LegendsControl(parameters) {
  112. this._params = parameters;
  113. this._items = [];
  114. parameters.container.virtualLink("legend-base")
  115. }
  116. LegendsControl.prototype = {
  117. constructor: LegendsControl,
  118. dispose: function() {
  119. _each(this._items, function(_, item) {
  120. item.dispose()
  121. });
  122. this._params = this._items = null
  123. },
  124. setOptions: function(options) {
  125. var optionList = options && options.length ? options : [];
  126. var items = this._items;
  127. var i;
  128. var ii = optionList.length;
  129. var params = this._params;
  130. var theme = params.themeManager.theme("legend");
  131. for (i = items.length; i < ii; ++i) {
  132. items[i] = new Legend(params)
  133. }
  134. for (i = items.length - 1; i >= ii; --i) {
  135. items[i].dispose();
  136. items.splice(i, 1)
  137. }
  138. params.layoutControl.suspend();
  139. for (i = 0; i < ii; ++i) {
  140. items[i].setOptions(_extend(true, {}, theme, optionList[i]))
  141. }
  142. params.layoutControl.resume()
  143. }
  144. };
  145. exports.LegendsControl = LegendsControl;