Quick, dirty and useful

Staring at the output of select histogram_bounds from pg_stats where tablename='im_page' and attname='network_id'; for a while made me wonder if this information could not be shown in a form more intuitively interpretable. So I came up with the very quick, dirty but useful php blurp below that visualises the information in the histogram bounds field. Wipe your ass with it, use it, snort about it, but don’t blame me if it explodes in your face!

Of course, you will need DejaVuSans.ttf somewhere php can find it. It only works for numeric attributes, and shows the distribution of values across the entire range in a pic like this:

<?php

$histo = array(3,1093532,2500008,3347370,3773220,4329046,4892919,5390692,5395070,5399958,5410010,6070732,6083634,6099913,6130248,6178513,6247523,6343449,6453638,6623753,6768751,7397769,8399216,9846065,10544718,11031060,11860674,12419158,13316261,13381469,13542198,13612939,13691688,14129002,14521475,15031702,15757599,16379081,16911263,17373535,17867201,18334590,19029738,19540986,20134883,20960943,21533740,22135095,22891305,23693757,24404628,24998347,25322610,25876449,26639871,27120442,27879107,28377881,28902544,29184971,29500076,29905742,30211641,30862716,31463170,32303386,32700442,33280207,34106019,34652112,35375896,36329008,36898304,37775921,38786805,39217310,39422963,39595039,39820321,39985192,40201842,40307867,40386696,40473160,40549746,40630896,40707828,40782292,41002531,41239758,41536689,41770244,41939655,42117194,42315945,42507594,42678925,42870015,43041425,43244698,43415095);

$min=$histo[0];
$max=$histo[count($histo)-1];

print "min $min, max $max\n";
print "number ".count($histo)."\n";

$im     = imagecreate(1044,200);

$black  = imagecolorallocate($im,0,0,0);
$white  = imagecolorallocate($im,255,255,255);

imagefill($im,0,0,$white);

imageline($im,10,49,1033,49,$black);

$step = ($max-$min)/1023;

$counter=0;

$lastx=-10000;

foreach($histo as $val) {
  $x=($val-$min)/$step;

 print "$x\n";
  imageline($im,$x+10,40,$x+10,58,$black);

  if($x+5>$lastx+10) {
    imagettftext($im,10,-90,$x+5,60,$black,"./DejaVuSans.ttf","$val");
    $lastx=$x+5;
  }
  $counter++;
}

imagepng($im,'line.png');

?>

Flattr this

Leave a Reply