Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyday567 committed Oct 13, 2024
1 parent eb0e046 commit bc1fc60
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 106 deletions.
42 changes: 22 additions & 20 deletions .github/workflows/haskell-ci.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
name: build
on: [push]
name: haskell-ci

# INFO: The following configuration block ensures that only one build runs per branch,
# which may be desirable for projects with a costly build process.
# Remove this block from the CI workflow to let each CI job run to completion.
concurrency:
group: build-${{ github.ref }}
cancel-in-progress: true

jobs:
hlint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: haskell-actions/hlint-setup@v2
- uses: haskell-actions/hlint-run@v2
with:
Expand All @@ -14,8 +22,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: haskell-actions/run-ormolu@v15
cabal:
- uses: haskell-actions/run-ormolu@v16
build:
name: GHC ${{ matrix.ghc-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
Expand All @@ -39,38 +47,32 @@ jobs:
with:
ghc-version: ${{ matrix.ghc-version }}

- name: Installed minor versions of GHC and Cabal
shell: bash
run: |
GHC_VERSION=$(ghc --numeric-version)
CABAL_VERSION=$(cabal --numeric-version)
echo "GHC_VERSION=${GHC_VERSION}" >> "${GITHUB_ENV}"
echo "CABAL_VERSION=${CABAL_VERSION}" >> "${GITHUB_ENV}"
- name: Configure the build
run: |
cabal configure --enable-tests --enable-benchmarks --disable-documentation
cabal build --dry-run
# The last step generates dist-newstyle/cache/plan.json for the cache key.

- name: Restore cached dependencies
uses: actions/cache/restore@v3
uses: actions/cache/restore@v4
id: cache
env:
key: ${{ runner.os }}-ghc-${{ steps.setup.outputs.ghc-version }}-cabal-${{ steps.setup.outputs.cabal-version }}
with:
path: ${{ steps.setup.outputs.cabal-store }}
key: ${{ runner.os }}-ghc-${{ env.GHC_VERSION }}-cabal-${{ env.CABAL_VERSION }}-plan-${{ hashFiles('**/plan.json') }}
restore-keys: |
${{ runner.os }}-ghc-${{ env.GHC_VERSION }}-cabal-${{ env.CABAL_VERSION }}-
key: ${{ env.key }}-plan-${{ hashFiles('**/plan.json') }}
restore-keys: ${{ env.key }}-

- name: Install dependencies
# If we had an exact cache hit, the dependencies will be up to date.
if: steps.cache.outputs.cache-hit != 'true'
run: cabal build all --only-dependencies

# Cache dependencies already here, so that we do not have to rebuild them should the subsequent steps fail.
- name: Save cached dependencies
uses: actions/cache/save@v3
# Caches are immutable, trying to save with the same key would error.
if: ${{ !steps.cache.outputs.cache-hit
|| steps.cache.outputs.cache-primary-key != steps.cache.outputs.cache-matched-key }}
uses: actions/cache/save@v4
# If we had an exact cache hit, trying to save the cache would error because of key clash.
if: steps.cache.outputs.cache-hit != 'true'
with:
path: ${{ steps.setup.outputs.cabal-store }}
key: ${{ steps.cache.outputs.cache-primary-key }}
Expand Down
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.7
===

- switch to harpie

0.6.1
===

Expand Down
8 changes: 0 additions & 8 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
packages:
chart-svg.cabal

allow-newer:
markup-parse:containers

source-repository-package
type: git
branch: master
location: https://github.com/conal/vector-space.git

write-ghc-environment-files: always

tests: True
6 changes: 1 addition & 5 deletions chart-svg.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tested-with:
extra-doc-files:
ChangeLog.md
other/*.svg
readme.org
readme.md

source-repository head
type: git
Expand All @@ -62,14 +62,11 @@ library
hs-source-dirs: src
build-depends:
, Color >=0.3.2 && <0.4
, adjunctions >=4.0 && <5
, attoparsec >=0.13.2 && <0.15
, base >=4.14 && <5
, bytestring >=0.11.3 && <0.13
, containers >=0.6 && <0.8
, cubicbezier >=0.6 && <0.7
, flatparse >=0.5 && <0.6
, foldl >=1.4 && <1.5
, formatn >=0.3 && <0.4
, harpie >=0.1 && <0.2
, markup-parse >=0.1 && <0.2
Expand Down Expand Up @@ -102,7 +99,6 @@ test-suite doctests
hs-source-dirs: test
build-depends:
, base >=4.14 && <5
, chart-svg
, doctest-parallel >=0.3 && <0.4
ghc-options: -threaded
type: exitcode-stdio-1.0
File renamed without changes.
45 changes: 45 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

# Table of Contents

1. [Usage](#org829f981)
2. [Examples](#orge2856e9)

[![img](https://img.shields.io/hackage/v/chart-svg.svg)](https://hackage.haskell.org/package/chart-svg) [![img](https://github.com/tonyday567/chart-svg/workflows/haskell-ci/badge.svg)](https://github.com/tonyday567/chart-svg/actions?query=workflow%3Ahaskell-ci)

![img](other/banner.svg)

A charting library targetting SVG.


<a id="org829f981"></a>

# Usage

:r
:set prompt "> "
:set -XOverloadedLabels
:set -XOverloadedStrings
import Chart
import Optics.Core
lines = [[Point 0.0 1.0, Point 1.0 1.0, Point 2.0 5.0],[Point 0.0 0.0, Point 2.8 3.0],[Point 0.5 4.0, Point 0.5 0]]
styles = (\c -> defaultLineStyle & #color .~ palette c & #size .~ 0.015) <$> [0..2]
cs = zipWith (\s x -> LineChart s [x]) styles lines
lineExample = mempty & #chartTree .~ named "line" cs & #hudOptions .~ defaultHudOptions :: ChartOptions
writeChartOptions "other/usage.svg" lineExample

![img](other/usage.svg)

See the haddock documentation for a detailed overview.


<a id="orge2856e9"></a>

# Examples

To redraw all the examples in Chart.Examples

import Chart.Examples
writeAllExamples

ok

61 changes: 1 addition & 60 deletions src/Data/Colour.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Colour representations and combinations.
module Data.Colour
Expand All @@ -17,11 +16,7 @@ module Data.Colour
showOpacity,
opac',
opac,
hex,
rgb,
toHex,
fromHex,
unsafeFromHex,

-- * Palette colours
palette,
Expand Down Expand Up @@ -67,16 +62,12 @@ module Data.Colour
where

import Chart.Data
import Data.Attoparsec.Text qualified as A
import Data.Bifunctor
import Data.Bool (bool)
import Data.ByteString (ByteString)
import Data.Char
import Data.Either
import Data.FormatN
import Data.List qualified as List
import Data.String.Interpolate
import Data.Text (Text, pack)
import Data.Text (Text)
import Data.Text qualified as Text
import GHC.Generics hiding (prec)
import Graphics.Color.Model as M hiding (LCH)
Expand Down Expand Up @@ -180,60 +171,10 @@ showOpacity c =
opac' :: Lens' Colour Double
opac' = lens opac (\(Colour r g b _) o -> Colour r g b o)

-- | Convert to CSS hex representation.
hex :: Colour -> Text
hex c = toHex c

-- | Sets RGB color but not opacity
rgb :: Colour -> Colour -> Colour
rgb (Colour r g b _) (Colour _ _ _ o) = Colour r g b o

-- | Parse CSS hex text.
parseHex :: A.Parser (Color RGB Double)
parseHex =
fmap toDouble
. ( \((r, g), b) ->
ColorRGB (fromIntegral r) (fromIntegral g) (fromIntegral b) :: Color RGB Word8
)
. (\(f, b) -> (f `divMod` (256 :: Int), b))
. (`divMod` 256)
<$> (A.string "#" *> A.hexadecimal)

-- | Convert CSS hex to Colour
fromHex :: Text -> Either Text (Color RGB Double)
fromHex = first pack . A.parseOnly parseHex

-- | Convert CSS hex to Colour, unsafely.
unsafeFromHex :: Text -> Color RGB Double
unsafeFromHex t = fromRight (ColorRGB 0 0 0) $ A.parseOnly parseHex t

-- | Convert from 'Colour' to CSS hex (#xxxxxx)
toHex :: Colour -> Text
toHex c =
"#"
<> Text.justifyRight 2 '0' (hex' r)
<> Text.justifyRight 2 '0' (hex' g)
<> Text.justifyRight 2 '0' (hex' b)
where
(ColorRGBA r g b _) = fromIntegral . toWord8 <$> color' c

hex' :: Int -> Text
hex' n
| n < 0 = "-" <> go (-n)
| otherwise = go n
where
go n'
| n' < 16 = hexDigit n'
| otherwise = go (n' `quot` 16) <> hexDigit (n' `rem` 16)

hexDigit :: Int -> Text
hexDigit n
| n <= 9 = Text.singleton $! i2d n
| otherwise = Text.singleton $! toEnum (n + 87)

i2d :: Int -> Char
i2d x = chr (ord '0' + x)

-- | Select a Colour from the palette
--
-- >>> palette 0
Expand Down
17 changes: 4 additions & 13 deletions src/Data/Path.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Data.Path
pointPath,
movePath,
scalePath,
projectPath,
projectPaths,
pathBoxes,
pathBox,
Expand Down Expand Up @@ -44,8 +45,6 @@ module Data.Path
where

import Chart.Data
import Control.Foldl qualified as L
import Control.Monad.State.Lazy
import GHC.Generics
import Geom2D.CubicBezier qualified as B
import NumHask.Prelude
Expand Down Expand Up @@ -107,16 +106,7 @@ scalePath x (ArcP i p) = ArcP i (fmap (x *) p)

-- | Project a list of connected PathDatas from one Rect (XY plave) to a new one.
projectPaths :: Rect Double -> Rect Double -> [PathData Double] -> [PathData Double]
projectPaths new old ps =
flip evalState zero $
mapM
( \p -> do
x <- get
let d = projectPath new old x p
put (pointPath d)
pure d
)
ps
projectPaths new old ps = snd $ mapAccumL (\s a -> let d = projectPath new old s a in (pointPath d, d)) zero ps

-- | Project a PathData from one Rect (XY plave) to a new one.
projectPath ::
Expand Down Expand Up @@ -493,7 +483,8 @@ pathBoxes :: [PathData Double] -> Maybe (Rect Double)
pathBoxes [] = Nothing
pathBoxes (x : xs) =
Just $
L.fold (L.Fold step begin snd) xs
snd $
foldl' step begin xs
where
begin :: (Point Double, Rect Double)
begin = (pointPath x, singleton (pointPath x))
Expand Down

0 comments on commit bc1fc60

Please sign in to comment.