Skip to content

Commit

Permalink
Filter special whitespace when rendering ImageGraphs without unifont (#…
Browse files Browse the repository at this point in the history
…21897)

* Test ImageGraph rendering with "week" dates

* Filter special whitespace when rendering ImageGraphs without unifont

* Update expected screenshots

* Remove usage of str_ends_with
  • Loading branch information
mneudert authored Feb 12, 2024
1 parent 92a4c3d commit 340cfba
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
33 changes: 27 additions & 6 deletions plugins/ImageGraph/StaticGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ public static function getAvailableStaticGraphTypes()
return array_keys(self::$availableStaticGraphTypes);
}

public static function fixWhitespaceNonUnifont($value)
{
if (!is_string($value)) {
return $value;
}

return strtr(
$value,
[
// thin space
"\xE2\x80\x89" => ' ',
// narrow non-break-space
"\xE2\x80\xAF" => "\xC2\xA0",
]
);
}

/**
* Save rendering to disk
*
Expand Down Expand Up @@ -248,14 +265,17 @@ protected function initpData()
}
}

// Use a different formating method if not using unifont
// Fix whitespace if not using unifont
$abscissaSeries = $this->abscissaSeries;
$formatMethodName = 'formatYAxis';
if (strpos($this->font, 'unifont') === false) {

if (false === strpos($this->font, API::UNICODE_FONT)) {
$abscissaSeries = array_map([$this, 'fixWhitespaceNonUnifont'], $abscissaSeries);
$formatMethodName = 'formatYAxisNonUnifont';
}
$this->pData->setAxisDisplay(0, AXIS_FORMAT_CUSTOM, '\\Piwik\\Plugins\\ImageGraph\\' . $formatMethodName);

$this->pData->addPoints($this->abscissaSeries, self::ABSCISSA_SERIE_NAME);
$this->pData->setAxisDisplay(0, AXIS_FORMAT_CUSTOM, '\\Piwik\\Plugins\\ImageGraph\\' . $formatMethodName);
$this->pData->addPoints($abscissaSeries, self::ABSCISSA_SERIE_NAME);
$this->pData->setAbscissa(self::ABSCISSA_SERIE_NAME);
}

Expand Down Expand Up @@ -401,6 +421,7 @@ function formatYAxis($value)
*/
function formatYAxisNonUnifont($value)
{
// Replace any narrow non-breaking spaces with non-breaking spaces as some fonts may not support it
return str_replace("\xE2\x80\xAF", "\xC2\xA0", NumberFormatter::getInstance()->format($value));
return StaticGraph::fixWhitespaceNonUnifont(
NumberFormatter::getInstance()->format($value)
);
}
6 changes: 6 additions & 0 deletions plugins/ImageGraph/tests/UI/ImageGraph_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ describe("ImageGraph", function () {
expect(await page.screenshot({ fullPage: true })).to.matchImage('evolution_graph');
});

it("should render evolution graphs correctly for week dates", async function() {
await page.goto(getImageGraphUrl('VisitsSummary', 'get', 'evolution', 'week', '2011-06-01,2012-06-01'));

expect(await page.screenshot({ fullPage: true })).to.matchImage('evolution_graph_week');
});

it("should render horizontal bar graphs correctly", async function() {
await page.goto(getImageGraphUrl('DevicesDetection', 'getBrowsers', 'horizontalBar', 'year', '2012-01-01'));

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 340cfba

Please sign in to comment.