Skip to content
Muhammad Aviv Burhanudin edited this page Dec 14, 2025 · 11 revisions

This chart:

was produced by this code:

module Main where

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

values :: [(String, Double, Bool)]
values =
  [ ("Mexico City", 19.2, False)
  , ("Mumbai",      12.9, False)
  , ("Sydney",       4.3, False)
  , ("London",       8.3, False)
  , ("New York",     8.2, True)
  ]

pitem :: (String, Double, Bool) -> PieItem
pitem (s,v,o) = pitem_value .~ v
              $ pitem_label .~ s
              $ pitem_offset .~ (if o then 25 else 0)
              $ def

main :: IO ()
main = toFile def "example5_big.png" $ do
  pie_title .= "Relative Populations"
  pie_plot . pie_data .= map pitem values

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

module Main where

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

import Data.Default.Class
import Control.Lens

values :: [(String, Double, Bool)]
values =
  [ ("Mexico City", 19.2, False)
  , ("Mumbai",      12.9, False)
  , ("Sydney",       4.3, False)
  , ("London",       8.3, False)
  , ("New York",     8.2, True)
  ]

pitem :: (String, Double, Bool) -> PieItem
pitem (s, v, o) = pitem_value  .~ v
                $ pitem_label  .~ s
                $ pitem_offset .~ (if o then 25 else 0)
                $ def

chart :: Renderable ()
chart = toRenderable layout 
  where
    layout = pie_title .~ "Relative Populations"
           $ pie_plot . pie_data .~ map pitem values
           $ def

main :: IO ()
main = renderableToFile def "example5_big.png" chart >>
       pure ()

the Verbose way:

module Main where

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

import Data.Colour       (opaque)
import Data.Colour.Names (white, black)

values :: [(String, Double, Bool)]
values =
  [ ("Mexico City", 19.2, False)
  , ("Mumbai",      12.9, False)
  , ("Sydney",       4.3, False)
  , ("London",       8.3, False)
  , ("New York",     8.2, True)
  ]

pitem :: (String, Double, Bool) -> PieItem
pitem (s, v, o) =
  PieItem { _pitem_label  = s
          , _pitem_value  = v
          , _pitem_offset = if o then 25 else 0
          }

fontStyle :: FontStyle
fontStyle =
  FontStyle { _font_name   = "sans-serif"
            , _font_size   = 10
            , _font_weight = FontWeightNormal
            , _font_slant  = FontSlantNormal
            , _font_color  = opaque black
            }

pieChart :: PieChart
pieChart =
 PieChart { _pie_data             = map pitem values
          , _pie_colors           = defaultColorSeq
          , _pie_label_style      = fontStyle
          , _pie_label_line_style = solidLine 1 $ opaque black
          , _pie_start_angle      = 0
          }

chart :: Renderable ()
chart = toRenderable layout
  where
    layout =
      PieLayout { _pie_title       = "Relative Population"
                , _pie_title_style = fontStyle
                , _pie_margin      = 20
                , _pie_plot        = pieChart
                , _pie_background  = solidFillStyle $ opaque white
                }

main :: IO ()
main =
  renderableToFile fileOpts "example5_big.png" chart >>
  pure ()
  where
    fileOpts = FileOptions { _fo_size   = (800, 600)
                           , _fo_format = PNG
                           }

Clone this wiki locally