histogram.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* histogram.c:
  2. * Draws a histogram along the left side of a smooth gradient legend
  3. * (stats fetching code adapted from d.histogram)
  4. *
  5. * Copyright (C) 2014 by Hamish Bowman, and the GRASS Development Team*
  6. * This program is free software under the GPL (>=v2)
  7. * Read the COPYING file that comes with GRASS for details.
  8. */
  9. #include <grass/gis.h>
  10. #include <grass/display.h>
  11. #include "local_proto.h"
  12. void draw_histogram(const char *map_name, int x0, int y0, int width,
  13. int height, int color, int flip, int horiz,
  14. int map_type)
  15. {
  16. int i, nsteps;
  17. long cell_count;
  18. double max_width, width_mult, dx;
  19. struct stat_list dist_stats;
  20. struct stat_node *ptr;
  21. if (horiz) {
  22. max_width = height * 1.75;
  23. nsteps = width - 3;
  24. }
  25. else {
  26. max_width = width * 1.75;
  27. nsteps = height - 3;
  28. }
  29. /* get the distribution statistics */
  30. get_stats(map_name, &dist_stats, nsteps, map_type);
  31. width_mult = max_width / dist_stats.maxstat;
  32. D_use_color(color);
  33. D_begin();
  34. ptr = dist_stats.ptr;
  35. for (i = dist_stats.mincat; i <= dist_stats.maxcat; i++) {
  36. if (!ptr)
  37. break;
  38. if (ptr->cat == i) { /* AH-HA!! found the stat */
  39. cell_count = ptr->stat;
  40. if (ptr->next != NULL)
  41. ptr = ptr->next;
  42. }
  43. else { /* we have to look for the stat */
  44. /* loop until we find it, or pass where it should be */
  45. while (ptr->cat < i && ptr->next != NULL)
  46. ptr = ptr->next;
  47. if (ptr->cat == i) { /* AH-HA!! found the stat */
  48. cell_count = ptr->stat;
  49. if (ptr->next != NULL)
  50. ptr = ptr->next;
  51. }
  52. else /* stat cannot be found */
  53. G_debug(4, "No matching stat found, i=%d", i);
  54. }
  55. if (!cell_count)
  56. continue;
  57. dx = cell_count * width_mult;
  58. if (horiz) {
  59. if (flip)
  60. D_move_abs(x0 + width - i - 1, y0 - 1);
  61. else
  62. D_move_abs(x0 + i + 1, y0 - 1);
  63. D_cont_rel(0, -dx);
  64. }
  65. else { /* vertical */
  66. if (flip)
  67. D_move_abs(x0 - 1, y0 - 1 + height - i);
  68. else
  69. D_move_abs(x0 - 1, y0 + 1 + i);
  70. D_cont_rel(-dx, 0);
  71. }
  72. }
  73. D_close();
  74. D_end();
  75. D_stroke();
  76. }