"rgba(0, 0, 0, 0.5)", "borderRadius" => "2", ]; public const DATALABELS_STROKE = [ "textStrokeColor" => "black", "textStrokeWidth" => 2, ]; public 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 bool $isPeriodic = false; private $type = "bar"; private $datasets = []; private $printDatasets = []; private $labels = []; private $printLabels = null; private $tooltipsMode = "nearest"; private $tooltipsIntersect = true; private $xAxes = []; private $yAxes = []; private $categories = []; private $datalabelSuffixes = []; private $datalabelFormatterCallback = false; private $printSuffixes = null; public $beforeHtml = null; public $afterHtml = null; public $print = false; public $printPercentages = false; public $printTotalColumn = false; public $numeratorColumnIndex = null; public $denominatorColumnIndex = null; public $tableSorter = null; // Static methods public static function getColourByIndex(int $index): string { $count = count(self::COLOURS); $colour_index = $index % $count; return self::COLOURS[$colour_index]; } // Constructor /** * Chart constructor. * @throws Exception */ public function __construct() { $this->id = bin2hex(random_bytes(8)); } // Object methods 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; $point_background_colour = ["point_background_colour"] ?? 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; if(isset($point_background_colour)) $dataset["pointBackgroundColor"] = $point_background_colour; $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() { ?> 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); ?> $dataset) { foreach($dataset->data as $row_index => $value) { $data[$row_index][$column_index] = $value; } } if($this->tableSorter) { usort($data, $this->tableSorter); } foreach($data as $row_index => $columns) { ?> $value) { $suffix = $suffixes[$column_index] ?? ""; ?> 0) { $percentage = floor(($value / $total_count) * 10000) / 100; } else { $percentage = 0; } ?> label; $is_percentage = strpos($label, "%"); if($is_percentage && $this->numeratorColumnIndex !== null && $this->denominatorColumnIndex !== null) { $numerator_sum = array_sum($datasets[$this->numeratorColumnIndex]->data); $denominator_sum = array_sum($datasets[$this->denominatorColumnIndex]->data); $text = number_format(($numerator_sum * 100) / $denominator_sum, 2) . "%"; } else { $text = number_format(array_sum($dataset->data)); } ?>
Name label)?>
%
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); } } } } public function setCategories(array $categories): void { $this->categories = $categories; } public function getCategories(): array { return $this->categories; } public function isPeriodic(): bool { return $this->isPeriodic; } public function setIsPeriodic(bool $isPeriodic): void { $this->isPeriodic = $isPeriodic; } }