DojoD3ScatterChart.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. define([
  2. "dojo/_base/declare",
  3. "dojo/_base/lang",
  4. "./DojoD3",
  5. "./Mapping",
  6. "dojo/text!./templates/DojoD3ScatterChart.css"
  7. ], function (declare, lang,
  8. DojoD3, Mapping,
  9. css) {
  10. return declare([Mapping, DojoD3], {
  11. mapping: {
  12. barChart: {
  13. display: "Bar Chart Data",
  14. fields: {
  15. x: "x",
  16. y: "y"
  17. }
  18. }
  19. },
  20. constructor: function (mappings, target) {
  21. if (mappings)
  22. this.setFieldMappings(mappings);
  23. if (target)
  24. this.renderTo(target);
  25. },
  26. renderTo: function (_target) {
  27. _target = lang.mixin({
  28. css: css,
  29. margin: { top: 0, right: 0, bottom: 20, left: 50 }
  30. }, _target);
  31. this.inherited(arguments);
  32. this.SvgG
  33. .style("fill", "none")
  34. ;
  35. this.x = d3.scale.linear()
  36. .domain([0, 10])
  37. .range([0, this.target.width - (this.target.margin.left + this.target.margin.right)])
  38. ;
  39. this.y = d3.scale.linear()
  40. .domain([0, 10])
  41. .range([this.target.height - (this.target.margin.top + this.target.margin.bottom), 0])
  42. ;
  43. this.xAxis = d3.svg.axis()
  44. .scale(this.x)
  45. .orient("bottom")
  46. ;
  47. this.yAxis = d3.svg.axis()
  48. .scale(this.y)
  49. .orient("left")
  50. ;
  51. this.SvgG
  52. .attr("transform", "translate(" + this.target.margin.left + "," + this.target.margin.top + ")")
  53. ;
  54. this.SvgX = this.Svg.append("g")
  55. .attr("transform", "translate(" + this.target.margin.left + "," + (this.target.height - this.target.margin.bottom) + ")")
  56. .attr("class", "x axis")
  57. .call(this.xAxis)
  58. ;
  59. this.SvgY = this.Svg.append("g")
  60. .attr("transform", "translate(" + this.target.margin.left + "," + this.target.margin.top + ")")
  61. .attr("class", "y axis")
  62. .call(this.yAxis)
  63. ;
  64. this.update([]);
  65. },
  66. display: function (data) {
  67. if (data)
  68. this.setData(data);
  69. var data = this.getMappedData();
  70. this.x.domain([d3.min(data, function (d) { return d.x; }), d3.max(data, function (d) { return d.x; })]);
  71. this.y.domain([d3.min(data, function (d) { return d.y; }), d3.max(data, function (d) { return d.y; })]);
  72. this.Svg.selectAll("g.y.axis").transition()
  73. .call(this.yAxis)
  74. ;
  75. this.Svg.selectAll("g.x.axis").transition()
  76. .call(this.xAxis)
  77. ;
  78. this.update(data);
  79. },
  80. update: function (data) {
  81. this.SvgG.selectAll(".dot").remove();
  82. var dot = this.SvgG.selectAll(".scatterDot").data(data);
  83. var x = dot.enter().append("g")
  84. .attr("class", "dot")
  85. ;
  86. x.append("line")
  87. .attr("x1", lang.hitch(this, function (d) { return this.x(d.x) - 4; }))
  88. .attr("y1", lang.hitch(this, function (d) { return this.y(d.y) - 4; }))
  89. .attr("x2", lang.hitch(this, function (d) { return this.x(d.x) + 4; }))
  90. .attr("y2", lang.hitch(this, function (d) { return this.y(d.y) + 4; }))
  91. ;
  92. x.append("line")
  93. .attr("x1", lang.hitch(this, function (d) { return this.x(d.x) - 4; }))
  94. .attr("y1", lang.hitch(this, function (d) { return this.y(d.y) + 4; }))
  95. .attr("x2", lang.hitch(this, function (d) { return this.x(d.x) + 4; }))
  96. .attr("y2", lang.hitch(this, function (d) { return this.y(d.y) - 4; }))
  97. ;
  98. }
  99. });
  100. });