| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- <?php /** @noinspection DuplicatedCode */
- /** @noinspection PhpUnused */
- /** @noinspection PhpUndefinedMethodInspection */
- /** @noinspection PhpUnusedLocalVariableInspection */
- use PhpOffice\PhpPresentation\Slide;
- use PhpOffice\PhpPresentation\Style\Alignment;
- class Chart {
- const DATALABELS_BOX = [
- "backgroundColor" => "rgba(0, 0, 0, 0.75)",
- "borderRadius" => "2",
- ];
- const COLOURS = [
- "#f44336",
- "#673ab7",
- "#03a9f4",
- "#4caf50",
- "#ffeb3b",
- "#ff5722",
- "#607d8b",
- "#e91e63",
- "#3f51b5",
- "#00bcd4",
- "#8bc34a",
- "#ffc107",
- "#795548",
- "#9c27b0",
- "#2196f3",
- "#009688",
- "#cddc39",
- "#ff9800",
- "#9e9e9e",
- ];
- private $id = "";
- private $title = "Untitled";
- private $type = "bar";
- private $datasets = [];
- private $printDatasets = [];
- private $labels = [];
- private $printLabels = null;
- private $tooltipsMode = "nearest";
- private $tooltipsIntersect = true;
- private $xAxes = [];
- private $yAxes = [];
- private $datalabelSuffixes = [];
- private $datalabelFormatterCallback = false;
- private $printSuffixes = null;
- public $beforeHtml = null;
- public $afterHtml = null;
- public $print = false;
- public $printPercentages = false;
- public $printTotalColumn = false;
- /**
- * Chart constructor.
- * @throws Exception
- */
- public function __construct() {
- $this->id = bin2hex(random_bytes(8));
- }
- public function addXAxis($options = []) {
- $type = $options["type"] ?? "linear";
- $position = $options["position"] ?? "bottom";
- $id = $options["id"] ?? "x-axis";
- $grid_lines = $options["grid_lines"] ?? true;
- $tick_config = $options["tick_config"] ?? null;
- $scale_label_config = $options["scale_label_config"] ?? null;
- $stacked = $options["stacked"] ?? false;
- $axis = [
- "type" => $type,
- "display" => true,
- "position" => $position,
- "id" => $id,
- "gridLines" => [
- "drawOnChartArea" => $grid_lines
- ],
- "stacked" => $stacked,
- ];
- if($tick_config) {
- $axis["ticks"] = $tick_config;
- }
- if($scale_label_config) {
- $axis["scaleLabel"] = $scale_label_config;
- }
- $this->xAxes[] = $axis;
- }
- public function addYAxis($options = []) {
- $type = $options["type"] ?? "linear";
- $position = $options["position"] ?? "left";
- $id = $options["id"] ?? "y-axis-0";
- $grid_lines = $options["grid_lines"] ?? true;
- $tick_config = $options["tick_config"] ?? null;
- $scale_label_config = $options["scale_label_config"] ?? null;
- $stacked = $options["stacked"] ?? false;
- $axis = [
- "type" => $type,
- "display" => true,
- "position" => $position,
- "id" => $id,
- "gridLines" => [
- "drawOnChartArea" => $grid_lines
- ],
- "stacked" => $stacked,
- ];
- if($tick_config) {
- $axis["ticks"] = $tick_config;
- }
- $this->yAxes[] = $axis;
- }
- public function getID() {
- return $this->id;
- }
- public function getTitle() {
- return $this->title;
- }
- public function setTitle($title) {
- $this->title = $title;
- }
- public function setType($type) {
- $this->type = $type;
- }
- public function addDataset($label, $data, $options = []) {
- $background_colours = $options["background_colours"] ?? false;
- $border_colours = $options["border_colours"] ?? false;
- $border_width = $options["border_width"] ?? false;
- $y_axis_id = $options["y_axis_id"] ?? false;
- $type = $options["type"] ?? false;
- $datalabels = $options["datalabels"] ?? false;
- $line_tension = $options["line_tension"] ?? null;
- $dataset = [
- "label" => $label,
- "data" => $data,
- ];
- if($background_colours) $dataset["backgroundColor"] = $background_colours;
- if($border_colours) $dataset["borderColor"] = $border_colours;
- if($border_width) $dataset["borderWidth"] = (int) $border_width;
- if($y_axis_id) $dataset["yAxisID"] = $y_axis_id;
- if($type) $dataset["type"] = $type;
- if($datalabels) $dataset["datalabels"] = $datalabels;
- if(isset($line_tension)) $dataset["lineTension"] = $line_tension;
- $this->datasets[] = (object) $dataset;
- }
- public function addPrintDataset($label, $data) {
- $dataset = [
- "label" => $label,
- "data" => $data,
- ];
- $this->printDatasets[] = (object) $dataset;
- }
- public function getDatasets() {
- return $this->datasets;
- }
- public function getPrintDatasets() {
- return $this->printDatasets;
- }
- public function getLabels() {
- return $this->labels;
- }
- public function setLabels($labels) {
- $this->labels = $labels;
- }
- public function getPrintLabels() {
- return $this->printLabels;
- }
- public function setPrintLabels($labels) {
- $this->printLabels = $labels;
- }
- public function getDatalabelSuffixes() {
- return $this->datalabelSuffixes;
- }
- public function setDatalabelSuffixes(... $suffixes) {
- $this->datalabelSuffixes = $suffixes;
- }
- public function setDatalabelFormatterCallback($callback) {
- $this->datalabelFormatterCallback = $callback;
- }
- public function getPrintSuffixes() {
- return $this->printSuffixes;
- }
- public function setPrintSuffixes(... $suffixes) {
- $this->printSuffixes = $suffixes;
- }
- public function getJSON() {
- $object = (object) [
- "type" => $this->type,
- "data" => (object) [
- "labels" => $this->labels,
- "datasets" => $this->datasets
- ],
- "options" => (object) [
- "title" => (object) [
- "display" => true,
- "fontSize" => 32,
- "fontFamily" => "Roboto",
- "fontStyle" => "300",
- "text" => $this->title
- ],
- "legend" => (object) [
- "position" => "top",
- ],
- "tooltips" => (object) [
- "mode" => $this->tooltipsMode,
- "intersect" => $this->tooltipsIntersect,
- ],
- "plugins" => (object) [
- "datalabels" => [
- "color" => "white",
- ],
- ],
- ],
- ];
- if($this->xAxes || $this->yAxes) {
- $object->options->scales = new stdClass;
- if($this->xAxes) {
- $object->options->scales->xAxes = $this->xAxes;
- }
- if($this->yAxes) {
- $object->options->scales->yAxes = $this->yAxes;
- }
- }
- if($this->datalabelSuffixes) {
- $object->options->datalabelSuffixes = $this->datalabelSuffixes;
- }
- if($this->datalabelFormatterCallback) {
- $object->datalabelFormatterCallback = $this->datalabelFormatterCallback;
- }
- return json_encode($object);
- }
- public function renderCanvasHtml() {
- ?>
- <canvas id="<?=$this->getID()?>" data-chart="<?=htmlspecialchars($this->getJSON())?>" width="400" height="300">
- </canvas>
- <?php
- }
- public function printDataTable(): void {
- $row_labels = $this->printLabels ?? $this->labels;
- $print_total_row = $this->printTotalColumn;
- $print_percentage_column = $this->printPercentages;
- $suffixes = $this->printSuffixes ?? $this->datalabelSuffixes;
- $datasets = array_merge($this->datasets, $this->printDatasets);
- $total_count = array_sum($datasets[0]->data);
- ?>
- <table class="mt-3 w-100 table table-sm small table-bordered">
- <thead>
- <tr>
- <th>Name</th>
- <?php
- foreach($datasets as $dataset) {
- ?>
- <th><?=htmlspecialchars($dataset->label)?></th>
- <?php
- }
- if($print_percentage_column) {
- ?>
- <th><?=htmlspecialchars(__("Percentage"))?></th>
- <?php
- }
- ?>
- </tr>
- </thead>
- <tbody>
- <?php
- $data = [];
- foreach($datasets as $column_index => $dataset) {
- foreach($dataset->data as $row_index => $value) {
- $data[$row_index][$column_index] = $value;
- }
- }
- foreach($data as $row_index => $columns) {
- ?>
- <tr>
- <td><?=htmlspecialchars($row_labels[$row_index])?></td>
- <?php
- foreach($columns as $column_index => $value) {
- $suffix = $suffixes[$column_index] ?? "";
- ?>
- <td class="text-right">
- <?php
- if($value === null) {
- ?>
- <small class="text-muted"><?=htmlspecialchars(__("N/A"))?></small>
- <?php
- continue;
- }
- ?>
- <?=number_format($value)?><?=htmlspecialchars($suffix)?>
- </td>
- <?php
- }
- if($print_percentage_column) {
- $value = $columns[0] ?? 0;
- if($total_count > 0) {
- $percentage = floor(($value / $total_count) * 10000) / 100;
- }
- else {
- $percentage = 0;
- }
- ?>
- <td class="text-right">
- <?=number_format($percentage, 2)?>%
- </td>
- <?php
- }
- ?>
- </tr>
- <?php
- }
- ?>
- </tbody>
- <?php
- if($print_total_row) {
- ?>
- <tfoot>
- <tr>
- <th><?=htmlspecialchars(__("Total"))?></th>
- <?php
- foreach($datasets as $dataset) {
- ?>
- <td class="text-right">
- <?=number_format(array_sum($dataset->data))?>
- </td>
- <?php
- }
- ?>
- </tr>
- </tfoot>
- <?php
- }
- ?>
- </table>
- <?php
- }
- public function setBeforeHtml($html) {
- $this->beforeHtml = $html;
- }
- public function setAfterHtml($html) {
- $this->afterHtml = $html;
- }
- public function setTooltipsMode($mode) {
- $this->tooltipsMode = $mode;
- }
- public function setTooltipsIntersect($intersect) {
- $this->tooltipsIntersect = $intersect;
- }
- // Presentation methods
- public function addChart(Slide $slide, string $path) {
- $chart_offset_y = 100;
- if($this->print) {
- $chart_offset_y = 10;
- }
- $shape = $slide->createDrawingShape();
- $shape->setName("Chart")
- ->setDescription("Chart")
- ->setPath($path)
- ->setHeight(520)
- ->setOffsetX(133)
- ->setOffsetY($chart_offset_y);
- return true;
- }
- /**
- * @param Slide $slide
- * @param int $offset_x
- * @param int $offset_y
- * @throws Exception
- */
- public function addDataTable(Slide $slide, int $offset_x = 30, int $offset_y = 540) {
- $labels = $this->getLabels();
- $datasets = array_merge($this->getDatasets(), $this->getPrintDatasets());
- $column_count = 1 + count($labels);
- // Table
- if($this->printTotalColumn) ++$column_count;
- $shape = $slide->createTableShape($column_count)
- ->setWidth(900)
- ->setOffsetX($offset_x)
- ->setOffsetY($offset_y);
- // Create header row
- $row = $shape->createRow();
- $row->setHeight(20);
- $cell = $row->nextCell();
- $labels = $this->getPrintLabels() ?? $this->getLabels();
- if($this->printTotalColumn) $labels[] = "Total";
- foreach($labels as $label) {
- $cell = $row->nextCell();
- $cell->getActiveParagraph()->getAlignment()
- ->setMarginBottom(4)
- ->setMarginLeft(4)
- ->setMarginRight(4)
- ->setMarginTop(4);
- $cell->createTextRun($label)
- ->getFont()->setBold(true);
- }
- $suffixes = $this->getPrintSuffixes() ?? $this->getDatalabelSuffixes();
- // Datasets
- foreach($datasets as $index => $dataset) {
- $label = $dataset->label;
- $data = $dataset->data;
- $print_data = array_slice($data, 0);
- $total = array_sum($data);
- $suffix = $suffixes[$index] ?? "";
- if($this->printTotalColumn) {
- $print_data[] = ($suffix == "%") ? "" : number_format(array_sum($data)) . $suffix;
- }
- $row = $shape->createRow();
- $row->setHeight(20);
- $cell = $row->nextCell();
- $cell->getActiveParagraph()->getAlignment()
- ->setMarginBottom(4)
- ->setMarginLeft(4)
- ->setMarginRight(4)
- ->setMarginTop(4);
- $cell->createTextRun($label)
- ->getFont()->setBold(true);
- foreach($print_data as $value) {
- $string = strlen($value) == 0 ? "" : $value . $suffix;
- $cell = $row->nextCell();
- $cell->getActiveParagraph()->getAlignment()
- ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
- ->setMarginBottom(4)
- ->setMarginLeft(4)
- ->setMarginRight(4)
- ->setMarginTop(4);
- $cell->createTextRun($string);
- }
- if($this->printPercentages) {
- $percentages = [];
- foreach($data as $value) {
- if($total == 0) {
- $percentages[] = 0;
- continue;
- }
- $percentages[] = (floor(($value * 10000) / $total) / 100) . "%";
- }
- $row = $shape->createRow();
- $row->setHeight(20);
- $cell = $row->nextCell();
- $cell->getActiveParagraph()->getAlignment()
- ->setMarginBottom(4)
- ->setMarginLeft(4)
- ->setMarginRight(4)
- ->setMarginTop(4);
- $cell->createTextRun("Percentage")
- ->getFont()->setBold(true);
- foreach($percentages as $string) {
- $cell = $row->nextCell();
- $cell->getActiveParagraph()->getAlignment()
- ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
- ->setMarginBottom(4)
- ->setMarginLeft(4)
- ->setMarginRight(4)
- ->setMarginTop(4);
- $cell->createTextRun($string);
- }
- }
- }
- }
- }
|