mrsteve
mrsteve

Reputation: 4132

Error while compiling the example code from Graphics-Rendering-Plot-Gtk

I tried to compile the example code from Graphics-Rendering-Plot-Gtk availiable on hackage.

This is the error I get:

src/DynamicPlot.hs:20:6: Not in scope: ‘linspace’

src/DynamicPlot.hs:21:6: Not in scope: ‘randomVector’

src/DynamicPlot.hs:21:21: Not in scope: data constructor ‘Gaussian’

src/DynamicPlot.hs:25:6: Not in scope: ‘constant’

src/DynamicPlot.hs:31:6: Not in scope: ‘buildMatrix’

I am using cabal sandboxes and this is my cabal file:

name: arduinoPlot
version: 1.0.0.2
cabal-version: >=1.6
build-type: Simple
license: BSD3
license-file: LICENSE
copyright: (c) Simon Marlow
maintainer: Simon Marlow <[email protected]>
stability: stable
homepage: http://www.haskell.org/hello/
bug-reports: mailto:[email protected]
synopsis: Hello World, an example package
description: This is an implementation of the classic "Hello World" program in
             Haskell, as an example of how to create a minimal Haskell
             application using Cabal and Hackage.  Please submit any suggestions and
             improvements.
category: Console, Text
author: Simon Marlow
data-dir: ""

source-repository head
    type: darcs
    location: http://darcs.haskell.org/hello/

flag threaded
    Default: False

executable arduinoPlot
    build-depends: base >=4.2 && <5, hArduino >=0.9, mtl >=2.2,
                   easyplot >=1.0,
                   process,
                   glib >= 0.11 && < 0.14,
                   gtk >= 0.11 && < 0.14,
                   hmatrix >= 0.10,
                   plot < 0.3, 
                   plot-gtk,
                   hmatrix-gsl-stats,
                   linda,
                   colour,
                   array


                   --plot-gtk -any, plot -any, colour -any, hmatrix == 0.16.0.6, array -any,
                   --random >= 1.0.1.1, storable-complex >= 0.2.1, primitive >= 0.5.0.1, 
                   --vector >= 0.10.9.1, hmatrix >= 0.15.2.0, hmatrix-gsl-stats >= 0.2, hstatistics >= 0.2.5.1,
                   --linda -any  

    if flag(threaded)
        buildable: True
        ghc-options: -threaded
    main-is: arduinoPlot.hs
    buildable: True
    hs-source-dirs: src

Finally here is my test code:

module DynamicPlot (
    dynamicMain
) where


import Data.Colour.Names

import qualified Data.Array.IArray as A

import Numeric.Vector
import Numeric.Matrix

import Numeric.GSL.Statistics

import Graphics.Rendering.Plot
import Graphics.Rendering.Plot.Gtk

ln = 25
ts = linspace ln (0,1)
rs = randomVector 0 Gaussian ln

ss = sin (15*2*pi*ts)
ds = 0.25*rs + ss
es = constant (0.25*(stddev rs)) ln

fs :: Double -> Double
fs = sin . (15*2*pi*)

ms :: Matrix Double
ms = buildMatrix 64 64 (\(x,y) -> sin (2*2*pi*(fromIntegral x)/64) * cos (5*2*pi*(fromIntegral y)/64))

figure = do
        withTextDefaults $ setFontFamily "OpenSymbol"
        withTitle $ setText "Testing plot package:"
        withSubTitle $ do
                       setText "with 1 second of a 15Hz sine wave"
                       setFontSize 10
        setPlots 1 2
        withPlot (1,1) $ do
                         setDataset (ts,[point (ds,es,"Sampled data") (Bullet,green)
                                       ,line (fs,"15 Hz sinusoid") blue])
                         addAxis XAxis (Side Lower) $ do
                                                      setGridlines Major True
                                                      withAxisLabel $ setText "time (s)"
                         addAxis YAxis (Side Lower) $ do
                                                      setGridlines Major True
                                                      withAxisLabel $ setText "amplitude"
                         addAxis XAxis (Value 0) $ return ()
                         setRangeFromData XAxis Lower
                         setRange YAxis Lower (-1.25) 1.25
                         setLegend True NorthEast Inside
                         withLegendFormat $ setFontSize 6

For development I use:

Has anyone an idea why I get the erros stated at the beginning of the question?




UPDATE

Probably the example code is outdated leading to the errors I got. I fixed some of the problems and got a running version working:

module Main (
    main
) where


-- imports needed for threaded GTK
--
import Control.Concurrent
import Control.Concurrent.MVar
import Graphics.UI.Gtk

import System.Exit
import Graphics.UI.Gtk
import Control.Monad.Trans(liftIO)

-- imports needed for plot
--
import Numeric.GSL.Statistics

import Graphics.Rendering.Plot
import Graphics.Rendering.Plot.Gtk

import Numeric.LinearAlgebra


main = do

    -- init
    --
    _ <- initGUI
    exit <- newEmptyMVar

    -- display figure 
    --
    figure1 <- displayFigure

    threadDelay 10000000; -- wait 10 seconds 
    putStrLn "finished"


