AbstractPlot
in package
Build the Plotly data and layout of a plot from the layer features.
The plot types only define their trace template and their specific behaviours with the constants and methods below.
The features are read one by one: the plot never keeps them in memory. Depending on the aggregation, the traces contain either one value per feature (raw traces) or one value per distinct X value (aggregated traces).
Table of Contents
Constants
- TYPE : string = ''
- AGGREGATION_ALWAYS : mixed = 'always'
- The values are always aggregated, with a sum by default
- AGGREGATION_MODE : string = self::AGGREGATION_OPTIONAL
- AGGREGATION_NEVER : mixed = 'never'
- The values are never aggregated: the plot displays a distribution
- AGGREGATION_OPTIONAL : mixed = 'optional'
- The values are aggregated if an aggregation is configured
- FEATURE_COLORS : null|string = self::FEATURE_COLORS_NONE
- FEATURE_COLORS_MARKER_COLOR : mixed = 'color'
- The feature colors are set in trace.marker.color
- FEATURE_COLORS_MARKER_COLORS : mixed = 'colors'
- The feature colors are set in trace.marker.colors
- FEATURE_COLORS_NONE : mixed = null
- No feature colors
- SORT_BY_X : bool = false
- X_MANDATORY : bool = true
- X_PROPERTY : string = 'x'
- Y_MANDATORY : bool = true
- Y_PROPERTY : string = 'y'
- Z_PROPERTY : null|string = null
- DATABASE_FUNCTIONS : mixed = array(\LizmapDataviz\Aggregation\AggregateFunct...
- The functions of the database aggregation, for each aggregate function.
Properties
- $aggregation : null|string
- $colorFields : array<int, null|string>
- $colors : array<int, null|string>
- $data : array<string|int, mixed>
- $displayLegend : bool
- $horizontal : bool
- $layerId : string
- $layout : array<string|int, mixed>
- $project : Project
- $stacked : bool
- $theme : string
- $title : string
- $xFields : array<int, string>
- $yFields : array<int, string>
- $zFields : array<int, null|string>
- $aliases : null|array<string, string>
- $unsortedFeatures : bool
Methods
- __construct() : mixed
- Create the plot from its configuration.
- buildTraces() : bool
- Build the plot traces from the given features.
- fetchData() : bool
- Fetch the layer data and build the plot traces.
- getAggregation() : null|string
- Get the aggregate function used for this plot.
- getData() : array<string|int, mixed>
- Get the Plotly traces with all their values.
- getLayerId() : string
- Get the identifier of the layer containing the data.
- getLayout() : array<string|int, mixed>
- Get the Plotly layout.
- getTitle() : string
- Get the plot title.
- getTraces() : array<string|int, mixed>
- Get the Plotly traces, in which the long lists of values are JsonColumn.
- getType() : string
- Get the plot type.
- isAggregated() : bool
- Tell if the values are aggregated by distinct X values.
- writeJson() : void
- Write the plot as JSON: title, data (traces) and layout.
- compareGroups() : int
- Compare two groups, to sort them as the database does.
- customizeLayout() : array<string|int, mixed>
- Customize the layout for the plot type.
- getColorColumn() : null|JsonColumn
- Get the colors of the groups of a trace, from a column of colors.
- getFieldAlias() : string
- Get the field alias, or the field name if it has no alias.
- getGroup() : mixed
- Get the group of a feature for the aggregation.
- getGroupFields() : array<int, string>
- Get the fields grouping the features in the database aggregation.
- getGroupFromValues() : mixed
- Get the group from the values of the group fields returned by the database.
- getTraceTemplate() : array<string|int, mixed>
- Get the trace template of the plot type.
- setAggregatedValues() : null|array<string|int, mixed>|JsonColumn
- Set the aggregated values in the trace.
- aggregateInDatabase() : null|array<string|int, mixed>
- Aggregate the values in the database, without getting the features.
- applyColors() : void
- Set the aggregated colors in the trace.
- buildAggregatedTraces() : null|array<string|int, mixed>
- Build the traces with one aggregated value per group (distinct X value).
- buildRawTraces() : null|array<string|int, mixed>
- Build the traces with one value per feature.
- buildSortedAggregatedTraces() : null|array<string|int, mixed>
- Build the traces with one aggregated value per group, from features sorted by group.
- createAggregatedTraces() : array<string|int, mixed>
- Create the traces from the aggregated values of the groups.
- createTrace() : array<string|int, mixed>
- Create a trace from the template of the plot type.
- emptyToNull() : mixed
- Replace an empty string by null.
- finalizeTrace() : array<string|int, mixed>
- Replace empty data arrays or columns by null.
- getAxisProperties() : array{0: string, 1: string}
- Get the properties of the trace containing the categories (X field) and the values (Y field). They are reverted for horizontal plots.
- getDefaultFeatureColor() : null|string
- Get the color of the groups without color of a trace.
- getLayoutTemplate() : array<string|int, mixed>
- Get the layout of the plot.
- getPropertyNames() : array<int, string>
- Get the fields to request: the fields used by the plot.
- getTraceFields() : array<int, array{y: string, z: null|string, color: null|string}>
- Get the fields used by each trace.
- hasMandatoryFields() : bool
- Check that the configuration contains the fields needed by the plot type.
- isValidFeature() : bool
- Check that the given feature contains the fields used by the plot.
- logDebug() : void
- Log a message in the Lizmap admin log, in debug mode only.
- parsePlotConfig() : void
- Read the plot configuration.
- resolveAggregation() : null|string
- Get the aggregate function to use for this plot type.
- setFeatureColors() : void
- Set the feature colors in the trace property used by the plot type.
- splitFields() : array<int, string>
- Split a comma separated list of fields.
Constants
TYPE
public
string
TYPE
= ''
The plot type
AGGREGATION_ALWAYS
The values are always aggregated, with a sum by default
protected
mixed
AGGREGATION_ALWAYS
= 'always'
AGGREGATION_MODE
protected
string
AGGREGATION_MODE
= self::AGGREGATION_OPTIONAL
When the values are aggregated
AGGREGATION_NEVER
The values are never aggregated: the plot displays a distribution
protected
mixed
AGGREGATION_NEVER
= 'never'
AGGREGATION_OPTIONAL
The values are aggregated if an aggregation is configured
protected
mixed
AGGREGATION_OPTIONAL
= 'optional'
FEATURE_COLORS
protected
null|string
FEATURE_COLORS
= self::FEATURE_COLORS_NONE
How the feature colors are given to Plotly
FEATURE_COLORS_MARKER_COLOR
The feature colors are set in trace.marker.color
protected
mixed
FEATURE_COLORS_MARKER_COLOR
= 'color'
FEATURE_COLORS_MARKER_COLORS
The feature colors are set in trace.marker.colors
protected
mixed
FEATURE_COLORS_MARKER_COLORS
= 'colors'
FEATURE_COLORS_NONE
No feature colors
protected
mixed
FEATURE_COLORS_NONE
= null
SORT_BY_X
protected
bool
SORT_BY_X
= false
The features of a plot without aggregation are sorted by X, for example to draw lines. The aggregated values are always sorted by group (X value), ascending.
X_MANDATORY
protected
bool
X_MANDATORY
= true
The X field is mandatory
X_PROPERTY
protected
string
X_PROPERTY
= 'x'
Trace property containing the X values
Y_MANDATORY
protected
bool
Y_MANDATORY
= true
The Y field is mandatory
Y_PROPERTY
protected
string
Y_PROPERTY
= 'y'
Trace property containing the Y values
Z_PROPERTY
protected
null|string
Z_PROPERTY
= null
Trace property containing the Z values
DATABASE_FUNCTIONS
The functions of the database aggregation, for each aggregate function.
private
mixed
DATABASE_FUNCTIONS
= array(\LizmapDataviz\Aggregation\AggregateFunction::SUM => \Lizmap\Request\WFSAggregateRequest::SUM, \LizmapDataviz\Aggregation\AggregateFunction::COUNT => \Lizmap\Request\WFSAggregateRequest::COUNT, \LizmapDataviz\Aggregation\AggregateFunction::AVG => \Lizmap\Request\WFSAggregateRequest::AVG, \LizmapDataviz\Aggregation\AggregateFunction::MIN => \Lizmap\Request\WFSAggregateRequest::MIN, \LizmapDataviz\Aggregation\AggregateFunction::MAX => \Lizmap\Request\WFSAggregateRequest::MAX, \LizmapDataviz\Aggregation\AggregateFunction::FIRST => \Lizmap\Request\WFSAggregateRequest::FIRST, \LizmapDataviz\Aggregation\AggregateFunction::LAST => \Lizmap\Request\WFSAggregateRequest::LAST, \LizmapDataviz\Aggregation\AggregateFunction::STDDEV => \Lizmap\Request\WFSAggregateRequest::STDDEV, \LizmapDataviz\Aggregation\AggregateFunction::MEDIAN => \Lizmap\Request\WFSAggregateRequest::MEDIAN)
Properties
$aggregation
protected
null|string
$aggregation
The aggregate function, null if the values are not aggregated
$colorFields
protected
array<int, null|string>
$colorFields
= array()
Field containing the feature color, for each trace
$colors
protected
array<int, null|string>
$colors
= array()
Color of each trace
$data
protected
array<string|int, mixed>
$data
= array()
The Plotly traces, the long lists of values are JsonColumn
$displayLegend
protected
bool
$displayLegend
Display the legend
$horizontal
protected
bool
$horizontal
Display horizontal bars
$layerId
protected
string
$layerId
$layout
protected
array<string|int, mixed>
$layout
The Plotly layout
$project
protected
Project
$project
$stacked
protected
bool
$stacked
Stack the bars of the traces
$theme
protected
string
$theme
The dataviz theme: dark or light
$title
protected
string
$title
$xFields
protected
array<int, string>
$xFields
= array()
$yFields
protected
array<int, string>
$yFields
= array()
$zFields
protected
array<int, null|string>
$zFields
= array()
Z field of each trace
$aliases
private
null|array<string, string>
$aliases
= null
Cache of the layer field aliases
$unsortedFeatures
private
bool
$unsortedFeatures
= false
True if features expected sorted by group were not sorted: they are then aggregated by value, without relying on the order
Methods
__construct()
Create the plot from its configuration.
public
__construct(Project $project, array<string|int, mixed> $plotConfig) : mixed
No data is fetched: call fetchData() or buildTraces() to build the traces.
Parameters
- $project : Project
-
the Lizmap project, the ACL must have been checked
- $plotConfig : array<string|int, mixed>
-
the plot configuration, see Project::parseDatavizPlotConfig()
buildTraces()
Build the plot traces from the given features.
public
buildTraces(iterable<string|int, mixed> $features) : bool
The features are read only once, so they can be given by a generator. For an aggregated plot, the features should be sorted by group (X value), as requested by fetchData(): the aggregation is done group by group. If they are not, an array of features is aggregated again by value; for a generator, false is returned and the features must be given again.
Parameters
- $features : iterable<string|int, mixed>
-
GeoJSON features (objects)
Return values
bool —false if the features are not valid, or not sorted by group
fetchData()
Fetch the layer data and build the plot traces.
public
fetchData([string $expFilter = '' ]) : bool
For aggregated plots, the values are aggregated by the database when possible (PostgreSQL layers). Otherwise, or if the database aggregation fails, all the features are requested with WFS and aggregated in PHP.
Parameters
- $expFilter : string = ''
-
a QGIS expression to filter the features
Return values
bool —false if the data cannot be fetched
getAggregation()
Get the aggregate function used for this plot.
public
getAggregation() : null|string
Return values
null|string —the aggregate function, null if the values are not aggregated
getData()
Get the Plotly traces with all their values.
public
getData() : array<string|int, mixed>
All the values are loaded in memory: for a large number of features, prefer writeJson() which uses less memory.
Return values
array<string|int, mixed> —the Plotly traces
getLayerId()
Get the identifier of the layer containing the data.
public
getLayerId() : string
Return values
string —the QGIS layer identifier
getLayout()
Get the Plotly layout.
public
getLayout() : array<string|int, mixed>
Return values
array<string|int, mixed> —the Plotly layout
getTitle()
Get the plot title.
public
getTitle() : string
Return values
string —the plot title
getTraces()
Get the Plotly traces, in which the long lists of values are JsonColumn.
public
getTraces() : array<string|int, mixed>
The traces can be written with JsonStreamWriter.
Return values
array<string|int, mixed> —the Plotly traces
getType()
Get the plot type.
public
getType() : string
Return values
string —the plot type, for example "bar" or "pie"
isAggregated()
Tell if the values are aggregated by distinct X values.
public
isAggregated() : bool
Return values
bool —true if the values are aggregated
writeJson()
Write the plot as JSON: title, data (traces) and layout.
public
writeJson(resource $output[, array<string|int, mixed> $properties = array() ]) : void
The values are copied from their temporary streams.
Parameters
- $output : resource
-
the output stream
- $properties : array<string|int, mixed> = array()
-
additional properties of the JSON object
compareGroups()
Compare two groups, to sort them as the database does.
protected
static compareGroups(mixed $a, mixed $b) : int
The groups are compared value by value when they are lists (sunburst: parent, label). The null values are after the others, as with ORDER BY ascending. The texts are compared by bytes, which may differ from the collation of the database: this order is only used when the features were not sorted by the server.
Parameters
- $a : mixed
-
the first group
- $b : mixed
-
the second group
Return values
int —negative, 0 or positive if $a is before, equal to or after $b
customizeLayout()
Customize the layout for the plot type.
protected
customizeLayout(array<string|int, mixed> $layout) : array<string|int, mixed>
By default, the axis titles are the aliases of the X and Y fields, when there is only one field for the axis.
Parameters
- $layout : array<string|int, mixed>
-
the default layout
Return values
array<string|int, mixed> —the layout of the plot type
getColorColumn()
Get the colors of the groups of a trace, from a column of colors.
protected
static getColorColumn(JsonColumn $column, null|string $defaultColor) : null|JsonColumn
The colors are read one by one, so the memory does not depend on the number of groups. The groups without color get the default color.
Parameters
- $column : JsonColumn
-
the color of each group, null if none
- $defaultColor : null|string
-
the color of the groups without color, null to keep them without color
Return values
null|JsonColumn —the colors, null if no group has a color
getFieldAlias()
Get the field alias, or the field name if it has no alias.
protected
getFieldAlias(string $field) : string
Parameters
- $field : string
-
the field name
Return values
string —the field alias or name
getGroup()
Get the group of a feature for the aggregation.
protected
getGroup(object $properties, mixed $x) : mixed
By default, the features are grouped by X value.
Parameters
- $properties : object
-
the feature properties
- $x : mixed
-
the feature X value, not null
Return values
mixed —the group value, a scalar or an array
getGroupFields()
Get the fields grouping the features in the database aggregation.
protected
getGroupFields() : array<int, string>
They must give the same groups as getGroup().
Return values
array<int, string> —the group fields, the X field by default
getGroupFromValues()
Get the group from the values of the group fields returned by the database.
protected
getGroupFromValues(array<int, mixed> $values) : mixed
Parameters
- $values : array<int, mixed>
-
the values of the fields given by getGroupFields()
Return values
mixed —the group value, as returned by getGroup()
getTraceTemplate()
Get the trace template of the plot type.
protected
abstract getTraceTemplate() : array<string|int, mixed>
Return values
array<string|int, mixed> —the Plotly trace properties, with empty data
setAggregatedValues()
Set the aggregated values in the trace.
protected
setAggregatedValues(array<string|int, mixed> &$trace, JsonColumn|JsonRows|array<int, mixed> $groups, JsonColumn|array<int, mixed> $values, null|array<string|int, mixed>|JsonColumn $colors) : null|array<string|int, mixed>|JsonColumn
By default, the groups are the categories and the aggregated values are the values of the trace.
The groups and the values are usually JsonColumn, never loaded in memory: a plot type processing them must read them with foreach. The groups given by several fields (sunburst) are JsonRows.
Parameters
- $trace : array<string|int, mixed>
-
the trace
- $groups : JsonColumn|JsonRows|array<int, mixed>
-
the groups, sorted
- $values : JsonColumn|array<int, mixed>
-
the aggregated value of each group
- $colors : null|array<string|int, mixed>|JsonColumn
-
the color of each group
Return values
null|array<string|int, mixed>|JsonColumn —the colors of the trace values
aggregateInDatabase()
Aggregate the values in the database, without getting the features.
private
aggregateInDatabase(string $expFilter) : null|array<string|int, mixed>
The same features as with WFS are aggregated: same filters, including the filter by login and by polygon, and same groups and functions.
PostgreSQL returns the groups and the aggregated values as JSON arrays, which are copied in JsonColumn without being decoded: the memory does not depend on the number of groups.
Parameters
- $expFilter : string
-
a QGIS expression to filter the features
Return values
null|array<string|int, mixed> —the traces, null if the aggregation can not be done by the database (not a PostgreSQL layer, error...)
applyColors()
Set the aggregated colors in the trace.
private
applyColors(array<string|int, mixed> &$trace, null|array<string|int, mixed>|JsonColumn $colors) : void
A JsonColumn already contains the trace color for the values without color.
Parameters
- $trace : array<string|int, mixed>
-
the trace
- $colors : null|array<string|int, mixed>|JsonColumn
-
the color of each value, null if none
buildAggregatedTraces()
Build the traces with one aggregated value per group (distinct X value).
private
buildAggregatedTraces(iterable<string|int, mixed> $features) : null|array<string|int, mixed>
The features sorted by group are aggregated group by group, see buildSortedAggregatedTraces(). Otherwise, the values are aggregated in a single pass, keeping the state needed by the aggregate function for each group, and the groups are then sorted.
Parameters
- $features : iterable<string|int, mixed>
-
the GeoJSON features
Return values
null|array<string|int, mixed> —the traces, null if the features are not valid
buildRawTraces()
Build the traces with one value per feature.
private
buildRawTraces(iterable<string|int, mixed> $features) : null|array<string|int, mixed>
The values are stored as JSON in temporary streams (JsonColumn), so the memory used does not depend on the number of features.
Parameters
- $features : iterable<string|int, mixed>
-
the GeoJSON features
Return values
null|array<string|int, mixed> —the traces, null if the features are not valid
buildSortedAggregatedTraces()
Build the traces with one aggregated value per group, from features sorted by group.
private
buildSortedAggregatedTraces(iterable<string|int, mixed> $features) : null|array<string|int, mixed>
The features of a group are consecutive: only the state of the current group is kept. When the group changes, its aggregated values are written in JsonColumn, so the memory does not depend on the number of groups. It gives the same traces as buildAggregatedTraces() with sorted features.
The order is checked: if a group appears again after another one, the features are not sorted (the server could not sort them). The unsortedFeatures property is then set, and null is returned so that the features are aggregated by value instead. To keep a low memory usage, the groups already seen are kept as 64-bit hashes: a hash collision only makes the aggregation by value used, which is always right.
Parameters
- $features : iterable<string|int, mixed>
-
the GeoJSON features, sorted by group
Return values
null|array<string|int, mixed> —the traces, null if the features are not valid or not sorted by group
createAggregatedTraces()
Create the traces from the aggregated values of the groups.
private
createAggregatedTraces(JsonColumn|JsonRows|array<int, mixed> $groups, array<int, JsonColumn|array<int, mixed>> $values, array<int, null|array<string|int, mixed>|JsonColumn> $colors) : array<string|int, mixed>
Used by the aggregation in PHP and in the database.
Parameters
- $groups : JsonColumn|JsonRows|array<int, mixed>
-
the groups, as returned by getGroup()
- $values : array<int, JsonColumn|array<int, mixed>>
-
for each trace, the aggregated value of each group
- $colors : array<int, null|array<string|int, mixed>|JsonColumn>
-
for each trace, the color of each group, null if none
Return values
array<string|int, mixed> —the traces
createTrace()
Create a trace from the template of the plot type.
private
createTrace(int $index, string $yField) : array<string|int, mixed>
Parameters
- $index : int
-
the trace index in the configuration
- $yField : string
-
the trace Y field
Return values
array<string|int, mixed> —the trace, with its name and color, without data
emptyToNull()
Replace an empty string by null.
private
static emptyToNull(mixed $value) : mixed
Parameters
- $value : mixed
-
the value
Return values
mixed —the value, or null if it is empty
finalizeTrace()
Replace empty data arrays or columns by null.
private
finalizeTrace(array<string|int, mixed> $trace) : array<string|int, mixed>
Parameters
- $trace : array<string|int, mixed>
-
the trace
Return values
array<string|int, mixed> —the trace
getAxisProperties()
Get the properties of the trace containing the categories (X field) and the values (Y field). They are reverted for horizontal plots.
private
getAxisProperties(array<string|int, mixed> $trace) : array{0: string, 1: string}
Parameters
- $trace : array<string|int, mixed>
-
the trace
Return values
array{0: string, 1: string} —the category and the value properties
getDefaultFeatureColor()
Get the color of the groups without color of a trace.
private
getDefaultFeatureColor(int $index, string $yField) : null|string
Only the plot types with one color per value (marker.color) use the trace color for these groups. The others keep them without color.
Parameters
- $index : int
-
the trace index in the configuration
- $yField : string
-
the trace Y field
Return values
null|string —the trace color, null if none or not used
getLayoutTemplate()
Get the layout of the plot.
private
getLayoutTemplate() : array<string|int, mixed>
Return values
array<string|int, mixed> —the Plotly layout
getPropertyNames()
Get the fields to request: the fields used by the plot.
private
getPropertyNames() : array<int, string>
Return values
array<int, string> —the fields to request, without duplicates
getTraceFields()
Get the fields used by each trace.
private
getTraceFields() : array<int, array{y: string, z: null|string, color: null|string}>
The Z field is used only by the plot types having a Z property.
Return values
array<int, array{y: string, z: null|string, color: null|string}> —the fields of each trace
hasMandatoryFields()
Check that the configuration contains the fields needed by the plot type.
private
hasMandatoryFields() : bool
Return values
bool —true if the mandatory fields are configured
isValidFeature()
Check that the given feature contains the fields used by the plot.
private
isValidFeature(mixed $feature) : bool
Only the first feature is checked.
Parameters
- $feature : mixed
-
the GeoJSON feature
Return values
bool —true if the feature has properties containing the X and Y fields
logDebug()
Log a message in the Lizmap admin log, in debug mode only.
private
logDebug(string $message) : void
Parameters
- $message : string
-
the message
parsePlotConfig()
Read the plot configuration.
private
parsePlotConfig(array<string|int, mixed> $plotConfig) : void
Parameters
- $plotConfig : array<string|int, mixed>
-
the plot configuration
resolveAggregation()
Get the aggregate function to use for this plot type.
private
resolveAggregation(mixed $aggregation) : null|string
Parameters
- $aggregation : mixed
-
the aggregation from the plot config
Return values
null|string —the aggregate function, null if values must not be aggregated
setFeatureColors()
Set the feature colors in the trace property used by the plot type.
private
setFeatureColors(array<string|int, mixed> &$trace, array<string|int, mixed>|JsonColumn $colors) : void
Parameters
- $trace : array<string|int, mixed>
-
the trace
- $colors : array<string|int, mixed>|JsonColumn
-
the color of each value
splitFields()
Split a comma separated list of fields.
private
static splitFields(string $fields) : array<int, string>
Parameters
- $fields : string
-
a comma separated list of fields
Return values
array<int, string> —the fields, without empty ones