Class: SimpleD3::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_d3/template.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(placeholder, settings) ⇒ Template

Returns a new instance of Template.



22
23
24
25
# File 'lib/simple_d3/template.rb', line 22

def initialize(placeholder, settings)
  @placeholder = placeholder
  @settings = settings
end

Class Method Details

.template(placeholder, settings) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/simple_d3/template.rb', line 3

def self.template(placeholder, settings)
  template = new(placeholder, settings)
  javascript = template.send(settings[:type])
  colors = settings[:colors].present? ? template.colors(settings[:colors]) : "d3.scale.category10()"
  
  graph = <<-EOJS
  <script type="text/javascript">
    //Width and height
    var w = #{settings[:width]};
    var h = #{settings[:height]};

    //Easy colors accessible via a 10-step ordinal scale
    var colors = #{colors};

    #{javascript}
  </script>
  EOJS
end

Instance Method Details

#barObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/simple_d3/template.rb', line 35

def bar
  graph = <<-EOJS

    var xScale = d3.scale.ordinal()
            .domain(d3.range(data.length))
            .rangeRoundBands([0, w], 0.05);

    var yScale = d3.scale.linear()
            .domain([0, d3.max(data)])
            .range([0, h]);

    //Create SVG element
    var #{@placeholder} = d3.select("##{@placeholder}")
          .append("svg")
          .attr("width", w)
          .attr("height", h);

    //Create bars
    #{@placeholder}.selectAll("rect")
       .data(data)
       .enter()
       .append("rect")
       .attr("x", function(d, i) {
          return xScale(i);
       })
       .attr("y", function(d) {
          return h - yScale(d);
       })
       .attr("width", xScale.rangeBand())
       .attr("height", function(d) {
          return yScale(d);
       })
       .attr("fill", function(d, i) {
          return colors(i);
        })
       .on("mouseover", function(d) {

        //Get this bar's x/y values, then augment for the tooltip
        var xPosition = parseFloat(d3.select(this).attr("x")) + xScale.rangeBand() / 2;
        var yPosition = parseFloat(d3.select(this).attr("y")) + 14;

        //Create the tooltip label
        #{@placeholder}.append("text")
           .attr("id", "tooltip")
           .attr("x", xPosition)
           .attr("y", yPosition)
           .attr("text-anchor", "middle")
           .attr("font-family", "sans-serif")
           .attr("font-size", "11px")
           .attr("font-weight", "bold")
           .attr("fill", "black")
           .text(d);

       })
       .on("mouseout", function() {

        //Remove the tooltip
        d3.select("#tooltip").remove();

       });

  EOJS
end

#colors(color_array) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/simple_d3/template.rb', line 27

def colors(color_array)
  colors = <<-EOJS
    d3.scale.ordinal()
      .range(#{color_array.to_s})
      .domain(d3.range(0,#{color_array.size}))
  EOJS
end

#pieObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/simple_d3/template.rb', line 99

def pie
  pie = <<-EOJS
    var outerRadius = w / 2;
    var innerRadius = 0;
    var arc = d3.svg.arc()
            .innerRadius(innerRadius)
            .outerRadius(outerRadius);

    var pie = d3.layout.pie();

    //Create SVG element
    var #{@placeholder} = d3.select("##{@placeholder}")
          .append("svg")
          .attr("width", w)
          .attr("height", h);

    //Set up groups
    var arcs = #{@placeholder}.selectAll("g.arc")
            .data(pie(data))
            .enter()
            .append("g")
            .attr("class", "arc")
            .attr("transform", "translate(" + outerRadius + "," + outerRadius + ")");

    //Draw arc paths
    arcs.append("path")
        .attr("fill", function(d, i) {
          return colors(i);
        })
        .attr("d", arc);

    //Labels
    arcs.append("text")
        .attr("transform", function(d) {
          return "translate(" + arc.centroid(d) + ")";
        })
        .attr("text-anchor", "middle")
        .text(function(d) {
          return d.value;
    });
  EOJS
end