PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/visualize/public/js/backbone-d3/visualisations/calendar.js

http://github.com/jspears/bobamo
JavaScript | 120 lines | 110 code | 9 blank | 1 comment | 4 complexity | e352383c0318f7af4735ce53fd27db97 MD5 | raw file
Possible License(s): Apache-2.0
  1. define(['Backbone.d3', 'd3'], function (Backbone, d3) {
  2. var Calendar = Backbone.d3.Canned.Calendar = {};
  3. Calendar.View = Backbone.d3.PlotView.extend({
  4. day:d3.time.format("%w"),
  5. week:d3.time.format("%U"),
  6. percent:d3.format(".1%"),
  7. format:d3.time.format("%Y-%m-%d"),
  8. m:[19, 20, 20, 19], // top right bottom left margin
  9. gw:700, // width
  10. gh:136, // height
  11. z:10, // cell size
  12. plot:function (options, plotdata) {
  13. var that = this;
  14. var w = 700 - this.m[1] - this.m[3], // width
  15. h = 136 - this.m[0] - this.m[2];
  16. var color = d3.scale.quantize()
  17. .domain([0, 1])
  18. .range(d3.range(9));
  19. var svg = this.div.selectAll("svg")
  20. .data(d3.range(this.minYear, this.maxYear))
  21. .enter().append("svg:svg")
  22. .attr("width", w + this.m[1] + this.m[3])
  23. .attr("height", h + this.m[0] + this.m[2])
  24. .attr("class", "RdYlGn")// Colour pallet.
  25. .append("svg:g")
  26. .attr("transform",
  27. "translate(" + (this.m[3] + (w - this.z * 53) / 2) + "," + (this.m[0] + (h - this.z * 7) / 2) + ")");
  28. svg.append("svg:text")
  29. .attr("transform", "translate(-6," + this.z * 3.5 + ")rotate(-90)")
  30. .attr("text-anchor", "middle")
  31. .text(String);
  32. var rect = svg.selectAll("rect.day")
  33. .data(function (d) {
  34. return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1));
  35. })
  36. .enter().append("svg:rect")
  37. .attr("class", "day")
  38. .attr("width", this.z)
  39. .attr("height", this.z)
  40. .attr("x", function (d) {
  41. return that.week(d) * that.z;
  42. })
  43. .attr("y", function (d) {
  44. return that.day(d) * that.z;
  45. });
  46. svg.selectAll("path.month")
  47. .data(function (d) {
  48. return d3.time.months(new Date(d, 0, 1), new Date(d + 1, 0, 1));
  49. })
  50. .enter().append("svg:path")
  51. .attr("class", "month")
  52. .attr("d", that.monthPath);
  53. var data = d3.nest()
  54. .key(function (d) {
  55. return d.date;
  56. })
  57. .rollup(function (d) {
  58. return d[0].count;
  59. })
  60. .map(plotdata);
  61. rect.attr("class", function (d) {
  62. return "day q" + color(data[that.format(d)]) + "-9";
  63. })
  64. .append("svg:title")
  65. .text(function (d) {
  66. return (d = that.format(d)) + (d in data ? ": " + that.percent(data[d]) : "");
  67. });
  68. },
  69. plotdata:function () {
  70. // return percent safety
  71. var max = _.max(this.collection.pluck('count'));
  72. this.minYear = 0;
  73. this.maxYear = 0;
  74. var fmt = d3.time.format("%Y-%m-%d");
  75. var data = this.collection.map(function (datapoint) {
  76. var date = datapoint.get('date');
  77. var year = fmt.parse(date).getUTCFullYear();
  78. this.minYear = (this.minYear == 0) ? year : Math.min(this.minYear, year);
  79. this.maxYear = (this.maxYear == 0) ? year : Math.max(this.maxYear, year);
  80. return {date:date, count:1 - parseFloat(datapoint.get('count')) / max}
  81. }, this)
  82. if (this.minYear == this.maxYear) this.maxYear++;
  83. return data;
  84. },
  85. monthPath:function (t0) {
  86. var t1 = new Date(t0.getUTCFullYear(), t0.getUTCMonth() + 1, 0),
  87. d0 = +this.day(t0), w0 = +this.week(t0),
  88. d1 = +this.day(t1), w1 = +this.week(t1);
  89. return "M" + (w0 + 1) * this.z + "," + d0 * this.z
  90. + "H" + w0 * this.z + "V" + 7 * this.z
  91. + "H" + w1 * this.z + "V" + (d1 + 1) * this.z
  92. + "H" + (w1 + 1) * this.z + "V" + 0
  93. + "H" + (w0 + 1) * this.z + "Z";
  94. }
  95. }),
  96. Calendar.Model = Backbone.Model.extend({
  97. initialize:function (data) {
  98. this.set({
  99. date:data.date,
  100. count:data.count
  101. });
  102. }
  103. }),
  104. Calendar.Collection =
  105. Backbone.d3.PlotCollection.extend({
  106. model:Calendar.Model,
  107. url:"calendar.json",
  108. plottype:'calendar'
  109. })
  110. return Calendar;
  111. });