hs-gchart 0.1.1 → 0.2
raw patch · 13 files changed
+832/−358 lines, 13 files
Files
- Graphics/GChart.hs +89/−28
- Graphics/GChart/ChartItems.hs +32/−230
- Graphics/GChart/ChartItems/Basics.hs +34/−0
- Graphics/GChart/ChartItems/Colors.hs +40/−0
- Graphics/GChart/ChartItems/Data.hs +24/−0
- Graphics/GChart/ChartItems/Labels.hs +117/−0
- Graphics/GChart/ChartItems/Styles.hs +110/−0
- Graphics/GChart/ChartItems/Util.hs +11/−0
- Graphics/GChart/DataEncoding.hs +8/−3
- Graphics/GChart/Types.hs +254/−89
- README.md +37/−3
- examples/Examples.hs +58/−3
- hs-gchart.cabal +18/−2
Graphics/GChart.hs view
@@ -6,18 +6,18 @@ For more examples, refer to @Examples.hs@ in the source tarball, or download it directly from Github : <http://github.com/deepakjois/hs-gchart/blob/master/examples/Examples.hs>. -For documentation regarding the full data model, refer to +For documentation regarding the full data model, refer to "Graphics.GChart.Types". For more information about the Google Chart API, refer to <http://code.google.com/apis/chart/> -}-module Graphics.GChart ( +module Graphics.GChart ( module Graphics.GChart.Types, -- * Setting Chart Parameters- {-| + {-| Use these functions to set the parameters of the chart. @@ -34,17 +34,23 @@ setLabels $ [\"Test 1\", \"Test 2\", \"Test 3\", \"Test 4\", \"Test 5\"] @ --} - setChartSize, setChartType, setDataEncoding, setChartTitle,addChartData, addChartDataXY, setColors, addColor, addFill,- setLegend, addAxis, setGrid, setLabels,+-}++ setChartSize, setChartType, setDataEncoding, setChartTitle,+ setChartTitleWithColor, setChartTitleWithColorAndFontSize, addChartData,+ addChartDataXY, setColors, addColor, addFill, setLegend, addAxis, setGrid,+ setLabels, setBarWidthSpacing,+ makeShapeMarker, makeRangeMarker, makeFinancialMarker,+ addShapeMarker, addRangeMarker, addFinancialMarker, -- * Retrieving Chart data getChartData, getChartUrl, convertToUrl, - -- * Smart Constructors + -- * Smart Constructors -- | These functions can be used to construct chart -- parameters more conveniently solid, legend, legendWithPosition, makeAxis, makeGrid,- simple,text,extended+ simple, text, extended, automatic, automaticWithSpacing,+ barwidth, barwidthspacing, relative ) where import Graphics.GChart.Types@@ -107,6 +113,38 @@ extended :: ChartData extended = Extended [] +-- | Set automatic bar width for bar chart+automatic :: BarChartWidthSpacing+automatic = (Just Automatic,Nothing)++-- | Set automatic bar width for bar chart, with spacing values+automaticWithSpacing :: Int -> Int -> BarChartWidthSpacing+automaticWithSpacing b g= (Just Automatic, Just (Fixed (b,g)))++-- | Set bar width for chart+barwidth :: Int -> BarChartWidthSpacing+barwidth n = (Just (BarWidth n), Nothing)++-- | Set bar width and spacing for chart+barwidthspacing :: Int -> Int -> Int -> BarChartWidthSpacing+barwidthspacing bw b g = (Just (BarWidth bw), Just (Fixed (b,g)))++-- | Set relative spacing+relative :: Float -> Float -> BarChartWidthSpacing+relative b g = (Nothing, Just (Relative (b,g)))++-- | Shape Marker+makeShapeMarker :: ShapeMarker+makeShapeMarker = defaultShapeMarker++-- | Range Marker+makeRangeMarker :: RangeMarker+makeRangeMarker = defaultRangeMarker++-- | Financial Marker+makeFinancialMarker :: FinancialMarker+makeFinancialMarker = defaultFinancialMarker+ {- Setting Chart Parameters-} -- | Set the chart size by passing the width and the height in pixels@@ -120,8 +158,17 @@ -- | Set the chart title by passing a 'ChartTitle' setChartTitle :: String -> ChartM ()-setChartTitle = set+setChartTitle title = set $ ChartTitle title Nothing Nothing +-- | Set the chart title with a color+setChartTitleWithColor :: String -> Color -> ChartM()+setChartTitleWithColor title color = set $ ChartTitle title (Just color) Nothing++-- | Set the chart title with color and font size+setChartTitleWithColorAndFontSize :: String -> Color -> FontSize -> ChartM ()+setChartTitleWithColorAndFontSize title color fontsize =+ set $ ChartTitle title (Just color) (Just fontsize)+ {-| Use it with 'simple', 'text' or 'extended' to specify the encoding. For e.g @@@ -134,7 +181,7 @@ setDataEncoding :: ChartData -> ChartM () setDataEncoding = set -{-| Add data to chart. Make sure you have set the data encoding using +{-| Add data to chart. Make sure you have set the data encoding using 'setDataEncoding' before calling this function, otherwise it may generate gibberish, or throw an error -}@@ -177,17 +224,48 @@ setGrid :: ChartGrid -> ChartM () setGrid = set +-- | Adds a shape marker. Use `makeShapeMarker` smart constructor when calling+-- this function If value of data set index is not specified when using+-- 'makeShapeMarker', it automatically adds a data index to refer to the latest+-- data set+addShapeMarker :: ShapeMarker -> ChartM ()+addShapeMarker marker | (shapeDataSetIdx marker) > -1 = addMarker marker+ | otherwise = do idx <- getDataSetIdx+ let newmarker = marker { shapeDataSetIdx = idx }+ addMarker marker++-- | Adds a range marker. You can use 'makeRangeMarker' smart constructor when+-- calling this function+addRangeMarker :: RangeMarker -> ChartM ()+addRangeMarker marker = addMarker marker++-- | Adds a financial marker. User `makeFinancialMarker` smart constructor when+-- calling this function. If value of data set index is not specified when using+-- 'makeFinancialMarker', it automatically adds a data index to refer to the latest+-- data set+addFinancialMarker :: FinancialMarker -> ChartM ()+addFinancialMarker marker | (financeDataSetIdx marker) > -1 = addMarker marker+ | otherwise = do idx <- getDataSetIdx+ let newmarker = marker { financeDataSetIdx = idx }+ addMarker marker++ -- | Set labels for the chart setLabels :: [String] -> ChartM () setLabels = set . ChartLabels ++-- | Set bar and width spacing+setBarWidthSpacing :: BarChartWidthSpacing -> ChartM ()+setBarWidthSpacing = set+ {- Retrieving Chart Data -} -- | Extracts the data out of the monad and returns a value of type 'Chart' getChartData :: ChartM () -> Chart getChartData = getChartDataFromChartM --- | Extracts the data out of the monad and returns a URL string for the chart +-- | Extracts the data out of the monad and returns a URL string for the chart getChartUrl :: ChartM () -> String getChartUrl = convertToUrl . getChartData @@ -197,20 +275,3 @@ baseURL = "http://chart.apis.google.com/chart?" urlparams = [urlEnc a ++ "=" ++ urlEnc b | (a,b) <- getParams chart] -debugPieChart = getChartUrl $ do setChartSize 640 400- setChartType Pie- setChartTitle "Test"- addChartData ([1,2,3,4,5]::[Int])- addColor "FF0000"- setLegend $ legend ["t1","t2", "t3","t4","t5"]- setLabels $ ["Test 1", "Test 2", "Test 3", "Test 4", "Test 5"]--debugBarChart = getChartUrl $ do setChartSize 640 400- setChartType BarVerticalGrouped- setDataEncoding text- addChartData ([100,200,300,400,500]::[Float])- addChartData ([3,4,5,6,7]::[Float])- addChartData ([4.0,5.0,6.0,7.0,8]::[Float])- addAxis $ makeAxis { axisType = AxisLeft,axisLabels = Just ["0","100"] }- setColors ["FF0000","00FF00","0000FF"]- setLegend $ legend ["Set 1", "Set 2", "Set 3"]
Graphics/GChart/ChartItems.hs view
@@ -1,281 +1,83 @@-{-# LANGUAGE TypeSynonymInstances, NoMonomorphismRestriction #-}+{-# LANGUAGE NoMonomorphismRestriction #-} module Graphics.GChart.ChartItems (- ChartM,- ChartItem(set),- ChartDataEncodable, getChartDataFromChartM, addDataToChart, addColorToChart, addFillToChart, addAxisToChart,+ addMarker,+ getDataSetIdx, getParams ) where import Graphics.GChart.Types import Graphics.GChart.DataEncoding +import Graphics.GChart.ChartItems.Basics+import Graphics.GChart.ChartItems.Data+import Graphics.GChart.ChartItems.Colors+import Graphics.GChart.ChartItems.Styles+import Graphics.GChart.ChartItems.Labels+ import Control.Monad.State import Data.List import Data.Maybe --- Monad-type ChartM a = State Chart a --- | Typeclass abstracting all the fields in a chart-class ChartItem c where- -- set the field- set :: c -> ChartM ()- -- encode the field into string params- encode :: c -> [(String,String)]----- Setting/Encoding Chart Data--updateChart u = do chart <- get- put $ u chart--asList a = [a]- getChartDataFromChartM m = execState m defaultChart --- size-instance ChartItem ChartSize where- set size = updateChart $ \chart -> chart { chartSize = size }-- encode size = asList ("chs", show width ++ "x" ++ show height) where- Size width height = size----- type-instance ChartItem ChartType where- set cType = updateChart $ \chart -> chart { chartType = cType }-- encode cType = asList ("cht",t)- where t = case cType of- Line -> "lc"- LineXY -> "lxy"- Sparklines -> "ls"- Pie -> "p"- Pie3D -> "p3"- PieConcentric -> "pc"- BarHorizontalStacked -> "bhs"- BarVerticalStacked -> "bvs"- BarHorizontalGrouped -> "bhg"- BarVerticalGrouped -> "bvg"- Venn -> "v"- ScatterPlot -> "s"- Radar -> "r"- GoogleOMeter -> "gom"---- title-instance ChartItem ChartTitle where- set title = updateChart $ \chart -> chart { chartTitle = Just title }-- encode title = asList ("chtt", title)----- data--- FIXME just a placeholder for now-instance ChartItem ChartData where- set cData = updateChart $ \chart -> chart { chartData = cData }-- encode datas = asList ("chd", encodeData datas)- where encodeData (Simple d) = encodeSimple d- encodeData (Text d) = encodeText d- encodeData (Extended d) = encodeExtended d---class Num a => ChartDataEncodable a where- addEncodedChartData :: [a] -> ChartData -> ChartData--instance ChartDataEncodable Int where- addEncodedChartData d cd@(Simple old) = Simple $ old ++ [d]- addEncodedChartData d cd@(Extended old) = Extended $ old ++ [d]- addEncodedChartData d _ = error "Invalid type for specified encoding. Use float data"--instance ChartDataEncodable Float where- addEncodedChartData d cd@(Text old) = Text $ old ++ [d]- addEncodedChartData d _ = error "Invalid type for specified encoding. Use int data"- addDataToChart d = do c <- get let old = chartData c set $ addEncodedChartData d old --- color--instance ChartItem ChartColors where- set colors = updateChart $ \chart -> chart { chartColors = Just colors }-- encode (ChartColors colors) = asList ("chco", intercalate "," colors)--- addColorToChart color = do chart <- get let (ChartColors old) = fromMaybe (ChartColors []) $ chartColors chart new = ChartColors $ old ++ [color] set new --- fill--instance ChartItem ChartFills where- set fills = updateChart $ \chart -> chart { chartFills = Just fills }-- encode fills = asList ("chf",intercalate "|" $ map encodeFill fills)---encodeFill (Fill kind fType) = case kind of- Solid color -> intercalate "," [fillType,"s",color]-- LinearGradient angle offsets -> intercalate "," [fillType,- "lg",- show angle,- intercalate "," $ map (\(c,o) -> c ++ "," ++ show o ) offsets]-- LinearStripes angle widths -> intercalate "," [fillType,- "ls",- show angle,- intercalate "," $ map (\(c,w) -> c ++ "," ++ show w) widths]- where fillType = case fType of- Background -> "bg"- Area -> "c"- Transparent -> "a"-- addFillToChart fill = do chart <- get let fills = fromMaybe [] $ chartFills chart- newFills = fills ++ asList fill+ newFills = fills ++ [fill] set newFills --- legend -instance ChartItem ChartLegend where- set legend = updateChart $ \chart -> chart { chartLegend = Just legend } - encode (Legend labels position) = encodeTitle : encodePosition position where- encodeTitle = ("chdl", intercalate "|" labels)- encodePosition Nothing = []- encodePosition (Just p) = let pos = case p of- LegendBottom -> "b"- LegendTop -> "t"- LegendVBottom -> "bv"- LegendVTop -> "tv"- LegendRight -> "r"- LegendLeft -> "l"- in asList ("chdlp",pos)---- AXIS-instance ChartItem ChartAxes where- set axes = updateChart $ \chart -> chart { chartAxes = Just axes }-- encode axes = filter (/= ("","")) $ map (\f -> f axes) [encodeAxesTypes,- encodeAxesLabels,- encodeAxesPositions,- encodeAxesRanges,- encodeAxesStyles]- addAxisToChart axis = do chart <- get let old = fromMaybe [] $ chartAxes chart new = old ++ [axis] set new --convertFieldToString encoder field = intercalate "|" .- map encoder .- filter (\(_,maybeField) -> maybeField /= Nothing) .- indexed . map field--indexed = zip [0..]--encodeFieldToParams fieldParam fieldStr | fieldStr == "" = ("","")- | otherwise = (fieldParam, fieldStr)--encodeAxesTypes axes = ("chxt",a) where- a = intercalate "," $ map toParam axes- toParam axes = case axisType axes of- AxisBottom -> "x"- AxisTop -> "t"- AxisLeft -> "y"- AxisRight -> "r"---- axis labels-strAxisLabels (idx,Just labels) = show idx ++ ":|" ++ intercalate "|" labels--strAxesLabels = convertFieldToString strAxisLabels axisLabels--encodeAxesLabels = encodeFieldToParams "chxl" . strAxesLabels---- axis positions-strAxisPositions (idx, Just positions) = show idx ++ "," ++ intercalate "," (map show positions)--strAxesPositions = convertFieldToString strAxisPositions axisPositions--encodeAxesPositions = encodeFieldToParams "chxp" . strAxesPositions---- axis ranges-strAxisRange (idx, Just range) = show idx ++ "," ++ intercalate "," (encodeRange range)- where encodeRange (Range (start,end) interval) | interval == Nothing = [show start, show end]- | otherwise = [show start, show end, show (fromJust interval)]--strAxesRanges = convertFieldToString strAxisRange axisRange--encodeAxesRanges = encodeFieldToParams "chxr" . strAxesRanges---- axis style-strAxisStyle (idx, Just style) = show idx ++ "," ++ intercalate "," (catMaybes (encodeStyle style))- where encodeStyle (Style a b c d e) = [Just a,- liftM show b,- liftM encodeAlign c,- liftM encodeDrawingControl d,- liftM id e]- encodeAlign c = case c of- AxisStyleLeft -> "-1"- AxisStyleCenter -> "0"- AxisStyleRight -> "1"- encodeDrawingControl d = case d of- DrawLines -> "l"- DrawTicks -> "t"- DrawLinesTicks -> "lt"--strAxesStyles = convertFieldToString strAxisStyle axisStyle--encodeAxesStyles = encodeFieldToParams "chxs" . strAxesStyles----- GRID-instance ChartItem ChartGrid where- set grid = updateChart $ \chart -> chart { chartGrid = Just grid }-- encode grid = asList ("chg", encodeGrid grid) where- encodeGrid (ChartGrid a b c d e f)= intercalate "," $ catMaybes [Just (show a),- Just (show b),- liftM show c,- liftM show d,- liftM show e,- liftM show f]---- LABELS (Pie Chart, Google-O-Meter-instance ChartItem ChartLabels where- set labels = updateChart $ \chart -> chart { chartLabels = Just labels }-- encode (ChartLabels labels) = asList ("chl", intercalate "|" labels)-+getDataSetIdx = do chart <- get+ return $ dataSetLength chart +dataSetLength chart = case chartData chart of+ Simple d -> length d - 1+ Text d -> length d - 1+ Extended d -> length d - 1 +addMarker :: ChartMarker m => m -> ChartM ()+addMarker marker = do chart <- get+ let old = fromMaybe [] $ chartMarkers chart+ new = old ++ [AnyChartMarker marker]+ set new -- URL Conversion--- FIXME : too much boilerplate. Can it be reduced? encodeMaybe Nothing = [("","")] encodeMaybe (Just x) = encode x +-- FIXME : too much boilerplate. Can it be reduced? getParams chart = filter (/= ("","")) $ concat [encode $ chartType chart, encode $ chartSize chart, encode $ chartData chart,- encodeMaybe $ chartTitle chart,- encodeMaybe $ chartColors chart,- encodeMaybe $ chartFills chart,- encodeMaybe $ chartLegend chart,- encodeMaybe $ chartAxes chart,- encodeMaybe $ chartGrid chart,- encodeMaybe $ chartLabels chart]+ encodeMaybe $ chartTitle chart,+ encodeMaybe $ chartColors chart,+ encodeMaybe $ chartFills chart,+ encodeMaybe $ chartLegend chart,+ encodeMaybe $ chartAxes chart,+ encodeMaybe $ chartGrid chart,+ encodeMaybe $ chartMarkers chart,+ encodeMaybe $ chartLabels chart,+ encodeMaybe $ chartMargins chart,+ encodeMaybe $ barChartWidthSpacing chart]
+ Graphics/GChart/ChartItems/Basics.hs view
@@ -0,0 +1,34 @@+module Graphics.GChart.ChartItems.Basics where++import Graphics.GChart.Types+import Graphics.GChart.ChartItems.Util+++-- Chart Size+instance ChartItem ChartSize where+ set size = updateChart $ \chart -> chart { chartSize = size }++ encode size = asList ("chs", show width ++ "x" ++ show height) where+ Size width height = size+++-- Chart Type+instance ChartItem ChartType where+ set cType = updateChart $ \chart -> chart { chartType = cType }++ encode cType = asList ("cht",t)+ where t = case cType of+ Line -> "lc"+ LineXY -> "lxy"+ Sparklines -> "ls"+ Pie -> "p"+ Pie3D -> "p3"+ PieConcentric -> "pc"+ BarHorizontalStacked -> "bhs"+ BarVerticalStacked -> "bvs"+ BarHorizontalGrouped -> "bhg"+ BarVerticalGrouped -> "bvg"+ Venn -> "v"+ ScatterPlot -> "s"+ Radar -> "r"+ GoogleOMeter -> "gom"
+ Graphics/GChart/ChartItems/Colors.hs view
@@ -0,0 +1,40 @@+{-# LANGUAGE TypeSynonymInstances #-}+module Graphics.GChart.ChartItems.Colors where++import Graphics.GChart.Types+import Graphics.GChart.ChartItems.Util++import Data.List(intercalate)+-- Chart Colors+instance ChartItem ChartColors where+ set colors = updateChart $ \chart -> chart { chartColors = Just colors }++ encode (ChartColors colors) = asList ("chco", intercalate "," colors)++-- TODO: Fill Area++-- Chart Fills+instance ChartItem ChartFills where+ set fills = updateChart $ \chart -> chart { chartFills = Just fills }++ encode fills = asList ("chf",intercalate "|" $ map encodeFill fills)+++encodeFill (Fill kind fType) = case kind of+ Solid color -> intercalate "," [fillType,"s",color]++ LinearGradient angle offsets -> intercalate "," [fillType,+ "lg",+ show angle,+ intercalate "," $ map (\(c,o) -> c ++ "," ++ show o ) offsets]++ LinearStripes angle widths -> intercalate "," [fillType,+ "ls",+ show angle,+ intercalate "," $ map (\(c,w) -> c ++ "," ++ show w) widths]+ where fillType = case fType of+ Background -> "bg"+ Area -> "c"+ Transparent -> "a"++
+ Graphics/GChart/ChartItems/Data.hs view
@@ -0,0 +1,24 @@+module Graphics.GChart.ChartItems.Data where++import Graphics.GChart.Types+import Graphics.GChart.ChartItems.Util++import Graphics.GChart.DataEncoding++-- Chart Data+instance ChartItem ChartData where+ set cData = updateChart $ \chart -> chart { chartData = cData }++ encode datas = asList ("chd", encodeData datas)+ where encodeData (Simple d) = encodeSimple d+ encodeData (Text d) = encodeText d+ encodeData (Extended d) = encodeExtended d++instance ChartDataEncodable Int where+ addEncodedChartData d cd@(Simple old) = Simple $ old ++ [d]+ addEncodedChartData d cd@(Extended old) = Extended $ old ++ [d]+ addEncodedChartData d _ = error "Invalid type for specified encoding. Use float data"++instance ChartDataEncodable Float where+ addEncodedChartData d cd@(Text old) = Text $ old ++ [d]+ addEncodedChartData d _ = error "Invalid type for specified encoding. Use int data"
+ Graphics/GChart/ChartItems/Labels.hs view
@@ -0,0 +1,117 @@+{-# LANGUAGE TypeSynonymInstances #-}+module Graphics.GChart.ChartItems.Labels where++import Graphics.GChart.Types+import Graphics.GChart.ChartItems.Util++import Data.List(intercalate)+import Control.Monad(liftM)+import Data.Maybe(catMaybes, fromJust)++-- Chart Title+instance ChartItem ChartTitle where+ set title = updateChart $ \chart -> chart { chartTitle = Just title }++ encode title = ("chtt", titleStr title) : [("chts", chts) | chts /= ""]+ where chts = encodeColorAndFontSize title++encodeColorAndFontSize title =+ intercalate "," $ catMaybes [titleColor title,+ liftM show . titleFontSize $ title]+++-- Chart Legend+instance ChartItem ChartLegend where+ set legend = updateChart $ \chart -> chart { chartLegend = Just legend }++ encode (Legend labels position) = encodeTitle : encodePosition position where+ encodeTitle = ("chdl", intercalate "|" labels)+ encodePosition Nothing = []+ encodePosition (Just p) = let pos = case p of+ LegendBottom -> "b"+ LegendTop -> "t"+ LegendVBottom -> "bv"+ LegendVTop -> "tv"+ LegendRight -> "r"+ LegendLeft -> "l"+ in asList ("chdlp",pos)++-- Chart Labels (Pie Chart, Google-O-Meter)+instance ChartItem ChartLabels where+ set labels = updateChart $ \chart -> chart { chartLabels = Just labels }++ encode (ChartLabels labels) = asList ("chl", intercalate "|" labels)++-- Axis Styles and Labels+instance ChartItem ChartAxes where+ set axes = updateChart $ \chart -> chart { chartAxes = Just axes }++ encode axes = filter (/= ("","")) $ map (\f -> f axes) [encodeAxesTypes,+ encodeAxesLabels,+ encodeAxesPositions,+ encodeAxesRanges,+ encodeAxesStyles]++convertFieldToString encoder field = intercalate "|" .+ map encoder .+ filter (\(_,maybeField) -> maybeField /= Nothing) .+ indexed . map field++indexed = zip [0..]++encodeFieldToParams fieldParam fieldStr | fieldStr == "" = ("","")+ | otherwise = (fieldParam, fieldStr)++encodeAxesTypes axes = ("chxt",a) where+ a = intercalate "," $ map toParam axes+ toParam axes = case axisType axes of+ AxisBottom -> "x"+ AxisTop -> "t"+ AxisLeft -> "y"+ AxisRight -> "r"++-- axis labels+strAxisLabels (idx,Just labels) = show idx ++ ":|" ++ intercalate "|" labels++strAxesLabels = convertFieldToString strAxisLabels axisLabels++encodeAxesLabels = encodeFieldToParams "chxl" . strAxesLabels++-- axis positions+strAxisPositions (idx, Just positions) = show idx ++ "," ++ intercalate "," (map show positions)++strAxesPositions = convertFieldToString strAxisPositions axisPositions++encodeAxesPositions = encodeFieldToParams "chxp" . strAxesPositions++-- axis ranges+strAxisRange (idx, Just range) = show idx ++ "," ++ intercalate "," (encodeRange range)+ where encodeRange (Range (start,end) interval) | interval == Nothing = [show start, show end]+ | otherwise = [show start, show end, show (fromJust interval)]++strAxesRanges = convertFieldToString strAxisRange axisRange++encodeAxesRanges = encodeFieldToParams "chxr" . strAxesRanges++-- axis style+strAxisStyle (idx, Just style) = show idx ++ "," ++ intercalate "," (catMaybes (encodeStyle style))+ where encodeStyle (Style a b c d e) = [Just a,+ liftM show b,+ liftM encodeAlign c,+ liftM encodeDrawingControl d,+ liftM id e]+ encodeAlign c = case c of+ AxisStyleLeft -> "-1"+ AxisStyleCenter -> "0"+ AxisStyleRight -> "1"+ encodeDrawingControl d = case d of+ DrawLines -> "l"+ DrawTicks -> "t"+ DrawLinesTicks -> "lt"++strAxesStyles = convertFieldToString strAxisStyle axisStyle++encodeAxesStyles = encodeFieldToParams "chxs" . strAxesStyles+++-- TODO: Data Point Labels
+ Graphics/GChart/ChartItems/Styles.hs view
@@ -0,0 +1,110 @@+{-# LANGUAGE TypeSynonymInstances #-}+module Graphics.GChart.ChartItems.Styles where++import Graphics.GChart.Types+import Graphics.GChart.ChartItems.Util++import Data.List(intercalate)+import Control.Monad(liftM)+import Data.Maybe(catMaybes)++-- Bar Width and Spacing+instance ChartItem BarChartWidthSpacing where+ set widthspacing = updateChart $ \chart -> chart { barChartWidthSpacing = Just widthspacing }++ encode (Nothing , Nothing) = error "Invalid Values"+ encode (Just Automatic , Just (Relative _)) = error "Invalid Values"+ encode (Just (BarWidth _) , Just (Relative _)) = error "Invalid Values"+ encode (Just Automatic , Nothing) = asList ("chbh", "a")+ encode (Just Automatic , Just (Fixed (b,g))) = asList ("chbh", intercalate "," ["a",show b, show g])+ encode (Just (BarWidth bw), Just (Fixed (b,g))) = asList ("chbh", intercalate "," [show bw, show b ,show g])+ encode (Just (BarWidth bw), Nothing) = asList ("chbh", show bw)+ encode (Nothing , Just (Relative (b,g))) = asList ("chbh", intercalate "," ["r", show b, show g])+ encode (_,_) = error "Invalid Values"++-- TODO: Bar Chart Zero Line++-- Chart Margins+instance ChartItem ChartMargins where+ set margins = updateChart $ \chart -> chart { chartMargins = Just margins }++ encode (ChartMargins a b c d e) = asList ("chma",intercalate "|" $ cm:[lm])+ where cm = intercalate "," [show a, show b, show c, show d]+ lm = case e of+ Just (x,y) -> show x ++ "," ++ show y+ _ -> ""++-- TODO: Line Styles++-- Grid Lines+instance ChartItem ChartGrid where+ set grid = updateChart $ \chart -> chart { chartGrid = Just grid }++ encode grid = asList ("chg", encodeGrid grid) where+ encodeGrid (ChartGrid a b c d e f)= intercalate "," $ catMaybes [Just (show a),+ Just (show b),+ liftM show c,+ liftM show d,+ liftM show e,+ liftM show f]+-- ChartItem instance for ChartMarkers+instance ChartItem ChartMarkers where+ set markers = updateChart $ \chart -> chart { chartMarkers = Just markers }+ encode markers = asList ("chm",intercalate "|" $ map encodeChartMarker markers)++-- Shape Markers+instance ChartMarker ShapeMarker where+ encodeChartMarker marker = optionalat ++ (intercalate "," $ [marker_type, color, idx, datapoint, size] ++ [show priority | priority /= 0]) where+ marker_type = case shapeType marker of+ ShapeArrow -> "a"+ ShapeCross -> "c"+ ShapeDiamond -> "d"+ ShapeCircle -> "o"+ ShapeSquare -> "s"+ VerticalLine -> "v"+ VerticalLineFull -> "V"+ HorizontalLine -> "h"+ ShapeX -> "x"++ color = shapeColor marker++ datapoint = case shapeDataPoint marker of+ DataPoint x -> show x+ DataPointEvery -> "-1"+ DataPointEveryN x -> "-" ++ show x+ DataPointEveryNRange (x,y) n -> intercalate ":"$ map show [x,y,n]+ DataPointXY (x,y) -> show x ++ ":" ++ show y++ idx = show $ shapeDataSetIdx marker+ size = show $ shapeSize marker+ priority = shapePriority marker++ optionalat = [ '@' | isDataPointXY $ shapeDataPoint marker ]+ isDataPointXY (DataPointXY _) = True+ isDataPointXY _ = False+-- Range Markers+instance ChartMarker RangeMarker where+ encodeChartMarker marker = intercalate "," [rangetype, color,"0",x,y] where+ rangetype = case rangeMarkerType marker of+ RangeMarkerHorizontal -> "r"+ RangeMarkerVertical -> "R"++ color = rangeMarkerColor marker+ x = show.fst $ rangeMarkerRange marker+ y = show.snd $ rangeMarkerRange marker++-- Financial Markers+instance ChartMarker FinancialMarker where+ encodeChartMarker marker = (intercalate "," $ ["F", color, idx, datapoint, size] ++ [show priority | priority /= 0]) where+ color = financeColor marker++ datapoint = case financeDataPoint marker of+ DataPoint x -> show x+ DataPointEvery -> "-1"+ DataPointEveryN x -> "-" ++ show x+ DataPointEveryNRange (x,y) n -> error "Invalid value for finanical marker"+ DataPointXY (x,y) -> show x ++ ":" ++ show y++ idx = show $ financeDataSetIdx marker+ size = show $ financeSize marker+ priority = financePriority marker
+ Graphics/GChart/ChartItems/Util.hs view
@@ -0,0 +1,11 @@+{-# LANGUAGE TypeSynonymInstances #-}+module Graphics.GChart.ChartItems.Util (+ updateChart,asList+) where++import Control.Monad.State++updateChart u = do chart <- get+ put $ u chart++asList a = [a]
Graphics/GChart/DataEncoding.hs view
@@ -2,7 +2,7 @@ import Graphics.GChart.Types -import Data.List+import Data.List(intercalate) import Data.Char (chr, ord) import Numeric (showHex) @@ -17,13 +17,18 @@ | i >= 52 && i <= 61 = chr (ord '0' + (i - 52)) | otherwise = '_' -+encSimpleReverse :: Char -> Int+encSimpleReverse c | ord c >= ord 'A' && ord c <= ord 'Z' = (ord c - ord 'A')+ | ord c >= ord 'a' && ord c <= ord 'z' = 26 + (ord c - ord 'a')+ | ord c >= ord '0' && ord c <= ord '9' = 52 + (ord c - ord '0')+ | otherwise = -1+ encodeText datas = "t:" ++ intercalate "|" (map encData datas) where encData = intercalate "," . map encDatum encDatum i | i >= 0 && i <= 100 = showDecimal i | otherwise = "-1" showDecimal :: Float -> String- showDecimal i | ((makeFloat (truncate i)) - i) == 0 = show $ truncate i+ showDecimal i | makeFloat (truncate i) - i == 0 = show $ truncate i | otherwise = show (fromIntegral (round (i * 10.0)) / 10.0) makeFloat i = fromIntegral i :: Float
Graphics/GChart/Types.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE ExistentialQuantification #-} {-| This module contains the Haskell data model for the Google Chart API. Details about the parameters can be found on the Google Chart API website :@@ -13,31 +14,24 @@ - Data Scaling <http://code.google.com/apis/chart/formats.html#data_scaling> -- Shape markers <http://code.google.com/apis/chart/styles.html#shape_markers>--- Range markers <http://code.google.com/apis/chart/styles.html#range_markers>--- Financial markers <http://code.google.com/apis/chart/styles.html#financial_markers>--- Line Styles <http://code.google.com/apis/chart/styles.html#line_styles>--- Fill area <http://code.google.com/apis/chart/colors.html#fill_area_marker>--- Bar width and spacing <http://code.google.com/apis/chart/styles.html#bar_width>- - Bar chart zero line <http://code.google.com/apis/chart/styles.html#zero_line> - Data point labels <http://code.google.com/apis/chart/labels.html#data_point_labels> -- Chart margins <http://code.google.com/apis/chart/styles.html#chart_margins>+- Fill area <http://code.google.com/apis/chart/colors.html#fill_area_marker> -- Chart title color and font customisation <http://code.google.com/apis/chart/labels.html#chart_title>+- Line Styles <http://code.google.com/apis/chart/styles.html#line_styles> - Pie chart orientation <http://code.google.com/apis/chart/types.html#pie_charts> -} module Graphics.GChart.Types (++ -- * Typeclasses+ -- | Typeclasses for abstraction+ ChartM, ChartItem(set,encode), ChartDataEncodable(addEncodedChartData),+ -- * Chart data -- | This type represents the Chart Chart(..),@@ -55,35 +49,55 @@ -- * Optional Parameters -- | All other parameters are optional - -- ** Chart Colors+ -- ** Colors++ -- *** Chart Colors ChartColors(..), Color, - -- ** Solid Fill, Linear Gradient, Linear Stripes+ -- ** Chart Fills : Solid Fill, Linear Gradient, Linear Stripes ChartFills, Fill(..), FillKind(..), FillType(..), Offset, Width, Angle,- -- ** Chart Title- ChartTitle, - -- ** Chart Legend+ -- ** Labels++ -- *** Chart Title+ ChartTitle(..),++ -- *** Chart Legend ChartLegend(..), LegendPosition(..), - -- ** Axis styles and labels+ -- *** Pie chart and Google-o-meter labels+ ChartLabels(..),++ -- *** Axis styles and labels ChartAxes, Axis(..), AxisType(..), AxisLabel, AxisPosition, FontSize, AxisRange(..), AxisStyle(..), DrawingControl(..), AxisStyleAlignment(..), - -- ** Grid Lines+ -- ** Styles++ -- *** Bar width and spacing+ BarChartWidthSpacing(..), BarWidth(..), BarGroupSpacing(..),++ -- *** Chart Margins+ ChartMargins(..),++ -- *** Grid Lines ChartGrid(..), - -- ** Pie chart and Google-o-meter labels- ChartLabels(..),+ -- *** Shape, Range and Financial Markers+ AnyChartMarker(..), ChartMarker(..), ChartMarkers,+ ShapeType(..), MarkerDataPoint(..), ShapeMarker(..),+ RangeMarkerType(..), RangeMarker(..), FinancialMarker(..), -- * Default Values {-| These functions return default values for complex parameters, which can be used as starting points to construct parameters when creating charts. -}- defaultChart, defaultAxis, defaultGrid-) where + defaultChart, defaultAxis, defaultGrid, defaultSpacing, defaultShapeMarker,+ defaultRangeMarker, defaultFinancialMarker+) where +import Control.Monad.State -- | Size of the chart. width and height specified in pixels data ChartSize = Size Int Int deriving Show@@ -109,37 +123,41 @@ -- | Title of the chart -- | <http://code.google.com/apis/chart/labels.html#chart_title>-type ChartTitle = String-+data ChartTitle =+ ChartTitle {+ titleStr ::String -- ^ Title+ , titleColor :: Maybe Color -- ^ Title Color+ , titleFontSize :: Maybe FontSize -- ^ Title Font Size+ } deriving Show -- | Chart data along with encoding. XY data for is encoded a pair of -- | consecutive data sets -- | <http://code.google.com/apis/chart/formats.html> data ChartData- = Simple [[Int]] -- ^ Simple - lets you specify integer values from 0—61, inclusive- | Text [[Float]] -- ^ Text - supports floating point numbers from 0—100, inclusive- | Extended [[Int]] -- ^ Extended - lets you specify integer values from 0—4095, inclusive+ = Simple [[Int]] -- ^ lets you specify integer values from 0-61, inclusive+ | Text [[Float]] -- ^ supports floating point numbers from 0-100, inclusive+ | Extended [[Int]] -- ^ lets you specify integer values from 0-4095, inclusive deriving Show -- | Color data specified as a hex string type Color = String -- | Chart colors specified as a list of 'Color' values for each data point.--- | <http://code.google.com/apis/chart/colors.html#chart_colors>+-- <http://code.google.com/apis/chart/colors.html#chart_colors> data ChartColors = ChartColors [Color] deriving Show -- | Specifies the angle of the gradient between 0 (horizontal) and 90--- | (vertical). Applicable to 'LinearGradient' and 'LinearStripes'+-- (vertical). Applicable to 'LinearGradient' and 'LinearStripes' type Angle = Float -- | Specifies at what point the color is pure. In this parameter, 0 specifies--- | the right-most chart position and 1 specifies the left-most chart--- | position. Applicable to 'LinearGradient'+-- the right-most chart position and 1 specifies the left-most chart+-- position. Applicable to 'LinearGradient' type Offset = Float -- | Width of the stripe. must be between 0 and 1, where 1 is the full width of--- | the chart+-- the chart type Width = Float -- | Specifies the kind of fill@@ -173,11 +191,11 @@ deriving Show -- | Specifies a chart legend--- | <http://code.google.com/apis/chart/labels.html#chart_legend>+-- <http://code.google.com/apis/chart/labels.html#chart_legend> data ChartLegend = Legend [String] (Maybe LegendPosition) deriving Show -- | Type of 'Axis'--- | <http://code.google.com/apis/chart/labels.html#axis_type>+-- <http://code.google.com/apis/chart/labels.html#axis_type> data AxisType = AxisBottom -- ^ Bottom x-axis | AxisTop -- ^ Top x-axis@@ -186,7 +204,7 @@ deriving Show -- | 'Axis' Labels.--- | <http://code.google.com/apis/chart/labels.html#axis_labels>+-- <http://code.google.com/apis/chart/labels.html#axis_labels> type AxisLabel = String {-| 'Axis' Label Positions. <http://code.google.com/apis/chart/labels.html#axis_label_positions>@@ -208,7 +226,7 @@ -} data AxisRange = Range (Float,Float) (Maybe Float) deriving (Show,Eq) --- | Font size in pixels. Applicable to 'AxisStyle'+-- | Font size in pixels. Applicable to 'AxisStyle' and 'ChartTitle' type FontSize = Int -- | Alignment of 'Axis' labels. Applies to 'AxisStyle'@@ -226,7 +244,7 @@ deriving (Show,Eq) -- | Specify 'Axis' style--- | <http://code.google.com/apis/chart/labels.html#axis_styles>+-- <http://code.google.com/apis/chart/labels.html#axis_styles> data AxisStyle = Style { axisColor :: Color, axisFontSize :: Maybe FontSize, axisStyleAlign :: Maybe AxisStyleAlignment,@@ -234,7 +252,7 @@ tickMarkColor :: Maybe Color } deriving (Show,Eq) -- | Specify an axis for chart.--- | <http://code.google.com/apis/chart/labels.html#axis_styles>+-- <http://code.google.com/apis/chart/labels.html#axis_styles> data Axis = Axis { axisType :: AxisType, axisLabels :: Maybe [AxisLabel], axisPositions :: Maybe [AxisPosition],@@ -245,7 +263,7 @@ type ChartAxes = [Axis] -- | Grid Lines for Chart--- | <http://code.google.com/apis/chart/styles.html#grid>+-- <http://code.google.com/apis/chart/styles.html#grid> data ChartGrid = ChartGrid { xAxisStep :: Float -- ^ x-axis step size (0-100)@@ -256,56 +274,190 @@ , yOffset :: Maybe Float -- ^ y axis offset } deriving Show +-- | Shape type of 'ShapeMarker'+data ShapeType = ShapeArrow -- ^ Arrow+ | ShapeCross -- ^ Cross+ | ShapeDiamond -- ^ Diamond+ | ShapeCircle -- ^ Circle+ | ShapeSquare -- ^ Square+ | VerticalLine -- ^ Vertical line from x-axis to data point+ | VerticalLineFull -- ^ Vertical line across the chart+ | HorizontalLine -- ^ Horizontal line across the chart+ | ShapeX -- ^ X shape+ deriving Show -{--data ShapeType = ShapeArrow | ShapeCross | ShapeDiamond | ShapeCircle | ShapeSquare | VerticalLine | VerticalLineFull | HorizontalLine | ShapeX deriving Show-data ShapeDataPoint = DataPoint Int | DataPointEvery | DataPointEveryN Int | DataPointEveryNRange Int Int Int | DataPointXY Float Float deriving Show-data RangeMarkerType = RangeMarkerHorizontal | RangeMarkerVertical deriving Show +-- | Data point value of `ShapeMarker`+data MarkerDataPoint =+ DataPoint Float -- ^ A specific data point in the dataset. Use a+ -- decimal value to interpolate between two points -data ChartMarker = ShapeMarker { shapeType :: ShapeType,- shapeColor :: Color,- shapeDataSetIdx :: Int,- shapeDataPoint :: ShapeDataPoint,- shapeSize :: Int,- shapePriority :: Int- } |- RangeMarker { rangeType :: RangeMarkerType,- rangeColor :: Color,- rangeSpan :: (Float,Float) } deriving Show+ | DataPointEvery -- ^ Draw a marker on each data point -type ChartMarkers = [ChartMarker]--}+ | DataPointEveryN Int -- ^ Draw a marker on every n-th data point + | DataPointEveryNRange (Int,Int) Int -- ^ @(x,y), n@ draw a marker on every n-th+ -- data point in a range, where x is the+ -- first data point in the range, and y is+ -- the last data point in the range++ | DataPointXY (Float,Float) -- ^ draw a marker at a specific point+ -- (x,y). Specify the coordinates as floating+ -- point values, where 0:0 is the bottom left+ -- corner of the chart, 0.5:0.5 is the center of+ -- the chart, and 1:1 is the top right corner of+ -- the chart+ deriving Show++-- | Shape Marker+data ShapeMarker =+ SM { shapeType :: ShapeType -- ^ Shape type+ , shapeColor :: Color -- ^ Shape Marker color+ , shapeDataSetIdx :: Int -- ^ Data Set Index+ , shapeDataPoint :: MarkerDataPoint -- ^ Data point value+ , shapeSize :: Int -- ^ Size in pixels+ , shapePriority :: Int -- ^ Priority of drawing. Can be one of -1,0,1+ } deriving Show++-- | 'RangeMarker' type+data RangeMarkerType = RangeMarkerHorizontal -- ^ horizontal range+ | RangeMarkerVertical -- ^ vertical range+ deriving Show++-- | Range Marker+data RangeMarker =+ RM { rangeMarkerType :: RangeMarkerType -- ^ Range marker type+ , rangeMarkerColor :: Color -- ^ Range marker color+ , rangeMarkerRange :: (Float, Float) -- ^ @(start,end) range. @For+ -- horizontal range markers, the+ -- (start,end) value is a position on+ -- the y-axis, where 0.00 is the+ -- bottom of the chart, and 1.00 is+ -- the top of the chart. For vertical+ -- range markers, the (start,end)+ -- value is a position on the x-axis,+ -- where 0.00 is the left of the+ -- chart, and 1.00 is the right of the+ -- chart.+ } deriving Show+++-- | Financial Marker, for line charts and vertical bar charts+data FinancialMarker =+ FM { financeColor :: Color -- ^ Finance Marker color+ , financeDataSetIdx :: Int -- ^ Data Set Index+ , financeDataPoint :: MarkerDataPoint -- ^ Data point value+ , financeSize :: Int -- ^ Size in pixels+ , financePriority :: Int -- ^ Priority of drawing. Can be one of -1,0,1+ } deriving Show++-- | Typeclass to abstract over different chart markers+class Show a => ChartMarker a where+ encodeChartMarker :: a -> String+ encodeChartMarker a = ""++-- | Data type to abstract over all kinds of ChartMarker+data AnyChartMarker = forall w. ChartMarker w => AnyChartMarker w++instance ChartMarker AnyChartMarker where+ encodeChartMarker (AnyChartMarker m) = encodeChartMarker m++instance Show AnyChartMarker where+ show (AnyChartMarker m) = show m++type ChartMarkers = [AnyChartMarker]+ -- | Labels for Pie Chart and Google-o-meter.--- | Specify a list with a single label for Google-o-meter+-- Specify a list with a single label for Google-o-meter data ChartLabels = ChartLabels [String] deriving Show +-- | Chart Margins. All margin values specified are the minimum margins around+-- the plot area, in pixels.+-- <http://code.google.com/apis/chart/styles.html#chart_margins>+data ChartMargins =+ ChartMargins {+ leftMargin :: Int -- ^ Left margin around plot area+ , rightMargin :: Int -- ^ Right margin around plot area+ , topMargin :: Int -- ^ Top margin around plot area+ , bottomMargin :: Int -- ^ Bottom margin around plot area+ , legendMargins :: Maybe (Int,Int) -- ^ Minimum width and height of legend+ } deriving Show+++-- | Bar Width+data BarWidth = Automatic -- ^ Automatic resizing+ | BarWidth Int -- ^ Bar width in pixels+ deriving Show++-- | Bar and Group Spacing+data BarGroupSpacing = Fixed (Int, Int) -- ^ Fixed spacing values in pixels+ | Relative (Float,Float) -- ^ Relative values as percentages+ deriving Show+++-- | Bar Width and Spacing.+type BarChartWidthSpacing = (Maybe BarWidth, Maybe BarGroupSpacing)+ -- | Data type for the chart-data Chart = Chart { chartSize :: ChartSize,- chartType :: ChartType,- chartData :: ChartData,- chartTitle :: Maybe ChartTitle,- chartColors :: Maybe ChartColors,- chartFills :: Maybe ChartFills,- chartLegend :: Maybe ChartLegend,- chartAxes :: Maybe ChartAxes,- chartGrid :: Maybe ChartGrid,- -- chartMarkers :: Maybe ChartMarkers,- chartLabels :: Maybe ChartLabels } deriving Show+data Chart =+ Chart {+ chartSize :: ChartSize+ , chartType :: ChartType+ , chartData :: ChartData+ , chartTitle :: Maybe ChartTitle+ , chartColors :: Maybe ChartColors+ , chartFills :: Maybe ChartFills+ , chartLegend :: Maybe ChartLegend+ , chartAxes :: Maybe ChartAxes+ , chartMarkers :: Maybe ChartMarkers+ , chartGrid :: Maybe ChartGrid+ , chartLabels :: Maybe ChartLabels+ , chartMargins :: Maybe ChartMargins+ , barChartWidthSpacing :: Maybe BarChartWidthSpacing+ } deriving Show ++-- | Chart monad which wraps a 'State' monad in turn+-- to keep track of the chart state and make it convenient+-- to update it+type ChartM a = State Chart a++-- | Typeclass abstracting all the fields in a chart+class ChartItem c where+ -- | sets the item in a chart+ set :: c -> ChartM ()++ -- | encode the field into a list string params that can+ -- then be converted into a query string URL+ encode :: c -> [(String,String)]+++-- | Typeclass abstracting the numeric data that can be encoded.+-- This helps in passing Int and Float values as chart data, which+-- are then encoded correctly+class Num a => ChartDataEncodable a where+ -- | Adds the array of numeric data to the existing chart data.+ -- Throws a error if the data passed in doesnt match with the+ -- current data encoding format.+ addEncodedChartData :: [a] -> ChartData -> ChartData++ -- | Default value for a chart-defaultChart = Chart { chartSize = Size 320 200,- chartType = Line,- chartData = Simple [],- chartTitle = Nothing,- chartColors = Nothing,- chartFills = Nothing,- chartLegend = Nothing,- chartAxes = Nothing,- chartGrid = Nothing,- -- chartMarkers = Nothing,- chartLabels = Nothing }+defaultChart =+ Chart { chartSize = Size 320 200,+ chartType = Line,+ chartData = Simple [],+ chartTitle = Nothing,+ chartColors = Nothing,+ chartFills = Nothing,+ chartLegend = Nothing,+ chartAxes = Nothing,+ chartGrid = Nothing,+ chartLabels = Nothing,+ chartMargins = Nothing,+ chartMarkers = Nothing,+ barChartWidthSpacing = Nothing+ } -- | Default value for an axis defaultAxis = Axis { axisType = AxisBottom,@@ -329,12 +481,25 @@ xOffset = Nothing, yOffset = Nothing } +-- | Default value for bar and group spacing in bar chart+defaultSpacing = Fixed (4,8) -{--defaultShapeMarker = ShapeMarker { shapeType = ShapeCircle,- shapeColor = "0000DD",- shapeDataSetIdx = 0,- shapeDataPoint = DataPointEvery,- shapeSize = 5,- shapePriority = 0 }--}+-- | Default value of a shape marker. Make sure you change the value of @shapeDataSetIdx@+defaultShapeMarker = SM { shapeType = ShapeCircle,+ shapeColor = "0000DD",+ shapeDataSetIdx = -1,+ shapeDataPoint = DataPointEvery,+ shapeSize = 5,+ shapePriority = 0 }++-- | Default value of range marker+defaultRangeMarker = RM { rangeMarkerType = RangeMarkerHorizontal,+ rangeMarkerColor = "0000DD",+ rangeMarkerRange = (0.0,1.0) }++-- | Default value of a financial marker. Make sure you change the value of @financeDataSetIdx@+defaultFinancialMarker = FM { financeColor = "0000DD",+ financeDataSetIdx = -1,+ financeDataPoint = DataPointEvery,+ financeSize = 5,+ financePriority = 0 }
README.md view
@@ -17,16 +17,17 @@ ## Documentation -* [API functions reference](http://deepak.jois.name/code/hs-gchart/Graphics-GChart.html)+* [API functions reference](http://hackage.haskell.org/package/hs-gchart) -* [Haskell data types reference](http://deepak.jois.name/code/hs-gchart/Graphics-GChart-Types.html), along with pending features and chart types+* [Haskell data types reference](http://hackage.haskell.org/packages/archive/hs-gchart/0.1.1/doc/html/Graphics-GChart-Types.html) * [Google Chart API] ## Getting Started These examples below are available in `examples/Examples.hs` in the source-tarball. All examples are taken from [this article](http://24ways.org/2007/tracking-christmas-cheer-with-google-charts)+tarball. Look at the source file for more Some examples are taken from [this+article](http://24ways.org/2007/tracking-christmas-cheer-with-google-charts) For examples 1 and 2, the following code is common @@ -153,3 +154,36 @@  +### Example 5 : Scatter Plot with Shape Markers++The code below ++ scatterPlotWithMarkers = getChartUrl $ do setChartSize 200 125+ setChartType ScatterPlot+ setDataEncoding simple+ addChartDataXY dataSeries5+ addAxis $ makeAxis { axisType = AxisBottom,+ axisLabels = Just $ blanks 1 ++ ["1","2","3","4","5"] }+ addAxis $ makeAxis { axisType = AxisLeft,+ axisLabels = Just $ blanks 1 ++ ["50","100"] }+ setGrid $ makeGrid { xAxisStep = 20, yAxisStep = 25 }+ + addShapeMarker $ makeShapeMarker { shapeType = ShapeSquare+ , shapeColor = "ff0000"+ , shapeSize = 10 }+ + -- Reverse engineering sample data from webpage+ dataSeries5 :: [(Int,Int)]+ dataSeries5 = zip xseries yseries where+ xseries = map encSimpleReverse "984sttvuvkQIBLKNCAIipr3z9"+ yseries = map encSimpleReverse "DEJPgq0uov17_zwopQOD"+ + encSimpleReverse :: Char -> Int+ encSimpleReverse c | ord c >= ord 'A' && ord c <= ord 'Z' = (ord c - ord 'A')+ | ord c >= ord 'a' && ord c <= ord 'z' = 26 + (ord c - ord 'a')+ | ord c >= ord '0' && ord c <= ord '9' = 52 + (ord c - ord '0')+ | otherwise = -1++Generates the following chart++
examples/Examples.hs view
@@ -1,5 +1,5 @@ import Graphics.GChart-+import Data.Char(ord) {- Some examples to demonstrate usage of GChart.@@ -34,7 +34,7 @@ axisLabels = Just labelSeries1 } addColor "00AF33" -linexyGraph1 = +linexyGraph1 = getChartUrl $ do setChartSize 800 300 setChartType LineXY setDataEncoding text@@ -49,7 +49,7 @@ axisLabels = Just $ ["Dec 1st"] ++ blanks 4 ++ ["6th"] ++ blanks 18 ++ ["25th","26th"] ++ blanks 4 ++ ["Dec 31st"] } addChartDataXY dataSeries2 -linexyGraph2 = +linexyGraph2 = getChartUrl $ do setChartSize 800 300 setChartType LineXY setDataEncoding text@@ -73,9 +73,49 @@ setLegend $ legendWithPosition ["2006","2007"] LegendRight +bargraph2 = getChartUrl $ do setChartSize 600 300+ setChartType BarHorizontalGrouped+ setDataEncoding simple+ addChartData dataSeries1+ setChartTitleWithColor "Food and Drink Consumed Christmas 2007" "00AF33"+ addAxis $ makeAxis { axisType = AxisBottom }+ addAxis $ makeAxis { axisType = AxisLeft,+ axisLabels = Just labelSeries1 }+ addColor "00AF33" +bargraphAutoSpacing = getChartUrl $ do setChartSize 190 125+ setChartType BarVerticalGrouped+ setDataEncoding simple+ addChartData ([10,15,20,25,30]::[Int])+ addChartData ([13,5,6,34,12]::[Int])+ setColors ["4d89f9","000000"]+ setBarWidthSpacing $ automatic ++bargraphRelativeSpacing = getChartUrl $ do setChartSize 190 125+ setChartType BarVerticalGrouped+ setDataEncoding simple+ addChartData ([10,15,20,25,30]::[Int])+ addChartData ([13,5,6,34,12]::[Int])+ setColors ["4d89f9","000000"]+ setBarWidthSpacing $ relative 0.5 1.5+++scatterPlotWithMarkers = getChartUrl $ do setChartSize 200 125+ setChartType ScatterPlot+ setDataEncoding simple+ addChartDataXY dataSeries5+ addAxis $ makeAxis { axisType = AxisBottom,+ axisLabels = Just $ blanks 1 ++ ["1","2","3","4","5"] }+ addAxis $ makeAxis { axisType = AxisLeft,+ axisLabels = Just $ blanks 1 ++ ["50","100"] }+ setGrid $ makeGrid { xAxisStep = 20, yAxisStep = 25 }++ addShapeMarker $ makeShapeMarker { shapeType = ShapeSquare+ , shapeColor = "ff0000"+ , shapeSize = 10 }+ blanks x = take x $ repeat "" dataSeries1 :: [Int]@@ -90,6 +130,11 @@ dataSeries4 :: [(Float,Float)] dataSeries4 = zip [0,10,16.7,26.7,33.3] [50,10,30,55,60] +dataSeries5 :: [(Int,Int)]+dataSeries5 = zip xseries yseries where+ xseries = map encSimpleReverse "984sttvuvkQIBLKNCAIipr3z9"+ yseries = map encSimpleReverse "DEJPgq0uov17_zwopQOD"+ labelSeries1 = ["Egg nog", "Christmas Ham", "Milk (not including egg nog)",@@ -100,7 +145,17 @@ "Various Other Foods", "Snacks"] +encSimpleReverse :: Char -> Int+encSimpleReverse c | ord c >= ord 'A' && ord c <= ord 'Z' = (ord c - ord 'A')+ | ord c >= ord 'a' && ord c <= ord 'z' = 26 + (ord c - ord 'a')+ | ord c >= ord '0' && ord c <= ord '9' = 52 + (ord c - ord '0')+ | otherwise = -1+ main = do putStrLn christmasPie putStrLn barGraph putStrLn linexyGraph1 putStrLn linexyGraph2+ putStrLn bargraph2+ putStrLn bargraphAutoSpacing+ putStrLn bargraphRelativeSpacing+ putStrLn scatterPlotWithMarkers
hs-gchart.cabal view
@@ -1,6 +1,11 @@ name: hs-gchart-version: 0.1.1+version: 0.2 synopsis: Haskell wrapper for the Google Chart API+description:+ This module is a wrapper around the Google Chart API. It exposes a rich+ set of Haskell data types to specify your chart data, which can then be+ converted into a URL that generates the PNG image of the chart.+ license: BSD3 license-file: LICENSE author: Deepak Jois@@ -11,9 +16,14 @@ category: Graphics stability: experimental homepage: http://github.com/deepakjois/hs-gchart-data-files: README.md, examples/Examples.hs+data-files: README.md+extra-source-files: examples/Examples.hs tested-with: GHC==6.12.1 +source-repository head+ type: git+ location: git@github.com:deepakjois/hs-gchart.git+ library build-depends: base >= 4 && < 5@@ -25,6 +35,12 @@ other-modules: Graphics.GChart.ChartItems+ ,Graphics.GChart.ChartItems.Basics+ ,Graphics.GChart.ChartItems.Data+ ,Graphics.GChart.ChartItems.Colors+ ,Graphics.GChart.ChartItems.Labels+ ,Graphics.GChart.ChartItems.Styles+ ,Graphics.GChart.ChartItems.Util ,Graphics.GChart.DataEncoding