Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How to calculate the column and print it in pie chart format using perl?

by finddata (Sexton)
on Mar 22, 2017 at 04:37 UTC ( #1185400=perlquestion: print w/replies, xml ) Need Help??

finddata has asked for the wisdom of the Perl Monks concerning the following question:

I have the following mysql table which obtained by following query(sel +ect * from tables); <code> | time | queue_name | description + | priority | jobs_pending | jobs_running | | 1490155563 | adice_long | ADICE Low-Priority/Long Runtime Job Q +ueue | 5 | 6 | 3 + + | 1490155563 | adice | ADICE +Low-Priority/Long Runtime Job Queue | 8 | 9 + | 3 + + + + + + | | 1490155563 | incisive | ADICE-Incisive Co-Simulation Queue + | 7 | 0 | | 1490155563 | vcs | ADICE High-Priority/Short Runtime Job + Queue | 10 | 0 | | +
My query is i want to convert the above table into pie chart format . i should built two piechart one is for jobs pending and jobs running by matching it with queue name. If the queue name is available twice in the column we should calculate those two rows of jobs pending and jobs running and make them into single. code which i had tried are as follows:
print "var data_running=[[\"Adice \($TABLE_DATA{1}{'ENTRY'}\)\",$TABLE +_DATA{5}{'ENTRY'}],\ [\"Incisive \($TABLE_DATA{1}{'ENTRY'}\)\",$TABLE_ +DATA{5}{'ENTRY'}],\ [\"Vcs \($TABLE_DATA{1}{'ENTRY'}\)\",$TABLE_DATA{ +5}{'ENTRY'}], \n"; print "var data_pending=[[\"Adice \($TABLE_DATA{1}{'ENT +RY'}\)\",$TABLE_DATA{4}{'ENTRY'}],\ [\"Incisive \($TABLE_DATA{1}{'ENTRY'}\)\",$TABLE_ +DATA{4}{'ENTRY'}],\ [\"Vcs \($TABLE_DATA{1}{'ENTRY'}\)\",$TABLE_DATA{ +4}{'ENTRY'}],\n"; +
print " \n + pieChart(\"placeholder39\",data_running,{title:\"<b><center>Jobs Run +ning</center></b>\"});\n"; print " \n pieChart(\"placeholder41\",data_pending,{title:\"<b>< +center>Jobs Pending</center></b>\"});\n";
But am not sure am in right way to do it. |

Replies are listed 'Best First'.
Re: How to calculate the column and print it in pie chart format using perl?
by marto (Archbishop) on Mar 22, 2017 at 10:03 UTC

    You don't specifically mention it, but by the look of some of your questions you're building a web interface, now with JavaScript charting. I suggest you make life easy for yourself and use an existing web framework, such as Mojolicious::Lite and an appropriate JavaScript charting solution which can read JSON data. The Mojolicious documentation is great, your time would be wel spent addressing the basics, which are perl and web skills that are framework agnostic.

Re: How to calculate the column and print it in pie chart format using perl?
by huck (Parson) on Mar 22, 2017 at 05:27 UTC

    It looks like you are writing commands that another program will use to create the pie chart. What program is that?

    How are you accessing the mysql database from perl and storing the data you get back from the select statement?

      am using perl cgi script.Here i had added the part of the code.
        sub print_overall_summary { my $str = shift; $DBH = &connect or die "Cannot connect to the sql server \n"; $DBH->do("USE $str;"); my $stmt="select distinct * from summary order by time desc ;"; ; my $sth = $DBH->prepare( $stmt ); $sth->execute() or print "Could not in7sert_overall_summarydata"; print "<script language=\"javascript\" type=\"text/javascript\"> \ +n"; print "\$(document).ready(function(){ \n"; %TABLE_DATA =(); print "var data_running=[[\"Adice \($TABLE_DATA{1}{'ENTRY'}\)\",$TABLE +_DATA{5}{'ENTRY'}],\ [\"Incisive \($TABLE_DATA{1}{'ENTRY'}\)\",$TABLE_ +DATA{5}{'ENTRY'}],\ [\"Vcs \($TABLE_DATA{1}{'ENTRY'}\)\",$TABLE_DATA{ +5}{'ENTRY'}], \n"; print "var data_pending=[[\"Adice \($TABLE_DATA{1}{'ENT +RY'}\)\",$TABLE_DATA{4}{'ENTRY'}],\ [\"Incisive \($TABLE_DATA{1}{'ENTRY'}\)\",$TABLE_ +DATA{4}{'ENTRY'}],\ [\"Vcs \($TABLE_DATA{1}{'ENTRY'}\)\",$TABLE_DATA{ +4}{'ENTRY'}],\n"; + [download] print " \n + pieChart(\"placeholder39\",data_running,{title:\"<b><center>Jobs Run +ning</center></b>\"});\n"; print " \n pieChart(\"placeholder41\",data_pending,{title:\"<b>< +center>Jobs Pending</center></b>\"});\n"; [download] print " \n pieChart(\"placeholder30\",data_machine,{title:\"<b>< +center>Machine Status<center></b>\"});"; print " \n pieChart(\"placeholder29\",data_cpu,{title:\"<b><cent +er>CPU Slots</center></b>\"});\n"; print " \n pieChart(\"placeholder39\",data_running,{title:\"<b>< +center>Jobs Running</center></b>\"});\n"; print " \n pieChart(\"placeholder41\",data_pending,{title:\"<b>< +center>Jobs Pending</center></b>\"});\n"; print "});"; print "</script>"; }
      <head> <link rel="stylesheet" type="text/css" href="../lib/DataTables/med +ia/css/jquery.dataTables.css"> <link rel="stylesheet" type="text/css" href="../lib/Dat +aTables/examples/resources/syntax/shCore.css"> <script language="javascript" type="text/javascript" src="../l +ib/flot/current/jquery.js"></script> <script type="text/javascript" language="javascript" src="../l +ib/DataTables/media/js/jquery.dataTables.js"></script> <script type="text/javascript" language="javascript" src="../l +ib/DataTables/examples/resources/syntax/shCore.js"></script> <script type="text/javascript" language="javascript" src="../l +ib/DataTables/examples/resources/demo.js"></script> <script type="text/javascript" language="javascript" src="../l +ib/DataTables/extensions/ColVis/js/dataTables.colVis.js"></script> + <script type="text/javascript" language="javascript" src="../l +ib/DataTables/extensions/TableTools/js/dataTables.tableTools.js"></sc +ript> <script language="javascript" type="text/javascript" src=" . +./lib/flot/current/jquery.flot.js"></script> <!-- <script language="javascript" type="text/javascript" src= +" ../lib/flot/current/demo_table.css"></script> --> <script language="javascript" type="text/javascript" +src=" ../lib/new_style.css"></script> <script language="javascript" type="text/javascript" src=" .. +/lib/flot/current/jquery.flot.categories.js"></script> <script language="javascript" type="text/javascript" src=" .. +/lib/flot/current/jquery.flot.pie.js"></script> <script language="javascript" type="text/javascript" src=" .. +/lib/flot/current/jquery.flot.stack.js"></script> <script language="javascript" type="text/javascript" src=" ../ +lib/flot/current/jquery.flot.axislabels.js"></script> <script language="javascript" type="text/javascript" src=" .. +/lib/flot/current/jquery.flot.resize.js"></script> <script language="javascript" type="text/javascript" src=" .. +/lib/flot/current/jquery.flot.tooltip.js"></script> <script language="javascript" type="text/javascript" src=" .. +/lib/flot/current/jquery.flot.navigate.js "></script> <script language="javascript" type="text/javascript" src=" .. +/lib/flot/current/date.js"></script> <script language="javascript" type="text/javascript" src=" .. +/lib/flot/current/jquery.dataTables.min.js"></script> <script language="javascript" type="text/javascript" src=" .. +/lib/flot/current/jquery.validate.js"></script> <script language="javascript" type="text/javascript" src="../l +ib/flot_test.js"></script> <script language="javascript" type="text/javascript" src="../l +ib/flot.js"></script> <link rel="stylesheet" type="text/css" href="../lib/stylesheet.css +"> <link rel="stylesheet" type="text/css" href="../lib/DataTables +/media/css/jquery.dataTables.css"> <link rel="stylesheet" type="text/css" href="../lib/DataTables/exa +mples/resources/syntax/shCore.css"> <!-- <link rel="stylesheet" type="text/css" href=" ../lib/mysty +le.css">--> <!-- <link rel="stylesheet" type="text/css" href="../lib/DataTa +bles/extensions/ColVis/css/dataTables.colVis.css"> <link rel="stylesheet" type="text/css" href="../lib/DataTables +/extensions/TableTools/css/dataTables.tableTools.css"> --> <!-- <link rel="stylesheet" type="text/css" href="../etc/css/st +yles.css" /> --> <script type="text/javascript" language="javascript" src="../l +ib/DataTables/examples/resources/syntax/shCore.js"></script> <script type="text/javascript" language="javascript" src="../l +ib/DataTables/examples/resources/demo.js"></script> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Expires" CONTENT="-1"> </head>

        since you are serving it from your website that seems to be some wort of homebrew javascript package. As such we will be of little use in helping you prepare the statements that will properly call it. Possibly somebody else at work can help you with that

        in the meantime you can work on getting your input data prepared correctly. From what you have shown %TABLE_DATA is empty.

        Each time you reference $TABLE_DATA{5}{'ENTRY'} it will have the same data, it makes no difference if the label is Vcs or Adice. possibly you meant $TABLE_DATA{5}{'Vcs'} instead. what would even make more sense would be $TABLE_DATA{'jobs_running'}{'Vcs'}. we once showed you a way to translate the data returned by  my $table = $sth->fetchall_arrayref; into data of that form.

        Because i would like this exercise in futility to end quicker i propose something like

        sub print_overall_summary { my $str = shift; $DBH = &connect or die "Cannot connect to the sql server \n"; $DBH->do("USE $str;"); my $stmt="select queue_name,jobs_pending,jobs_running from summary o +rder by time desc ;"; ; my $sth = $DBH->prepare( $stmt ); $sth->execute() or print "Could not prepare_overall_summarydata"; print "<script language=\"javascript\" type=\"text/javascript\"> \n" +; print "\$(document).ready(function(){ \n"; %TABLE_DATA =(); my @sys=qw/Adice Incisive Vcs other/; my $table = $sth->fetchall_hashref; for my $r (@$table) { my $name='other'; for my $s (@sys){ if ($r->{queue_name)=~m/$s/i) {$name=$s; last;} } $TABLE_DATA{$name}{jobs_pending}+=$r->{jobs_pending}; $TABLE_DATA{$name}{jobs_running}+=$r->{jobs_running}; } for my $var(qw/running pending/) { my @parts=(); for my $s (@sys){ push @parts,'["'.$s.'",' .$TABLE_DATA{$s}{'jobs_'.$var} .']'; } print 'var data_'.$var.'=['.join(',',@parts).']'."\n"; } print " \n pieChart(\"placeholder39\",data_running,{title:\"<b><ce +nter>Jobs Running</center></b>\"});\n"; print " \n pieChart(\"placeholder41\",data_pending,{title:\"<b><ce +nter>Jobs Pending</center></b>\"});\n"; print "});"; print "</script>"; }
        Untested, if there are syntax errors it s up to you to fix them.

        notice you have not made any variables for data_machine or data_cpu so those piecharts would fail.

        You need to talk to your boss about how these tasks are way beyond your abilities even though they are so simple. And how you need to get others to do your work for you.

        Or you could make and effort to learn perl instead

Re: How to calculate the column and print it in pie chart format using perl?
by duyet (Friar) on Mar 22, 2017 at 07:02 UTC

    Have a look at Google charts! Read the API and examples ...

    Retrieve the data from database or where ever it is. As long as you can feed your data to it, you will get your chart.

      Have a look at Google charts! Read the API and examples

      Disadvantages:

      • Internet Connection required
      • Javascript required
      • Javascript code dyamically loaded from the internet
      • A lot of trust in Google required

      To explain the last point: You have to load Javascript code from Google at runtime, which loads even more Javascript code from Google and NodeReaper knows from where else. That code has the potential to upload all of the data you pass to the API functions. Google can change the implementation of the Javascript code at any time - they just promise backwards compatibiliy for some time.

      "My data is not important, it will hide in the background noise." That background noise is well-analysed and sold by Google / Alphabet. They make money from it. A lot of money. So much money that they can afford developing self-driving cars (from scratch!) and many other things.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Maybe we can be driven to work someday :) Don't use it if you don't trust it :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1185400]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2020-01-21 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?