Skip to content
Open
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
4 changes: 2 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ pub struct Layer {

pub enum Geom {
// Basic geoms
Point, Line, Path, Bar, Col, Area, Tile, Polygon, Ribbon,
Point, Line, Path, Bar, Col, Area, Rect, Polygon, Ribbon,
// Statistical geoms
Histogram, Density, Smooth, Boxplot, Violin,
// Annotation geoms
Expand Down Expand Up @@ -1200,7 +1200,7 @@ All clauses (MAPPING, SETTING, PARTITION BY, FILTER) are optional.

**Geom Types**:

- **Basic**: `point`, `line`, `path`, `bar`, `col`, `area`, `tile`, `polygon`, `ribbon`
- **Basic**: `point`, `line`, `path`, `bar`, `col`, `area`, `rect`, `polygon`, `ribbon`
- **Statistical**: `histogram`, `density`, `smooth`, `boxplot`, `violin`
- **Annotation**: `text`, `label`, `segment`, `arrow`, `hline`, `vline`, `abline`, `errorbar`

Expand Down
2 changes: 1 addition & 1 deletion doc/ggsql.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<item>bar</item>
<item>col</item>
<item>area</item>
<item>tile</item>
<item>rect</item>
<item>polygon</item>
<item>ribbon</item>
<item>histogram</item>
Expand Down
103 changes: 103 additions & 0 deletions doc/syntax/layer/rect.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
title: "Rectangle"
---

> Layers are declared with the [`DRAW` clause](../clause/draw.qmd). Read the documentation for this clause for a thorough description of how to use it.

Rectangles can be used to draw heatmaps or indicate ranges.

## Aesthetics
The following aesthetics are recognised by the rectangle layer.

### Required

* Pick two of the following:
* `x`: The center along the x-axis.
* `width`: The size of the rectangle in the horizontal dimension.
* `xmin`: The left position along the x-axis. Unavailable when `x` is discrete.
* `xmax`: The right position laong the x-axis. Unavailable when `x` is discrete.

Alternatively, use only `x`, which will set `width` to 1 by default.

* Pick two of the following:
* `y`: The center along the y-axis.
* `height`: The size of the rectangle in the vertical dimension.
* `ymin`: The bottom position along the y-axis. Unavailable when `y` is discrete.
* `ymax`: The top position along the y-axis. Unailable when `y` is discrete.

Alternatively, use only `y`, which will set `height` to 1 by default.

### Optional
* `stroke`: The colour of the contour lines.
* `fill`: The colour of the inner area.
* `colour`: Shorthand for setting `stroke` and `fill` simultaneously.
* `opacity`: The opacity of colours.
* `linewidth`: The width of the contour lines.
* `linetype`: The dash pattern of the contour line.

## Settings
The rectangle layer has no additional settings.

## Data transformation.
When the x-aesthetics are continuous, x-data is reparameterised to `xmin` and `xmax`.
When the y-aesthetics are continuous, y-data is reparameterised to `ymin` and `ymax`.

## Examples

Just using `x` and `y`. Note that `width` and `height` are set to 1.

```{ggsql}
VISUALISE Day AS x, Month AS y, Temp AS colour FROM ggsql:airquality
DRAW rect
```

Customising `width` and `height` with either the `MAPPING` or `SETTING` clauses.

```{ggsql}
VISUALISE Day AS x, Month AS y, Temp AS colour FROM ggsql:airquality
DRAW rect MAPPING 0.5 AS width SETTING height => 0.8
```

If `x` is continuous, then `width` can be variable. Likewise for `y` and `height`.

```{ggsql}
SELECT *, Temp / (SELECT MAX(Temp) FROM ggsql:airquality) AS norm_temp
FROM ggsql:airquality
VISUALISE
Day AS x,
Month AS y,
Temp AS colour
DRAW rect
MAPPING
norm_temp AS width,
norm_temp AS height
```

Using top, right, bottom, left parameterisation instead.

```{ggsql}
SELECT
MIN(Date) AS start,
MAX(Date) AS end,
MIN(Temp) AS min,
MAX(Temp) AS max
FROM ggsql:airquality
GROUP BY
WEEKOFYEAR(Date)

VISUALISE start AS xmin, end AS xmax, min AS ymin, max AS ymax
DRAW rect
```

Using a rectangle as an annotation.

```{ggsql}
VISUALISE FROM ggsql:airquality
DRAW rect MAPPING
'1973-06-01' AS xmin,
'1973-06-30' AS xmax,
50 AS ymin,
100 AS ymax,
'June' AS colour
DRAW line MAPPING Date AS x, Temp AS y
```
2 changes: 1 addition & 1 deletion ggsql-vscode/syntaxes/ggsql.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
"patterns": [
{
"name": "support.type.geom.ggsql",
"match": "\\b(point|line|path|bar|col|area|tile|polygon|ribbon|histogram|density|smooth|boxplot|violin|text|label|segment|arrow|hline|vline|abline|errorbar)\\b"
"match": "\\b(point|line|path|bar|col|area|rect|polygon|ribbon|histogram|density|smooth|boxplot|violin|text|label|segment|arrow|hline|vline|abline|errorbar)\\b"
},
{ "include": "#common-clause-patterns" }
]
Expand Down
2 changes: 1 addition & 1 deletion src/parser/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ fn parse_geom_type(text: &str) -> Result<Geom> {
"path" => Ok(Geom::path()),
"bar" => Ok(Geom::bar()),
"area" => Ok(Geom::area()),
"tile" => Ok(Geom::tile()),
"rect" => Ok(Geom::rect()),
"polygon" => Ok(Geom::polygon()),
"ribbon" => Ok(Geom::ribbon()),
"histogram" => Ok(Geom::histogram()),
Expand Down
12 changes: 6 additions & 6 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ mod tests {
let query = r#"
SELECT x, y FROM data
VISUALIZE x, y
DRAW tile
DRAW point
"#;

let specs = parse_query(query).unwrap();
assert_eq!(specs.len(), 1);
assert_eq!(specs[0].layers[0].geom, Geom::tile());
assert_eq!(specs[0].layers[0].geom, Geom::point());
}

#[test]
Expand All @@ -163,7 +163,7 @@ mod tests {
VISUALIZE
DRAW bar MAPPING x AS x, y AS y
VISUALISE z AS x, y AS y
DRAW tile
DRAW point
"#;

let specs = parse_query(query).unwrap();
Expand Down Expand Up @@ -219,15 +219,15 @@ mod tests {
VISUALISE x, y
DRAW line
VISUALIZE
DRAW tile MAPPING x AS x, y AS y
DRAW point MAPPING x AS x, y AS y
VISUALISE
DRAW bar MAPPING x AS x, y AS y
"#;

let specs = parse_query(query).unwrap();
assert_eq!(specs.len(), 3);
assert_eq!(specs[0].layers[0].geom, Geom::line());
assert_eq!(specs[1].layers[0].geom, Geom::tile());
assert_eq!(specs[1].layers[0].geom, Geom::point());
assert_eq!(specs[2].layers[0].geom, Geom::bar());
}

Expand All @@ -245,7 +245,7 @@ mod tests {
VISUALIZE
DRAW bar MAPPING date AS x, revenue AS y
VISUALISE
DRAW tile MAPPING date AS x, revenue AS y
DRAW point MAPPING date AS x, revenue AS y
"#;

let specs = parse_query(query).unwrap();
Expand Down
16 changes: 8 additions & 8 deletions src/plot/layer/geom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ mod line;
mod path;
mod point;
mod polygon;
mod rect;
mod ribbon;
mod segment;
mod smooth;
mod text;
mod tile;
mod violin;
mod vline;

Expand All @@ -68,11 +68,11 @@ pub use line::Line;
pub use path::Path;
pub use point::Point;
pub use polygon::Polygon;
pub use rect::Rect;
pub use ribbon::Ribbon;
pub use segment::Segment;
pub use smooth::Smooth;
pub use text::Text;
pub use tile::Tile;
pub use violin::Violin;
pub use vline::VLine;

Expand All @@ -87,7 +87,7 @@ pub enum GeomType {
Path,
Bar,
Area,
Tile,
Rect,
Polygon,
Ribbon,
Histogram,
Expand All @@ -113,7 +113,7 @@ impl std::fmt::Display for GeomType {
GeomType::Path => "path",
GeomType::Bar => "bar",
GeomType::Area => "area",
GeomType::Tile => "tile",
GeomType::Rect => "rect",
GeomType::Polygon => "polygon",
GeomType::Ribbon => "ribbon",
GeomType::Histogram => "histogram",
Expand Down Expand Up @@ -251,9 +251,9 @@ impl Geom {
Self(Arc::new(Area))
}

/// Create a Tile geom
pub fn tile() -> Self {
Self(Arc::new(Tile))
/// Create a Rect geom
pub fn rect() -> Self {
Self(Arc::new(Rect))
}

/// Create a Polygon geom
Expand Down Expand Up @@ -339,7 +339,7 @@ impl Geom {
GeomType::Path => Self::path(),
GeomType::Bar => Self::bar(),
GeomType::Area => Self::area(),
GeomType::Tile => Self::tile(),
GeomType::Rect => Self::rect(),
GeomType::Polygon => Self::polygon(),
GeomType::Ribbon => Self::ribbon(),
GeomType::Histogram => Self::histogram(),
Expand Down
Loading