Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove CeTZ Axes, Plot & Chart #624

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# 0.3.0

CeTZ 0.3.0 requires Typst 0.11.0
The licence changed from Apache-2.0 to GPLv3.
The licence changed from Apache-2.0 to LGPLv3.

CeTZ' plotting and charting functionality has been moved to a separate
package called `cetz-plot`.

## Canvas
- Fixed a bug with `#set place(float: true)` affecting the canvas.
Expand All @@ -24,6 +27,7 @@ The licence changed from Apache-2.0 to GPLv3.
allow for centered marks.

## Plot
- **BREAKING** The plot library has been moved out of cetz
- Added support for automatically adding axis breaks (sawtooth lines) by setting the `break`
attribute of an axis to `true`.
- Added a new errorbar function: `add-errorbar`
Expand All @@ -32,6 +36,7 @@ The licence changed from Apache-2.0 to GPLv3.
- **BREAKING** Legend anchors got renamed and do not use the legend prefix anymore

## Chart
- **BREAKING** The chart library has been moved out of cetz
- Added errorbar support for bar- and columncharts
- Piecharts now support a legend (see `legend.label` style)
- **BREAKING** Legend anchors got renamed and do not use the legend prefix anymore
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ To use this package, simply add the following code to your document:
})
```

## CeTZ Libraries

- [cetz-plot - Plotting and Charts Library](https://github.com/cetz-package/cetz-plot)

## Installing

To install the CeTZ 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.
Expand Down
1 change: 1 addition & 0 deletions gallery/barchart.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "@preview/cetz:0.2.2": canvas, chart, draw
#import "@preview/cetz-plot:0.1.0": chart

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

Expand Down
2 changes: 2 additions & 0 deletions gallery/pie-chart.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#import "@preview/cetz:0.2.2"
#import "@preview/cetz-plot:0.1.0": chart

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

#let data = (
Expand Down
3 changes: 2 additions & 1 deletion gallery/plot.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "@preview/cetz:0.2.2": canvas, plot
#import "@preview/cetz:0.2.2": canvas
#import "@preview/cetz-plot:0.1.0": plot

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

Expand Down
242 changes: 0 additions & 242 deletions manual.typ
Original file line number Diff line number Diff line change
Expand Up @@ -594,248 +594,6 @@ The tree library allows the drawing diagrams with simple tree layout algorithms

#doc-style.parse-show-module("/src/lib/tree.typ")

== Plot

The library `plot` of CeTZ allows plotting data.

=== Types

Types commonly used by function of the `plot` library:
- #doc-style.show-type("domain"): Tuple representing a functions domain as closed interval.
Example domains are: `(0, 1)` for $[0, 1]$ or
`(-calc.pi, calc.pi)` for $[-pi, pi]$.
- #doc-style.show-type("axes"): Tuple of axis names. Plotting functions taking an `axes` tuple
will use those axes as their `x` and `y` axis for plotting.
To rotate a plot, you can simply swap its axes, for example `("y", "x")`.
- #doc-style.show-type("mark"): Plots feature their own set of marks. The following mark symbols are
available:
```example-vertical
let marks = ("+", "x", "-", "|", "o", "square", "triangle")
cetz.plot.plot(size: (14, 1), x-min: 0, x-max: marks.len() + 1,
x-ticks: marks.enumerate().map(((i, s)) => (i+1, raw(s))),
x-tick-step: none, y-tick-step: none,
x-label: none, y-label: none,
{
for (i, s) in marks.enumerate() {
cetz.plot.add(((i + 1, 0),), mark: s, mark-style: (stroke: blue, fill: white), mark-size: .5)
}
})
```

#doc-style.parse-show-module("/src/lib/plot.typ")

=== Legends <plot-legends>
A legend for a plot will be drawn if at least one set of data with a label that is not `none` is given.
The following anchors are available when placing a legend on a plot:
- `north`
- `south`
- `east`
- `west`
- `north-east`
- `north-west`
- `south-east`
- `south-west`
- `inner-north`
- `inner-south`
- `inner-east`
- `inner-west`
- `inner-north-east`
- `inner-north-west`
- `inner-south-east`
- `inner-south-west`
```example
import cetz.plot
plot.plot(
size: (3,2),
x-tick-step: none,
y-tick-step: none,
legend: "north", {
plot.add(
((-1, -1), (1, 1)),
mark: "o",
label: $ f(x) $
)
})
```

==== Styling
*Root:* `legend`
===== Keys
#doc-style.show-parameter-block("orientation", ("direction"), default: ttb, [
The direction the legend items get laid out to.
])
#doc-style.show-parameter-block("default-position", ("string", "coordinate"), default: "legend.north-east", [
The default position the legend gets placed at.
])
#doc-style.show-parameter-block("layer", ("number"), default: 1, [
The layer index the legend gets drawn at, see on-layer.
])
#doc-style.show-parameter-block("fill", ("paint"), default: rgb(255,255,255,200), [
The legends frame background color.
])
#doc-style.show-parameter-block("stroke", ("stroke"), default: black, [
The legends frame stroke style.
])
#doc-style.show-parameter-block("padding", ("float"), default: .1, [
The legends frame padding, that is the distance added between its items and its frame.
])
#doc-style.show-parameter-block("offset", ("tuple"), default: (0,0), [
An offset tuple (x and y coordinates) to add to the legends position.
])
#doc-style.show-parameter-block("spacing", ("number"), default: .1, [
The spacing between the legend position and its frame.
])
#doc-style.show-parameter-block("item.spacing", ("number"), default: .05, [
The spacing between two legend items in canvas units.
])
#doc-style.show-parameter-block("item.preview.width", ("number"), default: .75, [
The width of a legend items preview picture, a small preview of the graph the legend item belongs to.
])
#doc-style.show-parameter-block("item.preview.height", ("number"), default: .3, [
The height of a legend items preview picture.
])
#doc-style.show-parameter-block("item.preview.margin", ("number"), default: .1, [
Margin between the preview picture and the item label.
])


#doc-style.parse-show-module("/src/lib/plot/line.typ")
#doc-style.parse-show-module("/src/lib/plot/contour.typ")
#doc-style.parse-show-module("/src/lib/plot/boxwhisker.typ")
#doc-style.parse-show-module("/src/lib/plot/bar.typ")
#doc-style.parse-show-module("/src/lib/plot/annotation.typ")
#doc-style.parse-show-module("/src/lib/plot/sample.typ")

=== Examples

```example
import cetz.plot
plot.plot(size: (3,2), x-tick-step: calc.pi, y-tick-step: 1,
x-format: v => $#{v/calc.pi} pi$, {
plot.add(domain: (0, 4*calc.pi), calc.sin,
samples: 15, line: "hvh", style: (mark: (stroke: blue)))
plot.add(domain: (0, 4*calc.pi), calc.sin)
})
```

```example
import cetz.plot
import cetz.palette

