Add color support for charts

This commit is contained in:
Jordan Wages 2025-07-18 23:03:04 -05:00
commit 44ee039ca6
3 changed files with 41 additions and 17 deletions

View file

@ -27,6 +27,13 @@
FROM logs FROM logs
GROUP BY cache_status GROUP BY cache_status
ORDER BY value DESC ORDER BY value DESC
colors:
- "#3273dc"
- "#23d160"
- "#ffdd57"
- "#ff3860"
- "#7957d5"
- "#363636"
- name: domain_traffic - name: domain_traffic
label: Top Domains label: Top Domains
@ -87,7 +94,7 @@
- name: status_distribution - name: status_distribution
label: HTTP Statuses label: HTTP Statuses
chart: stackedBar chart: pie
query: | query: |
SELECT CASE SELECT CASE
WHEN status BETWEEN 200 AND 299 THEN '2xx' WHEN status BETWEEN 200 AND 299 THEN '2xx'
@ -99,3 +106,8 @@
FROM logs FROM logs
GROUP BY bucket GROUP BY bucket
ORDER BY bucket ORDER BY bucket
colors:
- "#48c78e"
- "#209cee"
- "#ffdd57"
- "#f14668"

View file

@ -108,14 +108,17 @@ def _generate_interval(interval: str, domain: Optional[str] = None) -> None:
data = [dict(zip(headers, row)) for row in rows] data = [dict(zip(headers, row)) for row in rows]
json_path = out_dir / f"{name}.json" json_path = out_dir / f"{name}.json"
_save_json(json_path, data) _save_json(json_path, data)
report_list.append( entry = {
{ "name": name,
"name": name, "label": definition.get("label", name.title()),
"label": definition.get("label", name.title()), "chart": definition.get("chart", "line"),
"chart": definition.get("chart", "line"), "json": f"{name}.json",
"json": f"{name}.json", }
} if "color" in definition:
) entry["color"] = definition["color"]
if "colors" in definition:
entry["colors"] = definition["colors"]
report_list.append(entry)
_save_json(out_dir / "reports.json", report_list) _save_json(out_dir / "reports.json", report_list)
_render_html(interval, report_list, out_dir / "index.html") _render_html(interval, report_list, out_dir / "index.html")

View file

@ -53,18 +53,27 @@
options.scales.x = { stacked: true }; options.scales.x = { stacked: true };
options.scales.y.stacked = true; options.scales.y.stacked = true;
} }
const dataset = {
label: rep.label,
data: values,
borderWidth: 1,
fill: rep.chart !== 'bar' && rep.chart !== 'stackedBar'
};
if (rep.colors) {
dataset.backgroundColor = rep.colors;
dataset.borderColor = rep.colors;
} else if (rep.color) {
dataset.backgroundColor = rep.color;
dataset.borderColor = rep.color;
} else {
dataset.backgroundColor = 'rgba(54, 162, 235, 0.5)';
dataset.borderColor = 'rgba(54, 162, 235, 1)';
}
new Chart(document.getElementById('chart-' + rep.name), { new Chart(document.getElementById('chart-' + rep.name), {
type: chartType, type: chartType,
data: { data: {
labels: labels, labels: labels,
datasets: [{ datasets: [dataset]
label: rep.label,
data: values,
backgroundColor: 'rgba(54, 162, 235, 0.5)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1,
fill: rep.chart !== 'bar' && rep.chart !== 'stackedBar',
}]
}, },
options: options options: options
}); });