Skip to content

A platform for community discussion. Free, open, simple.

UX: chart legend styling (#41694)

chapoi747e23b5db6f
diff --git a/frontend/discourse/admin/components/admin-report-stacked-chart.gjs b/frontend/discourse/admin/components/admin-report-stacked-chart.gjs
index 9253245b6da..78d424bd82f 100644
--- a/frontend/discourse/admin/components/admin-report-stacked-chart.gjs
+++ b/frontend/discourse/admin/components/admin-report-stacked-chart.gjs
@@ -1,7 +1,9 @@
import Component from "@glimmer/component";
import Report from "discourse/admin/models/report";
+import { buildLegendIcon, dimColor } from "discourse/lib/chart-legend-icon";
import { number } from "discourse/lib/formatter";
import { makeArray } from "discourse/lib/helpers";
+import { remToPx } from "discourse/lib/rem-to-px";
import I18n, { i18n } from "discourse-i18n";
import Chart from "./chart";
@@ -141,22 +143,18 @@ export default class AdminReportStackedChart extends Component {
},
labels: {
usePointStyle: true,
- pointStyle: "rectRounded",
- boxWidth: 10,
- boxHeight: 10,
+ padding: remToPx(1),
+ font: { size: remToPx(0.75) },
generateLabels: (chart) => {
const textColor = getCSSColor("--primary-high");
return chart.data.datasets.map((dataset, i) => {
const isVisible = chart.isDatasetVisible(i);
return {
text: dataset.label,
- fontColor: textColor,
- fillStyle: isVisible ? dataset._baseColor : "transparent",
- strokeStyle: dataset._baseColor,
- lineWidth: 2,
+ fontColor: isVisible ? textColor : dimColor(textColor),
hidden: false,
datasetIndex: i,
- pointStyle: "rectRounded",
+ pointStyle: buildLegendIcon(dataset._baseColor, isVisible),
};
});
},
diff --git a/frontend/discourse/app/lib/chart-legend-icon.js b/frontend/discourse/app/lib/chart-legend-icon.js
new file mode 100644
index 00000000000..65b0573be14
--- /dev/null
+++ b/frontend/discourse/app/lib/chart-legend-icon.js
@@ -0,0 +1,58 @@
+import { remToPx } from "discourse/lib/rem-to-px";
+
+export const INACTIVE_ALPHA = 0.6;
+
+export function buildLegendIcon(color, isVisible, size = remToPx(1)) {
+ const canvas = document.createElement("canvas");
+ canvas.width = size;
+ canvas.height = size;
+ const ctx = canvas.getContext("2d");
+
+ const borderWidth = 2;
+ const half = borderWidth / 2;
+ ctx.globalAlpha = isVisible ? 1 : INACTIVE_ALPHA;
+ ctx.strokeStyle = color;
+ ctx.lineWidth = borderWidth;
+ ctx.beginPath();
+ ctx.roundRect(half, half, size - borderWidth, size - borderWidth, 4);
+ ctx.stroke();
+
+ if (isVisible) {
+ const inset = 4;
+ ctx.fillStyle = color;
+ ctx.beginPath();
+ ctx.roundRect(inset, inset, size - inset * 2, size - inset * 2, 2);
+ ctx.fill();
+ }
+
+ return canvas;
+}
+
+export function dimColor(color, alpha = INACTIVE_ALPHA) {
+ const value = color?.trim();
+ if (!value) {
+ return color;
+ }
+
+ if (value.startsWith("#")) {
+ let hex = value.slice(1);
+ if (hex.length === 3) {
+ hex = hex
+ .split("")
+ .map((char) => char + char)
+ .join("");
+ }
+ const r = parseInt(hex.slice(0, 2), 16);
+ const g = parseInt(hex.slice(2, 4), 16);
+ const b = parseInt(hex.slice(4, 6), 16);
+ return `rgba(${r}, ${g}, ${b}, ${alpha})`;
+ }
+
+ const rgbMatch = value.match(/^rgba?\(([^)]+)\)$/);
+ if (rgbMatch) {
+ const [r, g, b] = rgbMatch[1].split(",").map((part) => part.trim());
+ return `rgba(${r}, ${g}, ${b}, ${alpha})`;
+ }
+
+ return value;
+}
diff --git a/frontend/discourse/app/lib/rem-to-px.js b/frontend/discourse/app/lib/rem-to-px.js
new file mode 100644
index 00000000000..b8b6856b59d
--- /dev/null
+++ b/frontend/discourse/app/lib/rem-to-px.js
@@ -0,0 +1,6 @@
+export function remToPx(rem) {
+ const rootFontSize = parseFloat(
+ getComputedStyle(document.documentElement).fontSize
+ );
+ return rem * rootFontSize;
+}
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/doughnut-chart.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/doughnut-chart.gjs
index a3dd9138f20..b863b1bb96e 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/doughnut-chart.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/doughnut-chart.gjs
@@ -1,5 +1,7 @@
import Component from "@glimmer/component";
import Chart from "discourse/admin/components/chart";
+import { buildLegendIcon, dimColor } from "discourse/lib/chart-legend-icon";
+import { remToPx } from "discourse/lib/rem-to-px";
export default class DoughnutChart extends Component {
get config() {
@@ -25,6 +27,31 @@ export default class DoughnutChart extends Component {
legend: {
display: this.args.displayLegend || false,
position: "bottom",
+ labels: {
+ usePointStyle: true,
+ padding: remToPx(1),
+ font: { size: remToPx(0.75) },
+ generateLabels: (chart) => {
+ const textColor = getComputedStyle(document.documentElement)
+ .getPropertyValue("--primary-high")
+ .trim();
+ const backgroundColor =
+ chart.data.datasets[0]?.backgroundColor || [];
+ return chart.data.labels.map((label, index) => {
+ const isVisible = chart.getDataVisibility(index);
+ return {
+ text: label,
+ fontColor: isVisible ? textColor : dimColor(textColor),
+ hidden: false,
+ index,
+ pointStyle: buildLegendIcon(
+ backgroundColor[index],
+ isVisible
+ ),
+ };
+ });
+ },
+ },
},
},
},
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/components/data-explorer-chart.gjs b/plugins/discourse-data-explorer/assets/javascripts/discourse/components/data-explorer-chart.gjs
index d2cfc39b117..1b8f3726e98 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/components/data-explorer-chart.gjs
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/components/data-explorer-chart.gjs
@@ -2,8 +2,10 @@ import Component from "@glimmer/component";
import { action } from "@ember/object";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import didUpdate from "@ember/render-modifiers/modifiers/did-update";
+import { buildLegendIcon, dimColor } from "discourse/lib/chart-legend-icon";
import { bind } from "discourse/lib/decorators";
import loadChartJS from "discourse/lib/load-chart-js";
+import { remToPx } from "discourse/lib/rem-to-px";
import I18n, { i18n } from "discourse-i18n";
import { formatChartDateLabel, SERIES_COLORS } from "../lib/chart-helpers";
import themeColor from "../lib/themeColor";
@@ -178,7 +180,26 @@ export default class DataExplorerChart extends Component {
responsive: true,
maintainAspectRatio: false,
plugins: {
- legend: { display: true, position: "bottom" },
+ legend: {
+ display: true,
+ position: "bottom",
+ labels: {
+ usePointStyle: true,
+ padding: remToPx(1),
+ font: { size: remToPx(0.75) },
+ generateLabels: (chart) =>
+ chart.data.datasets.map((dataset, i) => {
+ const isVisible = chart.isDatasetVisible(i);
+ return {
+ text: dataset.label,
+ fontColor: isVisible ? labelColor : dimColor(labelColor),
+ hidden: false,
+ datasetIndex: i,
+ pointStyle: buildLegendIcon(dataset.borderColor, isVisible),
+ };
+ }),
+ },
+ },
tooltip: this._multiSeriesTooltipOptions(stacked),
},
scales,
diff --git a/plugins/poll/assets/javascripts/discourse/components/poll-results-pie.gjs b/plugins/poll/assets/javascripts/discourse/components/poll-results-pie.gjs
index 183408f3eb2..da5143b60a8 100644
--- a/plugins/poll/assets/javascripts/discourse/components/poll-results-pie.gjs
+++ b/plugins/poll/assets/javascripts/discourse/components/poll-results-pie.gjs
@@ -3,6 +3,7 @@ import { action } from "@ember/object";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import { trustHTML } from "@ember/template";
import { modifier } from "ember-modifier";
+import { buildLegendIcon } from "discourse/lib/chart-legend-icon";
import loadChartJS from "discourse/lib/load-chart-js";
import {
applyHtmlDecorators,
@@ -26,28 +27,24 @@ export default class PollResultsPieComponent extends Component {
const items = chart.options.plugins.legend.labels.generateLabels(chart);
items.forEach((item) => {
+ const isVisible = chart.getDataVisibility(item.index);
+
const li = document.createElement("li");
li.classList.add("legend");
+ li.style.opacity = isVisible ? 1.0 : 0.4;
li.onclick = () => {
chart.toggleDataVisibility(item.index);
chart.update();
};
- const boxSpan = document.createElement("span");
- boxSpan.classList.add("swatch");
- boxSpan.style.background = item.fillStyle;
+ const icon = buildLegendIcon(item.fillStyle, isVisible);
+ icon.classList.add("swatch");
const textContainer = document.createElement("span");
textContainer.style.color = item.fontColor;
textContainer.innerHTML = item.text;
- if (!chart.getDataVisibility(item.index)) {
- li.style.opacity = 0.2;
- } else {
- li.style.opacity = 1.0;
- }
-
- li.appendChild(boxSpan);
+ li.appendChild(icon);
li.appendChild(textContainer);
ul.appendChild(li);