DojoD3Choropeth.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. define([
  2. "dojo/_base/declare",
  3. "dojo/_base/lang",
  4. "dojo/_base/array",
  5. "dojo/json",
  6. "./DojoD3",
  7. "./Mapping",
  8. "dojo/text!./map/us.json",
  9. "dojo/text!./map/us_counties.json",
  10. "dojo/text!./templates/DojoD3Choropeth.css",
  11. "d3/topojson.v1.min"
  12. ], function (declare, lang, arrayUtil, JSON,
  13. DojoD3, Mapping,
  14. us, us_counties, css) {
  15. return declare([Mapping, DojoD3], {
  16. mapping: {
  17. choropeth: {
  18. display: "Choropeth Data",
  19. fields: {
  20. county: "County ID",
  21. value: "Value"
  22. }
  23. }
  24. },
  25. constructor: function (mappings, target) {
  26. this.mappedData = d3.map();
  27. this.us = JSON.parse(us);
  28. this.us_counties = JSON.parse(us_counties);
  29. if (mappings)
  30. this.setFieldMappings(mappings);
  31. if (target)
  32. this.renderTo(target);
  33. },
  34. resize: function (args) {
  35. // No resize (yet - its too slow)
  36. },
  37. renderTo: function (_target) {
  38. _target = lang.mixin({
  39. css: css
  40. }, _target);
  41. this.inherited(arguments);
  42. this.SvgG
  43. .style("fill", "none")
  44. ;
  45. var path = d3.geo.path();
  46. var p = this.SvgG.selectAll("path").data(topojson.feature(this.us, this.us.objects.counties).features);
  47. var context = this;
  48. p.enter().append("path")
  49. .attr("class", "counties")
  50. .attr("d", path)
  51. .on("click", lang.hitch(this, function (d) {
  52. var evt = {};
  53. evt[this.getFieldMapping("county")] = d.id;
  54. this.emit("click", evt);
  55. }))
  56. .append("title").text(lang.hitch(this, function (d) { return context.us_counties[d.id]; }))
  57. ;
  58. p.exit().remove();
  59. this.Svg.append("path").datum(topojson.mesh(this.us, this.us.objects.states, function (a, b) { return a !== b; }))
  60. .attr("class", "states")
  61. .attr("d", path)
  62. ;
  63. this.Svg.append("path").datum(topojson.feature(this.us, this.us.objects.land))
  64. .attr("class", "usa")
  65. .attr("d", path)
  66. ;
  67. },
  68. display: function (data) {
  69. if (data)
  70. this.setData(data);
  71. var maxVal = this._prepData();
  72. var quantize = d3.scale.quantize()
  73. .domain([0, maxVal])
  74. .range(d3.range(255).map(lang.hitch(this, function (i) {
  75. var negRed = 255 - i;
  76. return "rgb(255, " + negRed + ", " + negRed + ")";
  77. })))
  78. ;
  79. this.SvgG.selectAll("path")
  80. .style("fill", lang.hitch(this, function (d) { return this.mappedData.get(d.id) == null ? "lightgrey" : quantize(this.mappedData.get(d.id)); }))
  81. .select("title")
  82. .text(lang.hitch(this, function (d) {
  83. return this.us_counties[d.id] + " (" + this.mappedData.get(d.id) + ")";
  84. }))
  85. ;
  86. },
  87. _prepData: function () {
  88. this.data = this.getMappedData();
  89. var maxVal = 0;
  90. this.mappedData = d3.map();
  91. arrayUtil.forEach(this.data, lang.hitch(this, function (item, idx) {
  92. if (+item.value > maxVal) {
  93. maxVal = +item.value;
  94. }
  95. this.mappedData.set(item.county, +item.value);
  96. }));
  97. return maxVal;
  98. }
  99. });
  100. });