Search code examples
haskellhaskell-diagramshaskell-chart

How to properly setup Chart using Diagrams backend?


I am trying to test Chart using the Diagrams backend (both from Stackage) with this example

import Graphics.Rendering.Chart.Easy
import Graphics.Rendering.Chart.Backend.Diagrams

signal :: [Double] -> [(Double,Double)]
signal xs = [ (x,(sin (x*3.14159/45) + 1) / 2 * (sin (x*3.14159/5))) | x <- xs ]

test :: IO ()
test = toFile def "example1_big.png" $ do
    layout_title .= "Amplitude Modulation"
    setColors [opaque blue, opaque red]
    plot (line "am" [signal [0,(0.5)..400]])
    plot (points "am points" (signal [0,7..400]))

from example-1 of the wiki.

In Main.hs file

main :: IO ()
main = test

The project builds and runs without any errors and does in fact produces a example1_big.png file in the project directory. But I cannot open it using feh or any other image viewer.

This is what feh says.

$ feh example1_big.png

feh WARNING: example1_big.png - No Imlib2 loader for that file format
feh: No loadable images specified.
See 'feh --help' or 'man feh' for detailed usage information

Using mediainfo on it shows that it doesn't have proper format information.

$ mediainfo example1_big.png
General
Complete name                            : example1_big.png
File size                                : 149 KiB

Looking around I did find this thread. Essentially they suggest to use a properly defined FileOptions instead of using def default.

So this is what I did

svgFileOpt :: FileOptions
svgFileOpt = FileOptions (800, 600) SVG loadCommonFonts

signal :: [Double] -> [(Double, Double)]
signal xs =
  [ (x, (sin (x * 3.14159 / 45) + 1) / 2 * (sin (x * 3.14159 / 5))) | x <- xs ]

plotDayVsDouble :: IO ()
plotDayVsDouble = toFile svgFileOpt "example1_big.png" $ do
  layout_title .= "Amplitude Modulation"
  setColors [opaque blue, opaque red]
  plot (line "am" [signal [0, (0.5) .. 400]])
  plot (points "am points" (signal [0, 7 .. 400]))

Unfortunately results where exactly the same. I was surprised to see mediainfo not even showing dimensional information.

$ mediainfo example1_big.png

General
Complete name                            : example1_big.png
File size                                : 149 KiB

Solution

  • Basically feh and other image viewers I tried didn't support SVG Scalable Vector Graphics image out of the box.

    Using feh --magick-timeout 1 or imagemagick's display worked!