1module Criterion.Collection.Chart
2  ( errBarChart
3  , defaultColors
4  ) where
5
6
7import Criterion.Measurement
8import Data.Accessor
9import Data.Colour
10import Data.Colour.Names
11import Graphics.Rendering.Chart                    hiding (Vector)
12
13import Criterion.Collection.Sample
14
15
16defaultColors :: [AlphaColour Double]
17defaultColors = cycle $ map opaque [
18                 blue,
19                 red,
20                 brown,
21                 black,
22                 darkgoldenrod,
23                 coral,
24                 cyan,
25                 darkcyan,
26                 darkkhaki,
27                 darkmagenta,
28                 darkslategrey
29                ]
30
31
32plotErrBars :: String
33            -> CairoLineStyle
34            -> [SampleData]
35            -> Plot Double Double
36plotErrBars name lineStyle samples = toPlot plot
37  where
38    value sd = symErrPoint size m 0 s
39      where
40        size  = fromIntegral $ sdInputSize sd
41        (m,s) = computeMeanAndStddev sd
42
43    plot = plot_errbars_values ^= map value samples
44         $ plot_errbars_line_style ^= lineStyle
45         $ plot_errbars_title ^= name
46         $ defaultPlotErrBars
47
48
49plotPoints :: String
50           -> CairoPointStyle
51           -> [SampleData]
52           -> Plot Double Double
53plotPoints name pointStyle samples = toPlot plot
54  where
55    value sd = (fromIntegral size, m)
56      where
57        size  = sdInputSize sd
58        (m,_) = computeMeanAndStddev sd
59
60    plot = plot_points_values ^= map value samples
61         $ plot_points_style ^= pointStyle
62         $ plot_points_title ^= name
63         $ defaultPlotPoints
64
65
66errBarChart :: Bool
67            -> Double
68            -> String
69            -> [(AlphaColour Double, String, [SampleData])]
70            -> Renderable ()
71errBarChart logPlot lineWidth plotTitle plotData = toRenderable layout
72  where
73    mkPlot (colour, plotName, samples) = joinPlot eb pts
74      where
75        lStyle = line_width ^= lineWidth
76               $ line_color ^= colour
77               $ defaultPlotErrBars ^. plot_errbars_line_style
78
79        pStyle = filledCircles (1.5 * lineWidth) colour
80
81        eb  = plotErrBars plotName lStyle samples
82        pts = plotPoints plotName pStyle samples
83
84    remapLabels = axis_labels ^: f
85      where
86        f labels = map (map g) labels
87        g (x,_) = (x, secs x)
88
89    axisfn = if logPlot
90               then autoScaledLogAxis defaultLogAxis
91               else autoScaledAxis defaultLinearAxis
92
93    layout = layout1_title ^= plotTitle
94           $ layout1_background ^= solidFillStyle (opaque white)
95           $ layout1_left_axis ^: laxis_generate ^= axisfn
96           $ layout1_left_axis ^: laxis_override ^= remapLabels
97           $ layout1_left_axis ^: laxis_title ^= "Time (seconds)"
98           $ layout1_bottom_axis ^: laxis_generate ^= axisfn
99           $ layout1_bottom_axis ^: laxis_title ^= "# of items in collection"
100           $ layout1_plots ^= (map (Left . mkPlot) plotData)
101           $ defaultLayout1
102