Class: GoogleImageCharts::ChartBase

Inherits:
Object
  • Object
show all
Defined in:
lib/google-image-charts.rb

Direct Known Subclasses

LineGraph, PieChart

Constant Summary collapse

CHART_URI_BASE =
"http://chart.apis.google.com/chart?"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chartOptionsHash) ⇒ ChartBase

Returns a new instance of ChartBase.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/google-image-charts.rb', line 10

def initialize(chartOptionsHash)
  @chartSpecificOptions = ""  # Should be overridden by child-classes
  
  @chartWidth   = chartOptionsHash[:width]
  @chartHeight  = chartOptionsHash[:height]
  @chartTitle   = chartOptionsHash[:title]
  self.chartData=(chartOptionsHash[:data])  # Should be an array of data arrays
  
  @chartLabels  = chartOptionsHash[:labels]  # Should be an array of labels
  @chartLabelPosition  = "b" if chartOptionsHash[:labels].nil? == false
  
  @chartColors = chartOptionsHash[:colors]
  
  # These might be set by someone who knows what they are doing, perhaps after reading:
  # https://developers.google.com/chart/image/docs/chart_params
  @additionalChartOptions = chartOptionsHash[:additionalOptions]
  
  # Support the Google Image Charts POST method which allows up to 16K of chart data
  # https://developers.google.com/chart/image/docs/post_requests
  @usePost = chartOptionsHash[:usePost] #((chartOptionsHash[:usePost].nil? == true) || (@usePost != true)) ? false : true
     
end

Instance Attribute Details

#additionalChartOptionsObject

Returns the value of attribute additionalChartOptions.



6
7
8
# File 'lib/google-image-charts.rb', line 6

def additionalChartOptions
  @additionalChartOptions
end

#chartColorsObject

Returns the value of attribute chartColors.



6
7
8
# File 'lib/google-image-charts.rb', line 6

def chartColors
  @chartColors
end

#chartHeightObject

Returns the value of attribute chartHeight.



6
7
8
# File 'lib/google-image-charts.rb', line 6

def chartHeight
  @chartHeight
end

#chartLabelsObject

Returns the value of attribute chartLabels.



6
7
8
# File 'lib/google-image-charts.rb', line 6

def chartLabels
  @chartLabels
end

#chartTitleObject

Returns the value of attribute chartTitle.



6
7
8
# File 'lib/google-image-charts.rb', line 6

def chartTitle
  @chartTitle
end

#chartWidthObject

Returns the value of attribute chartWidth.



6
7
8
# File 'lib/google-image-charts.rb', line 6

def chartWidth
  @chartWidth
end

#usePostObject

Returns the value of attribute usePost.



6
7
8
# File 'lib/google-image-charts.rb', line 6

def usePost
  @usePost
end

Instance Method Details

#chart_urlObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/google-image-charts.rb', line 70

def chart_url
  # Standard GET request
  chartURL = CHART_URI_BASE
  chartURL = chartURL + "cht="    + @chartType
  chartURL = chartURL + "&chs="   + @chartWidth.to_s + "x" + @chartHeight.to_s 
  chartURL = chartURL + "&chd=t:" + chartDataFlattened if @chartData.nil? == false # Simple text for now
  chartURL = chartURL + "&chdl="  + @chartLabels.join("|")  if @chartLabels.nil? == false
  chartURL = chartURL + "&chdlp="  + @chartLabelPosition    if @chartLabels.nil? == false
  chartURL = chartURL + "&chtt="  + @chartTitle             if @chartTitle.nil? == false
  chartURL = chartURL + "&chco="  + @chartColors.join(",")  if @chartColors.nil? == false
  
  chartURL = chartURL + @chartSpecificOptions + @additionalChartOptions
  
  return URI.escape(chartURL)
end

#chartData=(data) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/google-image-charts.rb', line 38

def chartData=(data)
  # Take array of arrays and flatten to array of strings
  
  @chartData = Array.new
  data.each_index do |index|
    # Join the data only if it contains an array
    if data[index].class == Array
      @chartData.push data[index].join(",").to_s 
    else
      @chartData.push data[index]
    end
  end
  
end

#chartDataFlattenedObject



33
34
35
36
# File 'lib/google-image-charts.rb', line 33

def chartDataFlattened
  # Override in subclasses
  @chartData
end

#get_chartObject



64
65
66
67
68
# File 'lib/google-image-charts.rb', line 64

def get_chart
  # returns a URL if usePost = false,
  # returns a PNG if usePost = true. Pass your encoding to the browser as appropriate.
  return @usePost == false ? chart_url : post_chart_data
end

#html_img_tagObject



121
122
123
# File 'lib/google-image-charts.rb', line 121

def html_img_tag
  return "<img src='#{chart_url}' alt='#{@chartTitle}' height='#{@chartHeight}' width='#{@chartWidth}' />"
end

#post_chart_dataObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/google-image-charts.rb', line 86

def post_chart_data
  # Used when you need to send more than 2K (up to 16K) of data to Google.
  require 'net/https'
  
  uri = URI.parse("https://chart.googleapis.com/chart")

  dataHash = {
    "cht" => @chartType,
    "chs" => @chartWidth.to_s + "x" + @chartHeight.to_s
  }
  
  dataHash.merge!( (@chartData.nil? == false) ? {"chd" => "t:#{chartDataFlattened.to_s}" } : {} )
  dataHash.merge!( (@chartLabels.nil? == false) ? {"chdl" => @chartLabels.join("|").to_s } : {} )
  dataHash.merge!( (@chartLabels.nil? == false) ? {"chdlp" => @chartLabelPosition } : {}        )
  dataHash.merge!( (@chartTitle.nil? == false) ? {"chtt" => @chartTitle } : {}                 )
  dataHash.merge!( (@chartColors.nil? == false) ? {"chco" => @chartColors.join(",").to_s } : {} )
  
  #For the remainder of these, we need to split on '&', then k,v on '='
  (@chartSpecificOptions.to_s + @additionalChartOptions.to_s).split('&').each do |option|
    pair = option.split('=')
    newElement = { pair[0].to_s => pair[1].to_s }
    dataHash.merge!( newElement )
  end

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data(dataHash)

  response = http.request(request)
  return response.body
  
end

#set_chart_label_position(position) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/google-image-charts.rb', line 53

def set_chart_label_position(position)
  @chartLabelPosition = case
                      when position == :top then "t"
                      when position == :left then "l"
                      when position == :right then "r"
                      when position == :bottom then "b"
                      else "b"
                      end
                        
end