add global_script statistics dimension property

this script is ran once at the start, and its result is stored in the 'global_value' variable, which is accessible to the regular script.
This commit is contained in:
GenevensiS
2025-07-21 04:31:30 +02:00
parent 51f882315a
commit 5cd8069bf1
4 changed files with 35 additions and 14 deletions
+21 -3
View File
@@ -472,13 +472,29 @@ void StatsPanel::showCategory(const GraphType* prefer_layout) {
dim->groups.empty() ? nullptr : &dim->groups
)
);
}
// find global_script values
vector<ScriptValueP> global_values;
Context& global_ctx = set->getContext();
ScriptValueP global_ctx_value = global_ctx.getVariableOpt("global_value");
for (size_t d = 0 ; d < dims.size() ; ++d) {
auto& dim = dims[d];
try {
ScriptValueP global_value = dim->global_script.invoke(global_ctx);
global_values.push_back(global_value);
} catch (ScriptError const& e) {
handle_error(ScriptError(e.what() + _("\n in global script for statistics dimension '") + dim->name + _("'")));
global_values.push_back(script_nil);
}
}
// find values for each card
// find script values for each card
for (size_t i = 0 ; i < set->cards.size() ; ++i) {
Context& ctx = set->getContext(set->cards[i]);
GraphElementP e = make_intrusive<GraphElement>(i);
bool show = true;
FOR_EACH(dim, dims) {
for (size_t d = 0 ; d < dims.size() ; ++d) {
auto& dim = dims[d];
ctx.setVariable("global_value", global_values[d]);
try {
String value = untag(dim->script.invoke(ctx)->toString());
e->values.push_back(value);
@@ -497,7 +513,9 @@ void StatsPanel::showCategory(const GraphType* prefer_layout) {
assert(e->values.size() == dims.size());
d.elements.push_back(e);
}
}
}
// restore old global value if any
if (global_ctx_value) global_ctx.setVariable("global_value", global_ctx_value);
// split lists
size_t dim_id = 0;
FOR_EACH(dim, dims) {