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…
- Import the Google Chart API library as follows:
import Graphics.Google.Chart
- 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)
- Create a chart URL out of the image by setting the attributes appropriately...