Skip to content
Tim Docker edited this page Jan 29, 2015 · 9 revisions

This chart:

was produced by this code:

import Graphics.Rendering.Chart.Easy
import Graphics.Rendering.Chart.Backend.Cairo

import Data.Time.LocalTime
import System.Random

import Prices(prices1)

values :: [ (LocalTime,Double,Int,Int) ]
values = [ (d, v, z, t) | ((d,v,_),z,t) <- zip3 prices1 zs ts ]
  where
    zs     = randoms $ mkStdGen 0
    ts     = randomRs (-2,27) $ mkStdGen 1

main = toFile def "example8_big.png" $ do
  layout_title .= "Price History"
  layout_background .= solidFillStyle (opaque white)
  layout_foreground .= (opaque black)
  layout_left_axis_visibility . axis_show_ticks .= False

  plot (line "price 1" [ [ (d,v) | (d,v,_) <- prices1] ] )

  plot $ liftEC $ do
    area_spots_4d_title .= "random value"
    area_spots_4d_max_radius .= 20
    area_spots_4d_values .= values

Here is equivalent code without using the Easy helper functions and monadic stateful API:

import Graphics.Rendering.Chart
import Graphics.Rendering.Chart.Backend.Cairo
import Data.Colour
import Data.Colour.Names
import Data.Default.Class
import Control.Lens
import System.Random
import System.Environment(getArgs)
import Prices(prices1)

-- demonstrate AreaSpots4D

chart :: Double -> Renderable ()
chart lwidth = toRenderable layout
  where
    layout = layout_title .~"Price History"
           $ layout_background .~ solidFillStyle (opaque white)
           $ layout_left_axis_visibility . axis_show_ticks .~ False
           $ layout_plots .~ [ toPlot price1, toPlot spots ]
           $ setLayoutForeground (opaque black)
           $ def

    price1 = plot_lines_style .~ lineStyle
           $ plot_lines_values .~ [ [ (d, v) | (d,v,_) <- prices1] ]
           $ plot_lines_title .~ "price 1"
           $ def

    spots = area_spots_4d_title .~ "random value"
          $ area_spots_4d_max_radius .~ 20
          $ area_spots_4d_values .~ values
          $ def
    
    points = map (\ (d,v,z,t)-> (d,v) ) values
    values = [ (d, v, z, t) | ((d,v,_),z,t) <- zip3 prices1 zs ts ]
    zs,ts :: [Int]
    zs     = randoms $ mkStdGen 0
    ts     = randomRs (-2,27) $ mkStdGen 1

    lineStyle = line_width .~ 3 * lwidth
              $ line_color .~ opaque blue
              $ def

main = renderableToFile def "example8_big.png" (chart 0.25)
Clone this wiki locally