Skip to content

Commit

Permalink
替换图形库为chart-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
xml committed Apr 10, 2024
1 parent 9d58318 commit 656ca55
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 93 deletions.
1 change: 0 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
134 changes: 43 additions & 91 deletions src/visualization.rs
Original file line number Diff line number Diff line change
@@ -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<std::fs::File>) -> (i32,i32,i32,i32,Vec<Vec<(i32, i32)>>) {
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::<i32>().unwrap();
for i in 1..record.len() {
let y = record[i].parse::<i32>().unwrap();
datas[i-1].push((record[0].parse::<i32>().unwrap(), record[i].parse::<i32>().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::<i32>().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<dyn std::error::Error>> {
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(())
}

0 comments on commit 656ca55

Please sign in to comment.