haskell-chart icon indicating copy to clipboard operation
haskell-chart copied to clipboard

chart may be freezing on small but non-zero values

Open ghorn opened this issue 9 years ago • 4 comments

I have seen chart freeze my application when I am giving small but non-zero values. It is annoying to reproduce but I think I could make a stand-alone example if needed.

I expect plotting something like [(0, 0), (1, 0.5e-320), (2, 1.0e-320)] would reproduce this.

ghorn avatar Jun 14 '16 02:06 ghorn

This example will reproduce it for me

{-# OPTIONS_GHC -Wall #-}

import Control.Lens
import Control.Monad ( void )
import Data.Default.Class
import Graphics.Rendering.Chart
import Graphics.Rendering.Chart.Backend.Cairo

values :: [(Double, Double)]
values =
  [ (0, 1.0e-322)
  , (1, 1.0e-322)
  ]

chart :: Renderable ()
chart = toRenderable layout
  where
    plotLines :: PlotLines Double Double
    plotLines = plot_lines_values .~ [values]
                $ def

    layout = layout_plots .~ [toPlot plotLines]
           $ def

main :: IO ()
main = void $ renderableToFile def "example1_big.png" chart

ghorn avatar Jun 14 '16 17:06 ghorn

Those values are very small :-)

I think the issue is in the scaledAxis function. In particular the logic that searches for a nice axis scale loops forever if the range has size 0. We protect against this here:

    range []  = (0,1)
    range _   | minV == maxV = if minV==0 then (-1,1) else
                               let d = abs (minV * 0.01) in (minV-d,maxV+d)

but the logic fails if d ends up equal to zero, which it does for 1e-322.

timbod7 avatar Jun 16 '16 07:06 timbod7

I realize they're abnormal values, but I did come across this in a real world situation. Thanks for the pointer, I'll try to fix it it you don't get to it first.

ghorn avatar Jun 16 '16 08:06 ghorn

I have a fix in https://github.com/timbod7/haskell-chart/pull/130

ghorn avatar Jun 19 '16 00:06 ghorn