module Chart.Svg
( SvgOptions(SvgOptions)
, renderSvg
, toText
, toFile
, fileSvg
, scratch
) where
import Chart.Core
import Data.Default
import Diagrams.Backend.SVG
import Graphics.Svg hiding (toText)
import NumHask.Pair
import NumHask.Prelude
import qualified Data.Text.Lazy as Lazy
import qualified Data.Text.Lazy.IO as Text
import qualified Diagrams.Prelude as D
data SvgOptions = SvgOptions
{ size :: Pair Double
, svgId :: Text
, attributes :: [Attribute]
, includeDocType :: Bool
} deriving (Show, Generic)
instance Default SvgOptions where
def = SvgOptions (Pair 600 400) "" [] True
renderSvg :: SvgOptions -> Chart SVG -> Element
renderSvg (SvgOptions (Pair x y) svgid atts dt) ch =
D.renderDia
SVG
(SVGOptions
(D.mkSizeSpec (D.V2 (Just x) (Just y))) Nothing svgid atts dt)
ch
toText :: SvgOptions -> Chart SVG -> Lazy.Text
toText opts ch = prettyText $ renderSvg opts ch
toFile :: FilePath -> Element -> IO ()
toFile f = Text.writeFile f . prettyText
fileSvg :: FilePath -> SvgOptions -> Chart SVG -> IO ()
fileSvg f opts ch = toFile f $ renderSvg opts ch
scratch :: Chart SVG -> IO ()
scratch = fileSvg "other/scratch.svg" def