Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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`, `rule`, `linear`, `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
108 changes: 108 additions & 0 deletions doc/syntax/layer/type/rect.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
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 for the primary axis:
* Center (e.g. `x`)
* `width`
* Start position (e.g. `xmin`). Unavailable when the center is discrete.
* End position (e.g. `xmax`). Unavailable when the center is discrete.

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

* Pick two of the following for the secondary axis:
* Center (e.g. `y`)
* `height`: The size of the rectangle in the vertical dimension.
* Start position (e.g. `ymin`). Unavailable when the center is discrete.
* End position (e.g. `ymax`) Unavailable when the center is discrete.

Alternatively, use only the center, 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
* `position`: Determines the position adjustment to use for the layer (default is `'identity'`)

## Data transformation.
When the primary aesthetics are continuous, primary data is reparameterised to {start, end}, e.g. `xmin` and `xmax`.
When the secondary aesthetics are continuous, secondary data is reparameterised to {start, end}, e.g. `ymin` and `ymax`.

## Orientation
The rectangle layer has no orientation. The axes are treated symmetrically.

## 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.

<!-- When annotations work, replace this with an annotation layer -->

```{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|rule|linear|errorbar)\\b"
"match": "\\b(point|line|path|bar|col|area|rect|polygon|ribbon|histogram|density|smooth|boxplot|violin|text|label|segment|arrow|rule|linear|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 @@ -602,7 +602,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 @@ -41,12 +41,12 @@ mod linear;
mod path;
mod point;
mod polygon;
mod rect;
mod ribbon;
mod rule;
mod segment;
mod smooth;
mod text;
mod tile;
mod violin;

// Re-export types
Expand All @@ -66,12 +66,12 @@ pub use linear::Linear;
pub use path::Path;
pub use point::Point;
pub use polygon::Polygon;
pub use rect::Rect;
pub use ribbon::Ribbon;
pub use rule::Rule;
pub use segment::Segment;
pub use smooth::Smooth;
pub use text::Text;
pub use tile::Tile;
pub use violin::Violin;

use crate::plot::types::{DefaultAestheticValue, ParameterValue, Schema};
Expand All @@ -86,7 +86,7 @@ pub enum GeomType {
Path,
Bar,
Area,
Tile,
Rect,
Polygon,
Ribbon,
Histogram,
Expand All @@ -111,7 +111,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 @@ -266,9 +266,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 @@ -349,7 +349,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
Loading