背景
Haskell 项目需要使用到柱状图,折线图等(demo 代码)
步骤(默认已安装 stack, cabal, ghcup)
name | version |
---|---|
stack | 2.11.1 |
cabal | 3.8.1.0 |
ghcup | 0.1.20.0 |
- 在 package.yaml 中添加所需依赖 Chart 和 Chart-cairo
name: a
version: 0.1.0.0
github: "githubuser/a"
license: BSD-3-Clause
author: "Author name here"
maintainer: "example@example.com"
copyright: "2023 Author name here"
extra-source-files:
- README.md
- CHANGELOG.md
# Metadata used when publishing your package
# synopsis: Short description of your package
# category: Web
# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description: Please see the README on GitHub at <https://github.com/githubuser/a#readme>
dependencies:
- base >= 4.7 && < 5
ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints
library:
source-dirs:
- src
- app
dependencies:
- bytestring
- http-conduit
- sqlite-simple
- Chart
- text
# - Chart-diagrams
# - diagrams
# - diagrams-svg
# - diagrams-lib
- colour
- data-default-class
# common
- Chart-cairo
- lens
- aeson
- stm
- split
- text
- time
- binary
- binary-orphans
- old-locale
- directory
- pretty-simple
- string-conversions
executables:
a-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- a
- sqlite-simple
- Chart
- text
- bytestring
# - Chart-diagrams
# - diagrams
# - diagrams-svg
# - diagrams-lib
- colour
- data-default-class
# common
- Chart-cairo
- lens
- aeson
- stm
- split
- text
- time
- binary
- binary-orphans
- old-locale
- directory
- pretty-simple
- string-conversions
tests:
a-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- a
运行 stack-build,编译失败,大概提示未指定 cairo 版本。
- 在 stack.yaml 中添加 chart 版本信息
# This file was automatically generated by 'stack init'
#
# Some commonly used options have been documented as comments in this file.
# For advanced use and comprehensive documentation of the format, please see:
# https://docs.haskellstack.org/en/stable/yaml_configuration/
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
# A snapshot resolver dictates the compiler version and the set of packages
# to be used for project dependencies. For example:
#
# resolver: lts-3.5
# resolver: nightly-2015-09-21
# resolver: ghc-7.10.2
#
# The location of a snapshot can be provided as a file or url. Stack assumes
# a snapshot provided as a file might change, whereas a url resource does not.
#
# resolver: ./custom-snapshot.yaml
# resolver: https://example.com/snapshots/2018-01-01.yaml
# resolver: lts-21.13
resolver: lts-21.13
# url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/22.yaml
# User packages to be built.
# Various formats can be used as shown in the example below.
#
# packages:
# - some-directory
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
# subdirs:
# - auto-update
# - wai
packages:
- .
# Dependency packages to be pulled from upstream that are not in the resolver.
# These entries can reference officially published versions as well as
# forks / in-progress versions pinned to a git hash. For example:
#
# extra-deps:
# - acme-missiles-0.3
# - git: https://github.com/commercialhaskell/stack.git
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
#
# extra-deps: []
# Override default flag values for local packages and extra-deps
# flags: {}
# Extra package databases containing global packages
# extra-package-dbs: []
# Control whether we use the GHC we find on the path
# system-ghc: true
#
# Require a specific version of Stack, using version ranges
# require-stack-version: -any # Default
# require-stack-version: ">=2.11"
#
# Override the architecture used by Stack, especially useful on Windows
# arch: i386
# arch: x86_64
#
# Extra directories used by Stack for building
# extra-include-dirs: [/path/to/dir]
# extra-lib-dirs: [/path/to/dir]
#
# Allow a newer minor version of GHC than the snapshot specifies
# compiler-check: newer-minor
extra-deps:
- Chart-1.9.5
- Chart-cairo-1.9.4.1
flags: {}
extra-package-dbs: []
运行 stack build,报错。
ErrorTrace:
cairo > configure
cairo > [1 of 3] Compiling Main ( C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\Setup.hs, C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\.stack-work\dist\22605e11\setup\Main.o )
cairo > [2 of 3] Compiling StackSetupShim ( C:\sr\setup-exe-src\setup-shim-9p6GVs8J.hs, C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\.stack-work\dist\22605e11\setup\StackSetupShim.o )
cairo > [3 of 3] Linking C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\.stack-work\dist\22605e11\setup\setup.exe
cairo > Configuring cairo-0.13.10.0...
cairo > Error: setup.EXE: The pkg-config package 'cairo' version >=1.2.0 is required
cairo > but it could not be found.
cairo >
Progress 1/3
Error: [S-7282]
Stack failed to execute the build plan.
While executing the build plan, Stack encountered the error:
[S-7011]
While building package cairo-0.13.10.0 (scroll up to its section to see the error) using:
C:\Users\df366\AppData\Local\Temp\stack-96faf9bec5d62ffb\cairo-0.13.10.0\.stack-work\dist\22605e11\setup\setup --verbose=1 --builddir=.stack-work\dist\22605e11 configure --with-ghc=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\ghc-9.4.7\bin\ghc-9.4.7.exe --with-ghc-pkg=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\ghc-9.4.7\bin\ghc-pkg-9.4.7.exe --user --package-db=clear --package-db=global --package-db=C:\sr\snapshots\9095fbbf\pkgdb --libdir=C:\sr\snapshots\9095fbbf\lib --bindir=C:\sr\snapshots\9095fbbf\bin --datadir=C:\sr\snapshots\9095fbbf\share --libexecdir=C:\sr\snapshots\9095fbbf\libexec --sysconfdir=C:\sr\snapshots\9095fbbf\etc --docdir=C:\sr\snapshots\9095fbbf\doc\cairo-0.13.10.0 --htmldir=C:\sr\snapshots\9095fbbf\doc\cairo-0.13.10.0 --haddockdir=C:\sr\snapshots\9095fbbf\doc\cairo-0.13.10.0 --dependency=Cabal=Cabal-3.8.1.0 --dependency=array=array-0.5.4.0 --dependency=base=base-4.17.2.0 --dependency=bytestring=bytestring-0.11.5.2 --dependency=gtk2hs-buildtools=gtk2hs-buildtools-0.13.10.0-7OxGn4htize3gcv93EYR9n --dependency=mtl=mtl-2.2.2 --dependency=text=text-2.0.2 --dependency=utf8-string=utf8-string-1.0.2-KqazO8nUWrDMJ2shK7HoQ -fcairo_pdf -fcairo_ps -fcairo_svg --ghc-options -haddock --extra-include-dirs=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\include --extra-lib-dirs=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\lib --extra-lib-dirs=C:\Users\df366\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\bin --exact-configuration --ghc-option=-fhide-source-paths
Process exited with code: ExitFailure 1
提示 gcc 自带的 pkg-config 没有高于 1.2.0 的 cairo 包
- 安装 MSYS2, 并配置 Path, 目的是用到 MSYS2 中的 pkg-config.
安装相关工具
# gcc
pacman -S mingw-w64-ucrt-x86_64-gcc
# gtk
pacman -S mingw-w64-x86_64-gtk4
# 语言开发包
pacman -S mingw-w64-x86_64-toolchain base-devel
- 重启 PowerShell 和 VSCode
- 在 VSCode 的 PowerShell 中运行
$Env:PATH -split ';'
PS C:\Users\df366\a\a> $Env:PATH -split ';'
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Windows\System32\OpenSSH\
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR
C:\Program Files\dotnet\
C:\Program Files\Docker\Docker\resources\bin
D:\Git\cmd
D:\MSYS2\mingw64\bin
D:\MSYS2\mingw64\lib
D:\MSYS2\mingw64\include
C:\Users\df366\AppData\Roaming\local\bin
C:\Users\df366\AppData\Local\Microsoft\WindowsApps
C:\Users\df366\.dotnet\tools
C:\Program Files\MongoDB\Server\7.0\bin
D:\Microsoft VS Code\bin
C:\ghcup\bin
D:\PyCharm 2023.2.2\bin
注意:这里必须重启 VSCode,否则系统环境中的变量不会更新。
- 检查 pkg-config
Get-Command pkg-config
CommandType Name Version Source
----------- ---- ------- ------
Application pkg-config.exe 0.0.0.0 D:\MSYS2\mingw64\bin\pkg-config.exe
发现 pkg-config 已经变成 MSYS2 下的了
- 重新执行 stack build,运行成功
PS C:\Users\df366\a\a> stack build
a-0.1.0.0: unregistering (local file changes: app\Main.hs)
a> build (lib + exe)
Preprocessing library for a-0.1.0.0..
Building library for a-0.1.0.0..
[6 of 7] Compiling Main [Source file changed]
app\Main.hs:3:1: warning: [-Wunused-imports]
The import of 鈥楩etch鈥?is redundant
except perhaps to import instances from 鈥楩etch鈥?[0m
To import instances alone, use: import Fetch()
|
3 | import Fetch
| ^^^^^^^^^^^^
app\Main.hs:4:1: warning: [-Wunused-imports]
The import of 鈥楧ata.Text鈥?is redundant
except perhaps to import instances from 鈥楧ata.Text鈥?[0m
To import instances alone, use: import Data.Text()
|
4 | import Data.Text (Text, splitOn)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app\Main.hs:5:1: warning: [-Wunused-imports]
The qualified import of 鈥楧ata.Text鈥?is redundant
except perhaps to import instances from 鈥楧ata.Text鈥?[0m
To import instances alone, use: import Data.Text()
|
5 | import qualified Data.Text as T
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app\Main.hs:6:1: warning: [-Wunused-imports]
The import of 鈥楧atabase鈥?is redundant
except perhaps to import instances from 鈥楧atabase鈥?[0m
To import instances alone, use: import Database()
|
6 | import Database
| ^^^^^^^^^^^^^^^
[7 of 7] Linking app\Main.exe [Objects changed]
Preprocessing executable 'a-exe' for a-0.1.0.0..
Building executable 'a-exe' for a-0.1.0.0..
[1 of 2] Compiling Main [Source file changed]
app\Main.hs:3:1: warning: [-Wunused-imports]
The import of 鈥楩etch鈥?is redundant
except perhaps to import instances from 鈥楩etch鈥?[0m
To import instances alone, use: import Fetch()
|
3 | import Fetch
| ^^^^^^^^^^^^
app\Main.hs:4:1: warning: [-Wunused-imports]
The import of 鈥楧ata.Text鈥?is redundant
except perhaps to import instances from 鈥楧ata.Text鈥?[0m
To import instances alone, use: import Data.Text()
|
4 | import Data.Text (Text, splitOn)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app\Main.hs:5:1: warning: [-Wunused-imports]
The qualified import of 鈥楧ata.Text鈥?is redundant
except perhaps to import instances from 鈥楧ata.Text鈥?[0m
To import instances alone, use: import Data.Text()
|
5 | import qualified Data.Text as T
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app\Main.hs:6:1: warning: [-Wunused-imports]
The import of 鈥楧atabase鈥?is redundant
except perhaps to import instances from 鈥楧atabase鈥?[0m
To import instances alone, use: import Database()
|
6 | import Database
| ^^^^^^^^^^^^^^^
[3 of 3] Linking .stack-work\dist\22605e11\build\a-exe\a-exe.exe [Objects changed]
a> copy/register
Installing library in C:\Users\df366\a\a\.stack-work\install\1b692ffa\lib\x86_64-windows-ghc-9.4.7\a-0.1.0.0-5LEhwaAmmakDiuPZZmv67T
Installing executable a-exe in C:\Users\df366\a\a\.stack-work\install\1b692ffa\bin
Registering library for a-0.1.0.0..
- Main 代码
module Main (main) where
import Fetch
import Data.Text (Text, splitOn)
import qualified Data.Text as T
import Database
-- import Diagrams.Prelude
-- import Diagrams.Backend.SVG
-- import Diagrams.Attributes
-- import Diagrams.Backend.SVG.CmdLine
import Graphics.Rendering.Chart
import Graphics.Rendering.Chart.Backend.Cairo
import Data.Colour
import Control.Lens
import Data.Default.Class
chart :: Bool -> Renderable ()
chart borders = 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 ()
-- spilitByLine :: String -> [String]
-- spilitByLine s = map T.unpack $ splitOn (T.pack "\n") (T.pack s)
-- main :: IO ()
-- main = do
-- a <- downloadURL "http://prod.publicdata.landregistry.gov.uk.s3-website-eu-west-1.amazonaws.com/pp-2022.csv"
-- let items = spilitByLine a
-- print $ length items
-- print $ head items
-- print $ items !! 1
-- print 33
-- print "3.6.2.0"
-
输出图
-
cabal 文件(无需修改,在 package.yaml 添加依赖后,运行 stack update 会同步更新)
cabal-version: 2.2
-- This file has been generated from package.yaml by hpack version 0.35.2.
--
-- see: https://github.com/sol/hpack
name: a
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/a#readme>
homepage: https://github.com/githubuser/a#readme
bug-reports: https://github.com/githubuser/a/issues
author: Author name here
maintainer: example@example.com
copyright: 2023 Author name here
license: BSD-3-Clause
license-file: LICENSE
build-type: Simple
extra-source-files:
README.md
CHANGELOG.md
source-repository head
type: git
location: https://github.com/githubuser/a
library
exposed-modules:
Database
Fetch
Lib
Types
Main
other-modules:
Paths_a
autogen-modules:
Paths_a
hs-source-dirs:
src
app
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
build-depends:
Chart
, Chart-cairo
, aeson
, base >=4.7 && <5
, binary
, binary-orphans
, bytestring
, colour
, data-default-class
, directory
, http-conduit
, lens
, old-locale
, pretty-simple
, split
, sqlite-simple
, stm
, string-conversions
, text
, time
default-language: Haskell2010
executable a-exe
main-is: Main.hs
other-modules:
Paths_a
autogen-modules:
Paths_a
hs-source-dirs:
app
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
Chart
, Chart-cairo
, a
, aeson
, base >=4.7 && <5
, binary
, binary-orphans
, bytestring
, colour
, data-default-class
, directory
, lens
, old-locale
, pretty-simple
, split
, sqlite-simple
, stm
, string-conversions
, text
, time
default-language: Haskell2010
test-suite a-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_a
autogen-modules:
Paths_a
hs-source-dirs:
test
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
a
, base >=4.7 && <5
default-language: Haskell2010
参考博客:
https://stackoverflow.com/questions/57940965/pkg-config-installed-but-not-found
https://stackoverflow.com/questions/44314060/haskell-stack-nix-build-fails-requiring-pkg-config-0-9-0-and-cairo-1
https://stackoverflow.com/questions/5336865/cant-install-cairo-with-cabal-on-windows-how-to-get-pkg-config-on-win
https://stackoverflow.com/questions/63396432/having-dependency-issues-while-trying-to-install-threadscope-with-stack-the-iss
https://github.com/commercialhaskell/stack/issues/377
https://stackoverflow.com/questions/77441659/haskell-after-install-the-conduit-package
https://zhuanlan.zhihu.com/p/623146940