{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE Strict #-}
module Granite.Chart (
scatterChart,
lineChart,
) where
import Data.Text (Text)
import Granite.Spec
scatterChart :: [(Text, [(Double, Double)])] -> Maybe Text -> Chart
scatterChart :: [(Text, [(Double, Double)])] -> Maybe Text -> Chart
scatterChart [(Text, [(Double, Double)])]
sers Maybe Text
title =
Chart
emptyChart
{ chartTitle = title
, chartLayers = map mkLayer sers
}
where
mkLayer :: (Text, [(Double, Double)]) -> Layer
mkLayer (Text
name, [(Double, Double)]
pts) =
let xs :: [Double]
xs = ((Double, Double) -> Double) -> [(Double, Double)] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (Double, Double) -> Double
forall a b. (a, b) -> a
fst [(Double, Double)]
pts
ys :: [Double]
ys = ((Double, Double) -> Double) -> [(Double, Double)] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (Double, Double) -> Double
forall a b. (a, b) -> b
snd [(Double, Double)]
pts
df :: DataFrame
df = [(Text, Column)] -> DataFrame
fromColumns [(Text
"x", [Double] -> Column
ColNum [Double]
xs), (Text
"y", [Double] -> Column
ColNum [Double]
ys)]
in (Geom -> Layer
defLayer Geom
GeomPoint)
{ layerData = Just df
, layerMapping =
emptyMapping
{ aesX = Just (ColumnRef "x")
, aesY = Just (ColumnRef "y")
, aesGroup = Just (ColumnRef name)
}
}
lineChart :: [(Text, [(Double, Double)])] -> Maybe Text -> Chart
lineChart :: [(Text, [(Double, Double)])] -> Maybe Text -> Chart
lineChart [(Text, [(Double, Double)])]
sers Maybe Text
title =
Chart
emptyChart
{ chartTitle = title
, chartLayers = map mkLayer sers
}
where
mkLayer :: (Text, [(Double, Double)]) -> Layer
mkLayer (Text
name, [(Double, Double)]
pts) =
let xs :: [Double]
xs = ((Double, Double) -> Double) -> [(Double, Double)] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (Double, Double) -> Double
forall a b. (a, b) -> a
fst [(Double, Double)]
pts
ys :: [Double]
ys = ((Double, Double) -> Double) -> [(Double, Double)] -> [Double]
forall a b. (a -> b) -> [a] -> [b]
map (Double, Double) -> Double
forall a b. (a, b) -> b
snd [(Double, Double)]
pts
df :: DataFrame
df = [(Text, Column)] -> DataFrame
fromColumns [(Text
"x", [Double] -> Column
ColNum [Double]
xs), (Text
"y", [Double] -> Column
ColNum [Double]
ys)]
in (Geom -> Layer
defLayer Geom
GeomLine)
{ layerData = Just df
, layerMapping =
emptyMapping
{ aesX = Just (ColumnRef "x")
, aesY = Just (ColumnRef "y")
, aesGroup = Just (ColumnRef name)
}
}