displayFigure :: IO PlotHandle
displayFigure = display figure



--
-- corrected example code
--
ln = 25
ts = linspace ln (0,1)
rs = randomVector 0 Gaussian ln

ss = sin (15*2*pi*ts)
ds = 0.25*rs + ss
es = constant (0.25*(stddev rs)) ln

fs :: Double -> Double
fs = sin . (15*2*pi*)

ms :: Matrix Double
ms = buildMatrix 64 64 (\(x,y) -> sin (2*2*pi*(fromIntegral x)/64) * cos (5*2*pi*(fromIntegral y)/64))

figure = do
        withTextDefaults $ setFontFamily "OpenSymbol"
        withTitle $ setText "Testing plot package:"
        withSubTitle $ do
                       setText "with 1 second of a 15Hz sine wave"
                       setFontSize 10
        setPlots 1 2
        withPlot (1,1) $ do
                         setDataset (ts,[point (ds,es,"Sampled data") (Bullet,green)
                                       ,line (fs,"15 Hz sinusoid") blue])
                         addAxis XAxis (Side Lower) $ do
                                                      setGridlines Major True
                                                      withAxisLabel $ setText "time (s)"
                         addAxis YAxis (Side Lower) $ do
                                                      setGridlines Major True
                                                      withAxisLabel $ setText "amplitude"
                         addAxis XAxis (Value 0) $ return ()

                         --
                         --  
                         -- WHY DO THE NEXT TWO LINES LEAD TO A TYPE ERROR ?
                         --
                         --

                         --setRangeFromData XAxis Lower
                         --setRange YAxis Lower (-1.25) 1.25

                         setLegend True NorthEast Inside
                         withLegendFormat $ setFontSize 6

Here is the cabal file:

-- Instructions on how to write this file are in the Cabal
-- documentation, which can be found here:
--   http://haskell.org/cabal/release/cabal-latest/doc/users-guide/

name: gtkPlotTest
version: 1.0.0.2
license: BSD3
license-file: LICENSE
copyright: (c) Simon Marlow
author: Simon Marlow
maintainer: Simon Marlow <[email protected]>
bug-reports: mailto:[email protected]
stability: stable
homepage: http://www.haskell.org/hello/
synopsis: Hello World, an example package
category: Console, Text
cabal-version: >= 1.6
build-type: Simple

Description:
  This is an implementation of the classic "Hello World" program in
  Haskell, as an example of how to create a minimal Haskell
  application using Cabal and Hackage.  Please submit any suggestions and
  improvements.

source-repository head
  type:     darcs
  location: http://darcs.haskell.org/hello/

flag threaded
  default: False

executable gtkPlotTest
  hs-source-dirs: src
  main-is: gtkPlottest.hs
  build-depends: base >= 4.2 && < 5,
                 gtk -any,
                 hmatrix -any, 
                 plot -any,
                 plot-gtk,
                 hmatrix-gsl-stats,
                 linda,
                 colour,
                 array, 
                 mtl
                 --hmatrix-static -any


-- gtk >= -any -- 0.11 && < 0.14,
--              plot any -- < 0.3,

--  if flag(threaded)
  ghc-options: -threaded

There are two problems left.

First the following two lines lead to a type error, if I don't comment them out (see code above):

   --
   --  
   -- WHY DO THE NEXT TWO LINES LEAD TO A TYPE ERROR ?
   --                   
   --setRangeFromData XAxis Lower
   --setRange YAxis Lower (-1.25) 1.25

Second, the main function isn't a proper GTK function: I just wait some time and close the window. I want that the window is shown indefinitely and that the window closes with a proper exit signal as the "x" button of the window is pressed.

Here is the workaround from the main function :

...
figure1 <- displayFigure

threadDelay 10000000; -- wait 10 seconds 
putStrLn "finished"

Upvotes: 1

Views: 252

Answers (1)

vivian
vivian

Reputation: 1434

The type error in those two lines is because you need to add Linear to

setRangeFromData XAxis Lower Linear 
setRange YAxis Lower Linear (-1.25) (1.25)

As for the second problem, how to quit nicely, Graphics.Rendering.Plot.Gtk is for interactive display through GHCi and you would destroy the window using destroy figure.

If you want to incorporate the plot in a standalone executable then use Graphics.UI.Gtk.Plot.plotNew and onDestroy window mainQUit.

fs <- newFigureState f
fig <- newMVar fs
handle <- newEmptyMVar :: IO (MVar DrawingArea)
--
postGUIAsync $ do
  window <- windowNew
  set window [ windowTitle := "Figure"
             , windowDefaultWidth := 400
             , windowDefaultHeight := 300
             , containerBorderWidth := 1
             ]
  --
  frame <- frameNew
  containerAdd window frame
  canvas <- plotNew fig
  containerAdd frame canvas
  --
  putMVar handle canvas
  --

...


da <- readMVar handle
Just fr <- widgetGetParent da
Just wi <- widgetGetParent fr
widgetDestroy wi

For more guidance look at Graphics.Rendering.Plot.Gtk in the plot-gtk package.

Upvotes: 2

Related Questions