// Let ticks point outwards by giving them negative length
set-style(axes: (tick: (length: -.2, minor-length: -.1)))

// Plot something
plot.plot(size: (3,3), x-tick-step: 1, x-minor-tick-step: .2,
y-tick-step: 1, y-minor-tick-step: .2, {
let z(x, y) = {
(1 - x/2 + calc.pow(x,5) + calc.pow(y,3)) * calc.exp(-(x*x) - (y*y))
}
plot.add-contour(x-domain: (-2, 3), y-domain: (-3, 3),
z, z: (.1, .4, .7), fill: true)
})
```

=== Styling <plot.style>

The following style keys can be used (in addition to the standard keys)
to style plot axes. Individual axes can be styled differently by
using their axis name as key below the `axes` root.

```typc
set-style(axes: ( /* Style for all axes */ ))
set-style(axes: (bottom: ( /* Style axis "bottom" */)))
```

Axis names to be used for styling:
- School-Book and Left style:
- `x`: X-Axis
- `y`: Y-Axis
- Scientific style:
- `left`: Y-Axis
- `right`: Y2-Axis
- `bottom`: X-Axis
- `top`: X2-Axis

#doc-style.parse-show-module("/src/lib/axes.typ")

==== Default `scientific` Style
#raw(repr(axes.default-style-scientific))

==== Default `school-book` Style
#raw(repr(axes.default-style-schoolbook))

== Chart

With the `chart` library it is easy to draw charts.

#doc-style.parse-show-module("/src/lib/chart/barchart.typ")
#doc-style.parse-show-module("/src/lib/chart/columnchart.typ")
#doc-style.parse-show-module("/src/lib/chart/piechart.typ")
#doc-style.parse-show-module("/src/lib/chart/boxwhisker.typ")

=== Examples -- Bar Chart <barchart-examples>
```example-vertical
import cetz.chart
// Left - Basic
let data = (("A", 10), ("B", 20), ("C", 13))
group(name: "a", {
chart.barchart(size: (4, 3), data)
})
// Center - Clustered
let data = (("A", 10, 12, 22), ("B", 20, 1, 7), ("C", 13, 8, 9))
group(name: "b", anchor: "south-west", {
anchor("default", "a.south-east")
chart.barchart(size: (4, 3), mode: "clustered", value-key: (1,2,3), data)
})
// Right - Stacked
let data = (("A", 10, 12, 22), ("B", 20, 1, 7), ("C", 13, 8, 9))
group(name: "c", anchor: "south-west", {
anchor("default", "b.south-east")
chart.barchart(size: (4, 3), mode: "stacked", value-key: (1,2,3), data)
})
```

=== Examples -- Column Chart <columnchart-examples>

==== Basic, Clustered and Stacked
```example-vertical
import cetz.chart
// Left - Basic
let data = (("A", 10), ("B", 20), ("C", 13))
group(name: "a", {
chart.columnchart(size: (4, 3), data)
})
// Center - Clustered
let data = (("A", 10, 12, 22), ("B", 20, 1, 7), ("C", 13, 8, 9))
group(name: "b", anchor: "south-west", {
anchor("default", "a.south-east")
chart.columnchart(size: (4, 3), mode: "clustered", value-key: (1,2,3), data)
})
// Right - Stacked
let data = (("A", 10, 12, 22), ("B", 20, 1, 7), ("C", 13, 8, 9))
group(name: "c", anchor: "south-west", {
anchor("default", "b.south-east")
chart.columnchart(size: (4, 3), mode: "stacked", value-key: (1,2,3), data)
})
```

=== Styling

Charts share their axis system with plots and therefore can be
styled the same way, see @plot.style.

==== Default `barchart` Style
#raw(repr(chart.barchart-default-style))

==== Default `columnchart` Style
#raw(repr(chart.columnchart-default-style))

==== Default `boxwhisker` Style
#raw(repr(chart.boxwhisker-default-style))

==== Default `piechart` Style
#raw(repr(chart.piechart-default-style))

== Palette <palette>

Expand Down
3 changes: 0 additions & 3 deletions src/lib.typ
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
#import "mark-shapes.typ"

// Libraries
#import "lib/axes.typ"
#import "lib/plot.typ"
#import "lib/chart.typ"
#import "lib/palette.typ"
#import "lib/angle.typ"
#import "lib/tree.typ"
Expand Down
Loading
Loading