Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-751 Reduce values used to display charts #787

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions classes/output/catscaledashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
*/
class catscaledashboard {

/**
* Sets the maximum number of values used for the chart.
* @var int
*/
public const CHART_MAX_NUM = 100;

/** @var int of catscaleid */
public int $catscaleid = 0;
Expand Down Expand Up @@ -96,6 +101,9 @@ private function render_itemdifficulties(array $itemlists) {
continue;
}

// To keep the time required to render the chart reasonable, do not
// display more values than required.
$data = $this->filter_values($data);
$chart = new \core\chart_line();
$series = new \core\chart_series('Series 1 (Line)', array_values($data));
$chart->set_smooth(true); // Calling set_smooth() passing true as parameter, will display smooth lines.
Expand Down Expand Up @@ -125,6 +133,11 @@ private function render_personabilities(model_person_param_list $personparams) {
if (empty($data)) {
return "";
}

// To keep the time required to render the chart reasonable, do not
// display more values than required.
$data = $this->filter_values($data);

$chart = new \core\chart_line();
$series = new \core\chart_series('Series 1 (Line)', array_values($data));
$chart->set_smooth(true); // Calling set_smooth() passing true as parameter, will display smooth lines.
Expand Down Expand Up @@ -209,12 +222,35 @@ public function export_scaledetails(\renderer_base $output): array {
'contextselector' => scaleandcontexselector::render_contextselector($this->catcontextid),
'backtoscaleslink' => $backbutton,
'scaledetailviewheading' => get_string('scaledetailviewheading', 'local_catquiz', $this->catscale->name),
// Rendering the chart causes JS timeouts for big scales. Commenting out until we have a proper fix.
// phpcs:disable
// 'itemdifficulties' => $this->render_itemdifficulties($itemdifficulties),
// 'personabilities' => $this->render_personabilities($personabilities),
// phpcs:enable
'itemdifficulties' => $this->render_itemdifficulties($itemdifficulties),
'personabilities' => $this->render_personabilities($personabilities),
'modelbutton' => $this->render_modelbutton($this->catcontextid),
];
}

/**
* If the given array contains more values than allowed, values are removed.
*
* This is used to remove values from the arrays of the ability and item difficulty charts if they contain too many.
*
* @param array $values
* @param int $max
* @return array
*/
private function filter_values(array $values, int $max = self::CHART_MAX_NUM): array {
if (count($values) <= $max) {
return $values;
}

// Show every Nth value.
$showeveryn = round(count($values) / $max);
$i = 0;
foreach (array_keys($values) as $key) {
if ($i % $showeveryn != 0) {
unset($values[$key]);
}
$i++;
}
return $values;
}
}
Loading