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

chart visualisation implementation #45

Open
wants to merge 1 commit into
base: v1/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public void Filter(List<PackageManifest> manifests)
"/App_Plugins/UmbracoCommunitySustainability/js/sustainability.resource.js",
"/App_Plugins/UmbracoCommunitySustainability/js/sustainability-stats.controller.js",
"/App_Plugins/UmbracoCommunitySustainability/js/sustainability-overview.controller.js",
"/App_Plugins/UmbracoCommunitySustainability/js/sustainability-content-app.controller.js"
"/App_Plugins/UmbracoCommunitySustainability/js/sustainability-content-app.controller.js",
"/App_Plugins/UmbracoCommunitySustainability/js/chart.min.js"

},
Stylesheets = new string[]
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,118 +1,108 @@
<div ng-controller="Umbraco.Sustainability.Stats.Controller as vm">

<umb-editor-view footer="false">

<umb-editor-header name="vm.name"
name-locked="true"
hide-alias="true"
hide-description="true"
hide-icon="true">
</umb-editor-header>

<umb-editor-container>
<uui-box ng-if="!vm.loading && vm.sustainabilityData.length != 0">
<uui-table>

<uui-table-head>
<uui-table-head-cell></uui-table-head-cell>

<uui-table-head-cell>
<button style="display: contents; font-weight: bold; padding: var(--uui-size-4) var(--uui-size-5);"
ng-click="vm.sortingHandler('RequestDate')">
Last Run Date
<uui-symbol-sort ng-attr-active="{{vm.sortingColumn === 'RequestDate' || undefined}}"
ng-attr-descending="{{vm.sortingDesc || undefined}}">
</uui-symbol-sort>
</button>
</uui-table-head-cell>

<uui-table-head-cell>
<button style="display: contents; font-weight: bold; padding: var(--uui-size-4) var(--uui-size-5);"
ng-click="vm.sortingHandler('CarbonRating')">
Carbon Rating
<uui-symbol-sort ng-attr-active="{{vm.sortingColumn === 'CarbonRating' || undefined}}"
ng-attr-descending="{{vm.sortingDesc || undefined}}">
</uui-symbol-sort>
</button>
</uui-table-head-cell>
<uui-table-head-cell>Scripts</uui-table-head-cell>
<uui-table-head-cell>Images</uui-table-head-cell>
<uui-table-head-cell>Styles</uui-table-head-cell>
<uui-table-head-cell>Other</uui-table-head-cell>
<uui-table-head-cell style="text-align: right;">
<button style="display: contents; font-weight: bold; padding: var(--uui-size-4) var(--uui-size-5);"
ng-click="vm.sortingHandler('TotalSize')">
Page Size
<uui-symbol-sort ng-attr-active="{{vm.sortingColumn === 'TotalSize' || undefined}}"
ng-attr-descending="{{vm.sortingDesc || undefined}}">
</uui-symbol-sort>
</button>
</uui-table-head-cell>
<uui-table-head-cell style="text-align: right;">
<button style="display: contents; font-weight: bold; padding: var(--uui-size-4) var(--uui-size-5);"
ng-click="vm.sortingHandler('TotalEmissions')">
CO₂ per page view
<uui-symbol-sort ng-attr-active="{{vm.sortingColumn === 'TotalEmissions' || undefined}}"
ng-attr-descending="{{vm.sortingDesc || undefined}}">
</uui-symbol-sort>
</button>
</uui-table-head-cell>
</uui-table-head>

<uui-table-row ng-repeat="data in vm.sustainabilityData">
<uui-table-cell>
<a style="text-decoration: underline;" href="/umbraco/#/content/content/edit/{{data.NodeKey}}">{{data.NodeName}}</a>
</uui-table-cell>

<uui-table-cell>
{{data.RequestDate}}
</uui-table-cell>

<uui-table-cell>
<uui-tag style="font-size: 14px" color="{{vm.getTagColour(data.CarbonRating)}}">
{{data.CarbonRating}}
</uui-tag>
</uui-table-cell>

<uui-table-cell>
{{data.Scripts}}
</uui-table-cell>

<uui-table-cell>
{{data.Images}}
</uui-table-cell>

<uui-table-cell>
{{data.Styles}}
</uui-table-cell>

<uui-table-cell>
{{data.Other}}
</uui-table-cell>

<uui-table-cell style="text-align: right;">
{{(data.TotalSize / 1024).toFixed(2)}}KB
</uui-table-cell>

<uui-table-cell style="text-align: right;">
{{data.TotalEmissions.toFixed(4)}}g
</uui-table-cell>
</uui-table-row>
</uui-table>

</uui-box>

<div ng-if="!vm.loading" class="flex justify-center">
<umb-pagination page-number="vm.pageNumber"
total-pages="vm.totalPages"
on-change="vm.changePageNumber(pageNumber)">
</umb-pagination>
</div>

