txtplot.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. *
  3. * Copyright (c) 2018, Jue Ruan <ruanjue@gmail.com>
  4. *
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __TEXT_PLOT_RJ_H
  20. #define __TEXT_PLOT_RJ_H
  21. #include "list.h"
  22. static inline char* barplot_txt_u4_simple(u4i w, u4i h, u4i *vals, u4i size, u4i max_val){
  23. char *g;
  24. double hdiv;
  25. u4i wdiv;
  26. u4i i, j, x, y;
  27. g = malloc((w + 1) * h + 1);
  28. memset(g, ' ', (w + 1) * h);
  29. for(i=1;i<=h;i++){
  30. g[(w + 1) * i - 1] = '\n';
  31. }
  32. g[(w + 1) * h] = 0;
  33. if(max_val == 0){
  34. max_val = 1;
  35. for(i=0;i<size;i++){
  36. if(vals[i] > max_val) max_val = vals[i];
  37. }
  38. }
  39. hdiv = 1.0 * max_val / h;
  40. wdiv = size / w;
  41. if(wdiv == 0) wdiv = 1;
  42. for(i=x=0;i<size;i+=wdiv,x++){
  43. y = UInt(vals[i] / hdiv);
  44. if(y == 0 && vals[i]) y = 1;
  45. if(y > h) y = h;
  46. for(j=1;j<=y;j++){
  47. g[(h - j) * (w + 1) + x] = '|';
  48. }
  49. }
  50. return g;
  51. }
  52. static inline char* barplot_txt_u8_simple(u4i w, u4i h, u8i *vals, u4i size, u8i max_val){
  53. char *g;
  54. double hdiv;
  55. u8i wdiv;
  56. u4i i, j, x, y;
  57. g = malloc((w + 1) * h + 1);
  58. memset(g, ' ', (w + 1) * h);
  59. for(i=1;i<=h;i++){
  60. g[(w + 1) * i - 1] = '\n';
  61. }
  62. g[(w + 1) * h] = 0;
  63. if(max_val == 0){
  64. max_val = 1;
  65. for(i=0;i<size;i++){
  66. if(vals[i] > max_val) max_val = vals[i];
  67. }
  68. }
  69. hdiv = 1.0 * max_val / h;
  70. wdiv = size / w;
  71. if(wdiv == 0) wdiv = 1;
  72. for(i=x=0;i<size;i+=wdiv,x++){
  73. y = UInt(vals[i] / hdiv);
  74. if(y == 0 && vals[i]) y = 1;
  75. if(y > h) y = h;
  76. for(j=1;j<=y;j++){
  77. g[(h - j) * (w + 1) + x] = '|';
  78. }
  79. }
  80. return g;
  81. }
  82. #endif