Skip to content

Instantly share code, notes, and snippets.

@JackTheEngineer
Last active March 29, 2020 23:16
Show Gist options
  • Save JackTheEngineer/3bc50d1b7b3b6f2416af4166438ef6b8 to your computer and use it in GitHub Desktop.
Save JackTheEngineer/3bc50d1b7b3b6f2416af4166438ef6b8 to your computer and use it in GitHub Desktop.
Chart rendering code
-- | This function bridges gi-cairo with the hand-written cairo
-- package. It takes a `GI.Cairo.Context` (as it appears in gi-cairo),
-- and a `Render` action (as in the cairo lib), and renders the
-- `Render` action into the given context.
renderWithContext :: GI.Cairo.Context -> ReaderT Cairo IO a -> IO (a)
renderWithContext ct rendered = withManagedPtr ct $ \p -> runReaderT rendered (Cairo (castPtr p))
activateRender :: (Int, Int) -> (Channel, DrawingArea) -> IO ()
activateRender (width, height) (channel, cairoArea) = do
widgetSetSizeRequest cairoArea (fromIntegral width) (fromIntegral height)
on cairoArea #draw $ \context -> do
values <- readChannel channel
let chartRendered = render (chart values) (fromIntegral width, fromIntegral height)
asRender = runBackend (defaultEnv bitmapAlignmentFns) chartRendered
!rendered = (runRender asRender)
-- As far as i understand, "!rendered"
renderWithContext context rendered
return True
return ()
chart :: [(Float, Float)] -> Renderable ()
chart xyTuples = toRenderable layout
where
fontsize = 16
sinusoid2 = plot_points_style .~ filledCircles 1.5 (opaque blue)
$ plot_points_values .~ xyTuples
$ plot_points_title .~ "Magic"
$ def
layout = layout_title .~ "MagicLines"
$ layout_plots .~ [toPlot sinusoid2]
$ layout_x_axis . laxis_title .~ "Time"
$ layout_y_axis . laxis_title .~ "Amplitude"
$ layout_all_font_styles . font_size .~ fontsize
$ def
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment