🔌 VidYen Plugins for WordPress
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.0 KiB

2 years ago
  1. <?php
  2. function imagechart_vis (){
  3. $data = [
  4. 'Jan' => 103,
  5. 'Feb' => 24,
  6. 'Mar' => 44,
  7. 'Apr' => 14,
  8. 'May' => 89,
  9. 'Jun' => 147,
  10. 'Jul' => 181,
  11. 'Aug' => 172,
  12. 'Sep' => 52,
  13. 'Oct' => 106,
  14. 'Nov' => 121,
  15. 'Dec' => 76,
  16. ];
  17. /*
  18. * Chart settings and create image
  19. */
  20. // Image dimensions
  21. $imageWidth = 700;
  22. $imageHeight = 400;
  23. // Grid dimensions and placement within image
  24. $gridTop = 40;
  25. $gridLeft = 50;
  26. $gridBottom = 340;
  27. $gridRight = 650;
  28. $gridHeight = $gridBottom - $gridTop;
  29. $gridWidth = $gridRight - $gridLeft;
  30. // Bar and line width
  31. $lineWidth = 1;
  32. $barWidth = 20;
  33. // Font settings
  34. $font = 'OpenSans-Regular.ttf';
  35. $fontSize = 10;
  36. // Margin between label and axis
  37. $labelMargin = 8;
  38. // Max value on y-axis
  39. $yMaxValue = 200;
  40. // Distance between grid lines on y-axis
  41. $yLabelSpan = 40;
  42. // Init image
  43. $chart = imagecreate($imageWidth, $imageHeight);
  44. // Setup colors
  45. $backgroundColor = imagecolorallocate($chart, 255, 255, 255);
  46. $axisColor = imagecolorallocate($chart, 85, 85, 85);
  47. $labelColor = $axisColor;
  48. $gridColor = imagecolorallocate($chart, 212, 212, 212);
  49. $barColor = imagecolorallocate($chart, 47, 133, 217);
  50. imagefill($chart, 0, 0, $backgroundColor);
  51. imagesetthickness($chart, $lineWidth);
  52. /*
  53. * Print grid lines bottom up
  54. */
  55. for($i = 0; $i <= $yMaxValue; $i += $yLabelSpan) {
  56. $y = $gridBottom - $i * $gridHeight / $yMaxValue;
  57. // draw the line
  58. imageline($chart, $gridLeft, $y, $gridRight, $y, $gridColor);
  59. // draw right aligned label
  60. $labelBox = imagettfbbox($fontSize, 0, $font, strval($i));
  61. $labelWidth = $labelBox[4] - $labelBox[0];
  62. $labelX = $gridLeft - $labelWidth - $labelMargin;
  63. $labelY = $y + $fontSize / 2;
  64. imagettftext($chart, $fontSize, 0, $labelX, $labelY, $labelColor, $font, strval($i));
  65. }
  66. /*
  67. * Draw x- and y-axis
  68. */
  69. imageline($chart, $gridLeft, $gridTop, $gridLeft, $gridBottom, $axisColor);
  70. imageline($chart, $gridLeft, $gridBottom, $gridRight, $gridBottom, $axisColor);
  71. /*
  72. * Draw the bars with labels
  73. */
  74. $barSpacing = $gridWidth / count($data);
  75. $itemX = $gridLeft + $barSpacing / 2;
  76. foreach($data as $key => $value) {
  77. // Draw the bar
  78. $x1 = $itemX - $barWidth / 2;
  79. $y1 = $gridBottom - $value / $yMaxValue * $gridHeight;
  80. $x2 = $itemX + $barWidth / 2;
  81. $y2 = $gridBottom - 1;
  82. imagefilledrectangle($chart, $x1, $y1, $x2, $y2, $barColor);
  83. // Draw the label
  84. $labelBox = imagettfbbox($fontSize, 0, $font, $key);
  85. $labelWidth = $labelBox[4] - $labelBox[0];
  86. $labelX = $itemX - $labelWidth / 2;
  87. $labelY = $gridBottom + $labelMargin + $fontSize;
  88. imagettftext($chart, $fontSize, 0, $labelX, $labelY, $labelColor, $font, $key);
  89. $itemX += $barSpacing;
  90. }
  91. /*
  92. * Output image to browser
  93. */
  94. header('Content-Type: image/png');
  95. imagepng($chart);
  96. $imageoutput = imagepng($chart);
  97. return $imageoutput;
  98. }
  99. add_shortcode( 'vyps-vis', 'vyps_refer_balance_short_func');
  100. ?>