Smoking habit tracker

fix(diagrams): cigarettes per day (last 30 days) chart is broken #5

Open io-industries.dev
Open opened by io-industries.dev

fix(diagrams): cigarettes per day (last 30 days) chart is broken #

Area: Data & Diagrams · Cigarettes per day · pages/diagrams.vue Type: bug Priority: p0

Summary #

The Cigarettes per day section on the data & diagrams page (/diagrams) is broken (flat/empty bars, wrong heights, or JS error on empty data). Fix rendering so 30-day bars are proportionally visible.

Current behavior #

pages/diagrams.vue:8-22:

const cigsPerDay = computed(() => heatData.value?.counts.map(Number) || [])
const last30 = computed(() => cigsPerDay.value.slice(-30))
const maxCigsPerDay = computed(() => {
  if (!last30.value?.length) return 1
  return Math.max(...last30.value.map(c => Math.max(1, c)))
})

Template pages/diagrams.vue:93-110:

<div class="flex items-end gap-[2px] h-40">
  <div v-for="(count,i) in last30" class="flex flex-col items-center flex-1">
    <div class="chart-bar" :style="{ height: Math.max(2, (count/maxCigsPerDay)*100)+'%' }"></div>
  </div>
</div>

Likely causes:

  • maxCigsPerDay is forced to ≥1 even when all counts are 0 → every bar stuck at 2% min, chart looks flat/broken.
  • heatData is null on first load → last30 empty → no empty-state, just empty flex row.
  • flex-1 bars inside shrinking container may have no width if parent has no min-width.
  • No graceful empty state (should show t('diagrams.no_data')).

Also note server/utils/stats/heatmap.ts:buildHeatmap returns counts: number[84] — last30 correctly slices but chart doesn't handle the all-zero case distinct from loading.

Expected behavior #

  • Bars proportionally fill h-40 with items-end, gap-[2px] working on mobile/desktop.
  • Zero-data case: show t('diagrams.no_data') (already has <p v-else class="empty-state">) — ensure v-if="last30?.length" correctly branches (check last30 is always an array, empty array is truthy — may need last30.length > 0 && max>0).
  • When data exists but max is 0 (new user, 0 cigs in last 30d), either show empty-state or show flat zero-height bars (not 2% fake height). Prefer: if max===0 or all zeros → show empty-state.
  • If data exists, maxCigsPerDay = Math.max(...last30) (without Math.max(1,c) per element) and clamp denominator to ≥1 once. Use height: count===0 ? '2px' : (count/max)*100+'%' or similar so zeros are visible but distinguishable.
  • Keep role="img" + aria-label per bar and sr-only table for a11y.
  • No console errors on initial load (heatData==null).

Files to touch #

  • pages/diagrams.vue:8-22 — maxCigsPerDay + empty handling
  • pages/diagrams.vue:93-110 — bar height/style, v-if condition
  • assets/css/marathon-components.css:.chart-bar — no change expected (still bg-primary)

Acceptance criteria #

How to test #

  1. New user (0 cigs) → open /diagrams → should see empty-state, not flat bars.
  2. Seed 30 days with varied counts (e.g. via scripts/screenshot-seed.ts or logging cigs across days) → verify bar heights scale.
  3. Check devtools console → no errors on hard reload.
Sign up or Sign in to add to the discussion.