Skip to content

Commit

Permalink
Adding a ChartDataEncodable typeclass
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakjois committed Feb 5, 2010
1 parent ead16e3 commit e59d15f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
34 changes: 21 additions & 13 deletions ChartItems.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,21 @@ instance ChartItem ChartData where
encodeData (Extended d) = encodeExtended d


addDataWithEncoding :: [Float] -> ChartData -> ChartData
addDataWithEncoding d (Simple old) = Simple $ old ++ [map truncate d]
addDataWithEncoding d (Text old) = Text $ old ++ [d]
addDataWithEncoding d (Extended old) = Extended $ old ++ [map truncate 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 $ addDataWithEncoding d old

set $ addEncodedChartData d old

-- color

Expand Down Expand Up @@ -269,8 +275,10 @@ encodeText datas = "t:" ++ intercalate "|" (map encData datas) where
encData = intercalate "," . map encDatum
encDatum i | i >= 0 && i <= 100 = showDecimal i
| otherwise = "-1"
showDecimal :: RealFrac a => a -> String
showDecimal i = show (fromIntegral (round (i * 10.0)) / 10.0)
showDecimal :: Float -> String
showDecimal i | ((makeFloat (truncate i)) - i) == 0 = show $ truncate i
| otherwise = show (fromIntegral (round (i * 10.0)) / 10.0)
makeFloat i = fromIntegral i :: Float

encodeExtended datas = "e:" ++ intercalate "," (map (concatMap encDatum) datas) where
encDatum i | i >= 0 && i < 4096 = let (a, b) = i `quotRem` 64 in
Expand Down Expand Up @@ -349,17 +357,17 @@ getUrl = convertToUrl . getChartData
debugPieChart = getUrl $ do setChartSize 640 400
setChartType Pie
setChartTitle "Test"
addChartData [1,2,3,4,5]
addChartData ([1,2,3,4,5]::[Int])
addColor "FF0000"
addLegend $ legend ["t1","t2", "t3","t4","t5"]
addLabels $ ["Test 1", "Test 2", "Test 3", "Test 4", "Test 5"]

debugBarChart = getUrl $ do setChartSize 640 400
setChartType BarVerticalGrouped
setDataEncoding extended
addChartData [100,200,300,400,500]
addChartData [3,4,5,6,7]
addChartData [4,5,6,7,8]
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"] }
addColors ["FF0000","00FF00","0000FF"]
addLegend $ legend ["Set 1", "Set 2", "Set 3"]
Expand Down
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
FEATURES
========

* Support for
- Chart Title, Font Size and Color (chts)
- Adding custom shapes
Expand All @@ -15,12 +14,13 @@ FEATURES
- Pie Chart Orientation
- Bar Chart Fit
- Radar with fill area
- Text Encoding With Data Scaling
* package as cabal
* documentation

CODE IMPROVEMENTS
=================
* A better function to show float
* A better function to show float values when they are integers
* Basic Error Handling when attempting to use items not allowed in a chart type
* Add helpers for contructing datatypes like Fill, Markers etc
* Find ways to remove boilerplate from ChartItem class instances
Expand Down
1 change: 0 additions & 1 deletion scratch.hs

This file was deleted.

0 comments on commit e59d15f

Please sign in to comment.