<umb-empty-state ng-if="vm.sustainabilityData.length == 0"
position="center">
<p>No data to show yet.</p>
</umb-empty-state>
</umb-editor-container>
</umb-editor-view>
</div>
<div ng-controller="Umbraco.Sustainability.Stats.Controller as vm">
<umb-editor-view footer="false">
<umb-editor-header name="vm.name"
name-locked="true"
hide-alias="true"
hide-description="true"
hide-icon="true">
</umb-editor-header>

<umb-editor-container>
<div style="display: flex; justify-content: right">
<umb-box>
<uui-tab-group>
<uui-tab label="Table" active=""
ng-click="vm.setViewMode('table')">
<uui-icon slot="icon" name="icon-info"></uui-icon>
</uui-tab>
<uui-tab label="Chart"
ng-click="vm.setViewMode('chart')">
<uui-icon slot="icon" name="icon-chart"></uui-icon>
</uui-tab>
</uui-tab-group>
</umb-box>
</div>

<uui-box ng-if="!vm.loading && vm.sustainabilityData.length != 0 && vm.viewMode === 'table'">
<uui-table>
<uui-table-head>
<uui-table-head-cell></uui-table-head-cell>
<uui-table-head-cell>
<button style="display: contents; font-weight: bold; padding: var(--uui-size-4) var(--uui-size-5);"
ng-click="vm.sortingHandler('RequestDate')">
Last Run Date
<uui-symbol-sort ng-attr-active="{{vm.sortingColumn === 'RequestDate' || undefined}}"
ng-attr-descending="{{vm.sortingDesc || undefined}}"></uui-symbol-sort>
</button>
</uui-table-head-cell>
<uui-table-head-cell>
<button style="display: contents; font-weight: bold; padding: var(--uui-size-4) var(--uui-size-5);"
ng-click="vm.sortingHandler('CarbonRating')">
Carbon Rating
<uui-symbol-sort ng-attr-active="{{vm.sortingColumn === 'CarbonRating' || undefined}}"
ng-attr-descending="{{vm.sortingDesc || undefined}}"></uui-symbol-sort>
</button>
</uui-table-head-cell>
<uui-table-head-cell>Scripts</uui-table-head-cell>
<uui-table-head-cell>Images</uui-table-head-cell>
<uui-table-head-cell>Styles</uui-table-head-cell>
<uui-table-head-cell>Other</uui-table-head-cell>
<uui-table-head-cell style="text-align: right;">
<button style="display: contents; font-weight: bold; padding: var(--uui-size-4) var(--uui-size-5);"
ng-click="vm.sortingHandler('TotalSize')">
Page Size
<uui-symbol-sort ng-attr-active="{{vm.sortingColumn === 'TotalSize' || undefined}}"
ng-attr-descending="{{vm.sortingDesc || undefined}}"></uui-symbol-sort>
</button>
</uui-table-head-cell>
<uui-table-head-cell style="text-align: right;">
<button style="display: contents; font-weight: bold; padding: var(--uui-size-4) var(--uui-size-5);"
ng-click="vm.sortingHandler('TotalEmissions')">
CO₂ per page view
<uui-symbol-sort ng-attr-active="{{vm.sortingColumn === 'TotalEmissions' || undefined}}"
ng-attr-descending="{{vm.sortingDesc || undefined}}"></uui-symbol-sort>
</button>
</uui-table-head-cell>
</uui-table-head>
<uui-table-row ng-repeat="data in vm.sustainabilityData">
<uui-table-cell>
<a style="text-decoration: underline;" href="/umbraco/#/content/content/edit/{{data.NodeKey}}">{{data.NodeName}}</a>
</uui-table-cell>
<uui-table-cell>{{data.RequestDate}}</uui-table-cell>
<uui-table-cell>
<uui-tag style="font-size: 14px" color="{{vm.getTagColour(data.CarbonRating)}}">
{{data.CarbonRating}}
</uui-tag>
</uui-table-cell>
<uui-table-cell>{{data.Scripts}}</uui-table-cell>
<uui-table-cell>{{data.Images}}</uui-table-cell>
<uui-table-cell>{{data.Styles}}</uui-table-cell>
<uui-table-cell>{{data.Other}}</uui-table-cell>
<uui-table-cell style="text-align: right;">{{(data.TotalSize / 1024).toFixed(2)}}KB</uui-table-cell>
<uui-table-cell style="text-align: right;">{{data.TotalEmissions.toFixed(4)}}g</uui-table-cell>
</uui-table-row>
</uui-table>
</uui-box>

<div ng-if="vm.viewMode === 'chart'" ng-init="initCharts()" style="display: flex; justify-content: space-between; align-items: flex-start;">
<div style="width: 48%; height: 400px; background-color: white; padding: 15px; border-radius: 8px; overflow: hidden;">
<canvas id="resourceBreakdown" width="400" height="400"></canvas>
</div>
<div style="width: 48%; height: 400px; background-color: white; padding: 15px; border-radius: 8px; overflow: hidden;">
<canvas id="co2Trend" width="400" height="400"></canvas>
</div>
</div>

