ravenous_typst/setup/generation/radar_graphs.typ

163 lines
5.4 KiB
Plaintext

// Aggregate the balancing columns from every school TSV and render comparable
// radar charts. Axis totals are normalized against the highest class total for
// that axis across the complete `school-sources` collection.
#import "../theme/variables.typ": theme
#import "../build_config.typ": tier-is-enabled
#import "@preview/primaviz:0.6.0": radar-chart
// Selected TSV fields and their reader-facing labels. Add, remove, reorder, or
// rename axes here. `aliases` handles known variations in source headers.
#let radar-fields = (
(field: "Support", display: "Support", aliases: ()),
(field: "Dégats", display: "Dégâts", aliases: ("Dégâts",)),
(field: "Survivabilité", display: "Survie", aliases: ()),
(field: "Mobilité", display: "Mobilité", aliases: ()),
(field: "C.C", display: "Contrôle", aliases: ()),
(field: "Altérations", display: "Altérations", aliases: ()),
(field: "QoL", display: "Confort", aliases: ()),
(field: "AB", display: "Armure", aliases: ()),
// (field: "Compléxité", display: "Complexité", aliases: ("Complexité", "Compl")),
)
// Canonical public class names and their data sources.
#let school-sources = (
artilleur: "/data/src/schools/artilleur.tsv",
bidouilleur: "/data/src/schools/bidouilleur.tsv",
einerjar: "/data/src/schools/einerjar.tsv",
franc-tireur: "/data/src/schools/franc_tirreur.tsv",
guerrier-mage: "/data/src/schools/guerrier_mage.tsv",
specialist-armes-lourdes: "/data/src/schools/specialist_armes_lourdes.tsv",
lancier: "/data/src/schools/lancier.tsv",
mage-bestial: "/data/src/schools/mage_bestial.tsv",
maitre-lames: "/data/src/schools/maitre_lames.tsv",
protecteur: "/data/src/schools/protecteur.tsv",
sangremage: "/data/src/schools/sangremage.tsv",
)
#let class-aliases = (
franc-tirreur: "franc-tireur",
heavy-weapon-specialist: "specialist-armes-lourdes",
specialiste-armes-lourdes: "specialist-armes-lourdes",
)
#let canonical-class-name(name) = {
let normalized = lower(name).replace("_", "-").replace(" ", "-")
class-aliases.at(normalized, default: normalized)
}
#let numeric-cell(value) = {
let value = value.trim()
if value.match(regex("^-?(?:\d+(?:\.\d+)?|\.\d+)$")) != none {
float(value)
} else {
none
}
}
#let field-index(header, field) = {
let names = (field.field,) + field.aliases
for name in names {
let index = header.position(value => value.trim() == name)
if index != none { return index }
}
panic(
"Radar field `" + field.field + "` was not found in TSV header: "
+ header.join(", "),
)
}
// Return the raw summed balancing totals for one TSV source.
// Blank and symbolic annotations such as `A`, `M`, or `1/X` are ignored.
#let balancing-totals(source, fields: radar-fields) = {
let rows = csv(source, delimiter: "\t")
let header-index = rows.position(row => "Technique" in row)
if header-index == none {
panic("No technique header found in `" + str(source) + "`")
}
let header = rows.at(header-index)
let indices = fields.map(field => field-index(header, field))
let totals = fields.map(_ => 0.0)
let current-tier = none
for row in rows.slice(header-index + 1) {
if row.len() > 0 and row.first().trim().starts-with("Tier") {
let digits = row.first().replace(regex("\D"), "")
if digits != "" { current-tier = int(digits) }
continue
}
if not tier-is-enabled(current-tier) { continue }
for (axis, column) in indices.enumerate() {
if column < row.len() {
let value = numeric-cell(row.at(column))
if value != none { totals.at(axis) += value }
}
}
}
totals
}
// Return raw totals for every configured class, keyed by canonical class name.
#let all-balancing-totals(fields: radar-fields) = {
let totals = (:)
for (name, source) in school-sources {
totals.insert(name, balancing-totals(source, fields: fields))
}
totals
}
#let axis-maxima(totals, axis-count) = {
range(axis-count).map(axis => {
let maximum = 0.0
for (_, values) in totals {
maximum = calc.max(maximum, values.at(axis))
}
maximum
})
}
// Render a class radar graph. `fields` can override `radar-fields` for a
// one-off graph while keeping the same field/display/aliases structure.
#let radar-graph(class-name, fields: radar-fields, size: 155pt) = {
let name = canonical-class-name(class-name)
if name not in school-sources {
panic(
"Unknown radar class `" + class-name + "`. Available classes: "
+ school-sources.keys().join(", "),
)
}
let source = school-sources.at(name)
let all-totals = all-balancing-totals(fields: fields)
let values = all-totals.at(name)
let maxima = axis-maxima(all-totals, fields.len())
let normalized = values.enumerate().map(((axis, value)) => {
let maximum = maxima.at(axis)
if maximum == 0 { 0 } else { value / maximum * 100 }
})
radar-chart(
(
labels: fields.map(field => field.display),
series: ((name: name, values: normalized),),
),
size: size,
axis-max-value: 100,
show-legend: false,
show-value-labels: false,
fill-opacity: 24%,
theme: (
palette: (theme.blood,),
text-color: theme.ink,
text-color-light: theme.muted-ink,
axis-stroke: 0.7pt + theme.dark-gold.transparentize(32%),
grid-stroke: 0.7pt + theme.dark-gold.transparentize(32%),
marker-stroke: 0.6pt + theme.parchment-light,
background: none,
border: none,
show-grid: true,
base-size: 6pt,
base-gap: 4pt,
),
)
}