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; $axis = [ "type" => $type, "display" => true, "position" => $position, "id" => $id, "gridLines" => [ "drawOnChartArea" => $grid_lines ], ]; if($tick_config) { $axis["ticks"] = $tick_config; } if($scale_label_config) { $axis["scaleLabel"] = $scale_label_config; } $this->xAxes[] = $axis; } public function addYAxis($type, $position, $id, $grid_lines = true, $tick_config = []) { $axis = [ "type" => $type, "display" => true, "position" => $position, "id" => $id, "gridLines" => [ "drawOnChartArea" => $grid_lines ], ]; 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; $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; $this->datasets[] = (object) $dataset; } public function setLabels($labels) { $this->labels = $labels; } public function setDatalabelSuffixes(... $suffixes) { $this->datalabelSuffixes = $suffixes; } public function setDatalabelFormatterCallback($callback) { $this->datalabelFormatterCallback = $callback; } 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", ], "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 printDataTable() { ?> labels as $label) { ?> datasets as $index => $dataset) { $suffix = ""; if($this->datalabelSuffixes) { $suffix = $this->datalabelSuffixes[$index]; } ?> data as $value) { ?>
label)?>
beforeHtml = $html; } public function setAfterHtml($html) { $this->afterHtml = $html; } } ?>