## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(message = FALSE, warning = FALSE)

## ----rule-behavior-tables, echo=FALSE-----------------------------------------
rule_behavior <- data.frame(
  Family = c(
    rep("Completeness", 6),
    rep("Readability", 10),
    rep("Integrity", 9)
  ),
  Rule = c(
    "R1 Clear title",
    "R2 Axis labels",
    "R3 Missing values and gaps",
    "R4 Legend clarity",
    "R5 Source and context",
    "R6 Annotation for key message",
    "R7 Readable text",
    "R8 Accessible color choices",
    "R9 Avoid clutter",
    "R10 Grid and guide balance",
    "R11 Consistent formatting",
    "R12 Appropriate chart type",
    "R13 Readable scale labels",
    "R14 Overplotting management",
    "R15 Direct labeling when useful",
    "R16 Visual hierarchy",
    "R17 Truthful scale",
    "R18 No distorted baseline",
    "R19 Avoid misleading encodings",
    "R20 Handle outliers honestly",
    "R21 Proportional areas",
    "R22 Uncertainty when needed",
    "R23 Avoid cherry picking",
    "R24 Respect data granularity",
    "R25 Neutral framing"
  ),
  Evidence = c(
    "`title` label",
    "`x_label`, `y_label`",
    "Missing-value and gap signals from built data",
    "Mapped legend aesthetics, guide settings, legend title, legend labels",
    "Caption, subtitle-like context, source fields",
    "Annotation layers and key-message need signals",
    "Theme text size and related text metadata",
    "Color count and palette accessibility signals",
    "Layer count, mark count, clutter score",
    "Gridline and guide metadata",
    "Formatting consistency signal",
    "Geom type, mapped data, chart-type plausibility signal",
    "Scale labels and scale readability signal",
    "Point count and overplotting signal",
    "Legend size, direct-label signal, direct-label usefulness signal",
    "Title/text hierarchy and hierarchy signal",
    "Scale truthfulness signal",
    "Baseline requirement and zero-baseline signal",
    "Encoding integrity signal",
    "Outlier detection and outlier handling signal",
    "Area encoding and proportionality signal",
    "Uncertainty need and uncertainty layer signals",
    "Scale limits, data selection, and context signals",
    "Data granularity and geom/data compatibility signal",
    "Title/subtitle wording and framing signal"
  ),
  PASS = c(
    "Title text is present.",
    "Both axis labels are present.",
    "No missing values or gaps were reported.",
    "A legend appears to have usable title or label text.",
    "A caption or source/context field is present.",
    "Annotation is present, or no annotation need was reported.",
    "Text size appears readable.",
    "Palette is reported accessible.",
    "No high clutter signal was reported.",
    "Grid/guide information appears acceptable.",
    "Formatting is reported consistent.",
    "Chart type information appears plausible or available.",
    "Scale labels appear readable.",
    "No overplotting signal was reported.",
    "Direct labels are reported.",
    "Visual hierarchy information is supplied and not weak.",
    "Scale is reported truthful.",
    "Required baseline is present, or no zero baseline is required.",
    "No misleading encoding was reported.",
    "No outliers are reported, or outliers appear handled.",
    "Area encoding is proportional, or no area encoding is used.",
    "Uncertainty is shown when needed, or no uncertainty need is reported.",
    "No cherry-picking risk is reported, or full context is supplied.",
    "Data granularity is respected.",
    "Framing is reported neutral."
  ),
  WARNING = c(
    "Title field exists but is empty.",
    "One or both labels appear missing.",
    "Missing values or gaps were found.",
    "A legend is present but lacks clear text.",
    "Not currently used.",
    "A key-message annotation appears needed but missing.",
    "Explicit text size appears small.",
    "Many colors are used and should be reviewed.",
    "Many layers, marks, or clutter signals are present.",
    "Grid or guide balance may need review.",
    "Formatting is reported inconsistent.",
    "Chart type is reported potentially inappropriate.",
    "Scale labels are reported hard to read.",
    "Dense point layers may need transparency, aggregation, jitter, or binning.",
    "Direct labels may improve readability.",
    "Visual hierarchy is reported weak.",
    "Not currently used.",
    "Not currently used.",
    "Not currently used.",
    "Outliers are present without handling context.",
    "Not currently used.",
    "Uncertainty appears needed but missing.",
    "Data selection context may be incomplete.",
    "Visual form may mismatch the data granularity.",
    "Framing is reported non-neutral."
  ),
  FAIL = c(
    "Not used.",
    "Not used.",
    "Not used.",
    "Not currently used.",
    "Not used.",
    "Not used.",
    "Not used.",
    "Palette is reported inaccessible.",
    "Not used.",
    "Not used.",
    "Not used.",
    "Not used.",
    "Not used.",
    "Not used.",
    "Not used.",
    "Not used.",
    "Scale is reported potentially untruthful.",
    "A zero baseline appears required but missing.",
    "A misleading encoding was reported.",
    "Not used.",
    "Area encoding is reported non-proportional.",
    "Not used.",
    "Not used.",
    "Not used.",
    "Not used."
  ),
  INFO = c(
    "Title field is unavailable.",
    "Axis label fields are unavailable.",
    "Missing-value information is unavailable.",
    "No legend evidence is available. This can mean no legend is needed, or that the current metadata does not expose enough legend information.",
    "No source/context field is available.",
    "Annotation need cannot be determined.",
    "Text-size information is unavailable.",
    "Color accessibility cannot be assessed.",
    "Clutter evidence is unavailable.",
    "Grid/guide evidence is unavailable.",
    "Formatting consistency cannot be assessed.",
    "Chart-type evidence is unavailable.",
    "Scale-label evidence is unavailable.",
    "Overplotting evidence is unavailable.",
    "Direct-labeling need cannot be determined.",
    "Hierarchy evidence is unavailable.",
    "Scale truthfulness cannot be assessed.",
    "Baseline requirement cannot be assessed.",
    "Encoding integrity cannot be assessed.",
    "Outlier evidence is unavailable.",
    "Area-encoding evidence is unavailable.",
    "Uncertainty need cannot be assessed.",
    "Data-selection context is unavailable.",
    "Granularity evidence is unavailable.",
    "Framing evidence is unavailable."
  ),
  check.names = FALSE
)

family_order <- c("Completeness", "Readability", "Integrity")
family_rows <- split(rule_behavior, factor(rule_behavior$Family, levels = family_order))

status_cell <- function(label, text) {
  htmltools::tags$div(
    class = "gv-check-status-cell",
    htmltools::tags$span(class = paste("gv-mini-status", paste0("gv-mini-status-", tolower(label))), label),
    htmltools::tags$p(text)
  )
}

behavior_family_table <- function(family_name, family_data) {
  htmltools::tags$section(
    class = "gv-rules-family gv-check-family",
    htmltools::tags$h2(class = "gv-rules-family-title", family_name),
    htmltools::tags$table(
      class = "gv-check-table",
      htmltools::tags$colgroup(
        htmltools::tags$col(class = "gv-check-col-rule"),
        htmltools::tags$col(class = "gv-check-col-evidence"),
        htmltools::tags$col(class = "gv-check-col-status"),
        htmltools::tags$col(class = "gv-check-col-status"),
        htmltools::tags$col(class = "gv-check-col-status"),
        htmltools::tags$col(class = "gv-check-col-status")
      ),
      htmltools::tags$thead(
        htmltools::tags$tr(
          htmltools::tags$th("Rule"),
          htmltools::tags$th("Evidence inspected"),
          htmltools::tags$th("PASS"),
          htmltools::tags$th("WARNING"),
          htmltools::tags$th("FAIL"),
          htmltools::tags$th("INFO")
        )
      ),
      htmltools::tags$tbody(
        lapply(seq_len(nrow(family_data)), function(i) {
          htmltools::tags$tr(
            htmltools::tags$td(
              htmltools::tags$span(class = "gv-rule-number", sub(" .*", "", family_data$Rule[[i]])),
              htmltools::tags$span(class = "gv-rule-name", sub("^R[0-9]+ ", "", family_data$Rule[[i]]))
            ),
            htmltools::tags$td(htmltools::HTML(gsub("`([^`]+)`", "<code>\\1</code>", family_data$Evidence[[i]]))),
            htmltools::tags$td(status_cell("PASS", family_data$PASS[[i]])),
            htmltools::tags$td(status_cell("WARNING", family_data$WARNING[[i]])),
            htmltools::tags$td(status_cell("FAIL", family_data$FAIL[[i]])),
            htmltools::tags$td(status_cell("INFO", family_data$INFO[[i]]))
          )
        })
      )
    )
  )
}

htmltools::tagList(Map(behavior_family_table, names(family_rows), family_rows))

