ravenous-tools/weaponry/src/export.rs

110 lines
4.3 KiB
Rust

// Workspace: weaponry
// File: weaponry/src/export.rs
//! Module regrouping all generic function related to the exporting
//!
//! Mostly graphs and csv auto conversion
/// A very complex macro created to add criterias to the graph
/// without repeating self explanatory parameter names in strings
///
/// The syntax involved in the usage of this macro hence become esoteric
///
/// # Example
/// ```
/// # use weaponry::interfaces::RecipeCollection;
/// # use weaponry::add_field_to_chart;
/// # use weaponry::weaponry_structs::RefinedEquipementList;
/// # use weaponry::export::initial_chart;
/// # let self_ = RefinedEquipementList::default();
/// # let initial_chart: charming::Chart = initial_chart("Damage for all cross-gen products");
/// add_field_to_chart!(
/// self_, // Usually used inside the implementation and therefore self there
/// initial_chart,
/// (
/// index # &String,
/// dps_safe # f64,
/// max_damage_per_action # f64,
/// armor_break_per_turn # f64,
/// ),
/// |ref_equipement| {
/// (
/// &ref_equipement.advanced.display_name,
/// ref_equipement.advanced.max_damage_per_action,
/// ref_equipement.advanced.damage_per_turn,
/// ref_equipement.advanced.armor_break_per_turn,
/// )
/// }
/// );
/// ```
///
/// # Complexity and maintanability
///
/// While the added complexity cannot be understated, the whole existence of this macro is
/// first and foremost based on the desire NOT to iterate multiple times on a dataset
/// which could easily reach 600k elements
///
/// Knowing this tradeoff, the difficulty to maintain is considered an acceptable tradeoff
#[macro_export]
macro_rules! add_field_to_chart {
($base_struct:expr, $chart:ident, ($names:ident # $name_type:ty, $($table_names:ident # $table_type:ty),* $(,)*), | $closure_name:ident | {($($fields:expr,)*)}
) => {
let data_collection = $base_struct.inner.iter().map(|$closure_name| ( $($fields,)*) );
let ($names, $($table_names),*) = data_collection.collect::<(Vec<$name_type>, $(Vec<$table_type>,)*)>();
let $chart = $chart
.x_axis(
charming::component::Axis::new()
.data($names.to_vec())
.type_(charming::element::AxisType::Category),
);
$(
let $chart = $chart
.series(
charming::series::Line::new()
.data($table_names)
.name(stringify!($table_names)), // .line_style(LineStyle::new().color("paleblue")),
);
)*
}
}
/// Generate a generic graph, ready to be filled with data
pub fn initial_chart(chart_name: &str) -> charming::Chart {
charming::Chart::new()
.title(
charming::component::Title::new()
.text(chart_name)
.left("center"),
)
.legend(
charming::component::Legend::new()
.left(10)
.top("middle")
.orient(charming::element::Orient::Vertical)
.data(vec![
"dps_safe",
"max_damage_per_action",
"max_damage_per_turn",
"armor_break_per_turn",
]),
)
.tooltip(charming::element::Tooltip::new().trigger(charming::element::Trigger::Axis))
.data_zoom(
charming::component::DataZoom::new()
.type_(charming::component::DataZoomType::Inside)
.start(0)
.end(100),
)
.data_zoom(charming::component::DataZoom::new().start(0).end(100))
.toolbox(
charming::component::Toolbox::new().feature(
charming::component::Feature::new()
.data_zoom(charming::component::ToolboxDataZoom::new().y_axis_index("none"))
.restore(charming::component::Restore::new())
.save_as_image(charming::component::SaveAsImage::new()), // .brush(Brush::new().type_(vec![BrushType::LineX, BrushType::Clear])),
),
)
.tooltip(charming::element::Tooltip::new().trigger(charming::element::Trigger::Axis))
.y_axis(charming::component::Axis::new())
.grid(charming::component::Grid::new().contain_label(true))
}