Snippets
Created by
Max Pietsch
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 | <?php
/*******************************************************************************************
Please see http://maxwell9.myweb.usf.edu/msdb.php for a demonstration of the output of this file.
This file generates an image using gnuplot based on user database queries (i.e. does the user want a 2d or 3d image? animated or not animated? what data to be plotted on which axis?)
This file was written by Maxwell Pietsch at the University of South Florida while working for Dr. Yicheng Tu. If you wish to re-use it, please mention you got it from us.
********************************************************************************************/
/***********************************************************************************
* This file is splint into
* 1) The base class for the gnuplot functionality
* 1.5) twod - base class for twodpng and twodgif
* 2) twodpng
* 3) twodgif - the class for plotting gif (animated) files in two dimensions
* 3.5) threed - base class for threedpng and threedgif
* 3.7) threedpng - 3d png's
* 4) threedgif - the class for plotting gif (animated) files in three dimensions
***************************************************************************************/
/***************************************************************************************
Debugging tip:
// The data files, with names like point231510.data and line231510.data, and the gnuplot script,
// with a name like gnuplot_script231510.glp, are written to disc by calls to file_put_contents().
// Viewing these files can be helpful in debugging (or noting that they are not being output at all).
// You can also run the gnuplot script with a command from the terminal like, "gnuplot gnuplot_script231510.glp"
// Doing so will yield additional debugging messages from the gnuplot program, if the error is within the gnuplot script.
// Because outputting to disc helps in debugging in this way, I chose not to execute the gnuplot commands directly from script
// for example by using the function popen() as Liu Yi does in A PHP Interface to GNU Plot or using the -e option in a command like
// shell_exec("gnuplot -e \"outputname='$gnuplot_gif_file'; point_data='$gnuplot_point_file'; zticlength='$zticlength';\"")
// though that approach will work if for some reason you do not wish to output the gnuplot commands to disc before executing them
*****************************************************************************************/
class gnuplot{
var $gnuplot_id;
// $num_of_tics will determine the number of tics on the axes
var $num_of_tics;
var $x_label, $y_label, $z_label;
// we choose what column we want to plot on the x, y, and z axis by setting x_axis, y_axis, z_axis
var $x_axis, $y_axis, $z_axis;
// $gnuplot_script is the string of gnuplot commands that will be written to disc
var $gnuplot_script;
// these are the file locations for the temporary files we wish to write to disc
var $gnuplot_script_file_location, $output_file_location;
// $line_data is the string of data points. when written to $line_data_location it will be in the row/column form gnuplot understands.
var $line_data_location, $line_data;
// $title is the graph title
var $title;
var $has_entry, $sample_num;
var $one_max, $one_min, $two_max, $two_min, $three_max, $three_min;
// we can set sample_max if we wish to limit the number of samples the user takes. I don't employ this, actually, because
// I ended up loading a smaller png to the webpage while the slower process creating a gif file takes place.
var $sample_max;
// the label used in the key within the gnuplot image (only animated images have a key at this point in time)
var $key_name;
function __construct(){
// we start with zero data points, where $this->has_entry is false.
$this->has_entry = false;
// Javascript generates a random number and php appends it to file names
// so we can differentiate between files and don't load the wrong one or write over one before it's used.
if (isset($_POST['random_num']) && is_numeric($_POST['random_num']))
$this->gnuplot_id = $_POST['random_num'];
else
$this->gnuplot_id = rand();
$this->gnuplot_script_file_location = $_SERVER['DOCUMENT_ROOT'] . "/gnuplot_tmp_files/gnuplot_script" . $this->gnuplot_id . ".glp";
$this->line_data_location = $_SERVER['DOCUMENT_ROOT'] . "/gnuplot_tmp_files/line" . $this->gnuplot_id . ".data";
// we could have a .png or .gif at the end of $this->output_file_location
// but if we did then we'd have to have the code that loads the file handle both .png or .gif
// depending on the query. This would take more code and just isn't necessary for it to work,
// though it might be easier during debugging to have a .gif or .png so the OS can figure out what program to open the image with.
$this->output_file_location = $_SERVER['DOCUMENT_ROOT'] . "/msdb/queries/gnuplot_tmp_files/gnuplot_output" . $this->gnuplot_id;
$this->sample_count = 0;
// set sample_max to whatever value you want. using an arbitrary large number here so we never reach a sample limit.
$this->sample_max = 10000000000000;
$this->line_data = "";
// number of tics on the X, Y, Z axes
// note that this is not actually the number of tics! it can be used as a rough estimate of the number of tics, though
// i.e. the higher $this->num_of_tics, then higher the actual number of tics, though not one-to-one!
$this->num_of_tics = 4;
// the desired number of decimal places in our tics along the axes
$this->desired_precision = 2;
}
//
public function set_key_name($name){
$this->key_name = $name;
}
// adds data point. called by record_data
protected function add_line_data ($one, $two, $three){
// we only want to add a sample data point if we're under an amount defined by $this->sample_max
if ($this->sample_count <= $this->sample_max){
$this->line_data .= "$one $two $three\r\n";
}
}
protected function add_point_data ($one, $two, $three){
// we only want to add a sample data point if we're under an amount defined by $this->sample_max
if ($this->sample_count <= $this->sample_max){
$this->point_data .= "$one $two $three\r\n\r\n\r\n";
}
}
protected function write_line_data(){
// the line_data file will be used by gnuplot to create an image and be available to the user for download
$this->line_data = "#$this->title query data organized by column\r\n" . $this->line_data;
file_put_contents($this->line_data_location, $this->line_data);
}
protected function write_point_data(){
$this->point_data = "#$this->title query data organized by column\r\n"
. "#this file has two spaces between points so gnuplot can use it to generate GIFs\r\n#1 2 3\r\n" . $this->point_data;
file_put_contents($this->point_data_location, $this->point_data);
}
protected function track_max_min($one, $two, $three){
// this if / else will allow us to keep track of the min / max of our data
// from the max and min we can calculate the range or tics on the axis
if ($this->has_entry)
{
if ($one < $this->one_min)
$this->one_min = $one;
if ($one > $this->one_max)
$this->one_max = $one;
if ($two < $this->two_min)
$this->two_min = $two;
if ($two > $this->two_max)
$this->two_max = $two;
if ($three < $this->three_min)
$this->three_min = $three;
if ($three > $this->three_max)
$this->three_max = $three;
}
// else if there has not yet been a data entry
else if (!$this->has_entry)
{
$this->has_entry = true;
$this->one_min = $one;
$this->one_max = $one;
$this->two_min = $two;
$this->two_max = $two;
$this->three_min = $three;
$this->three_max = $three;
}
}
public function set_title($title){
$this->title = $title;
}
// this function determines how many data points we'll have in our gnuplot script
public function set_max_samples($sample_max)
{
// basic input validation
if (is_numeric($sample_max) && $this->sample_count <= $sample_max && $sample_max >= 0)
$this->sample_max = $sample_max;
}
public function set_x_label($label){
$this->x_label = $label;
}
public function set_y_label($label){
$this->y_label = $label;
}
public function set_z_label($label){
$this->z_label = $label;
}
public function set_x_axis($column_num){
$this->x_axis = $column_num;
}
public function set_y_axis($column_num){
$this->y_axis = $column_num;
}
public function set_z_axis($column_num){
$this->z_axis = $column_num;
}
// helper function for our set_tics functions
protected function find_tic_length($max, $min){
// we want to check if max != min because if they are equal and we subtract them, we'll get zero for tic_length, which will give us errors down the road
if ($max != $min)
$tic_length = (float)($max - $min) / $this->num_of_tics;
else
$tic_length = (float) $max / $this->num_of_tics;
return $this->custom_round($tic_length);
}
// if we used the built-in PHP round function, for instance like, "$f = round(0.0001, 2)"
// then $f would equal 0. If we then had a gnuplot command "set xtics $f" i.e. "set xtics 0" then we would get an error
// so instead we have a custom round function that tries to get the number of decimal places as close to $this->desired_precision
// as possible without letting our $f equal zero
protected function custom_round($tic_length){
$n = $this->desired_precision;
$xtics = round($tic_length, $n);
while (!$xtics)
{
$xtics = round($tic_length, $n);
$n = $n+1;
}
return $xtics;
}
// when one of the axes is a frame then we have a different find_tic_length function for it
protected function find_tic_length_for_frames(){
$tic_length = (float) $this->sample_count / $this->num_of_tics;
return $this->custom_round_frames($tic_length);
}
// generally, the frame number will be an integer, which we'd like, so a custom function to find the distance between tics on an axis with frame # values won't be necessary
// i.e. the following function won't be necessary
// but, there is the case where the user selects start frame equal zero and last frame equal 1. in that case we need to have frame numbers with one decimal place of accuracy
// or else we get problems with gnuplot where we're setting tic length equal to zero
protected function custom_round_frames($tic_length){
$n = 0; // we prefer 0 decimal places for our frame count #
$xtics = round($tic_length, $n);
while (!$xtics)
{
$xtics = round($tic_length, $n);
$n = $n+1;
}
return $xtics;
}
protected function set_xtics(){
switch ($this->x_axis){
case 0: // x-axis has frame #'s
$this->xtics_string = "set xtics " . $this->find_tic_length_for_frames();
break;
case 1: // along x-axis we have column 1 data. find distance between tics based on number of tics desired and distance between column min and column max
$this->xtics_string = "set xtics " . $this->find_tic_length($this->one_max, $this->one_min);
break;
case 2: // along x-axis we have column 2 data. find distance between tics based on number of tics desired and distance between column min and column max
$this->xtics_string = "set xtics " . $this->find_tic_length($this->two_max, $this->two_min);
break;
case 3:
$this->xtics_string = "set xtics " . $this->find_tic_length($this->three_max, $this->three_min);
break;
}
}
protected function set_ytics(){
switch ($this->y_axis){
case 0: // y-axis has frame #'s
$this->ytics_string = "set ytics " . $this->find_tic_length_for_frames();
break;
case 1: // along y-axis we have column 1 data. find distance between tics based on number of tics desired and distance between column min and column max
$this->ytics_string = "set ytics " . $this->find_tic_length($this->one_max, $this->one_min);
break;
case 2: // along y-axis we have column 2 data. find distance between tics based on number of tics desired and distance between column min and column max
$this->ytics_string = "set ytics " . $this->find_tic_length($this->two_max, $this->two_min);
break;
case 3:
$this->ytics_string = "set ytics " . $this->find_tic_length($this->three_max, $this->three_min);
break;
}
}
protected function set_ztics(){
switch ($this->z_axis){
case 0: // z-axis has frame #'s
$this->ztics_string = "set ztics " . $this->find_tic_length_for_frames();
break;
case 1: // along z-axis we have column 1 data. find distance between tics based on number of tics desired and distance between column min and column max
$this->ztics_string = "set ztics " . $this->find_tic_length($this->one_max, $this->one_min);
break;
case 2: // along z-axis we have column 2 data. find distance between tics based on number of tics desired and distance between column min and column max
$this->ztics_string = "set ztics " . $this->find_tic_length($this->two_max, $this->two_min);
break;
case 3:
$this->ztics_string = "set ztics " . $this->find_tic_length($this->three_max, $this->three_min);
break;
}
}
protected function record_png_data ($one, $two, $three){
$this->sample_count++; // increase the number of samples we've taken by 1
// it's important that we include columns 1, 2 and 3 in the call to add_line_data, even for 2-d plots
// because the line data doubles as a data file that the user can download
$this->add_line_data($one, $two, $three);
$this->track_max_min($one, $two, $three);
}
protected function record_gif_data ($one, $two, $three){
$this->sample_count++; // increase the number of samples we've taken by 1
// the 3 dimensional gif needs both line and point data files for gnuplot
$this->add_line_data($one, $two, $three);
$this->add_point_data($one, $two, $three);
$this->track_max_min($one, $two, $three);
}
}
class twod extends gnuplot {
protected function set_tics(){
$this->set_xtics();
$this->set_ytics();
}
protected function export_2dpng() {
// create the text for the gnuplot_script
// note that if we have an indent in this text then there is an indent in the script file (which is why it's aligned so funny-looking here)
$this->gnuplot_script = "set title \"$this->title\"
#set to create a png file. size (width), (height) determines gif size.
set terminal png enhanced size 600, 600
set output '$this->output_file_location'
set xlabel '$this->x_label'
set ylabel '$this->y_label'
set nokey
#\"set xtics\" determines the distance between tics on the x-axis
$this->xtics_string
$this->ytics_string
plot '$this->line_data_location' using $this->x_axis:$this->y_axis linecolor rgb \"#CFC493\" title 'Center Of Mass' with lines;";
// outputs gnuplot_script to disk
file_put_contents($this->gnuplot_script_file_location, $this->gnuplot_script);
// calls gnuplot from the command line to execute the script and output the gif
shell_exec("gnuplot $this->gnuplot_script_file_location");
// return the location of the gif image
return $this->output_file_location;
}
}
class twodpng extends twod {
function __construct(){
parent::__construct();
}
public function record_data($one, $two, $three){
$this->record_png_data($one, $two, $three);
}
function export(){
$this->set_tics();
$this->write_line_data();
$this->export_2dpng();
}
}
class twodgif extends twod
{
// instantiate variables specific to gif
var $point_data;
function __construct(){
parent::__construct();
// we need the point_data for the gif, but not the PNGs
$this->point_data_location = $_SERVER['DOCUMENT_ROOT'] . "/msdb/queries/gnuplot_tmp_files/point" . $this->gnuplot_id . ".data";
}
// used to record 3 data points, for instance X, Y, Z
public function record_data ($one, $two, $three){
$this->record_gif_data($one, $two, $three);
}
}
class large_twodgif extends twodgif {
function __construct(){
parent::__construct();
// we need a location for the gif
$this->animated_output_location = $_SERVER['DOCUMENT_ROOT'] . "/msdb/queries/gnuplot_tmp_files/gnuplot_animated_output" . $this->gnuplot_id . ".gif";
}
private function export_2dgif_but_no_exec(){
// create the text for the gnuplot_script
// note that if we have an indent in this text then there is an indent in the script file (which is why it's aligned so funny-looking here)
$this->gnuplot_script = "set title \"$this->title\"
#set to create a .gif file. \"delay 100\" makes (100 * 1/100 seconds) delay between frames of the .gif file. size (width), (height) determines gif size.
set terminal gif enhanced animate delay 120 size 600, 600
set output '$this->animated_output_location'
set xlabel '$this->x_label'
set ylabel '$this->y_label'
set key bmargin
#\"set xtics\" determines the distance between tics on the x-axis
$this->xtics_string
$this->ytics_string
#the rgb colors codes are green and gold, USF's school colors.
#the sample_num variable determines the number of data points to use in the GIF.
#choosing less data points means the gif is generated much faster and occupies much less memory,
#choosing more data points means the gif shows more data points
#there are some differences between the points_data_file and line_data_file. both are output to disk if you wish to view them.
#these differences give us a moving point and a line, respectively, in our gif.
do for [n=0:($this->sample_count-1)]{
plot '$this->line_data_location' every ::0::($this->sample_count-1) using $this->x_axis:$this->y_axis linewidth 1 linecolor rgb \"#CFC493\" notitle with lines, '$this->point_data_location' index n ";
// we have to do some fancy stuff if the user asks to plot the frame number along one of the axes in the animated plot
if (!($this->x_axis) || !($this->y_axis)) { // one of these three axes variables will be 0 if the user asked to have the frame number along one of the variables
// split point_data by newline
$lines = preg_split("/\r\n|\n|\r/", $this->point_data);
//$lines = explode(PHP_EOL, $this->point_data);=
$this->point_data = "";
$i = 0;
foreach ($lines as $line){
// number the nonempty strings only
if (!empty($line)){
$this->point_data .= $i . " " . $line . "\n";
$i++;
}
else{
$this->point_data .= "\n";
}
}
// at this point we have made our point_data file have a series of integers as the first column
// conveniently, to make the axes lines up with what the user input and our new point_data file
// all we have to do is increment the axes by one
$modified_x_axis = $this->x_axis + 1;
$modified_y_axis = $this->y_axis + 1;
$this->gnuplot_script .= " using $modified_x_axis:$modified_y_axis ";
}
else {
// create the text for the gnuplot_script
// note that if we have an indent in this text then there is an indent in the script file
$this->gnuplot_script .= " using $this->x_axis:$this->y_axis ";
}
$this->write_point_data();
$this->gnuplot_script .= "linetype 3 linewidth 2 linecolor rgb \"#006747\" title sprintf(\"$this->key_name%i\",(n+1))
}";
// outputs gnuplot_script to disk
$gnuplot_animated_script_location = $_SERVER['DOCUMENT_ROOT'] . "/msdb/queries/gnuplot_tmp_files/gnuplot_animated_script" . $this->gnuplot_id . ".glp";
file_put_contents($gnuplot_animated_script_location, $this->gnuplot_script);
// calls gnuplot from the command line to execute the script and output the gif
//shell_exec("gnuplot $this->gnuplot_script_file_location");
// return the location of the gif image
return $this->output_file_location;
}
function export(){
// create the gnuplot data files needed to create the image
$this->write_line_data();
$this->set_tics();
$this->export_2dpng();
$this->export_2dgif_but_no_exec();
}
}
class small_twodgif extends twodgif {
function __construct(){
parent::__construct();
}
private function export_2dgif_and_exec(){
// create the text for the gnuplot_script
// note that if we have an indent in this text then there is an indent in the script file (which is why it's aligned so funny-looking here)
$this->gnuplot_script = "set title \"$this->title\"
#set to create a .gif file. \"delay 100\" makes (100 * 1/100 seconds) delay between frames of the .gif file. size (width), (height) determines gif size.
set terminal gif enhanced animate delay 120 size 600, 600
set output '$this->output_file_location'
set xlabel '$this->x_label'
set ylabel '$this->y_label'
set key bmargin
#\"set xtics\" determines the distance between tics on the x-axis
$this->xtics_string
$this->ytics_string
#the rgb colors codes are green and gold, USF's school colors.
#the sample_num variable determines the number of data points to use in the GIF.
#choosing less data points means the gif is generated much faster and occupies much less memory,
#choosing more data points means the gif shows more data points
#there are some differences between the points_data_file and line_data_file. both are output to disk if you wish to view them.
#these differences give us a moving point and a line, respectively, in our gif.
do for [n=0:($this->sample_count-1)]{
plot '$this->line_data_location' every ::0::($this->sample_count-1) using $this->x_axis:$this->y_axis linewidth 1 linecolor rgb \"#CFC493\" notitle with lines, '$this->point_data_location' index n ";
// we have to do some fancy stuff if the user asks to plot the frame number along one of the axes in the animated plot
if (!($this->x_axis) || !($this->y_axis)) { // one of these three axes variables will be 0 if the user asked to have the frame number along one of the variables
// split point_data by newline
$lines = preg_split("/\r\n|\n|\r/", $this->point_data);
//$lines = explode(PHP_EOL, $this->point_data);=
$this->point_data = "";
$i = 0;
foreach ($lines as $line){
// number the nonempty strings only
if (!empty($line)){
$this->point_data .= $i . " " . $line . "\n";
$i++;
}
else{
$this->point_data .= "\n";
}
}
// at this point we have made our point_data file have a series of integers as the first column
// conveniently, to make the axes lines up with what the user input and our new point_data file
// all we have to do is increment the axes by one
$modified_x_axis = $this->x_axis + 1;
$modified_y_axis = $this->y_axis + 1;
$this->gnuplot_script .= " using $modified_x_axis:$modified_y_axis ";
}
else {
// create the text for the gnuplot_script
// note that if we have an indent in this text then there is an indent in the script file
$this->gnuplot_script .= " using $this->x_axis:$this->y_axis ";
}
$this->write_point_data();
$this->gnuplot_script .= "linetype 3 linewidth 2 linecolor rgb \"#006747\" title sprintf(\"$this->key_name%i\",(n+1))
}";
// outputs gnuplot_script to disk
file_put_contents($this->gnuplot_script_file_location, $this->gnuplot_script);
// calls gnuplot from the command line to execute the script and output the gif
shell_exec("gnuplot $this->gnuplot_script_file_location");
// return the location of the gif image
return $this->output_file_location;
}
function export() {
$this->set_tics();
$this->write_line_data();
$this->export_2dgif_and_exec();
}
}
class threed extends gnuplot {
// declare variables specific to 3d images
var $point_data;
var $z_max, $z_min;
var $z_label;
var $ztics_string;
var $z_axis;
function __construct(){
parent::__construct();
// where the files will be temporarily located during execution
// I think it is easier to debug the script when it is written to file, though it could be passed directly to gnuplot via the command line
// we use .glp and .data by convention but not by necessity
$this->point_data_location = $_SERVER['DOCUMENT_ROOT'] . "/msdb/queries/gnuplot_tmp_files/point" . $this->gnuplot_id . ".data";
}
protected function set_tics(){
$this->set_xtics();
$this->set_ytics();
$this->set_ztics();
}
// the 3d plot needs to be able to change the column titles so they don't look so funny
protected function all_axes_change_spaces_to_newline(){
if (strcmp($this->x_label, "Frame #") == 0){
$this->x_label = "Frame\\n#";
}
if (strcmp($this->x_label, "Column 1") == 0){
$this->x_label = "Column\\n1";
}
if (strcmp($this->x_label, "Column 2") == 0){
$this->x_label = "Column\\n2";
}
if (strcmp($this->x_label, "Column 3") == 0){
$this->x_label = "Column\\n3";
}
if (strcmp($this->y_label, "Frame #") == 0){
$this->y_label = "Frame\\n#";
}
if (strcmp($this->y_label, "Column 1") == 0){
$this->y_label = "Column\\n1";
}
if (strcmp($this->y_label, "Column 2") == 0){
$this->y_label = "Column\\n2";
}
if (strcmp($this->y_label, "Column 3") == 0){
$this->y_label = "Column\\n3";
}
if (strcmp($this->z_label, "Frame #") == 0){
$this->z_label = "Frame\\n#";
}
if (strcmp($this->z_label, "Column 1") == 0){
$this->z_label = "Column\\n1";
}
if (strcmp($this->z_label, "Column 2") == 0){
$this->z_label = "Column\\n2";
}
if (strcmp($this->z_label, "Column 3") == 0){
$this->z_label = "Column\\n3";
}
}
protected function export_3dpng(){
$this->all_axes_change_spaces_to_newline();
// create the text for the gnuplot_script
// note that if we have an indent in this text then there is an indent in the script file (which is why it's aligned so funny-looking here)
$this->gnuplot_script = "set title \"$this->title\"
set title offset 0,-3
#I think 500 wide is optimal
set terminal png enhanced size 700, 500
set output '$this->output_file_location'
set xlabel \"$this->x_label\"
set ylabel \"$this->y_label\"
set zlabel \"$this->z_label\"
set zlabel offset -1,-0.5
set key bottom left
#sets the rotation of the 3d image
set view 60,17
#\"set xtics\" determins the distance between tics on the x-axis
$this->xtics_string
$this->ytics_string
$this->ztics_string
#the rgb color code is green and yellow, two of USF's school colors.
splot '$this->line_data_location' using $this->x_axis:$this->y_axis:$this->z_axis linewidth 1 linecolor rgb \"#CFC493\" notitle with lines";
if ($this->sample_count < 100) // plotting points with the line looks funny once sample_count gets too high
$this->gnuplot_script .= ", '$this->line_data_location' using $this->x_axis:$this->y_axis:$this->z_axis linetype 6 linewidth 1 linecolor rgb \"#CFC493\" notitle with points";
// outputs gnuplot_script to disk
file_put_contents($this->gnuplot_script_file_location, $this->gnuplot_script);
// calls gnuplot from the command line to execute the script and output the gif
shell_exec("gnuplot $this->gnuplot_script_file_location");
// return the location of the gif image
return $this->output_file_location;
}
}
class threedgif extends threed {
var $animated_output_location;
function __construct(){
parent::__construct();
// where the files will be temporarily located during execution
// I think it is easier to debug the script when it is written to file, though it could be passed directly to gnuplot via the command line
// we use .glp and .data by convention but not by necessity
$this->point_data_location = $_SERVER['DOCUMENT_ROOT'] . "/msdb/queries/gnuplot_tmp_files/point" . $this->gnuplot_id . ".data";
}
public function record_data ($one, $two, $three){
$this->record_gif_data($one, $two, $three);
}
}
class large_threedgif extends threedgif {
function __construct(){
parent::__construct();
// we need a location for the gif
$this->animated_output_location = $_SERVER['DOCUMENT_ROOT'] . "/msdb/queries/gnuplot_tmp_files/gnuplot_animated_output" . $this->gnuplot_id . ".gif";
}
// this function generates the gnuplot script necessary to generate a gif but doesn't execute the script
// generating the script takes little time relative to generating the gif
// so first we generate 3dgif script and show 3dpng image to user, then generate 3dgif image, then show 3dgif image
private function export_3dgif_but_no_exec(){
$this->all_axes_change_spaces_to_newline();
$this->gnuplot_script = "set title \"$this->title\"
set title offset 0,-2
#set to create a .gif file. \"delay 100\" makes (100 * 1/100 seconds) delay between frames of the .gif file. size (width), (height) determines gif size.
set terminal gif enhanced animate delay 120 size 700,500
set output '$this->animated_output_location'
set xlabel \"$this->x_label\"
set ylabel \"$this->y_label\"
set zlabel \"$this->z_label\"
set zlabel offset -1,-0.5
#sets the rotation of the 3d image
set view 60,17
#\"set xtics\" determins the distance between tics on the x-axis
$this->xtics_string
$this->ytics_string
$this->ztics_string
#reset the title offset so that the title Frame # gets shifted to the left
set key bmargin
#the rgb colors codes are green and gold, USF's school colors.
#the sample_num variable determines the number of data points to use in the GIF.
#choosing less data points means the gif is generated much faster and occupies much less memory,
#choosing more data points means the gif shows more data points
#there are some differences between the points_data_file and line_data_file. both are output to disk if you wish to view them.
#these differences give us a moving point and a line, respectively, in our gif.
do for [n=0:($this->sample_count-1)]{
splot '$this->line_data_location' every ::0::($this->sample_count-1) using $this->x_axis:$this->y_axis:$this->z_axis linetype 1 linewidth 1 linecolor rgb \"#CFC493\" notitle with lines, '$this->point_data_location' index n";
// we have to do some fancy stuff if the user asks to plot the frame number along one of the axes in the animated plot
if (!($this->x_axis) || !($this->y_axis) || !($this->z_axis)){ // one of these three axes variables will be 0 if the user asked to have the frame number along one of the variables
// split point_data by newline
$lines = preg_split("/\r\n|\n|\r/", $this->point_data);
//$lines = explode(PHP_EOL, $this->point_data);=
$this->point_data = "";
$i = 0;
foreach ($lines as $line){
// number the nonempty strings only
if (!empty($line)){
$this->point_data .= $i . " " . $line . "\n";
$i++;
}
else{
$this->point_data .= "\n";
}
}
// at this point we have made our point_data file have a series of integers as the first column
// conveniently, to make the axes lines up with what the user input and our new point_data file
// all we have to do is increment the axes by one
$modified_x_axis = $this->x_axis + 1;
$modified_y_axis = $this->y_axis + 1;
$modified_z_axis = $this->z_axis + 1;
$this->gnuplot_script .= " using $modified_x_axis:$modified_y_axis:$modified_z_axis ";
}
else {
// create the text for the gnuplot_script
// note that if we have an indent in this text then there is an indent in the script file
$this->gnuplot_script .= " using $this->x_axis:$this->y_axis:$this->z_axis ";
}
$this->write_point_data();
$this->gnuplot_script .= "linetype 3 linewidth 1 linecolor rgb \"#006747\" title sprintf(\"$this->key_name%i\",(n+1))
}";
// outputs gnuplot_script to disk
$gnuplot_animated_script_location = $_SERVER['DOCUMENT_ROOT'] . "/msdb/queries/gnuplot_tmp_files/gnuplot_animated_script" . $this->gnuplot_id . ".glp";
file_put_contents($gnuplot_animated_script_location, $this->gnuplot_script);
// calls gnuplot from the command line to execute the script and output the gif
// shell_exec("gnuplot $this->gnuplot_script_file_location");
// return the location of the gif image
return $gnuplot_animated_script_location;
}
// generates gif. returns gif location on disk.
public function export(){
// create the gnuplot data files needed to create the image
$this->write_line_data();
$this->set_tics();
// it's important that export_3dpng come before export_3dgif_but_no_exec because the second replaces files the first needs to generate the png
// for more info look at the comments for the export_3dgif_but_no_exec function
$this->export_3dpng();
$this->export_3dgif_but_no_exec();
}
}
class small_threedgif extends threedgif {
function __construct(){
parent::__construct();
}
// same as export_3dgif_but_no_exec except that it goes the distance and creates the gif file through the shell_exec command
private function export_3dgif_and_exec(){
$this->all_axes_change_spaces_to_newline();
$this->gnuplot_script = "set title \"$this->title\"
set title offset 0,-2
#set to create a .gif file. \"delay 100\" makes (100 * 1/100 seconds) delay between frames of the .gif file. size (width), (height) determines gif size.
set terminal gif enhanced animate delay 120 size 700,500
set output '$this->output_file_location'
set xlabel \"$this->x_label\"
set ylabel \"$this->y_label\"
set zlabel \"$this->z_label\"
set zlabel offset -1,-0.5
#sets the rotation of the 3d image
set view 60,17
#\"set xtics\" determins the distance between tics on the x-axis
$this->xtics_string
$this->ytics_string
$this->ztics_string
#reset the title offset so that the title Frame # gets shifted to the left
set key bmargin
#the rgb colors codes are green and gold, USF's school colors.
#the sample_num variable determines the number of data points to use in the GIF.
#choosing less data points means the gif is generated much faster and occupies much less memory,
#choosing more data points means the gif shows more data points
#there are some differences between the points_data_file and line_data_file. both are output to disk if you wish to view them.
#these differences give us a moving point and a line, respectively, in our gif.
do for [n=0:($this->sample_count-1)]{
splot '$this->line_data_location' every ::0::($this->sample_count-1) using $this->x_axis:$this->y_axis:$this->z_axis linetype 1 linewidth 1 linecolor rgb \"#CFC493\" notitle with lines, '$this->point_data_location' index n";
// we have to do some fancy stuff if the user asks to plot the frame number along one of the axes in the animated plot
if (!($this->x_axis) || !($this->y_axis) || !($this->z_axis)){ // one of these three axes variables will be 0 if the user asked to have the frame number along one of the variables
// split point_data by newline
$lines = preg_split("/\r\n|\n|\r/", $this->point_data);
//$lines = explode(PHP_EOL, $this->point_data);=
$this->point_data = "";
$i = 0;
foreach ($lines as $line){
// number the nonempty strings only
if (!empty($line)){
$this->point_data .= $i . " " . $line . "\n";
$i++;
}
else{
$this->point_data .= "\n";
}
}
// at this point we have made our point_data file have a series of integers as the first column
// conveniently, to make the axes lines up with what the user input and our new point_data file
// all we have to do is increment the axes by one
$modified_x_axis = $this->x_axis + 1;
$modified_y_axis = $this->y_axis + 1;
$modified_z_axis = $this->z_axis + 1;
$this->gnuplot_script .= " using $modified_x_axis:$modified_y_axis:$modified_z_axis ";
}
else {
// create the text for the gnuplot_script
// note that if we have an indent in this text then there is an indent in the script file
$this->gnuplot_script .= " using $this->x_axis:$this->y_axis:$this->z_axis ";
}
$this->write_point_data();
$this->gnuplot_script .= "linetype 3 linewidth 1 linecolor rgb \"#006747\" title sprintf(\"$this->key_name%i\",(n+1))
}";
// outputs gnuplot_script to disk
file_put_contents($this->gnuplot_script_file_location, $this->gnuplot_script);
// calls gnuplot from the command line to execute the script and output the gif
shell_exec("gnuplot $this->gnuplot_script_file_location");
// return the location of the gif image
return $this->output_file_location;
}
// generates gif. returns gif location on disk.
public function export(){
// create the gnuplot data files needed to create the image
$this->write_line_data();
$this->set_tics();
// for more info look at the comments for the export_3dgif_but_no_exec function
$this->export_3dgif_and_exec();
}
}
class threedpng extends threed {
function __construct(){
parent::__construct();
// where the files will be temporarily located during execution
// I think it is easier to debug the script when it is written to file, though it could be passed directly to gnuplot via the command line
// we use .glp and .data by convention but not by necessity
$this->line_data_location = $_SERVER['DOCUMENT_ROOT'] . "/msdb/queries/gnuplot_tmp_files/line" . $this->gnuplot_id . ".data";
}
function record_data ($one, $two, $three){
$this->record_png_data($one, $two, $three);
}
// generates png. returns png location on disk.
function export(){
// create the gnuplot data files needed to create the image
$this->write_line_data();
$this->set_tics();
$this->export_3dpng();
}
}
// if the user chooses none for all three axes then we'll create this dummy object
// just enough functionality to create a data file, but no image
class donotplot extends gnuplot
{
function __construct()
{
parent::__construct();
}
// we still need to create the line.data file to offer to user as download, despite not plotting
public function record_data($one, $two, $three)
{
$this->line_data .= "$one $two $three\r\n";
}
function set_max_samples($sample_max)
{
}
function export()
{
// the line_data file will be used by gnuplot to create an image and be available to the user for download
$this->line_data_final = "#$this->title query data organized by column\r\n" . $this->line_data;
file_put_contents($this->line_data_location, $this->line_data_final);
}
}
?>
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.