从Graphics-Rendering-Plot-Gtk编译示例代码时出错

时间:2014-11-14 03:47:11

标签: haskell plot gtk

我尝试编译来自Graphics-Rendering-Plot-Gtk的示例代码,可用于hackage。

这是我得到的错误:

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’

我使用的是cabal沙箱,这是我的cabal文件:

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 <marlowsd@gmail.com>
stability: stable
homepage: http://www.haskell.org/hello/
bug-reports: mailto:marlowsd@gmail.com
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

最后这是我的测试代码:

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

对于开发我使用:

  • cabal sandboxes with cabal version 1.20.0.3
  • arch linux
  • ghc 7.8.3
  • leksah 0.14.1.0(但是命令行的cabal构建也失败了)

有谁知道为什么我会在问题开头提到错误?




更新

可能示例代码已过时导致我得到的错误。我修复了一些问题并运行了一个正在运行的版本:

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

这是cabal文件:

-- 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 <marlowsd@gmail.com>
bug-reports: mailto:marlowsd@gmail.com
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

还有两个问题。

首先 以下两行会导致类型错误,如果我没有对其进行评论(请参阅上面的代码):

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

其次,主要功能不是一个合适的GTK功能:我只是等待一段时间并关闭窗口。我希望窗口无限期地显示,并且窗口以正确的退出信号关闭,因为&#34; x&#34;按下窗口的按钮。

以下是main函数的解决方法:

...
figure1 <- displayFigure

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

1 个答案:

答案 0 :(得分:2)

这两行中的类型错误是因为您需要将Linear添加到

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

关于第二个问题,如何很好地退出,Graphics.Rendering.Plot.Gtk用于通过GHCi进行交互式显示,你可以使用destroy figure销毁窗口。

如果要将绘图合并到独立的可执行文件中,请使用Graphics.UI.Gtk.Plot.plotNewonDestroy 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

有关更多指导,请查看Graphics.Rendering.Plot.Gtk包中的plot-gtk