Book Image

Haskell Data Analysis cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis cookbook

By: Nishant Shukla

Overview of this book

Step-by-step recipes filled with practical code samples and engaging examples demonstrate Haskell in practice, and then the concepts behind the code. This book shows functional developers and analysts how to leverage their existing knowledge of Haskell specifically for high-quality data analysis. A good understanding of data sets and functional programming is assumed.
Table of Contents (14 chapters)
13
Index

Plotting a pie chart using Google's Chart API

The Google Chart API provides a very elegant-looking pie chart interface. We can generate images of well-designed pie charts by feeding our input and labels properly, as described in this recipe.

Getting ready

Install the GoogleChart package as follows:

$ cabal install hs-gchart

Create a file called input.txt with numbers inserted line by line as follows:

$ cat input.txt 
2
5
3
7
4
1
19
18
17
14
15
16

How to do it…

  1. Import the Google Chart API library as follows:
    import Graphics.Google.Chart
  2. Gather the input from the text file and parse it as a list of integers, as shown in the following code snippet:
    main = do
      rawInput <- readFile "input.txt"
      let nums = map (read :: String -> Int) (lines rawInput)
  3. Print out the Google Chart URL from the pie chart attributes shown in the following code:
      putStrLn $ chartURL $
        setSize 500 400 $
        setTitle "Example of Plotting a Pie Chart in Haskell" $
        setData (encodeDataSimple...