ravenous-tools/weaponry/src/export.rs

184 lines
7.1 KiB
Rust

// Workspace: weaponry
// File: weaponry/src/export.rs
//! Module regrouping all generic function related to the exporting
//!
//! Mostly graphs and csv auto conversion
use charming::{
Chart,
component::{self, Axis, Grid, Title},
element::{Tooltip, Trigger},
};
/// 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,
/// ),
/// |ref_equipement| {
/// (
/// &ref_equipement.advanced.display_name,
/// ref_equipement.advanced.max_damage_per_attack,
/// )
/// }
/// );
/// ```
///
/// # 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
// TODO[4]: If the field accessed are only in advanced, remove the closure entirely and auto_generate it
// TODO[4]: A good part of this code shouldn't be generic and instead in weaponry
#[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 $chart = {
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()
// .stack("Total")
.data($table_names)
.name(stringify!($table_names))
.mark_point(charming::element::MarkPoint::new().data(vec![("max", "Max"), ("min", "Min")]))
.mark_line(charming::element::MarkLine::new().data(vec![charming::element::MarkLineVariant::Simple(
charming::element::MarkLineData::new()
.type_(charming::element::MarkLineDataType::Average)
.name("Avg"),
)])),
);
)*
$chart.legend(
charming::component::Legend::new()
.left(10)
.top("middle")
.orient(charming::element::Orient::Vertical)
.data(vec![
$(stringify!($table_names),)*
]),
)
};
}
}
/// Generate a generic graph, ready to be filled with data
pub fn initial_chart(chart_name: &str) -> Chart {
Chart::new()
.title(component::Title::new().text(chart_name).left("center"))
.tooltip(charming::element::Tooltip::new().trigger(charming::element::Trigger::Axis))
.data_zoom(
component::DataZoom::new()
.type_(component::DataZoomType::Inside)
.start(0)
.end(100),
)
.data_zoom(component::DataZoom::new().start(0).end(100))
.toolbox(
component::Toolbox::new().feature(
component::Feature::new()
.data_zoom(component::ToolboxDataZoom::new().y_axis_index("none"))
.restore(component::Restore::new())
.save_as_image(component::SaveAsImage::new()), // .brush(Brush::new().type_(vec![BrushType::LineX, BrushType::Clear])),
),
)
.tooltip(charming::element::Tooltip::new().trigger(charming::element::Trigger::Axis))
.y_axis(component::Axis::new())
.grid(component::Grid::new().contain_label(true))
}
/// Generate a new plot chart
pub fn initial_plot_chart(chart_name: &str, sub_charts: &[&str]) -> Chart {
// charming::Chart::new()
Chart::new()
.title(Title::new().text(chart_name).left("center"))
.title(Title::new().text(sub_charts[0]).left("17%").top("5%"))
.title(Title::new().text(sub_charts[1]).left("17%").top("52%"))
.title(Title::new().text(sub_charts[2]).right("12%").top("5%"))
.title(Title::new().text(sub_charts[3]).right("12%").top("52%"))
.tooltip(Tooltip::new().trigger(Trigger::Axis))
.data_zoom(
component::DataZoom::new()
.type_(component::DataZoomType::Inside)
.start(0)
.end(100),
)
.tooltip(charming::element::Tooltip::new().trigger(charming::element::Trigger::Axis))
.toolbox(
component::Toolbox::new().feature(
component::Feature::new()
.data_zoom(component::ToolboxDataZoom::new().y_axis_index("none"))
.restore(component::Restore::new())
.save_as_image(component::SaveAsImage::new()), // .brush(Brush::new().type_(vec![BrushType::LineX, BrushType::Clear])),
),
)
.grid(
Grid::new()
.left("7%")
.top("7%")
.width("38%")
.height("38%")
.contain_label(true),
)
.grid(
Grid::new()
.right("7%")
.top("7%")
.width("38%")
.height("38%")
.contain_label(true),
)
.grid(
Grid::new()
.left("7%")
.bottom("7%")
.width("38%")
.height("38%")
.contain_label(true),
)
.grid(
Grid::new()
.right("7%")
.bottom("7%")
.width("38%")
.height("38%")
.contain_label(true),
)
.x_axis(Axis::new().grid_index(0).min(1))
.x_axis(Axis::new().grid_index(1).min(1))
.x_axis(Axis::new().grid_index(2).min(1))
.x_axis(Axis::new().grid_index(3).min(1))
.y_axis(Axis::new().grid_index(0).min(0))
.y_axis(Axis::new().grid_index(1).min(0))
.y_axis(Axis::new().grid_index(2).min(0))
.y_axis(Axis::new().grid_index(3).min(0))
}