如何在Haskell图表包中设置图表的背景色?

时间:2018-12-29 17:57:02

标签: haskell haskell-chart

我在这里有一个完整的示例:https://github.com/chrissound/HaskellChartBarGraphExample/tree/backgroundColour

如何设置图表的背景色?我已经尝试过fillBackground

chart :: Bool -> Renderable ()
chart borders = fillBackground (FillStyleSolid $ opaque green)  $ toRenderable layout
...

但是它似乎没有任何作用。

完整的源代码:

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 ()

1 个答案:

答案 0 :(得分:1)

layout的默认背景颜色是纯白色。这在Default的{​​{1}}实例的文档中有些隐藏: http://hackage.haskell.org/package/Chart-1.9/docs/Graphics-Rendering-Chart-Layout.html

在您的示例中,绿色背景与layout的白色背景完全重叠。 可以使用layout镜头修改layout的背景色。

因此,要获得绿色,您可以将布局的backgorund颜色直接设置为绿色:

layout_background

或将布局的backgorund颜色设置为透明,并像您已经使用的那样使用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

fillBackground