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

如何在Haskell图表包中设置图表的背景色?,haskell,haskell-chart,Haskell,Haskell Chart,我这里有一个完整的例子: 如何设置图表的背景色?我已经尝试了fillBackground chart :: Bool -> Renderable () chart borders = fillBackground (FillStyleSolid $ opaque green) $ toRenderable layout ... 然而,它似乎没有任何效果 完整的源代码: module Main where import Graphics.Rendering.Chart import G

我这里有一个完整的例子:

如何设置图表的背景色?我已经尝试了
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 ()
modulemain其中
导入Graphics.Rendering.Chart
导入Graphics.Rendering.Chart.Backend.Cairo
进口数据.颜色
导入数据.颜色.名称
进口管制.镜头
导入Data.Default.Class
图表::布尔->可渲染()
图表边框=填充背景(填充样式实心$不透明绿色)$toRenderable布局
哪里
布局=
布局图标题~“样条”++B标题
$layout\u title\u样式。字体大小。~10
$layout_x_轴。laxis_generate.~autoIndexAxis alabels
$layout_y_轴。laxis_覆盖。~axisGridHide
$layout\u left\u axis\u可见性。轴显示刻度。~错误
$layout\u plots.~[plotBars-bars2]
$def::布局图索引双精度
bars2=地块\条形图\标题。~[“现金”、“权益”]
$plot\u bars\u value.~添加索引[[20,45]、[45,30]、[30,20]、[70,25]]
$plot\u bars\u样式。~条形
$plot\U bars\U间距~BarsFixGap 30 5
$plot_bars_item_styles.~map mkstyle(循环默认颜色顺序)
$def
阿拉贝尔=[“六月”、“七月”、“八月”、“九月”、“十月”]
btitle=如果有边框,则为“其他”(无边框)
b样式=如果边框,则只需(实线1.0$不透明黑色)其他内容
mkstyle c=(solidFillStyle c,bstyle)
main::IO()
main=do

_版面的默认背景色为纯白色。这在
布局
默认
实例的文档中有些隐藏:

在您的示例中,
布局的绿色背景与白色背景完全重叠。
可以使用
layout\u background
镜头修改
版面的背景色

因此,要实现绿色,您可以直接将布局的背景颜色设置为绿色:

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

...    
$ layout_background .~ (FillStyleSolid transparent)
...