Permalink
Browse files

Adding support for QR Codes

  • Loading branch information...
1 parent 115c271 commit 9cf18ac74cdc36e2ac99d4e4519477515d8aff45 @deepakjois committed Feb 12, 2010
View
@@ -80,11 +80,14 @@ module Graphics.GChart (
addRangeMarker ,
addFinancialMarker ,
setLabels ,
+ setLabel ,
setBarWidthSpacing ,
setPieChartOrientation ,
addLineStyle ,
setFormula ,
-
+ setQREncoding ,
+ setQRWidth ,
+ setQRErrorCorrection ,
-- * Retrieving Chart Data
getChartData,
@@ -302,6 +305,9 @@ addFinancialMarker marker | financeDataSetIdx marker > (- 1) = addMarker marker
setLabels :: [String] -> ChartM ()
setLabels = set . ChartLabels
+-- | Set label for a chart
+setLabel :: String -> ChartM ()
+setLabel label = set $ ChartLabels [label]
-- | Set bar and width spacing
setBarWidthSpacing :: BarChartWidthSpacing -> ChartM ()
@@ -319,6 +325,21 @@ addLineStyle = addLineStyleToChart
setFormula :: String -> ChartM ()
setFormula formula = setLabels [formula]
+-- | Set QR code output encoding. Valid for 'QRCode' only
+setQREncoding :: QREncoding -> ChartM ()
+setQREncoding = set
+
+-- | Sets the error correction level for 'QRCode'
+setQRErrorCorrection :: ErrorCorrectionLevel -> ChartM ()
+setQRErrorCorrection ec = set qrLabelData where
+ qrLabelData = QRLabelData ec defMargin
+ QRLabelData _ defMargin = defaultQREncodingLabelData
+
+-- | Sets the width (in rows) of the white border around the data portion of the 'QRCode'
+setQRWidth :: Int -> ChartM ()
+setQRWidth m = set qrLabelData where
+ qrLabelData = QRLabelData defEC m
+ QRLabelData defEC _ = defaultQREncodingLabelData
-- | Extracts the data out of the monad and returns a value of type 'Chart'
getChartData :: ChartM () -> Chart
@@ -91,5 +91,7 @@ getParams chart = filter (/= ("","")) $ concat [encode $ chartType chart,
encodeMaybe $ chartMargins chart,
encodeMaybe $ barChartWidthSpacing chart,
encodeMaybe $ pieChartOrientation chart,
- encodeMaybe $ chartLineStyles chart]
+ encodeMaybe $ chartLineStyles chart,
+ encodeMaybe $ qrEncoding chart,
+ encodeMaybe $ chartLabelData chart]
@@ -10,7 +10,7 @@ instance ChartItem ChartSize where
encode (Size width height) = asList ("chs", widthStr ++ show height) where
widthStr | width == 0 = ""
- | otherwise = "x" ++ show width
+ | otherwise = show width ++ "x"
-- Chart Type
instance ChartItem ChartType where
@@ -33,3 +33,4 @@ instance ChartItem ChartType where
Radar -> "r"
GoogleOMeter -> "gom"
Formula -> "tx"
+ QRCode -> "qr"
@@ -22,3 +22,18 @@ instance ChartDataEncodable Int where
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 }
+
+ encode qrEnc = asList ("choe", encStr)
+ where encStr = case qrEnc of
+ UTF8 -> "UTF-8"
+ Shift_JIS -> "Shift_JIS"
+ ISO8859_1 -> "ISO-8859-1"
+
+instance ChartItem ChartLabelData where
+ set chld = updateChart $ \chart -> chart { chartLabelData = Just chld }
+
+ encode (QRLabelData ec m) = asList ("chld", concat [show ec, "|", show m])
View
@@ -14,20 +14,14 @@ Some chart types are not supported yet:
- Dynamic Icons <http://code.google.com/apis/chart/docs/gallery/dynamic_icons.html>
-- Formula Chart <http://code.google.com/apis/chart/docs/gallery/formulas.html>
-
- Map Charts <http://code.google.com/apis/chart/docs/gallery/map_charts.html>
-- QR Codes <http://code.google.com/apis/chart/docs/gallery/qr_codes.html>
-
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>
-- QR code output encoding <http://code.google.com/apis/chart/docs/gallery/qr_codes.html>
-
- Bar chart zero line <http://code.google.com/apis/chart/docs/gallery/bar_charts.html#chp>
- Dynamic icon type <http://code.google.com/apis/chart/docs/gallery/dynamic_icons.html>
@@ -74,6 +68,9 @@ module Graphics.GChart.Types (
-- ** Pie chart labels, Google-o-meter label (TODO: QR code data, Formula TeX data)
ChartLabels(..),
+ -- ** Chart Label Data
+ ChartLabelData(..), ErrorCorrectionLevel(..),
+
-- ** Line Styles
ChartLineStyles(..), LineStyle(..),
@@ -85,6 +82,9 @@ module Graphics.GChart.Types (
-- ** Chart Margins
ChartMargins(..),
+ -- ** QR code output encoding
+ QREncoding(..),
+
-- ** Pie Chart Orientation
PieChartOrientation(..),
@@ -106,7 +106,7 @@ module Graphics.GChart.Types (
used as starting points to construct parameters when creating charts. -}
defaultChart, defaultAxis, defaultGrid, defaultSpacing, defaultShapeMarker,
- defaultRangeMarker, defaultFinancialMarker, defaultLineStyle
+ defaultRangeMarker, defaultFinancialMarker, defaultLineStyle, defaultQREncodingLabelData
) where
import Control.Monad.State
@@ -132,6 +132,7 @@ data ChartType
| Radar -- ^ Radar Chart
| GoogleOMeter -- ^ Google-o-meter
| Formula -- ^ Formula Chart
+ | QRCode -- ^ QR Codes
deriving Show
-- | Title of the chart
@@ -412,6 +413,25 @@ data LineStyle = LS { lineStyleThickness :: Float -- ^ Thickness
type ChartLineStyles = [LineStyle]
+-- | QR Code Output Encoding
+data QREncoding = UTF8 | Shift_JIS | ISO8859_1 deriving Show
+
+-- | Error Correction Level for QR Code
+data ErrorCorrectionLevel = L' -- ^ recovery of up to 7% data loss
+ | M' -- ^ recovery of up to 15% data loss
+ | Q' -- ^ recovery of up to 25% data loss
+ | H' -- ^ recovery of up to 30% data loss
+
+instance Show ErrorCorrectionLevel where
+ show L' = "L"
+ show M' = "M"
+ show Q' = "Q"
+ show H' = "H"
+
+-- | Chart Label Data. Applies to 'QRCode'
+data ChartLabelData = QRLabelData ErrorCorrectionLevel Int -- ^ Error Correction Level and Margin (as no. of rows)
+ deriving Show
+
-- | Data type for the chart
data Chart =
Chart {
@@ -430,6 +450,8 @@ data Chart =
, barChartWidthSpacing :: Maybe BarChartWidthSpacing
, pieChartOrientation :: Maybe PieChartOrientation
, chartLineStyles :: Maybe ChartLineStyles
+ , qrEncoding :: Maybe QREncoding
+ , chartLabelData :: Maybe ChartLabelData
} deriving Show
@@ -474,7 +496,9 @@ defaultChart =
chartMarkers = Nothing,
barChartWidthSpacing = Nothing,
pieChartOrientation = Nothing,
- chartLineStyles = Nothing
+ chartLineStyles = Nothing,
+ qrEncoding = Nothing,
+ chartLabelData = Nothing
}
-- | Default value for an axis
@@ -527,3 +551,6 @@ defaultLineStyle = LS { lineStyleThickness = 1,
lineStyleLineSegment = 1,
lineStyleBlankSegment = 0 }
+-- | Default chart label data for QR Encoding
+defaultQREncodingLabelData = QRLabelData L' 4
+
View
@@ -133,6 +133,12 @@ formulaChart = getChartUrl $ do setChartType Formula
addColor "FF0000"
addFill $ Fill (LinearGradient 20 [("76A4FB",1),("FFFFFF",0)]) Background
+qrCodeChart = getChartUrl $ do setChartType QRCode
+ setChartSize 150 150
+ setLabel "Hello World"
+ setQREncoding UTF8
+ setQRErrorCorrection L'
+
blanks x = take x $ repeat ""
dataSeries1 :: [Int]
@@ -177,3 +183,6 @@ main = do putStrLn christmasPie
putStrLn bargraphRelativeSpacing
putStrLn scatterPlotWithMarkers
putStrLn lineChartWithLineStyles
+ putStrLn formulaChart
+ putStrLn qrCodeChart
+

0 comments on commit 9cf18ac

Please sign in to comment.