A platform for community discussion. Free, open, simple.
FEATURE: allow pie chart colors to be overridden with CSS (#36495)
Kris2b08a3e745d4
diff --git a/plugins/poll/assets/javascripts/lib/chart-colors.js b/plugins/poll/assets/javascripts/lib/chart-colors.jsindex 1b526ff6f83..55aba01bef7 100644--- a/plugins/poll/assets/javascripts/lib/chart-colors.js+++ b/plugins/poll/assets/javascripts/lib/chart-colors.js@@ -1,4 +1,11 @@-export function getColors(count, palette) {+function getCSSColor(varName) {+ const value = getComputedStyle(document.documentElement)+ .getPropertyValue(varName)+ .trim();+ return value || null;+}++function generateGradientColors(count, palette) {palette = palette || "cool";let gradient;@@ -67,3 +74,21 @@ export function getColors(count, palette) {}return colors;}++export function getColors(count, palette) {+ const cssColors = [];+ for (let i = 1; i <= count; i++) {+ const color = getCSSColor(`--poll-pie-color-${i}`);+ if (color) {+ cssColors.push(color);+ }+ }++ if (cssColors.length >= count) {+ return cssColors.slice(0, count);+ }++ const remainingCount = count - cssColors.length;+ const gradientColors = generateGradientColors(remainingCount, palette);+ return [...cssColors, ...gradientColors];+}diff --git a/plugins/poll/assets/stylesheets/common/poll.scss b/plugins/poll/assets/stylesheets/common/poll.scssindex 63a9f08e1c2..1a551c59125 100644--- a/plugins/poll/assets/stylesheets/common/poll.scss+++ b/plugins/poll/assets/stylesheets/common/poll.scss@@ -1,5 +1,10 @@@use "lib/viewport";+:root {+ --poll-bar-color: var(--primary-medium);+ --poll-bar-color--chosen: var(--tertiary);+}+div.poll-outer {div.poll {margin: 1em 0;@@ -404,16 +409,16 @@ div.poll-outer {}.bar-back {- background: var(--primary-low);+ background: var(--poll-bar-background);}.bar {height: 0.75em;- background: var(--primary-medium);+ background: var(--poll-bar-color);}.chosen .bar {- background: var(--tertiary);+ background: var(--poll-bar-color--chosen);}}diff --git a/plugins/poll/test/javascripts/unit/chart-colors-test.js b/plugins/poll/test/javascripts/unit/chart-colors-test.jsnew file mode 100644index 00000000000..d84e43562a0--- /dev/null+++ b/plugins/poll/test/javascripts/unit/chart-colors-test.js@@ -0,0 +1,65 @@+import { module, test } from "qunit";+import { getColors } from "discourse/plugins/poll/lib/chart-colors";++module("Unit | Utility | chart-colors", function (hooks) {+ hooks.beforeEach(function () {+ this.originalGetComputedStyle = window.getComputedStyle;+ });++ hooks.afterEach(function () {+ window.getComputedStyle = this.originalGetComputedStyle;+ });++ test("returns gradient colors when no CSS variables defined", function (assert) {+ window.getComputedStyle = () => ({+ getPropertyValue: () => "",+ });++ const colors = getColors(3);++ assert.strictEqual(colors.length, 3, "returns 3 colors");+ assert.true(+ colors.every((c) => c.startsWith("rgb(")),+ "all colors are rgb format"+ );+ });++ test("returns CSS colors when all are defined", function (assert) {+ const cssColors = {+ "--poll-pie-color-1": "red",+ "--poll-pie-color-2": "blue",+ "--poll-pie-color-3": "green",+ };++ window.getComputedStyle = () => ({+ getPropertyValue: (varName) => cssColors[varName] || "",+ });++ const colors = getColors(3);++ assert.strictEqual(colors.length, 3, "returns 3 colors");+ assert.strictEqual(colors[0], "red", "first color is red");+ assert.strictEqual(colors[1], "blue", "second color is blue");+ assert.strictEqual(colors[2], "green", "third color is green");+ });++ test("mixes CSS and gradient colors when partially defined", function (assert) {+ const cssColors = {+ "--poll-pie-color-1": "hotpink",+ "--poll-pie-color-2": "cyan",+ };++ window.getComputedStyle = () => ({+ getPropertyValue: (varName) => cssColors[varName] || "",+ });++ const colors = getColors(5);++ assert.strictEqual(colors.length, 5, "returns 5 colors");+ assert.strictEqual(colors[0], "hotpink", "first color is from CSS");+ assert.strictEqual(colors[1], "cyan", "second color is from CSS");+ assert.true(colors[2].startsWith("rgb("), "third color is from gradient");+ assert.true(colors[3].startsWith("rgb("), "fourth color is from gradient");+ assert.true(colors[4].startsWith("rgb("), "fifth color is from gradient");+ });+});