2015-10-20 83 views
1

我在Haskell和使用Chart模塊。 在活動的GTK窗口中沒有渲染文檔。所以我tryed我自己:圖Gtk渲染失敗

import Graphics.Rendering.Chart.Easy 
import Graphics.Rendering.Chart.Gtk 

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

main = renderableToWindow def 300 300 $ 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])) 

但是,這是不正確的,造成ghc -o main main.hs回報:

[1 of 1] Compiling Main    (main.hs, main.o) 

main.hs:7:8: 
    Couldn't match expected type ‘Control.Monad.Trans.State.Lazy.StateT 
            (Layout Double Double) 
            (Control.Monad.Trans.State.Lazy.State CState) 
            () 
            -> t’ 
       with actual type ‘IO()’ 
    Relevant bindings include main :: t (bound at main.hs:7:1) 
    The first argument of ($) takes one argument, 
    but its type ‘IO()’ has none 
    In the expression: 
     renderableToWindow def 300 300 
     $ 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])) } 
    In an equation for ‘main’: 
     main 
      = renderableToWindow def 300 300 
      $ do { layout_title .= "Amplitude Modulation"; 
        setColors [opaque blue, ....]; 
        plot (line "am" [signal ...]); 
        .... } 

所以現在我的問題:如何做出正確的GTK渲染?

回答

2

renderableToWindow有型......

renderableToWindow :: Renderable a -> Int -> Int -> IO() 

...所以它不走EC計算你試圖傳遞的最後一個參數。我相信最簡單的解決方案是使用toWindow,這將默認狀態運行EC計算,其結果轉換爲Renderable,並把它傳遞給renderableToWindow

toWindow :: (Default r, ToRenderable r) => Int -> Int -> EC r() -> IO()

main = toWindow 300 300 $ 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])) 
+0

謝謝!完美解決方案 –