在 Haskell 中动态更新绘图

发布于 2024-12-07 18:52:22 字数 799 浏览 0 评论 0原文

我有一个程序执行长期计算,结果显示为绘图。 我目前正在使用 Chart-0.14 为此。我想显示部分结果,并在计算过程中更新。 Graphics.Rendering.Chart.Gtk.updateCanvas :: 可渲染 ->绘图区 -> IO Bool 似乎可以做到这一点,但我没有找到任何方法从图中获取 DrawingArea。函数renderableToWindow::Renderable a ->整数->整数-> IO() 不返回任何内容(而且在窗口关闭之前它也不会返回)。

我想做类似以下的事情:

main = do
drawingArea = forkRenderableToWindow (toRenderable $ plotLayout $
                     plot [0,0.1..10] sin "sin(x)") 640 480
updateCanvas  (toRenderable $ plotLayout $  plot [0,0.1..10] sin "sin(x)") drawingArea

我应该怎么做?我是否需要使用返回 DrawingArea 的版本以及以某种方式重新实现 Graphics.Rendering.Chart.Gtk 中的函数(我该怎么做?forkIO?)不关闭窗口就立即返回?

I have a program which performs a long-going calculation where the result is shown as a plot.
I am currently using Chart-0.14 for this. I want to show the partial results, and update during calculations.
Graphics.Rendering.Chart.Gtk.updateCanvas :: Renderable a -> DrawingArea -> IO Bool seems to do that, but I do not find any way to get a DrawingArea from the plot. The function renderableToWindow :: Renderable a -> Int -> Int -> IO () does not return anything (and furthermore it does not return before the window is closed).

I would like to do something like the following:

main = do
drawingArea = forkRenderableToWindow (toRenderable $ plotLayout $
                     plot [0,0.1..10] sin "sin(x)") 640 480
updateCanvas  (toRenderable $ plotLayout $  plot [0,0.1..10] sin "sin(x)") drawingArea

How should I do this? Would I need to reimplement the functions in Graphics.Rendering.Chart.Gtk with a version that returns the DrawingArea and in some way (how would I do this? forkIO?) returns immediately without closing the window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆沫 2024-12-14 18:52:22

您正在寻找 createRenderableWindow 然后您需要使用 GTK 操作来处理给定的 Window - 我不认为 Chart 包导出任何更高级别Window 上的操作。

EDIT2:所以忽略下面的内容 - 即使 GUI 初始化它也不起作用。我的评论是基于类型的猜测。
编辑:
这是一些示例代码。请理解,我只是根据类型将事物拼凑在一起。如果你询问真正了解图书馆的人,可能会有更好的方法。

下面我们使用:

  1. createRenderableWindow - 这是我答案的关键
  2. castToDrawingArea - 这是从 Window< 获取 DrawingArea 所必需的/code> GTK 提供的类型。我认为这些强制转换正在取代 C++ OO 继承。
  3. widgetShowAll - 因为我们还没有实际显示窗口,所以我们最好这样做。我在查看了 renderableToWindow 的源代码后盗用了这个函数。
  4. updateCanvas - 我刚刚在 haddock 文档中看到了这一点,并认为这就是为什么您首先需要 DrawingArea 的原因。

现在的代码:

import Graphics.Rendering.Chart.Gtk
import Graphics.Rendering.Chart.Renderable
import Graphics.UI.Gtk.Misc.DrawingArea
import qualified Graphics.UI.Gtk as G

main = do
        win  <- createRenderableWindow emptyRenderable 400 400
        let draw = castToDrawingArea win
        G.widgetShowAll win
        updateCanvas emptyRenderable draw

You are looking for createRenderableWindow and then you need to use the GTK operations to work on the given Window - I don't think the Chart package exports any higher level operations on Windows.

EDIT2: So ignore the below - it doesn't work even with GUI initilization. My comment was a guess based on types.
EDIT:
Here is some example code. Understand, I'm just piecing things together based on the types. There might be better ways to do things if you ask someone who actually knows the library.

Below we use:

  1. createRenderableWindow - this was the crux of my answer
  2. castToDrawingArea - This is needed to get a DrawingArea from the Window type provided by GTK. These casts are taking place of C++ OO inheritance, I think.
  3. widgetShowAll - because we haven't actually displayed the window, we best do that. I stole this function after looking at the source for renderableToWindow.
  4. updateCanvas - I just saw this in the haddock documentation and figured it is why you wanted a DrawingArea in the first place.

Now for the code:

import Graphics.Rendering.Chart.Gtk
import Graphics.Rendering.Chart.Renderable
import Graphics.UI.Gtk.Misc.DrawingArea
import qualified Graphics.UI.Gtk as G

main = do
        win  <- createRenderableWindow emptyRenderable 400 400
        let draw = castToDrawingArea win
        G.widgetShowAll win
        updateCanvas emptyRenderable draw
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文