Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 53 lines (45 sloc) 1.788 kb
fc22dbc @ezyang Add some graphs.
authored
1 import Text.CSV
2 import Graphics.Rendering.Chart
3 import Graphics.Rendering.Chart.Gtk
4 import Data.Accessor
5 import System.Environment
6 import Data.Maybe
7 import Data.Colour.Names
8 import Data.Colour
9 import Control.Monad
10 import qualified Data.Map as Map
11
12 main = do
13 args <- getArgs
14 case args of
6a13e3b @ezyang Add dense lookups; not much difference.
authored
15 x:[] -> run x (x++".csv") (x++".pdf")
16 otherwise -> putStrLn "Usage: runhaskell graph.hs NAME"
fc22dbc @ezyang Add some graphs.
authored
17
18 run name filename out = do
19 r <- parseCSVFromFile filename
20 either errorHandler (process name out) r
21
22 errorHandler = print
23
24 doubleRead "." = "."
25 doubleRead s@('.':_) = doubleRead' ('0':s)
26 doubleRead s = doubleRead' s
27 doubleRead' "." = ".0"
28 doubleRead' (c:cs) = c : doubleRead' cs
29 doubleRead' [] = []
30
31 process name out csv = do
32 let tuples = map processRecord . filter ((==3).length) $ csv
33 fulldata = Map.assocs $ foldl reduce Map.empty tuples
34 where reduce m (s,i,v) = Map.insertWith' (flip (++)) s [(i,v)] m
35 chart = layout1_title ^= name
36 $ layout1_left_axis .> laxis_title ^= "ticks"
37 $ layout1_bottom_axis .> laxis_title ^= "elements"
38 $ layout1_plots ^= zipWith makePlots colors fulldata
39 $ defaultLayout1
40 where makePlots x y = Left . toPlot $ line x y
41 renderableToPDFFile (toRenderable chart) 640 480 out
42 where
43 processRecord :: [String] -> (String, Int, Double)
44 processRecord (x:y:z:[]) = (x, read y, read $ doubleRead z)
45
46 line color (name, ticks) = plot_lines_values ^= [ticks]
47 $ plot_lines_title ^= name
48 $ plot_lines_style .> line_color ^= color
49 $ defaultPlotLines
50
51 colors :: [AlphaColour Double]
52 colors = cycle $ map opaque [ blue, red, green, yellow, cyan, magenta, black ]
Something went wrong with that request. Please try again.