timbod7 / haskell-chart

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

Point Coordinates are Truncated to Integers #256

Closed julmb closed 6 months ago

julmb commented 6 months ago

I am generating bitmap images of charts via Chart -> Chart-diagrams -> diagrams-rasterific -> JuicyPixels.

Consider this chart:

example :: EC (Layout Double Double) ()
example = do
    plot $ line "line" [[(-2, -1), (-1, 0), (0, 1e-12), (1, 0), (2, 1)]]
    let points step offset = p <$> [0, step .. 2] where p x = (x - 1, exp (x - 4) - offset)
    plot $ line "exp" [points 0.01 0.5, points 0.02 0.4, points 0.05 0.3, points 0.1 0.2]

On my system, this produces:

It seems to me like all start and end coordinates of lines are truncated to integer coordinates. This causes a single pixel tall bump in the blue line (despite 1e-12 being well below a single pixel at this resolution). It also causes aliasing artifacts in the lower two green lines and jaggedness (non-monotonous first derivative despite this being a plot of the exponential function) in the upper two green lines.

julmb commented 6 months ago

I have confirmed that this also happens when using diagrams-svg instead of diagrams-rasterific to render to SVG files directly:

julmb commented 6 months ago

Nevermind, I overlooked the AlignmentFns parameter to the createEnv function. This gets rid of the artifacts, at the cost of misaligning text and straight lines like axes and ticks.

It would be nice if there was a way to have text, axes, ticks, etc. aligned to the pixel grid, while allowing things like line charts to be drawn at fractional coordinates for accuracy and lack of artifacts.

However, turning off alignment via vectorAlignmentFns is good enough for me right now.