图表Gtk渲染失败

马丁·菲舍尔

我正在Haskell使用Chart模块。在活动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渲染?

杜普莱德

renderableToWindow 有类型...

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

...,因此它不会将EC您尝试传递计算作为最后一个参数。我相信最简单的解决方案是使用toWindow,它将EC以默认状态运行计算,将其结果转换为a并将其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]))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章