Book Image

Haskell Data Analysis Cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis Cookbook

By: Nishant Shukla

Overview of this book

Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
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 [nums]) $
        setLabels...