bar.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <grass/gis.h>
  2. #include <grass/vector.h>
  3. #include <grass/dbmi.h>
  4. #include <grass/display.h>
  5. #include <grass/symbol.h>
  6. #include "global.h"
  7. int
  8. bar(double cx, double cy, int size, double scale, double *val, int ncols,
  9. COLOR * ocolor, COLOR * colors, int y_center, double *max_reference,
  10. int do3d)
  11. {
  12. int i;
  13. double max;
  14. double x0, y0, x1, y1, dx, dy;
  15. double bw; /* bar width */
  16. double pixel; /* pixel size */
  17. struct line_pnts *Points, *max_Points;
  18. G_debug(4, "bar(): cx = %f cy = %f", cx, cy);
  19. Points = Vect_new_line_struct();
  20. max_Points = Vect_new_line_struct();
  21. pixel = D_d_to_u_col(2) - D_d_to_u_col(1); /* do it better */
  22. /* Bottom (y0) */
  23. max = 0;
  24. for (i = 0; i < ncols; i++) {
  25. if (val[i] > max)
  26. max = val[i];
  27. }
  28. /* debug */
  29. /* printf("%f \n", max_reference); */
  30. if (y_center == 0)
  31. /* draw the columns with the bottom at the y value of the point */
  32. y0 = cy;
  33. else
  34. /* center the columns around the y value of the point */
  35. y0 = cy - scale * max * pixel / 2;
  36. /* Left (x0) */
  37. x0 = cx - size * pixel / 2;
  38. bw = size * pixel / ncols;
  39. dx = bw / 5.0;
  40. dy = dx * 1.5;
  41. if (max_reference) {
  42. /* Draw polygon outlining max value in dataset with no fill color */
  43. for (i = 0; i < ncols; i++) {
  44. Vect_reset_line(max_Points);
  45. Vect_append_point(max_Points, x0 + i * bw, y0, 0);
  46. Vect_append_point(max_Points, x0 + (i + 1) * bw, y0, 0);
  47. Vect_append_point(max_Points, x0 + (i + 1) * bw,
  48. y0 + scale * max_reference[i] * pixel, 0);
  49. Vect_append_point(max_Points, x0 + i * bw,
  50. y0 + scale * max_reference[i] * pixel, 0);
  51. Vect_append_point(max_Points, x0 + i * bw, y0, 0);
  52. /* the outline color : default is black */
  53. D_RGB_color(ocolor->r, ocolor->g, ocolor->b);
  54. D_polyline_abs(max_Points->x, max_Points->y, max_Points->n_points);
  55. }
  56. }
  57. /* Draw polygon for each value */
  58. for (i = 0; i < ncols; i++) {
  59. Vect_reset_line(Points);
  60. Vect_append_point(Points, x0 + i * bw, y0, 0);
  61. Vect_append_point(Points, x0 + (i + 1) * bw, y0, 0);
  62. Vect_append_point(Points, x0 + (i + 1) * bw,
  63. y0 + scale * val[i] * pixel, 0);
  64. Vect_append_point(Points, x0 + i * bw, y0 + scale * val[i] * pixel,
  65. 0);
  66. Vect_append_point(Points, x0 + i * bw, y0, 0);
  67. if (!colors[i].none) {
  68. D_RGB_color(colors[i].r, colors[i].g, colors[i].b);
  69. D_polygon_abs(Points->x, Points->y, Points->n_points);
  70. }
  71. D_RGB_color(ocolor->r, ocolor->g, ocolor->b);
  72. D_polyline_abs(Points->x, Points->y, Points->n_points);
  73. if (do3d) {
  74. /* up */
  75. Vect_reset_line(Points);
  76. y1 = y0 + scale * val[i] * pixel;
  77. Vect_append_point(Points, x0 + i * bw, y1, 0);
  78. Vect_append_point(Points, x0 + i * bw + dx, y1 + dy, 0);
  79. Vect_append_point(Points, x0 + (i + 1) * bw + dx, y1 + dy, 0);
  80. Vect_append_point(Points, x0 + (i + 1) * bw, y1, 0);
  81. Vect_append_point(Points, x0 + i * bw, y1, 0);
  82. if (!colors[i].none) {
  83. D_RGB_color(colors[i].r, colors[i].g, colors[i].b);
  84. D_polygon_abs(Points->x, Points->y, Points->n_points);
  85. }
  86. D_RGB_color(ocolor->r, ocolor->g, ocolor->b);
  87. /* do not draw the same line twice */
  88. Points->n_points = 4;
  89. D_polyline_abs(Points->x, Points->y, Points->n_points);
  90. /* right */
  91. Vect_reset_line(Points);
  92. x1 = x0 + (i + 1) * bw;
  93. Vect_append_point(Points, x1 + dx + 0.5 * pixel, y1 + dy, 0);
  94. Vect_append_point(Points, x1 + dx + 0.5 * pixel, y0 + dy, 0);
  95. Vect_append_point(Points, x1, y0, 0);
  96. Vect_append_point(Points, x1, y1, 0);
  97. Vect_append_point(Points, x1 + dx + 0.5 * pixel, y1 + dy, 0);
  98. if (!colors[i].none && val[i] > 0) {
  99. D_RGB_color(colors[i].r, colors[i].g, colors[i].b);
  100. D_polygon_abs(Points->x, Points->y, Points->n_points);
  101. }
  102. D_RGB_color(ocolor->r, ocolor->g, ocolor->b);
  103. /* do not draw the same line twice */
  104. Points->n_points = 3;
  105. D_polyline_abs(Points->x, Points->y, Points->n_points);
  106. }
  107. }
  108. /* tidy up */
  109. Vect_destroy_line_struct(Points);
  110. Vect_destroy_line_struct(max_Points);
  111. return 0;
  112. }