Skip to content

Instantly share code, notes, and snippets.

@grk
Created March 24, 2009 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grk/84397 to your computer and use it in GitHub Desktop.
Save grk/84397 to your computer and use it in GitHub Desktop.
Graphs with Sinatra and Scruffy
require 'rubygems'
require 'sinatra'
require 'scruffy'
require 'md5'
get '/' do
'This is Grapher.'
end
get '/graph/?' do
title = params[:title] || ""
type = params[:type].nil? ? :line : params[:type].to_sym
data = params[:data]
if data.nil?
redirect '/'
end
digest = MD5.hexdigest(title.to_s + type.to_s + data.to_s)
file = "graphs/#{digest}.png"
if !File.exists?(file)
graph = Scruffy::Graph.new(:title => title)
sets = data.split(":")
sets.each do |s|
s = s.split(",")
t = s.shift
s.map! { |i| i = i.to_i }
graph.add(type, t, s)
end
graph.render(:to => file, :as => 'PNG')
end
send_file file, :type => 'image/png', :disposition => 'inline'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment