Skip to content

Commit

Permalink
treewide: Initial comit
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed May 23, 2024
0 parents commit 4de83e8
Show file tree
Hide file tree
Showing 95 changed files with 7,244 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2

[Makefile]
indent_style = tab
44 changes: 44 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Tests
on:
push:
branches:
- main
- master
pull_request:
branches:
- '**'

jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive
- uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: imagemagick cargo parallel
version: 1.0
- name: Install typos-cli from crates.io
uses: baptiste0928/[email protected]
with:
crate: typos-cli
- name: Install just from crates.io
uses: baptiste0928/[email protected]
with:
crate: just
- name: Install typst-test from github
uses: baptiste0928/[email protected]
with:
crate: typst-test
git: https://github.com/tingerrr/typst-test.git
tag: ci-semi-stable
- uses: typst-community/setup-typst@v3
with:
typst-version: '0.11.1'
cache-dependency-path: src/cetz.typ
- run: |
just install @local
just install @preview
just manual
just test
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# CeTZ-Plot

CeTZ-Plot is a library that adds plots and charts to [CeTZ](https://github.com/cetz-package/cetz), a library for drawing with [Typst](https://typst.app).

## Examples
<!-- img width is set so the table gets evenly spaced by GitHubs css -->
<table>
<tr>
<td>
<a href="gallery/line.typ">
<img src="gallery/line.png" width="250px">
</a>
</td>
<td>
<a href="gallery/piechart.typ">
<img src="gallery/piechart.png" width="250px">
</a>
</td>
<td>
<a href="gallery/barchart.typ">
<img src="gallery/barchart.png" width="250px">
</a>
</td>
</tr><tr>
<td>Plot</td>
<td>Pie Chart</td>
<td>Clustered Barchart</td>
</tr>
</table>

*Click on the example image to jump to the code.*


## Usage

For information, see the [manual (stable)](https://github.com/cetz-package/cetz-plot/blob/stable/manual.pdf?raw=true).

To use this package, simply add the following code to your document:
```
#import "@preview/cetz:0.2.2"
#import "@preview/cetz-plot:0.1.0": plot, chart
#cetz.canvas({
// Your plot/chart code goes here
})
```

## Installing

To install the CeTZ-Plot package under [your local typst package dir](https://github.com/typst/packages?tab=readme-ov-file#local-packages) you can use the `install` script from the repository.

### Just

This project uses [just](https://github.com/casey/just), a handy command runner.

You can run all commands without having `just` installed, just have a look into the `justfile`.
To install `just` on your system, use your systems package manager. On Windows, [Cargo](https://doc.rust-lang.org/cargo/) (`cargo install just`), [Chocolatey](https://chocolatey.org/) (`choco install just`) and [some other sources](https://just.systems/man/en/chapter_4.html) can be used. You need to run it from a `sh` compatible shell on Windows (e.g git-bash).

## Testing

This package comes with some unit tests under the `tests` directory.
To run all tests you can run the `just test` target. You need to have
[`typst-test`](https://github.com/tingerrr/typst-test/) in your `PATH`: `cargo install typst-test --git https://github.com/tingerrr/typst-test`.
Empty file added gallery/.gitkeep
Empty file.
Binary file added gallery/barchart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions gallery/barchart.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#import "@preview/cetz:0.2.2": canvas, draw
#import "@preview/cetz-plot:0.1.0": chart

#set page(width: auto, height: auto, margin: .5cm)

#let data2 = (
([15-24], 18.0, 20.1, 23.0, 17.0),
([25-29], 16.3, 17.6, 19.4, 15.3),
([30-34], 14.0, 15.3, 13.9, 18.7),
([35-44], 35.5, 26.5, 29.4, 25.8),
([45-54], 25.0, 20.6, 22.4, 22.0),
([55+], 19.9, 18.2, 19.2, 16.4),
)

#canvas({
draw.set-style(legend: (fill: white))
chart.barchart(mode: "clustered",
size: (9, auto),
label-key: 0,
value-key: (..range(1, 5)),
bar-width: .8,
x-tick-step: 2.5,
data2,
labels: ([Low], [Medium], [High], [Very high]),
legend: "legend.inner-north-east",)
})
Binary file added gallery/line.pdf
Binary file not shown.
Binary file added gallery/line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions gallery/line.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#import "@preview/cetz:0.2.2": canvas
#import "@preview/cetz-plot:0.1.0": plot

#set page(width: auto, height: auto, margin: .5cm)

#let style = (stroke: black, fill: rgb(0, 0, 200, 75))

#canvas(length: 1cm, {
plot.plot(size: (8, 6),
x-tick-step: none,
x-ticks: ((-calc.pi, $-pi$), (0, $0$), (calc.pi, $pi$)),
y-tick-step: 1,
{
plot.add(
style: style,
domain: (-calc.pi, calc.pi), calc.sin)
plot.add(
hypograph: true,
style: style,
domain: (-calc.pi, calc.pi), calc.cos)
plot.add(
hypograph: true,
style: style,
domain: (-calc.pi, calc.pi), x => calc.cos(x + calc.pi))
})
})
Binary file added gallery/piechart.pdf
Binary file not shown.
Binary file added gallery/piechart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions gallery/piechart.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#import "@preview/cetz:0.2.2"
#import "@preview/cetz-plot:0.1.0": chart

#set page(width: auto, height: auto, margin: .5cm)

#let data = (
([Belgium], 24),
([Germany], 31),
([Greece], 18),
([Spain], 21),
([France], 23),
([Hungary], 18),
([Netherlands], 27),
([Romania], 17),
([Finland], 26),
([Turkey], 13),
)

#cetz.canvas({
let colors = gradient.linear(red, blue, green, yellow)

chart.piechart(
data,
value-key: 1,
label-key: 0,
radius: 4,
slice-style: colors,
inner-radius: 1,
outset: 3,
inner-label: (content: (value, label) => [#text(white, str(value))], radius: 110%),
outer-label: (content: "%", radius: 110%))
})
22 changes: 22 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Local Variables:
# mode: makefile
# End:
gallery_dir := "./gallery"

package target *options:
./scripts/package "{{target}}" {{options}}

install target="@local":
./scripts/package "{{target}}"

test *filter:
typst-test run {{filter}}

update-test *filter:
typst-test update {{filter}}

manual:
typst c manual.typ manual.pdf

gallery:
for f in "{{gallery_dir}}"/*.typ; do typst c "$f" "${f/typ/png}"; done
Binary file added manual.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions manual.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO
91 changes: 91 additions & 0 deletions scripts/package
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bash
set -eu

PKG_PREFIX="cetz-plot"

# List of all files that get packaged
files=(
src/
gallery/
typst.toml
LICENSE
README.md
manual.typ
manual.pdf
)

# Local package directories per platform
if [[ "$OSTYPE" == "linux"* ]]; then
DATA_DIR="${XDG_DATA_HOME:-$HOME/.local/share}"
elif [[ "$OSTYPE" == "darwin"* ]]; then
DATA_DIR="$HOME/Library/Application Support"
else
DATA_DIR="${APPDATA}"
fi

if (( $# < 1 )) || [[ "${1:-}" == "help" ]]; then
echo "package TARGET [--relative-paths]"
echo ""
echo "Packages all relevant files into a directory named '${PKG_PREFIX}/<version>'"
echo "at TARGET. If TARGET is set to @local, the local Typst package directory"
echo "will be used so that the package gets installed for local use, if @preview"
echo "is used, Typsts preview cache dir will be used."
echo "The version is read from 'typst.toml' in the project root."
echo ""
echo "Local package prefix: $DATA_DIR/typst/package/local"
exit 1
fi

function read-toml() {
local file="$1"
local key="$2"
# Read a key value pair in the format: <key> = "<value>"
# stripping surrounding quotes.
perl -lne "print \"\$1\" if /^${key}\\s*=\\s*\"(.*)\"/" < "$file"
}

SOURCE="$(cd "$(dirname "$0")"; pwd -P)/.." # macOS has no realpath
TARGET="${1:?Missing target path or @local}"; shift
VERSION="$(read-toml "$SOURCE/typst.toml" "version")"

OPT_RELATIVE_PATHS=false
while [[ $# -gt 0 ]]; do
case "$1" in
--relative-paths)
OPT_RELATIVE_PATHS=true
shift
;;
*)
echo "Unexpected option $1!"
exit 1
;;
esac
done

if [[ "$TARGET" == "@local" ]] || [[ "$TARGET" == "install" ]]; then
TARGET="${DATA_DIR}/typst/packages/local/"
elif [[ "$TARGET" == "@preview" ]]; then
TARGET="${DATA_DIR}/typst/packages/preview/"
fi
echo "Install dir: $TARGET"

TMP="$(mktemp -d)"

for f in "${files[@]}"; do
mkdir -p "$TMP/$(dirname "$f")" 2>/dev/null
cp -r "$SOURCE/$f" "$TMP/$f"
done

TARGET="${TARGET:?}/${PKG_PREFIX:?}/${VERSION:?}"
echo "Packaged to: $TARGET"
if rm -rf "${TARGET:?}" 2>/dev/null; then
echo "Overwriting existing version."
fi

if $OPT_RELATIVE_PATHS; then
echo "Changing imports to relative."
"$SOURCE/scripts/relpaths" "$TMP"
fi

mkdir -p "$TARGET"
mv "$TMP"/* "$TARGET"
20 changes: 20 additions & 0 deletions scripts/relpaths
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/env python
import glob, os, sys, re

import_regexp = re.compile(f'#(import|include)\\s*"(/.+)"')

def replace_imports(filename):
s = None
with open(filename, "r") as file:
s = file.read()
def abs_to_rel(captures):
g = captures.groups()
p = os.path.relpath("." + g[1], os.path.dirname(filename))
return f'#{g[0]} "{p}"'
s = re.sub(import_regexp, abs_to_rel, s)
with open(filename, "w") as file:
file.write(s)

os.chdir(sys.argv[1])
for file in glob.iglob("./**/*.typ", recursive=True):
replace_imports(file)
Loading

0 comments on commit 4de83e8

Please sign in to comment.