Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to overlay line and bar chart? #198

Open
reygoch opened this issue Nov 29, 2018 · 1 comment
Open

How to overlay line and bar chart? #198

reygoch opened this issue Nov 29, 2018 · 1 comment

Comments

@reygoch
Copy link

reygoch commented Nov 29, 2018

I want to create a bar chart of sin function by sampling a value on every natural number (sin 1, sin 2, etc.) but, I also want to overlay continuous sin function over that bar chart. I kind of understand why this isn't directly possible with bar chart, but maybe there is some trick that could help?

Currently I'm using "points" to represent how tall the bar would be for my disc array, but I can't replace plot $ points mempty disc with plot $ fmap plotBars $ bars [""] $ addIndexes $ fmap ( (:[]) . snd ) disc otherwise I get an error for obvious reasons.

import Graphics.Rendering.Chart.Easy
import Graphics.Rendering.Chart.Backend.Cairo
--
sinG :: IO ()
sinG = toFile def "sinus-continuous.png" $ do
  -- hi res "continuous" sin data
  let cont = [ ( x, sin x ) | x <- [ 0,0.1..50 ] :: [ Double ] ]
  -- lo res discrete sin data
  let disc = [ ( x, sin x ) | x <- [ 0,1..50] :: [ Double ] ]

  layout_title .= "Sinus Function"

  plot $ line   mempty [ cont ]
  plot $ points mempty   disc

sinus-continuous


This is how my discrete graph would look:

sinG :: IO ()
sinG = toFile def "sinus-discrete.png" $ do
  -- lo res discrete sin data
  let disc = [ ( x, sin x ) | x <- [ 0,1..50] :: [ Double ] ]

  layout_title .= "Sinus Function"

  plot $ fmap plotBars $ bars [""] $ addIndexes $ fmap ( (:[]) . snd ) disc

sinus-discrete


And here is the final result that I want to achieve (rendered in photoshop 😄):

overlayed-sin-function

@chrissound
Copy link

I believe a solution for this is here: https://www.reddit.com/r/haskellquestions/comments/hfl7sk/drawing_bar_charts_over_line_chart_using_chart/

plot $ plotBars <$> bars seems to be the solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants