Create graphs based on html data attribute

This commit is contained in:
George Lacey 2022-04-11 11:30:15 +01:00
parent 3031454682
commit 29d5b51752
4 changed files with 31 additions and 91 deletions

View File

@ -1,19 +1,19 @@
function draw_time_series_graph(chartID, repo, dateLabels, sizeUnits) { function draw_time_series_graph(canvas, data) {
let datasets = [{ let datasets = [{
label: repo.label, label: data.label,
data: repo.size, data: data.size,
fill: false, fill: false,
borderColor: 'rgb(7, 59, 76)' borderColor: 'rgb(7, 59, 76)'
}] }]
const data = { const graphData = {
labels: dateLabels, labels: data.dates,
datasets: datasets datasets: datasets
}; };
const config = { const config = {
type: 'line', type: 'line',
data, data: graphData,
options: { options: {
plugins: { plugins: {
tooltip: { tooltip: {
@ -21,7 +21,7 @@ function draw_time_series_graph(chartID, repo, dateLabels, sizeUnits) {
label: function (context) { label: function (context) {
const yValue = context.parsed.y const yValue = context.parsed.y
if (yValue !== null) { if (yValue !== null) {
return `${yValue} ${sizeUnits}` return `${yValue} ${data.units}`
} else { } else {
return "" return ""
} }
@ -44,7 +44,7 @@ function draw_time_series_graph(chartID, repo, dateLabels, sizeUnits) {
}, },
ticks: { ticks: {
callback: function (value, index, values) { callback: function (value, index, values) {
return `${value} ${sizeUnits}` return `${value} ${data.units}`
} }
} }
} }
@ -52,69 +52,8 @@ function draw_time_series_graph(chartID, repo, dateLabels, sizeUnits) {
} }
} }
var newChart = new Chart( const newGraph = new Chart(
document.getElementById(chartID), canvas,
config
);
}
function draw_time_graph(chartID, repos, dateLabels, sizeUnits) {
let datasets = []
repos.forEach(function (repo) {
datasets.push({
label: repo.label,
data: repo.size,
fill: false,
borderColor: 'rgb(7, 59, 76)'
});
})
const data = {
labels: dateLabels,
datasets: datasets
};
const config = {
type: 'line',
data,
options: {
plugins: {
tooltip: {
callbacks: {
label: function (context) {
const yValue = context.parsed.y
if (yValue !== null) {
return `${yValue} ${sizeUnits}`
} else {
return ""
}
}
}
}
},
scales: {
y: {
min: 0,
title: {
display: true,
text: "Compressed Size",
font: {
size: 18
}
},
ticks: {
callback: function (value, index, values) {
return `${value} ${sizeUnits}`
}
}
}
}
}
}
var newChart = new Chart(
document.getElementById(chartID),
config config
); );
} }

View File

@ -16,7 +16,17 @@ function stringRequests() {
$('[data-json-string-request]').each(function (index, element) { $('[data-json-string-request]').each(function (index, element) {
$.getJSON($(this).attr("data-json-string-request"), function (data) { $.getJSON($(this).attr("data-json-string-request"), function (data) {
$(element).html(data['data']); $(element).html(data['data']);
}) });
});
}
function graphRequests() {
$('[data-json-graph-request]').each(function (index, element) {
$.getJSON($(this).attr("data-json-graph-request"), function (data) {
let newGraph = $('<canvas/>').width(400).height(200);
draw_time_series_graph(newGraph, data)
$(element).html(newGraph);
});
}); });
} }
@ -25,23 +35,14 @@ function colourRepos(repo_list) {
repo_list.labels.forEach(function (repo_label) { repo_list.labels.forEach(function (repo_label) {
$.getJSON(`/repo/${repo_label}.json`, function (repo_json) { $.getJSON(`/repo/${repo_label}.json`, function (repo_json) {
colourRepo(repo_json, repo_label, container); colourRepo(repo_json, repo_label, container);
}) });
});
}
function createGraphs(repo_list) {
repo_list.labels.forEach(function (repo_label) {
$.getJSON(`/repo/${repo_label}/monthly-size.json`, function (repo_size_json) {
draw_time_series_graph(`repo-${repo_label}-size-graph`, repo_size_json.repo,
repo_size_json.dates, repo_size_json.units);
})
}); });
} }
window.addEventListener("DOMContentLoaded", function () { window.addEventListener("DOMContentLoaded", function () {
setTimeout(stringRequests, 0); setTimeout(stringRequests, 0);
setTimeout(graphRequests, 0);
$.getJSON(`/repo-list.json`, function (repo_list) { $.getJSON(`/repo-list.json`, function (repo_list) {
setTimeout(createGraphs.bind(null, repo_list), 0);
setTimeout(colourRepos.bind(null, repo_list), 0); setTimeout(colourRepos.bind(null, repo_list), 0);
}); });
}, false); }, false);

View File

@ -58,8 +58,12 @@
</div> </div>
</dd> </dd>
</dl> </dl>
<canvas id="repo-{{ repo.label }}-size-graph" width="400" height="200"></canvas> <div id="repo-{{ repo.label }}-size-graph"
</dl> class="d-flex justify-content-center"
data-json-graph-request="/repo/{{ repo.label }}/monthly-size.json">
<div class="spinner-border" role="status">
</div>
</div>
</div> </div>
{% endfor %} {% endfor %}
{% else %} {% else %}

View File

@ -35,14 +35,10 @@ def repo_monthly_size_json(request, repo_label, months_ago: int = 12):
max_unit = get_units([repo]) max_unit = get_units([repo])
repo_dict = {"id": repo.id,
"label": repo.label.label,
"size": repo.size_on_months(max_unit, months_ago)}
response_dict = { response_dict = {
"dates": date_labels, "dates": date_labels,
"repo": repo_dict, "units": max_unit,
"units": max_unit "size": repo.size_on_months(max_unit, months_ago)
} }
return JsonResponse(response_dict) return JsonResponse(response_dict)