module Graphics.Google.Chart (
Chart,
chartURL,
setSize, setTitle, setTitleOpts, setData,
ChartData, encodeDataSimple, encodeDataText, encodeDataExtended,
LegendChart, setLegend,
LineChart, newLineChart,
PieChart, newPieChart, PieStyle(..), setLabels,
BarChart, newBarChart, Orientation(..), BarStyle(..)
) where
import Data.Char (chr, ord)
import Data.List (intercalate)
import Numeric (showHex)
urlEnc str = concatMap enc str where
enc c | c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' = [c]
| c >= '0' && c <= '9' = [c]
| c == '-' || c == '_' || c == '.' || c == '~' = [c]
| c == ' ' = "+"
| otherwise = '%':(showHex (ord c) "")
type Params = [(String,String)]
class Chart c where
params :: c -> Params
fromParams :: Params -> c
setParam :: (Chart c) => String -> String -> c -> c
setParam key val c = fromParams $ (key,val) : filter ((/= key) . fst) (params c)
setSize :: (Chart c) => Int -> Int -> c -> c
setSize width height = setParam "chs" (show width ++ "x" ++ show height)
setTitle :: (Chart c) => String -> c -> c
setTitle title = setParam "chtt" title
setTitleOpts :: (Chart c) => String
-> Int
-> c -> c
setTitleOpts color size = setParam "chts" (color ++ "," ++ show size)
setData :: (Chart c) => ChartData -> c -> c
setData (ChartData str) = setParam "chd" str
chartURL :: (Chart c) => c -> String
chartURL chart = baseURL ++ intercalate "&" urlparams where
baseURL = "http://chart.apis.google.com/chart?"
urlparams = [urlEnc a ++ "=" ++ urlEnc b | (a,b) <- params chart]
newtype ChartData = ChartData String deriving Show
encodeDataSimple :: [[Int]] -> ChartData
encodeDataSimple datas =
ChartData $ "s:" ++ intercalate "," (map (map enc) datas) where
enc i | i >= 0 && i <= 25 = chr (ord 'A' + i)
| i >= 26 && i <= 51 = chr (ord 'a' + (i 26))
| i >= 52 && i <= 61 = chr (ord '0' + (i 52))
| otherwise = '_'
encodeDataText :: [[Int]] -> ChartData
encodeDataText = undefined
encodeDataExtended :: [[Int]] -> ChartData
encodeDataExtended = undefined
class Chart c => LegendChart c where
setLegend :: [String] -> c -> c
setLegend strs = setParam "chdl" (intercalate "|" strs)
newtype LineChart = LineChart Params
instance Chart LineChart where
params (LineChart p) = p
fromParams = LineChart
instance LegendChart LineChart
newLineChart :: LineChart
newLineChart = fromParams [("cht","lc")]
newtype PieChart = PieChart Params
instance Chart PieChart where
params (PieChart p) = p
fromParams = PieChart
data PieStyle = Pie2D | Pie3D
newPieChart :: PieStyle -> PieChart
newPieChart Pie2D = fromParams [("cht","p")]
newPieChart Pie3D = fromParams [("cht","p3")]
setLabels :: [String] -> PieChart -> PieChart
setLabels labels = setParam "chl" $ intercalate "|" labels
newtype BarChart = BarChart Params
instance Chart BarChart where
params (BarChart p) = p
fromParams = BarChart
instance LegendChart BarChart
data Orientation = Horizontal | Vertical
data BarStyle = Stacked | Grouped
newBarChart :: Orientation -> BarStyle -> BarChart
newBarChart orient style =
fromParams [("cht",'b':oLetter orient:sLetter style:[])] where
oLetter Horizontal = 'h'
oLetter Vertical = 'v'
sLetter Stacked = 's'
sLetter Grouped = 'g'