<div ng-if="!vm.loading" class="flex justify-center">
<umb-pagination page-number="vm.pageNumber"
total-pages="vm.totalPages"
on-change="vm.changePageNumber(pageNumber)">
</umb-pagination>
</div>

<umb-empty-state ng-if="vm.sustainabilityData.length == 0" position="center">
<p>No data to show yet.</p>
</umb-empty-state>
</umb-editor-container>
</umb-editor-view>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@ angular.module('umbraco').controller('Umbraco.Sustainability.Stats.Controller',
vm.sustainabilityData = [];
vm.sortingDesc = false;
vm.sortingColumn = 'CarbonRating';
vm.resourceLabels = ['Scripts', 'Images', 'Styles', 'Other'];
vm.resourceData = [0, 0, 0, 0];
vm.co2TrendLabels = [];
vm.co2TrendData = [];
vm.viewMode = 'table';

vm.changePageNumber = changePageNumber;
vm.getTagColour = sustainabilityResource.getTagColour;
vm.sortingHandler = sortingHandler;
vm.setViewMode = setViewMode;

init();

$scope.$on('$routeChangeSuccess', function () {
init();
});

function init() {
vm.loading = true;
sustainabilityResource.getOverviewData(vm.pageNumber, vm.pageSize, vm.sortingColumn, vm.sortingDesc).then(function (data) {
vm.sustainabilityData = data.items;

Expand All @@ -29,36 +40,113 @@ angular.module('umbraco').controller('Umbraco.Sustainability.Stats.Controller',
vm.totalItems = data.totalItems;
vm.totalPages = data.totalPages;

angular.forEach(vm.sustainabilityData, function (item) {
item.RequestDate = moment(item.RequestDate).format(
"MMM Do YYYY HH:mm:ss"
);
vm.co2TrendLabels = [];
vm.co2TrendData = [];
vm.resourceData = [0, 0, 0, 0];

angular.forEach(vm.sustainabilityData, function (item) {
item.RequestDate = moment(item.RequestDate).format("MMM Do YYYY HH:mm:ss");
item.PageData = JSON.parse(item.PageData);
item.Scripts = item.PageData.ResourceGroups.find(x => x.Name === 'Scripts').Resources.length;
item.Images = item.PageData.ResourceGroups.find(x => x.Name === 'Images').Resources.length;
item.Styles = item.PageData.ResourceGroups.find(x => x.Name === 'Styles').Resources.length;
item.Other = item.PageData.ResourceGroups.find(x => x.Name === 'Other').Resources.length;

updateResourceCounts(item);
vm.co2TrendLabels.push(item.RequestDate);
vm.co2TrendData.push(item.TotalEmissions);
});

prepareChartData();
vm.loading = false;
});
}

function changePageNumber(pageNumber) {
vm.pageNumber = pageNumber;
init();
function updateResourceCounts(item) {
item.Scripts = getResourceCount(item, 'Scripts');
item.Images = getResourceCount(item, 'Images');
item.Styles = getResourceCount(item, 'Styles');
item.Other = getResourceCount(item, 'Other');
}

function getResourceCount(item, resourceName) {
const resourceGroup = item.PageData.ResourceGroups.find(x => x.Name === resourceName);
return resourceGroup ? resourceGroup.Resources.length : 0;
}

function prepareChartData() {
vm.sustainabilityData.forEach(function (item) {
vm.resourceData[0] += item.Scripts;
vm.resourceData[1] += item.Images;
vm.resourceData[2] += item.Styles;
vm.resourceData[3] += item.Other;
});
}

function getTagColour(carbonRating) {
if (carbonRating == "E" || carbonRating == "F") {
return "danger";
function setViewMode(mode) {
vm.viewMode = mode;
if (mode === 'chart') {
setTimeout(initCharts, 100);
}
else if (carbonRating == "D") {
return "warning";
}

function initCharts() {
if (typeof Chart !== 'undefined') {
let doughnutCtx = document.getElementById('resourceBreakdown').getContext('2d');
new Chart(doughnutCtx, {
type: 'doughnut',
data: {
labels: vm.resourceLabels,
datasets: [{
label: 'Resource Breakdown',
data: vm.resourceData,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
layout: { padding: 10 }
}
});

let barCtx = document.getElementById('co2Trend').getContext('2d');
new Chart(barCtx, {
type: 'bar',
data: {
labels: vm.co2TrendLabels,
datasets: [{
label: 'CO₂ per page view (g)',
data: vm.co2TrendData,
backgroundColor: 'rgba(54, 162, 235, 0.2)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1,
fill: true
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: { beginAtZero: true },
y: { beginAtZero: true }
},
layout: { padding: 10 }
}
});
}
else return "positive";
}

function changePageNumber(pageNumber) {
vm.pageNumber = pageNumber;
init();
}

function sortingHandler(columnName) {
Expand All @@ -68,3 +156,4 @@ angular.module('umbraco').controller('Umbraco.Sustainability.Stats.Controller',
}
}
]);