Method: Plotlyrb::Plot#create_from_grid

Defined in:
lib/plotlyrb/plot.rb

#create_from_grid(plot_spec, grid_json) ⇒ Object

Takes a list of data hashes (each element is a trace?), response from grid creation, x and y column names (must appear in grid), and a layout. Extracts the column references from the grid response to populate the xsrc # and ysrc fields in data. See api.plot.ly/v2/plots#create



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/plotlyrb/plot.rb', line 17

def create_from_grid(plot_spec, grid_json)
  grid_response_body = JSON.parse(grid_json)
  return Response.fail('No :figure key in plot spec') unless plot_spec.has_key?(:figure)
  unless plot_spec[:figure].has_key?(:data)
    return Response.fail('No :data key at {:figure => {:data => ...}} in plot spec')
  end

  begin
    payload_data = plot_spec[:figure][:data].map { |d|
      self.class.replace_column_names_with_uids(grid_response_body, d)
    }
  rescue => e
    return Response.fail(e.to_s)
  else
    payload = self.class.replace_data_in_spec(plot_spec, payload_data)
    request = Net::HTTP::Post.new(ApiV2::PLOTS.path, @headers)
    request.body = payload.to_json
    Response.from_http_response(@https.request(request))
  end
end