如何将 mysql 数据插入 perl 图表(已编辑)
How to insert mysql data into perl graph (Editted)
所以我的任务是创建 MySQL table 内的数据图表(大约 41 条数据,7 行)。我只做了一个基本图表...所以我什至可以使用 perl 脚本使用 MySQL table 中的数据创建图表。
抱歉没有编码,因为我什至不知道如何使用 MySQL 数据
创建 perl 图
编辑
我试着做图表,但数据似乎没有显示出来。它只显示了一个空图,并且出于某种原因,值以负数开头...我做错了什么吗?
我的sqltable
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();
这是可以做到的。可以使用 DBI 从 MySQL 获取数据。然后,您可以将数据排列成一种格式,以便使用图形库将其图形化。我用过GD::Graph,很好用
链接:
MySQL: https://metacpan.org/pod/DBD::mysql
图:https://metacpan.org/pod/GD::Graph
数据结构不正确。
不确定你的数据库 table,但如果你有时间字段,那么像下面这样的东西应该可以工作。
my $data = GD::Graph::Data->new();
my $sth = $dbh_mysql->prepare('SELECT hour, CPU_USER, CPU_System, CPU_Waiting FROM top1');
$sth->execute();
while (my @row = $sth->fetchrow_array)
{
$data->add_point(@row);
}
所以我的任务是创建 MySQL table 内的数据图表(大约 41 条数据,7 行)。我只做了一个基本图表...所以我什至可以使用 perl 脚本使用 MySQL table 中的数据创建图表。
抱歉没有编码,因为我什至不知道如何使用 MySQL 数据
创建 perl 图编辑
我试着做图表,但数据似乎没有显示出来。它只显示了一个空图,并且出于某种原因,值以负数开头...我做错了什么吗?
我的sqltable
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();
这是可以做到的。可以使用 DBI 从 MySQL 获取数据。然后,您可以将数据排列成一种格式,以便使用图形库将其图形化。我用过GD::Graph,很好用
链接: MySQL: https://metacpan.org/pod/DBD::mysql 图:https://metacpan.org/pod/GD::Graph
数据结构不正确。
不确定你的数据库 table,但如果你有时间字段,那么像下面这样的东西应该可以工作。
my $data = GD::Graph::Data->new();
my $sth = $dbh_mysql->prepare('SELECT hour, CPU_USER, CPU_System, CPU_Waiting FROM top1');
$sth->execute();
while (my @row = $sth->fetchrow_array)
{
$data->add_point(@row);
}