这会做,但可以大大改进(尤其是通过删除 unsafePerformIO)。
{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
{-# OPTIONS_GHC -fno-warn-missing-methods #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
import Graphics.Rendering.Chart hiding ( translate )
import Graphics.Rendering.Chart.Backend.Diagrams
import Diagrams.Backend.Cairo.CmdLine
import Diagrams.Prelude hiding ( render, Renderable )
import Data.Default.Class
import Diagrams.Backend.CmdLine
import System.IO.Unsafe
pointVals :: [(Double, Double)]
pointVals = map (\(x,y) -> (log x, log y)) [(1,10), (10, 100), (100, 1000)]
dataPts :: PlotPoints Double Double
dataPts = plot_points_style .~ filledCircles 2 (opaque red)
$ plot_points_values .~ pointVals
$ plot_points_title .~ "Data points"
$ def
layout :: Layout Double Double
layout = layout_title .~ "Log vs Log"
$ layout_y_axis . laxis_generate .~ scaledAxis def (0,10)
$ layout_x_axis . laxis_generate .~ scaledAxis def (0,10)
$ layout_plots .~ [toPlot dataPts]
$ def
myChart :: Renderable ()
myChart = toRenderable layout
denv :: DEnv
denv = unsafePerformIO $ defaultEnv vectorAlignmentFns 500 500
displayHeader :: FilePath -> Diagram B R2 -> IO ()
displayHeader fn =
mainRender ( DiagramOpts (Just 900) (Just 700) fn
, DiagramLoopOpts False Nothing 0
)
myDiagram :: Diagram Cairo R2
myDiagram = fst $ runBackend denv (render myChart (500, 500))
main :: IO ()
main = displayHeader "LogChart.png" myDiagram