hs-gchart 0.4 → 0.4.1
raw patch · 10 files changed
+171/−24 lines, 10 files
Files
- Graphics/GChart.hs +28/−3
- Graphics/GChart/ChartItems.hs +9/−0
- Graphics/GChart/ChartItems/Colors.hs +1/−0
- Graphics/GChart/ChartItems/Data.hs +7/−1
- Graphics/GChart/ChartItems/Labels.hs +1/−0
- Graphics/GChart/ChartItems/Styles.hs +24/−2
- Graphics/GChart/DataEncoding.hs +8/−7
- Graphics/GChart/Types.hs +60/−10
- examples/Examples.hs +32/−0
- hs-gchart.cabal +1/−1
Graphics/GChart.hs view
@@ -57,10 +57,10 @@ makeShapeMarker , makeRangeMarker , makeFinancialMarker ,+ makeLineMarker , makeLineStyle , -- * Setting Chart Parameters- setChartSize , setChartHeight , setChartType ,@@ -69,6 +69,7 @@ setChartTitleWithColorAndFontSize, setDataEncoding , addChartData ,+ addDataScale , addChartDataXY , setColors , addColor ,@@ -79,6 +80,8 @@ addShapeMarker , addRangeMarker , addFinancialMarker ,+ addLineMarker ,+ addLineFill , setLabels , setLabel , setBarWidthSpacing ,@@ -184,6 +187,10 @@ makeRangeMarker :: RangeMarker makeRangeMarker = defaultRangeMarker +-- | Line Marker+makeLineMarker :: LineMarker+makeLineMarker = defaultLineMarker+ -- | Financial Marker makeFinancialMarker :: FinancialMarker makeFinancialMarker = defaultFinancialMarker@@ -239,6 +246,12 @@ addChartData :: ChartDataEncodable a => [a] -> ChartM () addChartData = addDataToChart ++-- | Add a scale to chart.If more than one scale is added, it applies+-- the scale in order to each data series+addDataScale :: DataScale -> ChartM ()+addDataScale = addScaleToChart+ -- | Works like 'addChartData', but for XY datasets for line XY chart etc addChartDataXY :: ChartDataEncodable a => [(a,a)] -> ChartM () addChartDataXY series = do addDataToChart xseries@@ -290,7 +303,7 @@ addRangeMarker :: RangeMarker -> ChartM () addRangeMarker = addMarker --- | Adds a financial marker. User `makeFinancialMarker` smart constructor when+-- | Adds a financial marker. Use '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@@ -298,8 +311,20 @@ addFinancialMarker marker | financeDataSetIdx marker > (- 1) = addMarker marker | otherwise = do idx <- getDataSetIdx let newmarker = marker { financeDataSetIdx = idx }- addMarker marker+ addMarker newmarker+-- | Adds a line marker. Use 'makeLineMarker' smart constructor when calling+-- this function. If value of data set index is not specified when using+-- 'makeLineMarker', it automatically adds a data index to refer to the+-- latest data set+addLineMarker :: LineMarker -> ChartM ()+addLineMarker marker | lineDataSetIdx marker > (- 1) = addMarker marker+ | otherwise = do idx <- getDataSetIdx+ let newmarker = marker { lineDataSetIdx = idx }+ addMarker newmarker +-- | Adds a line fill to the chart+addLineFill :: LineFillType -> Color -> ChartM ()+addLineFill fillType color = addMarker (LineFillMarker fillType color) -- | Set labels for the chart setLabels :: [String] -> ChartM ()
Graphics/GChart/ChartItems.hs view
@@ -2,6 +2,7 @@ module Graphics.GChart.ChartItems ( getChartDataFromChartM, addDataToChart,+ addScaleToChart, addColorToChart, addFillToChart, addAxisToChart,@@ -33,6 +34,13 @@ Just cd -> set $ addEncodedChartData d cd _ -> error "Please set data encoding before adding data" +addScaleToChart scale = do c <- get+ let old = chartDataScales c+ case old of+ Just (CDS cds) -> set $ CDS $ cds ++ [scale]+ Nothing -> set $ CDS [scale]++ addColorToChart color = do chart <- get let (ChartColors old) = fromMaybe (ChartColors []) $ chartColors chart new = ChartColors $ old ++ [color]@@ -80,6 +88,7 @@ getParams chart = filter (/= ("","")) $ concat [encode $ chartType chart, encodeMaybe $ chartSize chart, encodeMaybe $ chartData chart,+ encodeMaybe $ chartDataScales chart, encodeMaybe $ chartTitle chart, encodeMaybe $ chartColors chart, encodeMaybe $ chartFills chart,
Graphics/GChart/ChartItems/Colors.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TypeSynonymInstances #-} module Graphics.GChart.ChartItems.Colors where
Graphics/GChart/ChartItems/Data.hs view
@@ -5,6 +5,8 @@ import Graphics.GChart.DataEncoding +import Data.List(intercalate)+ -- Chart Data instance ChartItem ChartData where set cData = updateChart $ \chart -> chart { chartData = Just cData }@@ -14,6 +16,11 @@ encodeData (Text d) = encodeText d encodeData (Extended d) = encodeExtended d +instance ChartItem ChartDataScales where+ set scales = updateChart $ \chart -> chart { chartDataScales = Just scales }++ encode (CDS scales) = asList ("chds", intercalate "," $ map (\(min,max) -> showFloat min ++ "," ++ showFloat max) scales)+ instance ChartDataEncodable Int where addEncodedChartData d cd@(Simple old) = Simple $ old ++ [d] addEncodedChartData d cd@(Extended old) = Extended $ old ++ [d]@@ -22,7 +29,6 @@ instance ChartDataEncodable Float where addEncodedChartData d cd@(Text old) = Text $ old ++ [d] addEncodedChartData d _ = error "Invalid type for specified encoding. Use int data"- instance ChartItem QREncoding where set qrEnc = updateChart $ \chart -> chart { qrEncoding = Just qrEnc }
Graphics/GChart/ChartItems/Labels.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TypeSynonymInstances #-} module Graphics.GChart.ChartItems.Labels where
Graphics/GChart/ChartItems/Styles.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TypeSynonymInstances #-} module Graphics.GChart.ChartItems.Styles where @@ -57,9 +58,23 @@ set markers = updateChart $ \chart -> chart { chartMarkers = Just markers } encode markers = asList ("chm",intercalate "|" $ map encodeChartMarker markers) +-- Line Markers+instance ChartMarker LineMarker where+ encodeChartMarker marker = intercalate "," ["D",color,series_index, which_points, width, z_order] where+ color = lineColor marker+ series_index = show $ lineDataSetIdx marker+ which_points = case lineWhichPoints marker of+ PointsAll -> "0"+ Points (Just s,Just e) -> show s ++ ":" ++ show e+ Points (Just s, Nothing) -> show s+ Points (Nothing, Just e) -> show e+ _ -> error "Invalid points specification"+ width = show $ lineSize marker+ z_order = showFloat $ lineZorder marker+ -- Shape Markers instance ChartMarker ShapeMarker where- encodeChartMarker marker = optionalat ++ (intercalate "," $ [marker_type, color, idx, datapoint, size ++ width] ++ [show zorder | zorder /= 0]) where+ encodeChartMarker marker = optionalat ++ intercalate "," ([marker_type, color, idx, datapoint, size ++ width] ++ [show zorder | zorder /= 0]) where marker_type = case shapeType marker of ShapeArrow -> "a" ShapeCross -> "c"@@ -87,7 +102,7 @@ size = show $ shapeSize marker width = case shapeWidth marker of Nothing -> ""- Just x -> ":" ++ show x+ Just x -> ':' : show x zorder = shapeZorder marker optionalat = [ '@' | isDataPointXY $ shapeDataPoints marker ]@@ -119,6 +134,13 @@ idx = show $ financeDataSetIdx marker size = show $ financeSize marker priority = financePriority marker++-- Line Fills+instance ChartMarker LineFillMarker where+ encodeChartMarker (LineFillMarker lineFillType color) = intercalate "," [b_or_B, color, show startIdx, show endIdx, "0"] where+ (b_or_B, startIdx, endIdx) = case lineFillType of+ LineFillFrom s -> ("B",s,0)+ LineFillBetween s e -> ("b",s,e) -- Pie Chart Orientation instance ChartItem PieChartOrientation where
Graphics/GChart/DataEncoding.hs view
@@ -23,14 +23,15 @@ | ord c >= ord '0' && ord c <= ord '9' = 52 + (ord c - ord '0') | otherwise = -1 +-- FIXME This assumes all data is in range. 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- | otherwise = show (fromIntegral (round (i * 10.0)) / 10.0)- makeFloat i = fromIntegral i :: Float+ encData = intercalate "," . map showDecimal++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
Graphics/GChart/Types.hs view
@@ -18,18 +18,22 @@ Some parameters are not supported yet: -- Chart Data Scaling <http://code.google.com/apis/chart/docs/data_formats.html#data_scaling>- - Text and Data Value Markers <http://code.google.com/apis/chart/docs/chart_params.html#gcharts_data_point_labels> -- Bar chart zero line <http://code.google.com/apis/chart/docs/gallery/bar_charts.html#chp>+- Shape offset feature for shape markers <http://code.google.com/apis/chart/docs/chart_params.html#gcharts_shape_markers> +- Bug in 'BarChartWidthSpacing'. Not fully accurate++- Modfy FillType to conform to new API <http://code.google.com/apis/chart/docs/chart_params.html#gcharts_gradient_fills>+ - Dynamic icon type <http://code.google.com/apis/chart/docs/gallery/dynamic_icons.html> - Geographic area <http://code.google.com/apis/chart/docs/gallery/map_charts.html> -- Shape offset feature for shape markers <http://code.google.com/apis/chart/docs/chart_params.html#gcharts_shape_markers>+- Vertical slice filling <http://code.google.com/apis/chart/docs/chart_params.html#gcharts_line_fills> +- Bar chart zero line <http://code.google.com/apis/chart/docs/gallery/bar_charts.html#chp>+ -} module Graphics.GChart.Types (@@ -55,7 +59,7 @@ ChartColors(..), Color, -- ** Chart Data- ChartData(..),+ ChartData(..), ChartDataScales(..), DataScale(..), -- ** Chart Legend Text and Style ChartLegend(..), LegendPosition(..),@@ -76,11 +80,16 @@ -- ** Line Styles ChartLineStyles(..), LineStyle(..), - -- ** Shape, Range and Financial Markers+ -- ** Line, Shape, Range and Financial Markers AnyChartMarker(..), ChartMarker(..), ChartMarkers,+ LineWhichPoints(..),LineMarker(..), ShapeType(..), MarkerDataPoint(..), ShapeMarker(..),- RangeMarkerType(..), RangeMarker(..), FinancialMarker(..),+ RangeMarkerType(..), RangeMarker(..),+ FinancialMarker(..), + -- ** Line Fills+ LineFillType(..), LineFillMarker(..),+ -- ** Chart Margins ChartMargins(..), @@ -108,7 +117,8 @@ used as starting points to construct parameters when creating charts. -} defaultChart, defaultAxis, defaultGrid, defaultSpacing, defaultShapeMarker,- defaultRangeMarker, defaultFinancialMarker, defaultLineStyle, defaultQREncodingLabelData+ defaultRangeMarker, defaultFinancialMarker, defaultLineStyle, defaultLineMarker,+ defaultQREncodingLabelData ) where import Control.Monad.State@@ -146,8 +156,14 @@ , titleFontSize :: Maybe FontSize -- ^ Title Font Size } deriving Show +-- | Data scaling expressed as (@series_min@,@series_max@). Applies to text encoding only+type DataScale = (Float,Float)++-- | List of Data scaling values+data ChartDataScales = CDS [DataScale] deriving Show+ -- | Chart data along with encoding. XY data for is encoded a pair of--- | consecutive data sets+-- consecutive data sets data ChartData = Simple [[Int]] -- ^ lets you specify integer values from 0-61, inclusive | Text [[Float]] -- ^ supports floating point numbers from 0-100, inclusive@@ -191,9 +207,10 @@ -- | Constructor for a chart fill data Fill = Fill FillKind FillType deriving Show --- | Chart fills, as a list of `Fill's+-- | Chart fills, as a list of 'Fill's type ChartFills = [Fill] + -- | Position of legend on chart. Applies to 'ChartLegend' data LegendPosition = LegendBottom -- ^ Bottom of chart, horizontally@@ -369,6 +386,30 @@ , financePriority :: Int -- ^ Priority of drawing. Can be one of -1,0,1 } deriving Show +-- | Which points in a series to use to draw the line.+data LineWhichPoints = PointsAll -- ^ Use all the points in the series.+ | Points (Maybe Float, Maybe Float) -- ^ (start,end) indicating a specific range of points+ deriving Show++-- | Line Marker+data LineMarker =+ LM { lineColor :: Color -- ^ Line Marker Color+ , lineDataSetIdx :: Int -- ^ Data set index+ , lineWhichPoints :: LineWhichPoints -- ^ Which points to draw the line markers on+ , lineSize :: Int -- ^ Width of line in pixels+ , lineZorder :: Float -- ^ Floating point between -1 and 1 indicating+ -- the layer on which to draw.+ } deriving Show++-- | Line fill type for 'LineFill'+data LineFillType = LineFillFrom Int -- ^ Line fill starting from a start index+ | LineFillBetween Int Int -- ^ Line fill between a start index and end index+ deriving Show++-- | Line Fill Marker+data LineFillMarker = LineFillMarker LineFillType Color deriving Show++ -- | Typeclass to abstract over different chart markers class Show a => ChartMarker a where encodeChartMarker :: a -> String@@ -451,6 +492,7 @@ chartSize :: Maybe ChartSize , chartType :: ChartType , chartData :: Maybe ChartData+ , chartDataScales :: Maybe ChartDataScales , chartTitle :: Maybe ChartTitle , chartColors :: Maybe ChartColors , chartFills :: Maybe ChartFills@@ -498,6 +540,7 @@ Chart { chartSize = Nothing, chartType = Line, chartData = Nothing,+ chartDataScales = Nothing, chartTitle = Nothing, chartColors = Nothing, chartFills = Nothing,@@ -559,6 +602,13 @@ financeDataPoint = DataPointEvery, financeSize = 5, financePriority = 0 }++-- | Default value of a line marker. Make sure you change the value of @lineDataSetIdx@+defaultLineMarker = LM { lineColor = "0000DD"+ , lineDataSetIdx = -1+ , lineWhichPoints = PointsAll+ , lineSize = 5+ , lineZorder = 0.0 } -- | Default value of a line style defaultLineStyle = LS { lineStyleThickness = 1,
examples/Examples.hs view
@@ -139,6 +139,38 @@ setQREncoding UTF8 setQRErrorCorrection L' +barGraphWithShiftedZeroLine = getChartUrl $ do setChartType BarVerticalGrouped+ setChartSize 320 200+ setDataEncoding text+ addChartData ([30,-60,50,140,80,-90]::[Float])+ addDataScale (-80,140)+ addAxis $ makeAxis { axisType = AxisLeft,+ axisRange = Just $ Range (-80,140) Nothing}++lineMarkerSample = getChartUrl $ do setChartType BarVerticalGrouped+ setChartSize 200 150+ setDataEncoding simple+ setBarWidthSpacing $ barwidth 20+ addChartData $ map encSimpleReverse "1XQbnf4"+ addColor "76A4FB"+ addLineMarker $ makeLineMarker { lineColor = "0033FF",+ lineWhichPoints = PointsAll,+ lineZorder = 1 }++lineFillSample = getChartUrl $ do setChartType Line+ setChartSize 200 125+ setDataEncoding simple+ addChartData $ map encSimpleReverse "cefhjkqwrlgYcfgc"+ addLineFill (LineFillBetween 0 1) "224499"+ addChartData $ map encSimpleReverse "QSSVXXdkfZUMRTUQ"+ addLineFill (LineFillBetween 1 2) "FF0000"+ addChartData $ map encSimpleReverse "HJJMOOUbVPKDHKLH"+ addLineFill (LineFillBetween 2 3) "80C65A"+ addAxis $ makeAxis { axisType = AxisBottom,+ axisLabels = Just ["Sep","Oct","Nov","Dec"] }+ addAxis $ makeAxis { axisType = AxisLeft,+ axisLabels = Just ["","50","100"] }+ blanks x = replicate x "" dataSeries1 :: [Int]
hs-gchart.cabal view
@@ -1,5 +1,5 @@ name: hs-gchart-version: 0.4+version: 0.4.1 synopsis: Haskell wrapper for the Google Chart API description: This module is a wrapper around the Google Chart API. It exposes a rich