#![allow(clippy::expect_used, clippy::unwrap_used)]
use trueno_viz::output::PngEncoder;
use trueno_viz::plots::{Heatmap, HeatmapPalette};
use trueno_viz::prelude::WithDimensions;
fn main() {
println!("Heatmap Correlation Matrix Example");
println!("===================================\n");
println!("Step 1: Creating correlation matrix...");
let (matrix, labels) = create_correlation_matrix();
println!(" Matrix size: {}x{}", labels.len(), labels.len());
println!(" Variables: {labels:?}");
println!("\nStep 2: Building heatmap...");
let heatmap = Heatmap::new()
.data_2d(&matrix)
.palette(HeatmapPalette::RedBlue) .dimensions(600, 600)
.margin(40)
.borders(true)
.build()
.expect("Failed to build heatmap");
println!(" Cells: {}", heatmap.cell_count());
println!(" Palette: RedBlue (diverging)");
println!("\nStep 3: Rendering...");
let fb = heatmap.to_framebuffer().expect("Failed to render");
println!(" Framebuffer: {}x{}", fb.width(), fb.height());
println!("\nStep 4: Saving to PNG...");
let output_path = "heatmap_correlation.png";
PngEncoder::write_to_file(&fb, output_path).expect("Failed to write PNG");
println!(" Saved to: {output_path}");
println!("\n--- Correlation Matrix ---");
print!(" ");
for label in &labels {
print!("{label:>8}");
}
println!();
for (i, row) in matrix.iter().enumerate() {
print!("{:>8}", labels[i]);
for &val in row {
print!("{val:>8.2}");
}
println!();
}
println!("\nHeatmap successfully generated!");
}
fn create_correlation_matrix() -> (Vec<Vec<f32>>, Vec<&'static str>) {
let labels = vec!["Height", "Weight", "Age", "Income", "Score"];
let matrix = vec![
vec![1.00, 0.85, 0.12, 0.23, 0.15], vec![0.85, 1.00, 0.18, 0.31, 0.22], vec![0.12, 0.18, 1.00, 0.45, -0.35], vec![0.23, 0.31, 0.45, 1.00, 0.67], vec![0.15, 0.22, -0.35, 0.67, 1.00], ];
(matrix, labels)
}