Skip to content

Beautiful Open-Source Form Builder

Improve summary chart colors and readability (#1304)

Julien Nahumc80ec4a1cab2
diff --git a/client/components/open/forms/components/summary/BooleanSummary.vue b/client/components/open/forms/components/summary/BooleanSummary.vue
index 095bd8168..036291a56 100644
--- a/client/components/open/forms/components/summary/BooleanSummary.vue
+++ b/client/components/open/forms/components/summary/BooleanSummary.vue
@@ -12,19 +12,17 @@
<!-- Bar Chart View -->
<div v-else-if="!showPieChart">
<!-- Single stacked bar -->
- <div class="flex rounded-lg h-10 overflow-hidden ring-1 ring-neutral-200">
+ <div class="flex rounded-lg h-10 overflow-hidden gap-0.5 bg-white">
<div
- class="bg-blue-500 flex items-center justify-center text-sm font-semibold text-white transition-all duration-500 relative group overflow-hidden"
- :style="{ width: yesPercentage + '%' }"
+ class="flex items-center justify-center text-sm font-semibold text-white transition-all duration-500 relative group overflow-hidden"
+ :style="{ width: yesPercentage + '%', backgroundColor: chartColors[0] }"
>
- <div class="absolute inset-0 bg-white/10 opacity-0 group-hover:opacity-100 transition-opacity" />
<span v-if="yesPercentage >= 10">{{ yesPercentage }}%</span>
</div>
<div
- class="bg-neutral-100 flex items-center justify-center text-sm font-semibold text-neutral-600 transition-all duration-500 relative group overflow-hidden"
- :style="{ width: noPercentage + '%' }"
+ class="flex items-center justify-center text-sm font-semibold text-neutral-900 transition-all duration-500 relative group overflow-hidden"
+ :style="{ width: noPercentage + '%', backgroundColor: chartColors[1] }"
>
- <div class="absolute inset-0 bg-black/5 opacity-0 group-hover:opacity-100 transition-opacity" />
<span v-if="noPercentage >= 10">{{ noPercentage }}%</span>
</div>
</div>
@@ -32,42 +30,42 @@
<!-- Legend -->
<div class="flex justify-between mt-3 px-1">
<div class="flex items-center gap-2">
- <div class="w-3 h-3 rounded-full bg-blue-500" />
+ <div class="w-3 h-3 rounded-full" :style="{ backgroundColor: chartColors[0] }" aria-hidden="true" />
<div class="text-sm">
<span class="font-medium text-neutral-900">Yes</span>
- <span class="text-neutral-500 ml-1">({{ yesCount }})</span>
+ <span class="text-neutral-600 ml-1">{{ yesCount }} ({{ yesPercentage }}%)</span>
</div>
</div>
<div class="flex items-center gap-2">
<div class="text-sm text-right">
<span class="font-medium text-neutral-900">No</span>
- <span class="text-neutral-500 ml-1">({{ noCount }})</span>
+ <span class="text-neutral-600 ml-1">{{ noCount }} ({{ noPercentage }}%)</span>
</div>
- <div class="w-3 h-3 rounded-full bg-neutral-200" />
+ <div class="w-3 h-3 rounded-full" :style="{ backgroundColor: chartColors[1] }" aria-hidden="true" />
</div>
</div>
</div>
<!-- Pie Chart View -->
<div v-else class="flex flex-col sm:flex-row items-center justify-center gap-8 py-2">
- <div class="w-48 h-48 relative">
+ <div class="w-48 h-48 relative shrink-0" aria-hidden="true">
<Pie :data="chartData" :options="chartOptions" />
</div>
<!-- Legend -->
<div class="space-y-3">
<div class="flex items-center gap-3">
- <div class="w-3 h-3 rounded-full bg-blue-500 shadow-sm shadow-blue-200" />
+ <div class="w-3 h-3 rounded-full" :style="{ backgroundColor: chartColors[0] }" aria-hidden="true" />
<div class="flex flex-col">
<span class="text-sm font-medium text-neutral-900">Yes</span>
- <span class="text-xs text-neutral-500">{{ yesCount }} responses ({{ yesPercentage }}%)</span>
+ <span class="text-xs text-neutral-600">{{ yesCount }} responses ({{ yesPercentage }}%)</span>
</div>
</div>
<div class="flex items-center gap-3">
- <div class="w-3 h-3 rounded-full bg-neutral-200 shadow-sm" />
+ <div class="w-3 h-3 rounded-full" :style="{ backgroundColor: chartColors[1] }" aria-hidden="true" />
<div class="flex flex-col">
<span class="text-sm font-medium text-neutral-900">No</span>
- <span class="text-xs text-neutral-500">{{ noCount }} responses ({{ noPercentage }}%)</span>
+ <span class="text-xs text-neutral-600">{{ noCount }} responses ({{ noPercentage }}%)</span>
</div>
</div>
</div>
@@ -78,6 +76,7 @@
<script setup>
import { Pie } from 'vue-chartjs'
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js'
+import { getSummaryChartColor } from '~/lib/summary-chart-colors'
ChartJS.register(ArcElement, Tooltip, Legend)
@@ -86,7 +85,7 @@ const props = defineProps({
showPieChart: { type: Boolean, default: false },
})
-const chartColors = ['#51a2ff', '#D1D5DB'] // blue-400, neutral-300
+const chartColors = [getSummaryChartColor(0), getSummaryChartColor(1)]
const distribution = computed(() => props.field.data?.distribution || [])
@@ -100,11 +99,13 @@ const noPercentage = computed(() => noItem.value.percentage)
const hasData = computed(() => yesCount.value > 0 || noCount.value > 0)
const chartData = computed(() => ({
- labels: distribution.value.map(item => item.value),
+ labels: ['Yes', 'No'],
datasets: [{
data: [yesCount.value, noCount.value],
backgroundColor: chartColors,
- borderWidth: 0,
+ borderColor: '#FFFFFF',
+ borderWidth: 2,
+ hoverBorderColor: '#FFFFFF',
}]
}))
@@ -128,4 +129,3 @@ const chartOptions = {
}
}
</script>
-
diff --git a/client/components/open/forms/components/summary/DistributionSummary.vue b/client/components/open/forms/components/summary/DistributionSummary.vue
index ba6e20cfb..64c6ff161 100644
--- a/client/components/open/forms/components/summary/DistributionSummary.vue
+++ b/client/components/open/forms/components/summary/DistributionSummary.vue
@@ -18,9 +18,9 @@
class="group"
>
<div class="flex items-center justify-between text-sm mb-1">
- <span class="font-medium text-neutral-700 truncate max-w-[70%]">{{ item.value }}</span>
- <div class="flex items-center gap-3">
- <span class="text-neutral-500">{{ item.count }}</span>
+ <span class="font-medium text-neutral-700 break-words min-w-0 max-w-[70%]">{{ item.value }}</span>
+ <div class="flex items-center gap-3 shrink-0">
+ <span class="text-neutral-600"><span class="sr-only">Responses: </span>{{ item.count }}</span>
<span class="font-medium text-neutral-900 w-10 text-right">{{ item.percentage }}%</span>
</div>
</div>
@@ -28,39 +28,36 @@
<div class="h-2.5 bg-neutral-100 rounded-full overflow-hidden">
<div
class="h-full rounded-full transition-all duration-500 ease-out"
- :class="[
- 'bg-neutral-900 group-hover:bg-neutral-800'
- ]"
- :style="{ width: item.percentage + '%', backgroundColor: chartColors[index % chartColors.length] }"
+ :style="{ width: item.percentage + '%', backgroundColor: getSummaryChartColor(index) }"
/>
</div>
</div>
</div>
<!-- Pie Chart View -->
- <div v-else class="flex flex-col sm:flex-row items-center justify-center gap-8 py-2">
- <div class="w-56 h-56 relative">
+ <div v-else class="flex flex-col lg:flex-row items-center justify-center gap-8 py-2">
+ <div class="w-56 h-56 relative shrink-0" aria-hidden="true">
<Pie :data="chartData" :options="chartOptions" />
</div>
<!-- Legend -->
- <div class="space-y-2 max-h-60 overflow-y-auto custom-scrollbar pr-2">
- <div
+ <ul class="grid gap-x-6 gap-y-3 sm:grid-cols-2 min-w-0 max-w-full">
+ <li
v-for="(item, index) in distribution"
:key="item.value"
class="flex items-center gap-2.5"
>
<div
class="w-3 h-3 rounded-full flex-shrink-0"
- :style="{ backgroundColor: chartColors[index % chartColors.length] }"
+ aria-hidden="true"
+ :style="{ backgroundColor: getSummaryChartColor(index) }"
/>
- <div class="text-sm">
- <span class="text-neutral-700 font-medium">{{ item.value }}</span>
- <span class="text-neutral-400 mx-1">•</span>
- <span class="text-neutral-500">{{ item.percentage }}%</span>
+ <div class="text-sm min-w-0 break-words">
+ <span class="text-neutral-900 font-medium">{{ item.value }}</span>
+ <span class="block text-xs text-neutral-600">{{ item.count }} responses ({{ item.percentage }}%)</span>
</div>
- </div>
- </div>
+ </li>
+ </ul>
</div>
</template>
</div>
@@ -69,6 +66,7 @@
<script setup>
import { Pie } from 'vue-chartjs'
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js'
+import { getSummaryChartColor } from '~/lib/summary-chart-colors'
ChartJS.register(ArcElement, Tooltip, Legend)
@@ -77,25 +75,16 @@ const props = defineProps({
showPieChart: { type: Boolean, default: false },
})
-const chartColors = [
- '#FEF3C7', // amber-100
- '#FDE68A', // amber-200
- '#FCD34D', // amber-300
- '#FBBF24', // amber-400
- '#F59E0B', // amber-500
- '#D97706', // amber-600
- '#B45309', // amber-700
- '#92400E', // amber-800
-]
-
const distribution = computed(() => props.field.data?.distribution || [])
const chartData = computed(() => ({
labels: distribution.value.map(item => item.value),
datasets: [{
data: distribution.value.map(item => item.count),
- backgroundColor: chartColors.slice(0, distribution.value.length),
- borderWidth: 0,
+ backgroundColor: distribution.value.map((_, index) => getSummaryChartColor(index)),
+ borderColor: '#FFFFFF',
+ borderWidth: 2,
+ hoverBorderColor: '#FFFFFF',
}]
}))
@@ -117,4 +106,3 @@ const chartOptions = {
}
}
</script>
-
diff --git a/client/components/open/forms/components/summary/MatrixSummary.vue b/client/components/open/forms/components/summary/MatrixSummary.vue
index 10639c799..3a3d7e366 100644
--- a/client/components/open/forms/components/summary/MatrixSummary.vue
+++ b/client/components/open/forms/components/summary/MatrixSummary.vue
@@ -13,7 +13,7 @@
<thead>
<tr>
<th class="sticky left-0 bg-white z-10 p-3 border-b border-r border-neutral-100 w-1/4 min-w-[150px]">
- <span class="text-xs font-medium text-neutral-400 uppercase tracking-wider">Rows / Columns</span>
+ <span class="text-xs font-medium text-neutral-600 uppercase tracking-wider">Rows / Columns</span>
</th>
<th
v-for="col in columns"
@@ -42,7 +42,7 @@
>
<span class="font-bold">{{ formatPercentage(getPercentage(rowName, col)) }}%</span>
<span
- class="text-[10px] opacity-75 mt-0.5"
+ class="text-xs mt-0.5"
v-if="getPercentage(rowName, col) > 0"
>
{{ getCount(rowName, col) }}
@@ -105,11 +105,11 @@ const getCellClass = (percentage) => {
} else if (percentage >= 40) {
return 'bg-neutral-700 text-white shadow-sm'
} else if (percentage >= 20) {
- return 'bg-neutral-400 text-white'
+ return 'bg-neutral-300 text-neutral-900'
} else if (percentage > 0) {
return 'bg-neutral-100 text-neutral-700'
}
- return 'bg-transparent text-neutral-300'
+ return 'bg-transparent text-neutral-600'
}
</script>
diff --git a/client/components/open/forms/components/summary/RatingSummary.vue b/client/components/open/forms/components/summary/RatingSummary.vue
index 339ff784a..76cefc4d5 100644
--- a/client/components/open/forms/components/summary/RatingSummary.vue
+++ b/client/components/open/forms/components/summary/RatingSummary.vue
@@ -19,7 +19,8 @@
v-for="i in maxRating"
:key="i"
:name="i <= Math.round(data.average || 0) ? 'i-heroicons-star-solid' : 'i-heroicons-star'"
- class="w-5 h-5 text-amber-400"
+ class="w-5 h-5"
+ :style="{ color: getSummaryChartColor(6) }"
/>
</div>
<span class="text-xs text-neutral-500 uppercase tracking-wider font-medium">Average Rating</span>
@@ -39,8 +40,8 @@
<div class="flex-1 bg-neutral-100 rounded-full h-2.5 overflow-hidden">
<div
- class="bg-amber-400 h-full rounded-full transition-all duration-500 ease-out"
- :style="{ width: getPercentage(rating) + '%' }"
+ class="h-full rounded-full transition-all duration-500 ease-out"
+ :style="{ width: getPercentage(rating) + '%', backgroundColor: getSummaryChartColor(6) }"
/>
</div>
@@ -56,6 +57,8 @@
</template>
<script setup>
+import { getSummaryChartColor } from '~/lib/summary-chart-colors'
+
const props = defineProps({
field: { type: Object, required: true },
})
@@ -87,4 +90,3 @@ const getPercentage = (rating) => {
return Math.round((count / total) * 100)
}
</script>
-
diff --git a/client/components/open/forms/components/summary/SummaryFieldCard.vue b/client/components/open/forms/components/summary/SummaryFieldCard.vue
index 2807f8e0c..0206aa73a 100644
--- a/client/components/open/forms/components/summary/SummaryFieldCard.vue
+++ b/client/components/open/forms/components/summary/SummaryFieldCard.vue
@@ -36,7 +36,10 @@
<button
v-for="mode in ['bar', 'pie']"
:key="mode"
- class="px-2 py-1 rounded-md text-xs font-medium transition-all"
+ type="button"
+ :aria-label="`${mode === 'pie' ? 'Pie' : 'Bar'} chart for ${field.name}`"
+ :aria-pressed="mode === 'pie' ? showPieChart : !showPieChart"
+ class="px-2 py-1 rounded-md text-xs font-medium transition-all focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-900"
:class="[
(mode === 'pie' ? showPieChart : !showPieChart)
? 'bg-white text-neutral-900 shadow-sm'
@@ -51,7 +54,7 @@
</div>
<!-- Content -->
- <div class="max-h-96 overflow-y-auto overflow-x-auto custom-scrollbar">
+ <div class="overflow-y-auto overflow-x-auto custom-scrollbar" :class="{ 'max-h-96': !showPieChart }">
<component
:is="summaryComponent"
:field="field"
@@ -99,4 +102,3 @@ const summaryComponent = computed(() => {
return componentMap[props.field.summary_type] || TextListSummary
})
</script>
-
diff --git a/client/lib/summary-chart-colors.js b/client/lib/summary-chart-colors.js
new file mode 100644
index 000000000..e9d050f33
--- /dev/null
+++ b/client/lib/summary-chart-colors.js
@@ -0,0 +1,18 @@
+// Each color contrasts at least 3:1 with white and the neutral-100 bar track.
+// Keep labels and values visible: color alone cannot identify every category.
+export const summaryChartColors = [
+ '#2563EB', // blue
+ '#EA580C', // orange
+ '#059669', // emerald
+ '#9333EA', // purple
+ '#E11D48', // rose
+ '#0891B2', // cyan
+ '#B87900', // gold
+ '#DB2777', // pink
+ '#4F46E5', // indigo
+ '#568F0B', // lime
+]
+
+export function getSummaryChartColor(index) {
+ return summaryChartColors[index % summaryChartColors.length]
+}
diff --git a/client/test/nuxt/summary-charts.spec.ts b/client/test/nuxt/summary-charts.spec.ts
new file mode 100644
index 000000000..623176edb
--- /dev/null
+++ b/client/test/nuxt/summary-charts.spec.ts
@@ -0,0 +1,73 @@
+import { mount } from '@vue/test-utils'
+import { describe, expect, it, vi } from 'vitest'
+import { defineComponent } from 'vue'
+import DistributionSummary from '~/components/open/forms/components/summary/DistributionSummary.vue'
+import BooleanSummary from '~/components/open/forms/components/summary/BooleanSummary.vue'
+import { summaryChartColors } from '~/lib/summary-chart-colors'
+
+vi.mock('vue-chartjs', () => ({
+ Pie: defineComponent({ name: 'Pie', props: ['data', 'options'], template: '<canvas />' }),
+}))
+
+const global = { stubs: { UIcon: true } }
+
+describe('summary charts', () => {
+ it('keeps every pie slice, legend and bar color aligned beyond the palette length', async () => {
+ const distribution = Array.from({ length: summaryChartColors.length + 2 }, (_, i) => ({
+ value: `Option ${i + 1}`, count: i + 1, percentage: i + 1,
+ }))
+ const wrapper = mount(DistributionSummary, {
+ props: { field: { data: { distribution } }, showPieChart: true }, global,
+ })
+ const colors = wrapper.findComponent({ name: 'Pie' }).props('data').datasets[0].backgroundColor
+ expect(colors).toHaveLength(distribution.length)
+ const swatches = wrapper.findAll('li [aria-hidden="true"]')
+ const swatchColors = swatches.map(swatch => (swatch.element as HTMLElement).style.backgroundColor)
+ expect(swatches).toHaveLength(distribution.length)
+ distribution.forEach((item, i) => {
+ const sample = document.createElement('div')
+ sample.style.backgroundColor = colors[i]
+ expect(swatchColors[i]).toBe(sample.style.backgroundColor)
+ expect(wrapper.findAll('li')[i].text()).toContain(`${item.count} responses (${item.percentage}%)`)
+ })
+ await wrapper.setProps({ showPieChart: false })
+ const bars = wrapper.findAll('[style*="background-color"]')
+ expect(bars.map(bar => (bar.element as HTMLElement).style.backgroundColor)).toEqual(swatchColors)
+ expect(wrapper.text()).toContain('Option 12')
+ })
+
+ it('keeps Yes/No labels aligned with values when the response order differs', () => {
+ const wrapper = mount(BooleanSummary, {
+ props: {
+ field: { data: { distribution: [
+ { value: 'No', count: 9, percentage: 90 },
+ { value: 'Yes', count: 1, percentage: 10 },
+ ] } }, showPieChart: true,
+ }, global,
+ })
+ const data = wrapper.findComponent({ name: 'Pie' }).props('data')
+ expect(data.labels).toEqual(['Yes', 'No'])
+ expect(data.datasets[0].data).toEqual([1, 9])
+ expect(wrapper.text()).toContain('1 responses (10%)')
+ expect(wrapper.text()).toContain('9 responses (90%)')
+ })
+
+ it('shows the percentage of small boolean segments outside the bar', () => {
+ const wrapper = mount(BooleanSummary, {
+ props: { field: { data: { distribution: [
+ { value: 'Yes', count: 1, percentage: 1 },
+ { value: 'No', count: 99, percentage: 99 },
+ ] } } }, global,
+ })
+ expect(wrapper.text()).toContain('1 (1%)')
+ expect(wrapper.text()).toContain('99 (99%)')
+ })
+
+ it('preserves the empty state', () => {
+ for (const component of [DistributionSummary, BooleanSummary]) {
+ const wrapper = mount(component, { props: { field: { data: {} }, showPieChart: true }, global })
+ expect(wrapper.text()).toContain('No responses yet')
+ expect(wrapper.find('canvas').exists()).toBe(false)
+ }
+ })
+})
diff --git a/client/test/unit/summary-chart-colors.spec.ts b/client/test/unit/summary-chart-colors.spec.ts
new file mode 100644
index 000000000..c99e22a56
--- /dev/null
+++ b/client/test/unit/summary-chart-colors.spec.ts
@@ -0,0 +1,28 @@
+import { describe, expect, it } from 'vitest'
+import { summaryChartColors } from '../../lib/summary-chart-colors'
+
+function luminance(hex: string) {
+ const [r, g, b] = hex.slice(1).match(/.{2}/g)!.map(value => {
+ const channel = parseInt(value, 16) / 255
+ return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4
+ })
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b
+}
+
+describe('summary chart contrast', () => {
+ it('provides ten distinct category colors', () => {
+ expect(new Set(summaryChartColors).size).toBeGreaterThanOrEqual(10)
+ })
+
+ it.each(summaryChartColors)('%s contrasts with chart backgrounds and white separators', (color) => {
+ const foreground = luminance(color)
+ expect((luminance('#F5F5F5') + 0.05) / (foreground + 0.05)).toBeGreaterThanOrEqual(3)
+ expect((luminance('#FFFFFF') + 0.05) / (foreground + 0.05)).toBeGreaterThanOrEqual(3)
+ })
+
+ it('keeps boolean percentages readable on both segment colors', () => {
+ // BooleanSummary uses white text for Yes, neutral-900 for No.
+ expect(1.05 / (luminance(summaryChartColors[0]) + 0.05)).toBeGreaterThanOrEqual(4.5)
+ expect((luminance(summaryChartColors[1]) + 0.05) / (luminance('#171717') + 0.05)).toBeGreaterThanOrEqual(4.5)
+ })
+})