-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add chart to ?action=info #144
base: master
Are you sure you want to change the base?
Changes from all commits
be70ed1
528938c
24d661e
92f01ef
0f1bd9b
1ac52d6
9479165
888a1b1
1943022
6596a74
837aaaa
85a54e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,7 +2,6 @@ | |||||
|
||||||
namespace Miraheze\MatomoAnalytics\HookHandlers; | ||||||
|
||||||
use MediaWiki\Context\IContextSource; | ||||||
use MediaWiki\Hook\InfoActionHook; | ||||||
use MediaWiki\Hook\SkinAfterBottomScriptsHook; | ||||||
use MediaWiki\Html\Html; | ||||||
|
@@ -85,22 +84,25 @@ public function onSkinAfterBottomScripts( $skin, &$text ) { | |||||
return true; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Display total pageviews in the last 30 days and show a graph with details when clicked. | ||||||
* @param IContextSource $context | ||||||
* @param array &$pageInfo | ||||||
*/ | ||||||
public function onInfoAction( $context, &$pageInfo ) { | ||||||
$mA = new MatomoAnalyticsWiki( $context->getConfig()->get( MainConfigNames::DBname ) ); | ||||||
|
||||||
$context->getOutput()->addModules( [ 'ext.matomoanalytics.charts', 'ext.matomoanalytics.infochart' ] ); | ||||||
$context->getOutput()->addModuleStyles( [ 'ext.matomoanalytics.infopage' ] ); | ||||||
|
||||||
$title = $context->getTitle(); | ||||||
$url = $title->getFullURL(); | ||||||
$data = $mA->getPageViews( $url ); | ||||||
$total = array_sum( $data ); | ||||||
$data = $mA->getPageViews( $url, 'day' ); | ||||||
$total = array_sum( array_filter( $data, 'is_numeric' ) ); | ||||||
|
||||||
$pageInfo['header-basic'][] = [ | ||||||
$context->msg( 'matomoanalytics-labels-pastmonth' ), | ||||||
$context->msg( 'matomoanalytics-count' )->numParams( $total )->parse() | ||||||
]; | ||||||
|
||||||
$pageInfo['header-basic'][] = [ | ||||||
$context->msg( 'matomoanalytics-labels-rawdata' ), | ||||||
$context->msg( 'matomoanalytics-count' )->rawParams( json_encode( $data ) )->parse() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
]; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Add Chart.js plugin for displaying "No data to display" if no valid data | ||
Chart.register({ | ||
id: 'noData', | ||
afterDraw: function (chart) { | ||
var datasets = chart.data.datasets; | ||
var hasData = datasets.some(dataset => | ||
dataset.data.length > 0 && | ||
dataset.data.some(value => value !== null && value !== undefined && value !== 0 && !Number.isNaN(value)) | ||
); | ||
|
||
if (!hasData) { | ||
var ctx = chart.ctx; | ||
var width = chart.width; | ||
var height = chart.height; | ||
|
||
ctx.save(); | ||
ctx.clearRect(0, 0, width, height); | ||
ctx.textAlign = 'center'; | ||
ctx.textBaseline = 'middle'; | ||
ctx.font = '16px Arial'; | ||
ctx.fillStyle = 'gray'; | ||
ctx.fillText('No data to display', width / 2, height / 2); | ||
ctx.restore(); | ||
} | ||
}, | ||
}); | ||
|
||
// Function to parse JSON data from the second <td> tag and create a chart | ||
function parseJSONAndCreateChart() { | ||
const trElement = document.getElementById("mw-matomoanalytics-labels-rawdata"); | ||
if (!trElement) return; | ||
|
||
// Select the second <td> element within the | ||
const tdElements = trElement.querySelectorAll("td"); | ||
if (tdElements.length < 2) return; | ||
const jsonData = JSON.parse(tdElements[1].textContent.trim()); | ||
|
||
const labels = Object.keys(jsonData); | ||
const data = Object.values(jsonData).map(value => value === "-" ? null : Number(value)); | ||
|
||
// Create a popup window with Codex for the chart | ||
const popup = new OO.ui.WindowManager(); | ||
document.body.appendChild(popup.$element[0]); | ||
const chartWindow = new OO.ui.MessageDialog(); | ||
popup.addWindows([chartWindow]); | ||
|
||
// Open the window and directly render the chart on success | ||
popup.openWindow(chartWindow, { | ||
title: 'Data Chart', | ||
message: $('<canvas id="matomoanalytics-chart-info"></canvas>'), | ||
size: 'large' | ||
}); | ||
|
||
const canvas = document.getElementById("matomoanalytics-chart-info"); | ||
Comment on lines
+47
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we create the canvas element and put it in a variable, and then pass that variable to the |
||
if (canvas) { | ||
new Chart(canvas, { | ||
type: 'line', | ||
data: { | ||
labels: labels, | ||
datasets: [{ | ||
label: 'Data over Time', | ||
data: data, | ||
borderWidth: 1, | ||
borderColor: 'blue', | ||
backgroundColor: 'lightblue', | ||
fill: true | ||
}] | ||
}, | ||
options: { | ||
plugins: { | ||
legend: { | ||
display: false | ||
} | ||
}, | ||
scales: { | ||
y: { | ||
beginAtZero: true | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
// Add a clickable link to trigger the chart display | ||
document.querySelectorAll('#mw-matomoanalytics-labels-pastmonth').forEach(element => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Multiple elements for an ID is a bit of an HTML smell... |
||
element.style.cursor = 'pointer'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be in the .less file? |
||
element.addEventListener('click', parseJSONAndCreateChart); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#mw-matomoanalytics-labels-rawdata { | ||
display: none; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can get away without a system message (since it won't be displayed anyway)