Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to render purely? #196

Open
jwaldmann opened this issue Nov 4, 2018 · 1 comment
Open

how to render purely? #196

jwaldmann opened this issue Nov 4, 2018 · 1 comment

Comments

@jwaldmann
Copy link

I am unable to figure out how to transform a chart to SVG in a pure way.
I thought that's one of the main reasons for having these native back-ends?

It seems the roadblock is defaultEnv which is in IO. Why is this?
It tries to determine something about fonts - by reading something from disk. But what's the use if the resulting SVG document will be rendered elsewhere?

Here is my best attempt at understanding the rendering process. All of this is pure, except for defaultEnv (that's the point of this issue) and putStrLn (which is not the point, since it's just for testing)

{-# language OverloadedStrings #-}

import Graphics.Rendering.Chart.Easy
import Diagrams.Backend.SVG
import Diagrams.Core.Compile
import Diagrams.Core.Types (QDiagram)
import Data.Monoid (Any)
import Diagrams.TwoD.Types (V2)
import Diagrams.TwoD (mkWidth)
import Diagrams.Core.V (N)
import Graphics.Rendering.Chart.Backend.Diagrams
import Graphics.Svg.Core (renderBS)
import qualified Data.ByteString.Lazy as BS

main = do
  let r :: Renderable ()
      r = toRenderable $ plot
        $ line "sin" [ [ (x , sin x) | x <- [ 0 :: Double, 0.1 .. 10 ] ] ]

  de <- defaultEnv (AlignmentFns id id) 100 100
  
  let b :: BackendProgram (PickFn ())
      b = render r (100,100)
  let (q, _) = runBackend de b :: (QDiagram SVG V2 (N SVG) Any, PickFn ())
  let opts = (SVGOptions (mkWidth 250) Nothing "" [] True)
  let res :: BS.ByteString
      res = renderBS $ renderDia Diagrams.Backend.SVG.SVG opts q

  BS.putStrLn res
@timbod7
Copy link
Owner

timbod7 commented Nov 4, 2018

You are correct. In standard usage, Chart rendering with the diagrams backend is pure except for the loading of the fonts (see loadFont in the the SVG fonts library http://hackage.haskell.org/package/SVGFonts-1.7/docs/Graphics-SVGFonts.html).

Your application doesn't have to follow this pattern however. You could arrange for the fonts of interest to you to be embedded in your code, and rather than calling defaultEnv construct the DEnv context directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants