MDL-55015 core: Add support for axis label in charts

Part of MDL-54987 epic.
This commit is contained in:
Frederic Massart
2016-07-25 10:42:28 +01:00
committed by Dan Poltawski
parent 858cbfdfd3
commit f5474e65fc
8 changed files with 298 additions and 8 deletions
+76
View File
@@ -0,0 +1,76 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Chart axis.
*
* @package core
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core;
defined('MOODLE_INTERNAL') || die();
use coding_exception;
use JsonSerializable;
use renderable;
/**
* Chart axis class.
*
* @package core
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class chart_axis implements JsonSerializable {
const POS_DEFAULT = null;
const POS_BOTTOM = 'bottom';
const POS_LEFT = 'left';
const POS_RIGHT = 'right';
const POS_TOP = 'top';
protected $label = null;
protected $position = self::POS_DEFAULT;
public function __construct() {
}
public function get_label() {
return $this->label;
}
public function get_position() {
return $this->position;
}
public function jsonSerialize() {
return [
'label' => $this->label,
'position' => $this->position,
];
}
public function set_label($label) {
return $this->label = $label;
}
public function set_position($position) {
return $this->position = $position;
}
}
+49 -1
View File
@@ -41,6 +41,8 @@ class chart_base implements JsonSerializable, renderable {
protected $series = [];
protected $labels = [];
protected $title = null;
protected $xaxes = [];
protected $yaxes = [];
public function __construct() {
}
@@ -54,10 +56,31 @@ class chart_base implements JsonSerializable, renderable {
'type' => $this->get_type(),
'series' => $this->series,
'labels' => $this->labels,
'title' => $this->title
'title' => $this->title,
'axes' => [
'x' => $this->xaxes,
'y' => $this->yaxes,
]
];
}
private function get_axis($type, $index, $createifnotexists) {
if ($type === 'x') {
$axes = &$this->xaxes;
} else {
$axes = &$this->yaxes;
}
if (!isset($axes[$index])) {
if (!$createifnotexists) {
throw new coding_exception('Unknown axis.');
}
$axes[$index] = new chart_axis();
}
return $axes[$index];
}
public function get_labels() {
return $this->labels;
}
@@ -75,6 +98,22 @@ class chart_base implements JsonSerializable, renderable {
return substr($classname, strpos($classname, '_') + 1);
}
public function get_xaxes() {
return $this->xaxes;
}
public function get_xaxis($index = 0, $createifnotexists = false) {
return $this->get_axis('x', $index, $createifnotexists);
}
public function get_yaxes() {
return $this->yaxes;
}
public function get_yaxis($index = 0, $createifnotexists = false) {
return $this->get_axis('y', $index, $createifnotexists);
}
public function set_labels(array $labels) {
$this->labels = $labels;
}
@@ -82,4 +121,13 @@ class chart_base implements JsonSerializable, renderable {
public function set_title($title) {
$this->title = $title;
}
public function set_xaxis(chart_axis $axis, $index = 0) {
return $this->xaxes[$index] = $axis;
}
public function set_yaxis(chart_axis $axis, $index = 0) {
return $this->yaxes[$index] = $axis;
}
}