From 656ca551b8ebed42cfb2b4123d7926ff2a111474 Mon Sep 17 00:00:00 2001 From: xml Date: Wed, 10 Apr 2024 17:16:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E5=9B=BE=E5=BD=A2=E5=BA=93?= =?UTF-8?q?=E4=B8=BAchart-rs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/rust.yml | 1 - Cargo.toml | 3 +- src/visualization.rs | 134 ++++++++++++------------------------- 3 files changed, 45 insertions(+), 93 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 848d304..7d83616 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -35,7 +35,6 @@ jobs: # Create an result directory setup: | - echo "pwd=${PWD}" mkdir -p "${PWD}/result" # Mount the result directory as /result in the container diff --git a/Cargo.toml b/Cargo.toml index 7bf1be6..b2cd02c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ walkdir = "2" regex = "1" clap = { version = "4.4", features = ["derive"]} csv = "1.3" -plotters = { version = "0.3.5"} +#plotters = { version = "0.3.5"} +charts-rs = "0.3.5" diff --git a/src/visualization.rs b/src/visualization.rs index 204bfff..5702a03 100644 --- a/src/visualization.rs +++ b/src/visualization.rs @@ -1,103 +1,55 @@ -use plotters::prelude::*; +//use plotters::prelude::*; +use charts_rs::{Box, LineChart, Series}; +use std::io::Write; -const COLORS:[RGBColor;20] = [ - RGBColor(0xd2, 0x69, 0x1e), - RGBColor(0x64, 0x95, 0xed), - RGBColor(0x88, 0x14, 0x00), - RGBColor(0xff, 0x00, 0xff), - RGBColor(0xfe, 0xee, 0xed), - - RGBColor(0xf0, 0x5b, 0x72), - RGBColor(0xca, 0x86, 0x87), - RGBColor(0xed, 0x19, 0x41), - RGBColor(0x90, 0x5a, 0x3d), - RGBColor(0xfa, 0xa7, 0x55), - - RGBColor(0xe0, 0x86, 0x1a), - RGBColor(0xb7, 0xba, 0x6b), - RGBColor(0x7f, 0xb8, 0x0e), - RGBColor(0x19, 0xd5, 0x3f), - RGBColor(0x50, 0xb7, 0xc1), - - RGBColor(0x44, 0x46, 0x93), - RGBColor(0x85, 0x52, 0xa1), - RGBColor(0xea, 0x66, 0xa6), - RGBColor(0x7f, 0xff, 0x00), - RGBColor(0xff, 0x7f, 0x50), -]; - -fn datas(rdr: &mut csv::Reader) -> (i32,i32,i32,i32,Vec>) { - let mut min_x = -1; - let mut min_y = -1; - let mut max_x = 0; - let mut max_y = 0; - let mut datas = Vec::new(); +// cpufreq.csv -> cpufreq.svg +// capacity.csv -> capacity.svg +pub fn show_datas(infile: &str, outfile: &str, desc: &str) -> std::io::Result<()> { + let mut rdr = csv::Reader::from_path(infile)?; + let mut series_list = Vec::new(); + let mut x_axis_data = Vec::new(); + let mut record_index = 0; for result in rdr.records() { let record = result.unwrap(); - if record.position().unwrap().line() == 1 { - continue; - } - - if record.len() -1 > datas.len() { - datas.resize(record.len() -1, Vec::new()); + if record_index == 0 { + println!("find head."); + let mut index = 0; + for r in record.into_iter() { + if index > 0 { + let tag = r.to_string(); + series_list.push(Series::new(tag, Vec::new())); + println!("insert head."); + } + index = index + 1; + } } - let x = record[0].parse::().unwrap(); - for i in 1..record.len() { - let y = record[i].parse::().unwrap(); - datas[i-1].push((record[0].parse::().unwrap(), record[i].parse::().unwrap())); - if min_x < 0 { - min_x = x; - } - if min_y < 0 { - min_y = y; - } - if x > max_x { - max_x =x; - } else if x < min_x { - min_x = x; - } - - if y > max_y { - max_y = y; - } else if y < min_y { - min_y = y; + for i in 0..record.len() { + if i == 0 { + x_axis_data.push( record[i].to_string()); + } else { + series_list[i - 1].data.push(record[i].parse::().unwrap() as f32); + series_list[i - 1].label_show = true } } - } - - let mut v = (max_y - min_y) / 5; - if v <= 0 { - v = 10; - } - min_y = min_y - v; - max_y = max_y + v; - (min_x, max_x, min_y, max_y, datas) -} - -// cpufreq.csv -> cpufreq.svg -// capacity.csv -> capacity.svg -pub fn show_datas(infile: &str, outfile: &str, desc: &str) -> Result<(), Box> { - let mut rdr = csv::Reader::from_path(infile)?; - let (min_x, max_x, min_y, max_y, datas) = datas(&mut rdr); - - let root = SVGBackend::new(outfile, (1920, 1080)).into_drawing_area(); - root.fill(&WHITE)?; - let mut chart = ChartBuilder::on(&root) - .caption(desc, ("sans-serif", 40)) - .set_label_area_size(LabelAreaPosition::Left, 50) - .set_label_area_size(LabelAreaPosition::Bottom, 50) - .build_cartesian_2d(min_x..max_x, min_y..max_y)?; - chart.configure_mesh().draw()?; - - let mut index = 0; - for data in datas { - chart.draw_series(LineSeries::new(data, COLORS[/*index % 20*/6]).point_size(5))?; - index = index + 1; + record_index = record_index + 1; + } + let mut line_chart = LineChart::new(series_list, x_axis_data); + line_chart.title_text = "Stacked Area Chart".to_string(); + line_chart.sub_title_text = "Hello World".to_string(); + line_chart.legend_margin = Some(Box { + top: 50.0, + bottom: 10.0, + ..Default::default() + }); + line_chart.width = 1920.0; + line_chart.height = 1080.0; + + if let Ok(svg_data) = line_chart.svg() { + let mut buffer = std::fs::File::create(outfile)?; + buffer.write_all(svg_data.as_bytes())?; } - - root.present()?; Ok(()) } \ No newline at end of file