I've got a complete example here: https://github.com/chrissound/HaskellChartBarGraphExample/tree/backgroundColour
How can I set a background colour of a chart? I've tried fillBackground
.
chart :: Bool -> Renderable ()
chart borders = fillBackground (FillStyleSolid $ opaque green) $ toRenderable layout
...
However it does not seem to have any effect.
Complete source code:
module Main where
import Graphics.Rendering.Chart
import Graphics.Rendering.Chart.Backend.Cairo
import Data.Colour
import Data.Colour.Names
import Control.Lens
import Data.Default.Class
chart :: Bool -> Renderable ()
chart borders = fillBackground (FillStyleSolid $ opaque green) $ toRenderable layout
where
layout =
layout_title .~ "Sample Bars" ++ btitle
$ layout_title_style . font_size .~ 10
$ layout_x_axis . laxis_generate .~ autoIndexAxis alabels
$ layout_y_axis . laxis_override .~ axisGridHide
$ layout_left_axis_visibility . axis_show_ticks .~ False
$ layout_plots .~ [ plotBars bars2 ]
$ def :: Layout PlotIndex Double
bars2 = plot_bars_titles .~ ["Cash","Equity"]
$ plot_bars_values .~ addIndexes [[20,45],[45,30],[30,20],[70,25]]
$ plot_bars_style .~ BarsClustered
$ plot_bars_spacing .~ BarsFixGap 30 5
$ plot_bars_item_styles .~ map mkstyle (cycle defaultColorSeq)
$ def
alabels = [ "Jun", "Jul", "Aug", "Sep", "Oct" ]
btitle = if borders then "" else " (no borders)"
bstyle = if borders then Just (solidLine 1.0 $ opaque black) else Nothing
mkstyle c = (solidFillStyle c, bstyle)
main :: IO ()
main = do
_ <- renderableToFile def "example11_big.png" (chart True)
return ()
The default background color of a layout
is a solid white. This is somewhat hidden in the documentation of the Default
instance of layout
:
http://hackage.haskell.org/package/Chart-1.9/docs/Graphics-Rendering-Chart-Layout.html
In your example the green background is overlapped entirely by the white background of the layout
.
The background color of a layout
can be modified using the layout_background
lens.
Therefore to achieve a green you can either set the backgorund color of the layout to green directly:
layout =
layout_title .~ "Sample Bars" ++ btitle
$ layout_title_style . font_size .~ 10
$ layout_x_axis . laxis_generate .~ autoIndexAxis alabels
$ layout_y_axis . laxis_override .~ axisGridHide
$ layout_left_axis_visibility . axis_show_ticks .~ False
$ layout_plots .~ [ plotBars bars2 ]
$ layout_background .~ (FillStyleSolid $ opaque green)
$ def :: Layout PlotIndex Double
or set the backgorund color of the layout to transparent and use fillBackground
like you already do:
...
$ layout_background .~ (FillStyleSolid transparent)
...