Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Test all supported Chart.js charts.
  19   *
  20   * @package    core
  21   * @copyright  2016 Simey Lameze <simey@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require(__DIR__ . '/../../../config.php');
  26  
  27  require_login();
  28  $context = context_system::instance();
  29  require_capability('moodle/site:config', $context);
  30  
  31  $PAGE->set_context($context);
  32  $PAGE->set_url('/lib/tests/other/chartjstestpage.php');
  33  $PAGE->set_heading('Chart.js library test');
  34  $PAGE->set_pagelayout('standard');
  35  echo $OUTPUT->header();
  36  
  37  $sales = new \core\chart_series('Sales', [1000, 1170, 660, 1030]);
  38  $expenses = new \core\chart_series('Expenses', [400, 460, 1120, 540]);
  39  $labels = ['2004', '2005', '2006', '2007'];
  40  
  41  $chart = new \core\chart_pie();
  42  $chart->set_title('PIE CHART');
  43  $chart->add_series($sales);
  44  $chart->set_labels($labels);
  45  
  46  $chart2 = new \core\chart_pie();
  47  $chart2->set_title('DOUGHNUT CHART');
  48  $chart2->set_doughnut(true);
  49  $chart2->add_series($sales);
  50  $chart2->set_labels($labels);
  51  
  52  $chart3 = new \core\chart_line();
  53  $chart3->set_title('TENSIONED LINES CHART');
  54  $chart3->add_series($sales);
  55  $chart3->add_series($expenses);
  56  $chart3->set_labels($labels);
  57  
  58  $chart4 = new \core\chart_line();
  59  $chart4->set_smooth(true);
  60  $chart4->set_title('SMOOTH LINES CHART');
  61  $chart4->add_series($sales);
  62  $chart4->add_series($expenses);
  63  $chart4->set_labels($labels);
  64  
  65  $chart5 = new \core\chart_bar();
  66  $chart5->set_title('BAR CHART');
  67  $chart5->add_series($sales);
  68  $chart5->add_series($expenses);
  69  $chart5->set_labels($labels);
  70  
  71  $chart6 = new \core\chart_bar();
  72  $chart6->set_title('HORIZONTAL BAR CHART');
  73  $chart6->set_horizontal(true);
  74  $chart6->add_series($sales);
  75  $chart6->add_series($expenses);
  76  $chart6->set_labels($labels);
  77  
  78  $chart7 = new \core\chart_bar();
  79  $chart7->set_title('STACKED BAR CHART');
  80  $chart7->set_stacked(true);
  81  $chart7->add_series($sales);
  82  $chart7->add_series($expenses);
  83  $chart7->set_labels($labels);
  84  
  85  $chart8 = new \core\chart_bar();
  86  $chart8->set_title('BAR CHART COMBINED WITH LINE CHART');
  87  $expensesline = new \core\chart_series('Expenses', [400, 460, 1120, 540]);
  88  $expensesline->set_type(\core\chart_series::TYPE_LINE);
  89  $chart8->add_series($expensesline);
  90  $chart8->add_series($sales);
  91  $chart8->set_labels($labels);
  92  
  93  $hills = new \core\chart_series('Hills', [700, 870, 660, 950]);
  94  $mountain = new \core\chart_series('Mountain', [400, 460, 1350, 540]);
  95  $sky = new \core\chart_series('Sky', [1400, 1500, 1550, 1500]);
  96  $chart9 = new \core\chart_line();
  97  $chart9->set_title('AREA FILL CHART');
  98  $chart9->add_series($hills);
  99  $chart9->add_series($mountain);
 100  $chart9->add_series($sky);
 101  $chart9->set_labels($labels);
 102  $hills->set_smooth(true);
 103  $hills->set_fill('origin');
 104  $mountain->set_fill('-1');
 105  $sky->set_fill('end');
 106  
 107  $chart10 = new \core\chart_bar();
 108  $chart10->set_title('BAR CHART WITH LEGEND OPTIONS (LEGEND POSITION IN THE LEFT)');
 109  $expensesline = new \core\chart_series('Expenses', [400, 460, 1120, 540]);
 110  $expensesline->set_type(\core\chart_series::TYPE_LINE);
 111  $chart10->add_series($expensesline);
 112  $chart10->set_legend_options(['position' => 'left', 'reverse' => true]);
 113  $chart10->add_series($sales);
 114  $chart10->set_labels($labels);
 115  
 116  $chart11 = new \core\chart_bar();
 117  $chart11->set_title('BAR CHART WITH LEGEND OPTIONS (LEGEND HIDDEN)');
 118  $expensesline = new \core\chart_series('Expenses', [400, 460, 1120, 540]);
 119  $expensesline->set_type(\core\chart_series::TYPE_LINE);
 120  $chart11->add_series($expensesline);
 121  $chart11->set_legend_options(['display' => false]);
 122  $chart11->add_series($sales);
 123  $chart11->set_labels($labels);
 124  
 125  echo $OUTPUT->render($chart);
 126  echo $OUTPUT->render($chart2);
 127  echo $OUTPUT->render($chart3);
 128  echo $OUTPUT->render($chart4);
 129  echo $OUTPUT->render($chart5);
 130  echo $OUTPUT->render($chart6);
 131  echo $OUTPUT->render($chart7);
 132  echo $OUTPUT->render($chart8);
 133  echo $OUTPUT->render($chart9);
 134  echo $OUTPUT->render($chart10);
 135  echo $OUTPUT->render($chart11);
 136  
 137  echo $OUTPUT->footer();