_Feature.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.geo.charting._Feature"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.geo.charting._Feature"] = true;
  8. dojo.provide("dojox.geo.charting._Feature");
  9. dojo.require("dojox.gfx.fx");
  10. dojo.declare("dojox.geo.charting._Feature", null, {
  11. _isZoomIn: false,
  12. _isFocused: false,
  13. markerText:null,
  14. constructor: function(parent, name, shapeData){
  15. this.id = name;
  16. this.shape = parent.mapObj.createGroup();
  17. this.parent = parent;
  18. this.mapObj = parent.mapObj;
  19. this._bbox = shapeData.bbox;
  20. this._center = shapeData.center;
  21. //TODO: fill color would be defined by charting data and legend
  22. //this._defaultFill = ["#FFCE52", "#CE6342", "#63A584"][Math.floor(Math.random() * 3)];
  23. this._defaultFill = parent.defaultColor;
  24. this._highlightFill = parent.highlightColor;
  25. this._defaultStroke = {
  26. width: this._normalizeStrokeWeight(.5),
  27. color: "white"
  28. };
  29. this._scale = Math.min(this.parent.containerSize.w / this._bbox.w, this.parent.containerSize.h / this._bbox.h);
  30. var shapes = (dojo.isArray(shapeData.shape[0])) ? shapeData.shape : [shapeData.shape];
  31. dojo.forEach(shapes, function(points){
  32. this.shape.createPolyline(points).setStroke(this._defaultStroke).setFill(this._defaultFill);
  33. }, this);
  34. },
  35. setValue:function(value){
  36. this.value = value;
  37. if(this.parent.series.length != 0){
  38. for(var i = 0;i < this.parent.series.length;i++){
  39. var range = this.parent.series[i];
  40. if((value>=range.min)&&(value<range.max)){
  41. this._setFillWith(range.color);
  42. this._defaultFill = range.color;
  43. }
  44. }
  45. }
  46. },
  47. _setFillWith: function(color){
  48. var borders = (dojo.isArray(this.shape.children)) ? this.shape.children : [this.shape.children];
  49. dojo.forEach(borders, function(item){
  50. item.setFill(color);
  51. });
  52. },
  53. _setStrokeWith: function(stroke){
  54. var borders = (dojo.isArray(this.shape.children)) ? this.shape.children : [this.shape.children];
  55. dojo.forEach(borders, function(item){
  56. item.setStroke({
  57. color: stroke.color,
  58. width: stroke.width,
  59. join: "round"
  60. });
  61. });
  62. },
  63. _normalizeStrokeWeight: function(weight){
  64. var matrix = this.shape._getRealMatrix();
  65. return (dojox.gfx.renderer != "vml")?weight/(this.shape._getRealMatrix()||{xx:1}).xx:weight;
  66. },
  67. _onmouseoverHandler: function(evt){
  68. this.parent.onFeatureOver(this);
  69. this._setFillWith(this._highlightFill);
  70. this.mapObj.marker.show(this.id);
  71. },
  72. _onmouseoutHandler: function(){
  73. this._setFillWith(this._defaultFill);
  74. this.mapObj.marker.hide();
  75. dojo.style("mapZoomCursor", "display", "none");
  76. },
  77. _onmousemoveHandler: function(evt){
  78. if(this._isFocused){
  79. var evt = dojo.fixEvent(evt || window.event);
  80. dojo.style("mapZoomCursor", "left", evt.pageX + 12 + "px");
  81. dojo.style("mapZoomCursor", "top", evt.pageY + "px");
  82. dojo.byId("mapZoomCursor").className = (this._isZoomIn)?"mapZoomOut":"mapZoomIn";
  83. dojo.style("mapZoomCursor", "display", "block");
  84. }
  85. },
  86. _onclickHandler: function(){
  87. if(!this._isFocused){
  88. for (var name in this.mapObj.features){
  89. if (this.mapObj.features[name] != this){
  90. this.mapObj.features[name]._setStrokeWith(this._defaultStroke);
  91. this.mapObj.features[name]._setFillWith(this.mapObj.features[name]._defaultFill);
  92. this.mapObj.features[name]._isFocused = false;
  93. this.mapObj.features[name]._isZoomIn = false;
  94. }
  95. }
  96. this._focus();
  97. }
  98. else if (this._isZoomIn){
  99. this._zoomOut();
  100. }
  101. else {
  102. this._zoomIn();
  103. }
  104. },
  105. _focus: function(){
  106. this.shape._moveToFront();
  107. this._setStrokeWith({color:"black",width:this._normalizeStrokeWeight(2)});
  108. this.parent.onFeatureClick(this);
  109. this._isFocused = true;
  110. },
  111. _zoomIn: function(){
  112. var anim = dojox.gfx.fx.animateTransform({
  113. duration: 1000,
  114. shape: this.mapObj,
  115. transform: [{
  116. name: "translate",
  117. start: [-this.mapObj.currentBBox.x, -this.mapObj.currentBBox.y],
  118. end: [-this._bbox.x, -this._bbox.y]
  119. },{
  120. name: "scaleAt",
  121. start: [this.mapObj.currentScale, this.mapObj.currentBBox.x, this.mapObj.currentBBox.y],
  122. end: [this._scale, this._bbox.x, this._bbox.y]
  123. }]
  124. });
  125. dojo.connect(anim,"onEnd",this,function(){
  126. this._setStrokeWith({color:"black",width:this._normalizeStrokeWeight(2)});
  127. this.parent.onZoomEnd(this);
  128. });
  129. anim.play();
  130. this.mapObj.currentScale = this._scale;
  131. this.mapObj.currentBBox = {
  132. x: this._bbox.x,
  133. y: this._bbox.y
  134. };
  135. this._isZoomIn = true;
  136. dojo.byId("mapZoomCursor").className = "";
  137. },
  138. _zoomOut: function(){
  139. var anim = dojox.gfx.fx.animateTransform({
  140. duration: 1000,
  141. shape: this.mapObj,
  142. transform: [{
  143. name: "translate",
  144. start: [-this._bbox.x, -this._bbox.y],
  145. end: [-this.mapObj.boundBox[0], -this.mapObj.boundBox[1]]
  146. }, {
  147. name: "scaleAt",
  148. start: [this._scale, this._bbox.x, this._bbox.y],
  149. end: [this.mapObj.scale, this.mapObj.boundBox[0], this.mapObj.boundBox[1]]
  150. }]
  151. });
  152. dojo.connect(anim,"onEnd",this,function(){
  153. this._setStrokeWith({color:"black",width:this._normalizeStrokeWeight(2)});
  154. });
  155. anim.play();
  156. this.mapObj.currentScale = this.mapObj.scale;
  157. this.mapObj.currentBBox = {
  158. x: this.mapObj.boundBox[0],
  159. y: this.mapObj.boundBox[1]
  160. };
  161. this._isZoomIn = false;
  162. dojo.byId("mapZoomCursor").className = "";
  163. },
  164. init: function(){
  165. this.shape.rawNode.id = this.id;
  166. this.tooltip = null;
  167. this.shape.connect("onmouseover", this, this._onmouseoverHandler);
  168. this.shape.connect("onmouseout", this, this._onmouseoutHandler);
  169. this.shape.connect("onmousemove", this, this._onmousemoveHandler);
  170. this.shape.connect("onclick", this, this._onclickHandler);
  171. }
  172. });
  173. }