所以我的任务是创建 MySQL 表中的数据图(大约 41 个数据,7 行)。我只做了一个基本的图表......所以我什至可以使用 perl 脚本使用 MySQL 表中的数据创建一个图表。
抱歉没有编码,因为我什至不知道如何使用 MySQL 数据创建 perl 图
编辑
我试着做图表,但似乎数据没有显示出来。它只显示了一个空图,并且由于某种原因这些值以负数开头......我做错了什么吗?
我的 sql 表
create table Top_1 (
CPU_User float, CPU_System float, CPU_Waiting float);
我的脚本
#!/usr/bin/perl
use DBI;
use warnings;
use strict;
use autodie;
use Data::Dumper;
use GD::Graph::bars;
use GD::Graph::Data;
my $username = "root";
my $password = "";
my $db = "Top_Data_1";
my $host = "127.0.0.1";
my $port = "3306";
my $dsn = "DBI:mysql:database=$db;host=$host;port=$port";
my %attr = (PrintError=>0,RaiseError=>1 );
my $dbh = DBI->connect($dsn,$username,$password,\%attr) or die $DBI::errstr;
my $sth = $dbh->prepare('CPU_User, CPU_System, CPU_Waiting from Top_1');
$sth->execute();
my @row;
while ( @row = $sth->fetchrow_array) {
print "this is CPU_User\n";
print "$row[0]\n";
print "this is CPU_System \n";
print "$row[1]\n";
print "this is CPU_Waiting \n";
print "$row[2]\n";
}
my $data = GD::Graph::Data->new([
["8 am","10 pm","12 pm"],
['$row[0]'],
['$row[1]'],
['$row[2]'],
]) or die GD::Graph::Data->error;
my $graph = GD::Graph::bars->new;
$graph->set(
x_label => 'File_Name',
y_label => 'Value',
title => 'TOP CPU DISPLAY',
x_labels_vertical => 1,
bar_spacing => 10,
bar_width => 3,
long_ticks => 1,
) or die $graph->error;
$graph->set_legend_font(GD::gdMediumBoldFont);
$graph->set_legend('CPU USER','CPU_System','CPU_Waiting');
$graph->plot($data) or die $graph->error;
my $file = 'bars.png';
print "Your Picture Has Been Added To Your Directory\n";
open(my $out, '>', $file) or die "Cannot open '$file' for write: $!";
binmode $out;
print $out $graph->gd->png;
close $out;
$sth->finish();
$dbh->disconnect();