Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

Chart base.

Copyright: 2016 Frédéric Massart - FMCorz.net
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 306 lines (9 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

chart_base:: (20 methods):
  __construct()
  add_series()
  jsonSerialize()
  get_axis()
  get_labels()
  get_legend_options()
  get_series()
  get_title()
  get_type()
  get_xaxes()
  get_xaxis()
  get_yaxes()
  get_yaxis()
  set_defaults()
  set_labels()
  set_legend_options()
  set_title()
  set_xaxis()
  set_yaxis()
  validate_axis()


Class: chart_base  - X-Ref

Chart base class.

__construct()   X-Ref
Constructor.

Must not take any argument.

Most of the time you do not want to extend this, rather extend the
method {@link self::set_defaults} to set the defaults on instantiation.

add_series(chart_series $serie)   X-Ref
Add a series to the chart.

param: chart_series $serie The serie.

jsonSerialize()   X-Ref
Serialize the object.

return: array

get_axis($type, $index, $createifnotexists)   X-Ref
Get an axis.

return: chart_axis
param: string $type Accepts values 'x' or 'y'.
param: int $index The index of this axis.
param: bool $createifnotexists Whether to create the axis if not found.

get_labels()   X-Ref
Get the labels of the X axis.

return: string[]

get_legend_options()   X-Ref
Get an array of options for the chart legend.

return: array

get_series()   X-Ref
Get the series.

return: chart_series[]

get_title()   X-Ref
Get the title.

return: string

get_type()   X-Ref
Get the chart type.

return: string

get_xaxes()   X-Ref
Get the X axes.

return: chart_axis[]

get_xaxis($index = 0, $createifnotexists = false)   X-Ref
Get an X axis.

return: chart_axis
param: int $index The index of the axis.
param: bool $createifnotexists When true, create an instance of the axis if none exist at this index yet.

get_yaxes()   X-Ref
Get the Y axes.

return: chart_axis[]

get_yaxis($index = 0, $createifnotexists = false)   X-Ref
Get a Y axis.

return: chart_axis
param: int $index The index of the axis.
param: bool $createifnotexists When true, create an instance of the axis if none exist at this index yet.

set_defaults()   X-Ref
Set the defaults for this chart type.

Child classes can extend this to set default values on instantiation.

In general the constructor could be used, but this method is here to
emphasize and self-document the default values set by the chart type.

return: void

set_labels(array $labels)   X-Ref
Set the chart labels.

param: string[] $labels The labels.

set_legend_options(array $legendoptions)   X-Ref
Set options for the chart legend.
See https://www.chartjs.org/docs/2.7.0/configuration/legend.html for options.

Note: Setting onClick and onHover events is not directly supported through
this method. These config options must be set directly within Javascript
on the page.

param: array $legendoptions Whether or not to display the chart's legend.

set_title($title)   X-Ref
Set the title.

param: string $title The title.

set_xaxis(chart_axis $axis, $index = 0)   X-Ref
Set an X axis.

Note that this will override any predefined axis without warning.

param: chart_axis $axis The axis.
param: int $index The index of the axis.

set_yaxis(chart_axis $axis, $index = 0)   X-Ref
Set an Y axis.

Note that this will override any predefined axis without warning.

param: chart_axis $axis The axis.
param: int $index The index of the axis.

validate_axis($xy, chart_axis $axis, $index = 0)   X-Ref
Validate an axis.

We validate this from PHP because not doing it here could result in errors being
hard to trace down. For instance, if we were to add axis at keys without another
axis preceding, we would effectively contain the axes in an associative array
rather than a simple array, and that would have consequences on serialisation.

param: string $xy Accepts x or y.
param: chart_axis $axis The axis to validate.
param: index $index The index of the axis.