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

This chart:

was produced by this code:

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

circle :: [(Double,Double)]
circle = [ (r a * sin (a*dr),r a * cos (a*dr)) | a <- [0,0.5..360::Double] ]
  where
    dr = 2 * pi / 360
    r a = 0.8 * cos (a * 20 * pi /360)

main = toFile def "example7_big.png" $ do
    layout_title .= "Parametric Plot"
    plot (line "" [circle])

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 Control.Lens
import Data.Default.Class
import System.Environment(getArgs)

chart = toRenderable layout
  where
    circle = [ (r a * sin (a*dr),r a * cos (a*dr)) | a <- [0,0.5..360::Double] ]
      where
        dr = 2 * pi / 360
        r a = 0.8 * cos (a * 20 * pi /360)

    circleP = plot_lines_values .~ [circle]
            $ plot_lines_style .~ solidLine 1.0 (opaque blue) 
            $ def

    layout = layout_title .~ "Parametric Plot"
           $ layout_plots .~ [toPlot circleP]
           $ def

main = renderableToFile def "example7_big.png" chart
Clone this wiki locally