MoveSlice.js 3.7 KB

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