MoveSlice.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.charting.action2d.MoveSlice"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.charting.action2d.MoveSlice"] = true;
  8. dojo.provide("dojox.charting.action2d.MoveSlice");
  9. dojo.require("dojox.charting.action2d.Base");
  10. dojo.require("dojox.gfx.matrix");
  11. dojo.require("dojox.lang.functional");
  12. dojo.require("dojox.lang.functional.scan");
  13. dojo.require("dojox.lang.functional.fold");
  14. /*=====
  15. dojo.declare("dojox.charting.action2d.__MoveSliceCtorArgs", dojox.charting.action2d.__BaseCtorArgs, {
  16. // summary:
  17. // Additional arguments for highlighting actions.
  18. // scale: Number?
  19. // The amount to scale the pie slice. Default is 1.05.
  20. scale: 1.05,
  21. // shift: Number?
  22. // The amount in pixels to shift the pie slice. Default is 7.
  23. shift: 7
  24. });
  25. =====*/
  26. (function(){
  27. var DEFAULT_SCALE = 1.05,
  28. DEFAULT_SHIFT = 7, // px
  29. m = dojox.gfx.matrix,
  30. gf = dojox.gfx.fx,
  31. df = dojox.lang.functional;
  32. dojo.declare("dojox.charting.action2d.MoveSlice", dojox.charting.action2d.Base, {
  33. // summary:
  34. // Create an action for a pie chart that moves and scales a pie slice.
  35. // the data description block for the widget parser
  36. defaultParams: {
  37. duration: 400, // duration of the action in ms
  38. easing: dojo.fx.easing.backOut, // easing for the action
  39. scale: DEFAULT_SCALE, // scale of magnification
  40. shift: DEFAULT_SHIFT // shift of the slice
  41. },
  42. optionalParams: {}, // no optional parameters
  43. constructor: function(chart, plot, kwArgs){
  44. // summary:
  45. // Create the slice moving action and connect it to the plot.
  46. // chart: dojox.charting.Chart2D
  47. // The chart this action belongs to.
  48. // plot: String?
  49. // The plot this action is attached to. If not passed, "default" is assumed.
  50. // kwArgs: dojox.charting.action2d.__MoveSliceCtorArgs?
  51. // Optional keyword arguments object for setting parameters.
  52. if(!kwArgs){ kwArgs = {}; }
  53. this.scale = typeof kwArgs.scale == "number" ? kwArgs.scale : DEFAULT_SCALE;
  54. this.shift = typeof kwArgs.shift == "number" ? kwArgs.shift : DEFAULT_SHIFT;
  55. this.connect();
  56. },
  57. process: function(o){
  58. // summary:
  59. // Process the action on the given object.
  60. // o: dojox.gfx.Shape
  61. // The object on which to process the slice moving action.
  62. if(!o.shape || o.element != "slice" || !(o.type in this.overOutEvents)){ return; }
  63. if(!this.angles){
  64. // calculate the running total of slice angles
  65. var startAngle = m._degToRad(o.plot.opt.startAngle);
  66. if(typeof o.run.data[0] == "number"){
  67. this.angles = df.map(df.scanl(o.run.data, "+", startAngle),
  68. "* 2 * Math.PI / this", df.foldl(o.run.data, "+", 0));
  69. }else{
  70. this.angles = df.map(df.scanl(o.run.data, "a + b.y", startAngle),
  71. "* 2 * Math.PI / this", df.foldl(o.run.data, "a + b.y", 0));
  72. }
  73. }
  74. var index = o.index, anim, startScale, endScale, startOffset, endOffset,
  75. angle = (this.angles[index] + this.angles[index + 1]) / 2,
  76. rotateTo0 = m.rotateAt(-angle, o.cx, o.cy),
  77. rotateBack = m.rotateAt( angle, o.cx, o.cy);
  78. anim = this.anim[index];
  79. if(anim){
  80. anim.action.stop(true);
  81. }else{
  82. this.anim[index] = anim = {};
  83. }
  84. if(o.type == "onmouseover"){
  85. startOffset = 0;
  86. endOffset = this.shift;
  87. startScale = 1;
  88. endScale = this.scale;
  89. }else{
  90. startOffset = this.shift;
  91. endOffset = 0;
  92. startScale = this.scale;
  93. endScale = 1;
  94. }
  95. anim.action = dojox.gfx.fx.animateTransform({
  96. shape: o.shape,
  97. duration: this.duration,
  98. easing: this.easing,
  99. transform: [
  100. rotateBack,
  101. {name: "translate", start: [startOffset, 0], end: [endOffset, 0]},
  102. {name: "scaleAt", start: [startScale, o.cx, o.cy], end: [endScale, o.cx, o.cy]},
  103. rotateTo0
  104. ]
  105. });
  106. if(o.type == "onmouseout"){
  107. dojo.connect(anim.action, "onEnd", this, function(){
  108. delete this.anim[index];
  109. });
  110. }
  111. anim.action.play();
  112. },
  113. reset: function(){
  114. delete this.angles;
  115. }
  116. });
  117. })();
  118. }