Skip to content

Commit

Permalink
Adding data scaling to chart
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakjois committed Feb 17, 2010
1 parent d842988 commit 374145c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Graphics/GChart.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module Graphics.GChart (
setChartTitleWithColorAndFontSize,
setDataEncoding ,
addChartData ,
addDataScale ,
addChartDataXY ,
setColors ,
addColor ,
Expand Down Expand Up @@ -239,6 +240,12 @@ setDataEncoding = set
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
Expand Down
9 changes: 9 additions & 0 deletions Graphics/GChart/ChartItems.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module Graphics.GChart.ChartItems (
getChartDataFromChartM,
addDataToChart,
addScaleToChart,
addColorToChart,
addFillToChart,
addAxisToChart,
Expand Down Expand Up @@ -33,6 +34,13 @@ addDataToChart d = do c <- get
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]
Expand Down Expand Up @@ -80,6 +88,7 @@ encodeMaybe (Just x) = encode x
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,
Expand Down
10 changes: 8 additions & 2 deletions Graphics/GChart/ChartItems/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Graphics.GChart.ChartItems.Util

import Graphics.GChart.DataEncoding

import Data.List(intercalate)

-- Chart Data
instance ChartItem ChartData where
set cData = updateChart $ \chart -> chart { chartData = Just cData }
Expand All @@ -14,6 +16,11 @@ instance ChartItem ChartData where
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]
Expand All @@ -23,7 +30,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 }

Expand All @@ -36,4 +42,4 @@ instance ChartItem QREncoding where
instance ChartItem ChartLabelData where
set chld = updateChart $ \chart -> chart { chartLabelData = Just chld }

encode (QRLabelData ec m) = asList ("chld", concat [show ec, "|", show m])
encode (QRLabelData ec m) = asList ("chld", concat [show ec, "|", show m])
14 changes: 10 additions & 4 deletions Graphics/GChart/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ Some chart types are not supported yet:
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>
Expand Down Expand Up @@ -55,7 +53,7 @@ module Graphics.GChart.Types (
ChartColors(..), Color,

-- ** Chart Data
ChartData(..),
ChartData(..), ChartDataScales(..), DataScale(..),

-- ** Chart Legend Text and Style
ChartLegend(..), LegendPosition(..),
Expand Down Expand Up @@ -146,8 +144,14 @@ data ChartTitle =
, 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
Expand Down Expand Up @@ -451,6 +455,7 @@ data Chart =
chartSize :: Maybe ChartSize
, chartType :: ChartType
, chartData :: Maybe ChartData
, chartDataScales :: Maybe ChartDataScales
, chartTitle :: Maybe ChartTitle
, chartColors :: Maybe ChartColors
, chartFills :: Maybe ChartFills
Expand Down Expand Up @@ -498,6 +503,7 @@ defaultChart =
Chart { chartSize = Nothing,
chartType = Line,
chartData = Nothing,
chartDataScales = Nothing,
chartTitle = Nothing,
chartColors = Nothing,
chartFills = Nothing,
Expand Down
8 changes: 8 additions & 0 deletions examples/Examples.hs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ qrCodeChart = getChartUrl $ do setChartType QRCode
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}

blanks x = replicate x ""

dataSeries1 :: [Int]
Expand Down

0 comments on commit 374145c

Please sign in to comment.