ChristopherJKoen commited on
Commit
e722874
·
1 Parent(s): cf17e86

Make category/priority badges adaptive

Browse files

Replace qualitative category labels with percentage-based labels (e.g. 100% Original Strength, 95-100% Original Strength, 75-95%, 50-75%, <50%). Replace the fixed badge width with dynamic sizing: measure text widths via pdf.stringWidth (Helvetica-Bold, size 10), add padding, clamp to min/max widths, and compute badge_gap so both badges stay centered. If badges overflow the content area, shrink the gap or scale badge widths to fit. Update label positions and roundedRect draws to use the computed cat_w/pr_w values and adjust minor layout constants for improved responsiveness and readability.

Files changed (1) hide show
  1. server/app/services/pdf_reportlab.py +40 -15
server/app/services/pdf_reportlab.py CHANGED
@@ -579,12 +579,12 @@ def render_report_pdf(
579
  category = _safe_text(template.get("category"))
580
  priority = _safe_text(template.get("priority"))
581
  cat_scale = {
582
- "0": {"label": "Excellent", "bg": green_100, "text": colors.HexColor("#166534")},
583
- "1": {"label": "Good", "bg": green_200, "text": colors.HexColor("#166534")},
584
- "2": {"label": "Fair", "bg": yellow_100, "text": colors.HexColor("#854d0e")},
585
- "3": {"label": "Poor", "bg": yellow_200, "text": colors.HexColor("#854d0e")},
586
- "4": {"label": "Worse", "bg": orange_200, "text": colors.HexColor("#9a3412")},
587
- "5": {"label": "Severe", "bg": red_200, "text": colors.HexColor("#991b1b")},
588
  }
589
  pr_scale = {
590
  "1": {"label": "Immediate", "bg": red_200, "text": colors.HexColor("#991b1b")},
@@ -596,30 +596,55 @@ def render_report_pdf(
596
  cat_text, cat_bg, cat_text_color = _badge_style(category, cat_scale)
597
  pr_text, pr_bg, pr_text_color = _badge_style(priority, pr_scale)
598
 
599
- badge_w = 40 * mm
600
  badge_h = 10 * mm
601
  y -= 1 * mm
602
  pdf.setFillColor(gray_500)
603
  pdf.setFont("Helvetica", 9)
604
- badge_gap = 20 * mm
605
- total_badge_w = badge_w * 2 + badge_gap
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
606
  start_x = (width - total_badge_w) / 2
607
  cat_x = start_x
608
- pr_x = start_x + badge_w + badge_gap
609
- cat_label_x = cat_x + badge_w / 2
610
- pr_label_x = pr_x + badge_w / 2
611
  label_y = y
612
  pdf.drawCentredString(cat_label_x, label_y, "Category")
613
  pdf.drawCentredString(pr_label_x, label_y, "Priority")
614
  y -= 10 * mm
615
  pdf.setFillColor(cat_bg)
616
  pdf.setStrokeColor(gray_200)
617
- pdf.roundRect(cat_x, y - 2, badge_w, badge_h, 2 * mm, stroke=1, fill=1)
618
  pdf.setFillColor(cat_text_color)
619
- pdf.setFont("Helvetica-Bold", 10)
620
  pdf.drawCentredString(cat_label_x, y - 2 + badge_h / 2 - 3, cat_text)
621
  pdf.setFillColor(pr_bg)
622
- pdf.roundRect(pr_x, y - 2, badge_w, badge_h, 2 * mm, stroke=1, fill=1)
623
  pdf.setFillColor(pr_text_color)
624
  pdf.drawCentredString(pr_label_x, y - 2 + badge_h / 2 - 3, pr_text)
625
  y -= 8 * mm
 
579
  category = _safe_text(template.get("category"))
580
  priority = _safe_text(template.get("priority"))
581
  cat_scale = {
582
+ "0": {"label": "100% Original Strength", "bg": green_100, "text": colors.HexColor("#166534")},
583
+ "1": {"label": "100% Original Strength", "bg": green_200, "text": colors.HexColor("#166534")},
584
+ "2": {"label": "95-100% Original Strength", "bg": yellow_100, "text": colors.HexColor("#854d0e")},
585
+ "3": {"label": "75-95% Original Strength", "bg": yellow_200, "text": colors.HexColor("#854d0e")},
586
+ "4": {"label": "50-75% Original Strength", "bg": orange_200, "text": colors.HexColor("#9a3412")},
587
+ "5": {"label": "<50% Original Strength", "bg": red_200, "text": colors.HexColor("#991b1b")},
588
  }
589
  pr_scale = {
590
  "1": {"label": "Immediate", "bg": red_200, "text": colors.HexColor("#991b1b")},
 
596
  cat_text, cat_bg, cat_text_color = _badge_style(category, cat_scale)
597
  pr_text, pr_bg, pr_text_color = _badge_style(priority, pr_scale)
598
 
 
599
  badge_h = 10 * mm
600
  y -= 1 * mm
601
  pdf.setFillColor(gray_500)
602
  pdf.setFont("Helvetica", 9)
603
+
604
+ # Make badge width adapt to text length while keeping both badges centered.
605
+ badge_font = "Helvetica-Bold"
606
+ badge_size = 10
607
+ badge_padding = 6 * mm
608
+ min_badge_w = 28 * mm
609
+ max_badge_w = 85 * mm
610
+ content_w = width - 2 * margin
611
+
612
+ cat_text_w = pdf.stringWidth(cat_text, badge_font, badge_size) + badge_padding * 2
613
+ pr_text_w = pdf.stringWidth(pr_text, badge_font, badge_size) + badge_padding * 2
614
+
615
+ cat_w = max(min_badge_w, min(max_badge_w, cat_text_w))
616
+ pr_w = max(min_badge_w, min(max_badge_w, pr_text_w))
617
+ badge_gap = 12 * mm
618
+
619
+ total_badge_w = cat_w + pr_w + badge_gap
620
+ if total_badge_w > content_w:
621
+ badge_gap = max(4 * mm, content_w - (cat_w + pr_w))
622
+ total_badge_w = cat_w + pr_w + badge_gap
623
+
624
+ if total_badge_w > content_w:
625
+ available = max(20 * mm, content_w - badge_gap)
626
+ scale = available / max(cat_w + pr_w, 1)
627
+ cat_w *= scale
628
+ pr_w *= scale
629
+ total_badge_w = cat_w + pr_w + badge_gap
630
+
631
  start_x = (width - total_badge_w) / 2
632
  cat_x = start_x
633
+ pr_x = start_x + cat_w + badge_gap
634
+ cat_label_x = cat_x + cat_w / 2
635
+ pr_label_x = pr_x + pr_w / 2
636
  label_y = y
637
  pdf.drawCentredString(cat_label_x, label_y, "Category")
638
  pdf.drawCentredString(pr_label_x, label_y, "Priority")
639
  y -= 10 * mm
640
  pdf.setFillColor(cat_bg)
641
  pdf.setStrokeColor(gray_200)
642
+ pdf.roundRect(cat_x, y - 2, cat_w, badge_h, 2 * mm, stroke=1, fill=1)
643
  pdf.setFillColor(cat_text_color)
644
+ pdf.setFont(badge_font, badge_size)
645
  pdf.drawCentredString(cat_label_x, y - 2 + badge_h / 2 - 3, cat_text)
646
  pdf.setFillColor(pr_bg)
647
+ pdf.roundRect(pr_x, y - 2, pr_w, badge_h, 2 * mm, stroke=1, fill=1)
648
  pdf.setFillColor(pr_text_color)
649
  pdf.drawCentredString(pr_label_x, y - 2 + badge_h / 2 - 3, pr_text)
650
  y -= 8 * mm