ProfileBarChart.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. define([ "require", "d3", "d3-tip" ], function(require, d3, d3Tip) {
  2. "use strict";
  3. var ProfileBarChart = {
  4. render: function(options) {
  5. function make_x_gridlines() {
  6. return d3.axisBottom(x).ticks(5);
  7. }
  8. function make_y_gridlines() {
  9. return d3.axisLeft(y).ticks(5);
  10. }
  11. var el = options.el, type = options.data.key, data = options.data.values, xAxisLabel = (options.formatValue,
  12. options.xAxisLabel), yAxisLabel = options.yAxisLabel, rotateXticks = options.rotateXticks, onBarClick = options.onBarClick, size = el.getBoundingClientRect(), svg = d3.select(el), margin = {
  13. top: 20,
  14. right: 30,
  15. bottom: 100,
  16. left: 80
  17. }, width = size.width - margin.left - margin.right, height = size.height - margin.top - margin.bottom, x = d3.scaleBand().range([ 0, width ]).padding(.5), y = d3.scaleLinear().range([ height, 0 ]), g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  18. x.domain(data.map(function(d) {
  19. return d.value;
  20. })), y.domain([ 0, d3.max(data, function(d) {
  21. return d.count;
  22. }) ]), g.append("g").attr("class", "grid").attr("transform", "translate(0," + height + ")").call(make_x_gridlines().tickSize(-height).tickFormat("")),
  23. g.append("g").attr("class", "grid").call(make_y_gridlines().tickSize(-width).tickFormat(""));
  24. var xAxis = g.append("g").attr("transform", "translate(0," + height + ")").call(d3.axisBottom(x));
  25. rotateXticks && xAxis.selectAll("text").style("text-anchor", "end").attr("dx", "-.8em").attr("dy", ".15em").attr("transform", "rotate(-45)"),
  26. g.append("g").call(d3.axisLeft(y).ticks(3, "s")), g.append("text").attr("transform", "translate(" + width / 2 + " ," + (margin.top - 25) + ")").attr("class", "axislabel").text(xAxisLabel),
  27. g.append("text").attr("transform", "rotate(-90)").attr("y", 0 - margin.left).attr("x", 0 - height / 2).attr("dy", "1em").attr("class", "axislabel").text(yAxisLabel);
  28. var tooltip = d3Tip().attr("class", "d3-tip").offset([ 10, 0 ]).html(function(d) {
  29. return console.log(d), '<table><thead><tr><td colspan="3"><strong class="x-value">' + d.value + '</strong></td></tr></thead><tbody><tr><td class="key">' + type + '</td><td class="value">' + d.count + "</td></tr></tbody></table>";
  30. });
  31. g.selectAll(".bar").data(data).enter().append("rect").attr("class", "profile-bar").attr("x", function(d) {
  32. return x(d.value);
  33. }).attr("width", x.bandwidth()).attr("y", function(d) {
  34. return height;
  35. }).attr("height", 0).on("click", function(e) {
  36. tooltip.hide(), onBarClick && onBarClick(e);
  37. }).on("mouseover", function(d) {
  38. tooltip.show(d, this);
  39. }).on("mouseout", function(d) {
  40. tooltip.hide();
  41. }).transition().duration(450).delay(function(d, i) {
  42. return 50 * i;
  43. }).attr("y", function(d) {
  44. return y(d.count);
  45. }).attr("height", function(d) {
  46. return height - y(d.count);
  47. }), g.call(tooltip);
  48. }
  49. };
  50. return ProfileBarChart;
  51. });