module Test.SBench.Plot.Gnuplot (
series2plot
, series2scaledPlot
, series2plotWith
, series2scaledPlotWith
, series2plotWithLinestyle
, series2scaledPlotWithLinestyle
, sbench2plot
, sbench2scaledPlot
, sbench2plotWithLinestyle
, sbench2scaledPlotWithLinestyle
, sbench2plotWith
, sbench2scaledPlotWith
, toDiagram
, toDiagramWith
) where
import qualified Graphics.Gnuplot.Plot.TwoDimensional as Plot2D
import qualified Graphics.Gnuplot.Graph.TwoDimensional as Graph2D
import qualified Graphics.Gnuplot.Frame.OptionSet as Opts
import qualified Graphics.Gnuplot.LineSpecification as LineSpec
import qualified Graphics.Gnuplot.Advanced as GPlot ( plot )
import qualified Graphics.Gnuplot.Frame as Frame
import qualified Graphics.Gnuplot.Terminal.PostScript as PostScript
import qualified Graphics.Gnuplot.Value.Tuple as TVal
import qualified Graphics.Gnuplot.Value.Atom as AVal
import qualified Graphics.Gnuplot.Graph as Graph
import Data.Monoid ( mappend, mempty )
import System.FilePath ( (<.>) )
import System.Process ( system )
import Test.SBench.Options ( Title )
import Test.SBench.File.FileOps ( sbench2series )
import Test.SBench.File.Types ( MetaInfo(..) )
series2plot :: (TVal.C a, AVal.C a, TVal.C b, AVal.C b) =>
Title
-> [(a, b)]
-> Plot2D.T a b
series2plot = series2scaledPlot id id
series2scaledPlot :: (TVal.C c, AVal.C c, TVal.C d, AVal.C d) =>
(a -> c)
-> (b -> d)
-> Title
-> [(a, b)]
-> Plot2D.T c d
series2scaledPlot xscaler yscaler t s =
let s' = scaleSeries xscaler yscaler s in
Graph2D.lineSpec (LineSpec.title t $ LineSpec.deflt) `fmap` Plot2D.list Graph2D.lines s'
series2plotWithLinestyle :: (TVal.C a, AVal.C a, TVal.C b, AVal.C b) =>
Int
-> Title
-> [(a, b)]
-> Plot2D.T a b
series2plotWithLinestyle linestyle = series2scaledPlotWithLinestyle linestyle id id
series2scaledPlotWithLinestyle :: (TVal.C c, AVal.C c, TVal.C d, AVal.C d) =>
Int
-> (a -> c)
-> (b -> d)
-> Title
-> [(a, b)]
-> Plot2D.T c d
series2scaledPlotWithLinestyle linestyle xscaler yscaler title =
series2scaledPlotWith [LineSpec.title title, LineSpec.lineStyle linestyle] xscaler yscaler
series2plotWith :: (TVal.C a, AVal.C a, TVal.C b, AVal.C b) =>
[LineSpec.T -> LineSpec.T]
-> [(a, b)]
-> Plot2D.T a b
series2plotWith opts = series2scaledPlotWith opts id id
series2scaledPlotWith :: (TVal.C c, AVal.C c, TVal.C d, AVal.C d) =>
[LineSpec.T -> LineSpec.T]
-> (a -> c)
-> (b -> d)
-> [(a, b)]
-> Plot2D.T c d
series2scaledPlotWith opts xscaler yscaler s =
let s' = scaleSeries xscaler yscaler s in
Graph2D.lineSpec (makeGraphOpts opts) `fmap` Plot2D.list Graph2D.lines s'
sbench2plot :: FilePath -> IO (Plot2D.T Double Double)
sbench2plot = sbench2scaledPlot id id
sbench2scaledPlot :: (Double -> Double)
-> (Double -> Double)
-> FilePath
-> IO (Plot2D.T Double Double)
sbench2scaledPlot xscaler yscaler file = do
(mi, s) <- sbench2series file
return $ series2scaledPlot xscaler yscaler (miGraphTitle mi) s
sbench2plotWithLinestyle :: Int
-> FilePath
-> IO (Plot2D.T Double Double)
sbench2plotWithLinestyle linestyle = sbench2scaledPlotWithLinestyle linestyle id id
sbench2scaledPlotWithLinestyle :: Int
-> (Double -> Double)
-> (Double -> Double)
-> FilePath
-> IO (Plot2D.T Double Double)
sbench2scaledPlotWithLinestyle linestyle xscaler yscaler file = do
(mi, s) <- sbench2series file
return $ series2scaledPlotWith [LineSpec.title (miGraphTitle mi), LineSpec.lineStyle linestyle] xscaler yscaler s
sbench2plotWith :: [LineSpec.T -> LineSpec.T]
-> FilePath
-> IO (Plot2D.T Double Double)
sbench2plotWith lineOpts = sbench2scaledPlotWith lineOpts id id
sbench2scaledPlotWith :: [LineSpec.T -> LineSpec.T]
-> (Double -> Double)
-> (Double -> Double)
-> FilePath
-> IO (Plot2D.T Double Double)
sbench2scaledPlotWith lineOpts xscaler yscaler file = do
(mi, s) <- sbench2series file
return $ series2scaledPlotWith lineOpts xscaler yscaler s
toDiagramWith terminal opts plots =
GPlot.plot terminal gr
where
gr = Frame.cons (makeFrameOpts opts) $ combinePlots plots
toDiagram name topts opts plots =
let file = name <.> "eps"
plt = (Frame.cons (makeFrameOpts opts) (combinePlots plots))
in
GPlot.plot (foldr ($) (PostScript.cons file) topts) plt
>> system ("epstopdf " ++ file)
>> return (name <.> "pdf")
scaleSeries xscaler yscaler = map (\(x,y) -> (xscaler x, yscaler y))
makeGraphOpts :: [LineSpec.T -> LineSpec.T] -> LineSpec.T
makeGraphOpts = foldr ($) LineSpec.deflt
makeFrameOpts :: Graph.C graph => [Opts.T graph -> Opts.T graph] -> Opts.T graph
makeFrameOpts = foldr ($) Opts.deflt
combinePlots :: (TVal.C a, AVal.C a, TVal.C b, AVal.C b) => [Plot2D.T a b] -> Plot2D.T a b
combinePlots = foldr mappend mempty