Skip to content
Merged
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
23 changes: 9 additions & 14 deletions SAPSec.Web/AssetSrc/js/chart-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const datasetColorKeys = ['school', 'similarSchools', 'localAuthority', 'england'];
const CHART_CONFIG = {
defaults: {
axisStep: 20,
axisSuffix: '%',
maxDevicePixelRatio: 2,
resizeDebounceMs: 100,
Expand Down Expand Up @@ -151,7 +150,7 @@
};
}

function buildChartOptions(type, gdsStyles, axisStep, axisSuffix, axisMax, showLegend, showDataLabels, showXGrid, barLabelAlign) {
function buildChartOptions(type, gdsStyles, axisSuffix, showLegend, showDataLabels, showXGrid, barLabelAlign) {
const common = {
responsive: true,
maintainAspectRatio: false,
Expand All @@ -163,7 +162,6 @@
size: gdsStyles.fontSize
};

const stepSize = axisStep;
const legendOptions = {
display: showLegend,
position: CHART_CONFIG.legend.position,
Expand All @@ -188,7 +186,6 @@
scales: {
y: {
beginAtZero: true,
suggestedMax: axisMax ?? undefined,
grace: CHART_CONFIG.line.axis.grace,
grid: {
display: true,
Expand All @@ -204,7 +201,6 @@
ticks: {
color: gdsStyles.text,
font: fonts,
stepSize: stepSize,
callback: (value) => `${value}${axisSuffix}`
}
},
Expand Down Expand Up @@ -259,7 +255,6 @@
scales: {
x: {
beginAtZero: true,
max: axisMax ?? undefined,
grid: {
display: true,
drawBorder: false,
Expand All @@ -274,7 +269,6 @@
ticks: {
color: gdsStyles.text,
font: fonts,
stepSize: stepSize,
callback: (value) => `${value}${axisSuffix}`
}
},
Expand Down Expand Up @@ -443,12 +437,6 @@
const showLegend = canvas.dataset.showLegend === "true";
const showDataLabels = canvas.dataset.showDatalabels !== "false";
const showXGrid = canvas.dataset.showXGrid === "true";
const axisStep = canvas.dataset.axisStep
? parseInt(canvas.dataset.axisStep, 10)
: CHART_CONFIG.defaults.axisStep;
const axisMax = canvas.dataset.axisMax
? parseFloat(canvas.dataset.axisMax)
: null;
const axisSuffix = canvas.dataset.axisSuffix !== undefined
? canvas.dataset.axisSuffix
: CHART_CONFIG.defaults.axisSuffix;
Expand Down Expand Up @@ -484,7 +472,14 @@
barPercentage
})
},
options: buildChartOptions(type, gdsStyles, axisStep, axisSuffix, axisMax, showLegend, showDataLabels, showXGrid, barLabelAlign),
options: buildChartOptions(
type,
gdsStyles,
axisSuffix,
showLegend,
showDataLabels,
showXGrid,
barLabelAlign),
plugins: [
...(showDataLabels ? [ChartDataLabels] : []),
noDataBarLabelsPlugin
Expand Down
50 changes: 2 additions & 48 deletions SAPSec.Web/AssetSrc/js/data-view-switcher.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
(function () {
function hasOwnProperty(obj, key) {
return Object.prototype.hasOwnProperty.call(obj, key);
}

function updateTableRow(cells, values) {
cells.forEach(function (cell, index) {
if (cell) {
Expand Down Expand Up @@ -55,33 +51,6 @@
tableBody.appendChild(tr);
});
}

function updateChartAxis(chart, axisSettings) {
if (!chart || !chart.options || !chart.options.scales || !axisSettings) {
return;
}

var isBarChart = chart.config && chart.config.type === "bar";
var axisKey = isBarChart ? "x" : "y";
var axis = chart.options.scales[axisKey];
if (!axis || !axis.ticks) {
return;
}

if (typeof axisSettings.max === "number") {
if (isBarChart) {
axis.max = axisSettings.max;
} else {
axis.max = undefined;
axis.suggestedMax = axisSettings.max;
}
}

if (typeof axisSettings.step === "number") {
axis.ticks.stepSize = axisSettings.step;
}
}

function readJsonAttribute(element, attributeName) {
var raw = element.getAttribute(attributeName);
if (!raw) {
Expand All @@ -96,15 +65,6 @@
}
}

function readAxisSettings(select, responseKey) {
var config = readJsonAttribute(select, "data-axis-config");
if (!config || !responseKey) {
return null;
}

return config[responseKey] || null;
}

function buildTableCellMap(seriesKeys, cellAttribute, cellPrefixes) {
var suffixes = ["prev2", "prev", "current", "avg"];
var map = {};
Expand Down Expand Up @@ -132,15 +92,14 @@
return endpoint + separator + queryKey + "=" + encodeURIComponent(selectedValue);
}

function applyDataView(data, config, axisSettings) {
function applyDataView(data, config) {
if (!data) {
return;
}

var barChart = window.Chart && config.barChartCanvas ? window.Chart.getChart(config.barChartCanvas) : null;
if (barChart && barChart.data && barChart.data.datasets && barChart.data.datasets[0]) {
barChart.data.datasets[0].data = data.bar || [];
updateChartAxis(barChart, axisSettings);
barChart.update();
}

Expand All @@ -154,7 +113,6 @@
config.seriesKeys.forEach(function (seriesKey, index) {
lineChart.data.datasets[index].data = data.line && data.line[seriesKey] ? data.line[seriesKey] : [];
});
updateChartAxis(lineChart, axisSettings);
lineChart.update();
}
}
Expand All @@ -177,7 +135,6 @@
var barChartId = select.getAttribute("data-bar-chart-id");
var lineChartId = select.getAttribute("data-line-chart-id");
var queryKey = select.getAttribute("data-query-key");
var axisConfigKey = select.getAttribute("data-axis-config-key") || queryKey;
var cellAttribute = select.getAttribute("data-cell-attribute") || "data-view-cell";
var seriesKeys = readJsonAttribute(select, "data-series-keys") || [];
var cellPrefixes = readJsonAttribute(select, "data-cell-prefixes") || {};
Expand Down Expand Up @@ -226,10 +183,7 @@

applyDataView(
data,
config,
readAxisSettings(
select,
hasOwnProperty(data, axisConfigKey) ? data[axisConfigKey] : selectedValue));
config);
})
.catch(function (error) {
console.error("Failed to load view data.", error);
Expand Down
Loading
Loading