Polygon

The Polygon mark can be used to draw a variety of shapes. Use cases include maps or contours.

<script>
  import { Graphic, Polygon, PolygonLayer, XAxis, YAxis } from '@snlab/florence'
</script>

<Graphic width={200} height={200} scaleX={[0, 10]} scaleY={[0, 10]} padding={20}>

  <Polygon
    x={[0, 2.5, 2.5]}
    y={[0, 2.5, 0]}
    fill={'red'}
  />

  <PolygonLayer 
    x={[[5, 7, 5, 3], [10, 7.5, 7.5]]} 
    y={[[7, 5, 3, 5], [10, 7.5, 10]]}
    fill={['blue', 'green']}
  />

  <XAxis />
  <YAxis />

</Graphic>
012345678910 012345678910

Properties

Positioning

Prop Required Type(s) Default Unit(s)(s)
x if (geometry === undefined) Number[], String[], Date[], Function undefined Local coordinate
y if (geometry === undefined) Number[], String[], Date[], Function undefined Local coordinate
geometry if (x === undefined && y === undefined) Array of GeoJSON LineString or MultiLineString features undefined Local coordinate

x and y have to be arrays of Numbers, Strings or Dates, depending on the scale used in the parent Section. geometry accepts arrays of GeoJSON Polygon and MultiPolygon objects only. For all positioning props, a function can be supplied that bypasses the scaling step- see the local coordinates documentation. When using the PolygonLayer, all positioning props will be supplied as Arrays of Arrays of Numbers, Strings and Dates, or Functions that return Arrays of Arrays of scaled coordinates- see the marks versus layers documentation.

Note that Polygons have to ‘close’, meaning that the last coordinate pair has to be identical to the first. florence will close unclosed x and y arrays automatically. GeoJSON geometries are (according to their specification) supposed to be ‘closed’ by default, so in theory you should not have to worry about closing polygons when using the geometry prop. In practice, however, this is not always the case. So, if the drawn polygons look weird, it might be worth checking of the geometries are closed!

Aesthetics

Prop Required Type(s) Default Unit(s)
fill false String '#000000' Named, hex, rgb or hsl color
fillOpacity false Number undefined Number between 0 and 1
stroke false String 'none' Named, hex, rgb or hsl color
strokeWidth false Number 0 Pixel
strokeOpacity false Number undefined Number between 0 to 1
opacity false Number 1 Number between 0 and 1
lineCap false String 'butt' 'butt', 'round' or 'square'
lineJoin false String 'bevel' 'bevel', 'round' or 'miter'
miterLimit false Number 10 Pixel
dashArray false String undefined A string of numbers seperated by spaces. See here
dashOffset false Number undefined Pixel

These aesthetic props are similar to attributes of the SVG polygon element. The analogous polygon attributes are shown below in brackets:

  • fill refers to the color of the polygon (fill)
  • fillOpacity refers to the transparency of the polygon, 0 being fully transparent (fill-opacity)
  • stroke refers to the color of the polygon’s outline (stroke)
  • strokeWidth refers to the width of the polygon’s outline (stroke-width)
  • strokeOpacity refers to the transparency of the polygon’s outline, 0 being fully transparent (stroke-opacity)
  • opacity sets both the strokeOpacity and the fillOpacity (opacity)
  • lineCap controls the endings of strokes (in this case only relevant when dashArray is used) (stroke-linecap)
  • lineJoin controls the shape to be used at the corners of the stroke (stroke-linejoin)
  • miterLimit, if lineJoin is 'miter', sets a limit on how long the miter can be (stroke-miterlimit)
  • dashArray can be used to make dashed strokes (stroke-dasharray)
  • dashOffset shifts the pattern defined in dashArray by a number of pixels (stroke-dashoffset)

Interactions

Mouse events

Prop Required Type(s) Default Unit(s)
onClick false Function undefined -
onMousedown false Function undefined -
onMouseup false Function undefined -
onMouseover false Function undefined -
onMouseout false Function undefined -
onMousedrag false Function undefined -

Touch events

Prop Required Type(s) Default Unit(s)
onTouchdown false Function undefined -
onTouchup false Function undefined -
onTouchover false Function undefined -
onTouchout false Function undefined -
onTouchdrag false Function undefined -

Select events

Prop Required Type(s) Default Unit(s)
onSelect false Function undefined -
onDeselect false Function undefined -

See the interactivity documentation for more information.

Other

Prop Required Type(s) Default Unit(s)
outputSettings false Object undefined -
blockReindexing false Boolean false -
clip false String, undefined undefined Either 'padding', 'outer' or undefined
  • outputSettings: see the advanced rendering documentation for more information.
  • blockReindexing: see the interactivity documentation for more information.
  • clip: if defined, overrides the clip mode of the parent Graphic, Section or Glyph.

In addition, the PolygonLayer has the following props:

Prop Required Type(s) Default Unit(s)
keys false String[] undefined Array of unique keys
asOnePath false Boolean false -

See the marks versus layers documentation for more information on how keys and asOnePath work.

Examples

The Polygon and PolygonLayer can be seen in action in the following examples: