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 line chart using Google's Chart API


We will use the convenient Google Chart API (https://developers.google.com/chart) to render a line chart. This API produces a URL that points to a PNG image of the graph. This lightweight URL can be easier to handle than the actual image itself.

Our data will come from a text file that contains a list of numbers separated by lines. The code will generate a URL to present this data.

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:

    main = do
      rawInput <- readFile "input.txt"
      let nums = map (read :: String -> Int) (lines rawInput)
  3. Create a chart URL out of the image by setting the attributes appropriately, as shown in the following...