timbod7 / haskell-chart

A 2D charting library for haskell
429 stars 85 forks source link

"Exception: Prelude.!!: negative index" when using AreaSpots4D #225

Open chrissound opened 3 years ago

chrissound commented 3 years ago
import Graphics.Rendering.Chart.Easy
import Graphics.Rendering.Chart.Backend.Cairo
import Data.Time.LocalTime
import Control.Lens
import Data.Colour
import Data.Colour.Names

renderCfg :: FileOptions
renderCfg = FileOptions (2100, 1600) PNG

renderCfg' :: Int -> Int -> FileOptions
renderCfg' a b = FileOptions (a, b) PNG

spots :: [(Double, Int, Double, Double)] -> AreaSpots4D Double Double Double Int 
spots v = area_spots_4d_title .~ "random value"
          $ area_spots_4d_max_radius .~ 20
          $ area_spots_4d_values .~ v
          $ area_spots_4d_palette .~ [red, blue]
          $ def

chart' :: Renderable ()
chart' = toRenderable $
    (layout_plots .~ [toPlot (spots v)]) (def )
      where v =
              [ (0,0,0,0)
              ]

Just trying to get a simple chart based on https://github.com/timbod7/haskell-chart/wiki/example-8 however getting a Exception: Prelude.!!: negative index error unfortunately...

Definitely related to the v = [ (0,0,0,0)] as when I instead set it to [] no error occurs...

I'm just not sure why this exception is being triggered.


Hacking the source code of the library:

-              let colour  = _area_spots_4d_palette p !! t 
+
+              let colour  = case _area_spots_4d_palette p !!? t of
+                               Nothing -> do
+                                 error $ show t ++ "out of" ++ (show $ _area_spots_4d_palette p)
+                               Just colour' -> colour'

Which then errors with:

*** Exception: -9223372036854775808out of[Data.Colour.SRGB.Linear.rgb 1.0 0.0 0.0,Data.Colour.SRGB.Linear.rgb 0.0 0.0 1.0]

No idea where -9223372036854775808 is coming from... ?

chrissound commented 3 years ago

The issue seem to be that it requires at least 2 different values for the area(?).

If I change v to [ (0,0,0,0) , (0,0,0,1) ] it works correctly - no error is encountered.