bar.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. {
  11. int i;
  12. double max;
  13. double x0, y0;
  14. double bw; /* bar width */
  15. double pixel; /* pixel size */
  16. struct line_pnts *Points, *max_Points;
  17. G_debug(4, "bar(): cx = %f cy = %f", cx, cy);
  18. Points = Vect_new_line_struct();
  19. max_Points = Vect_new_line_struct();
  20. pixel = D_d_to_u_col(2) - D_d_to_u_col(1); /* do it better */
  21. /* Bottom (y0) */
  22. max = 0;
  23. for (i = 0; i < ncols; i++) {
  24. if (val[i] > max)
  25. max = val[i];
  26. }
  27. /* debug */
  28. /* printf("%f \n", max_reference); */
  29. if (y_center == 0)
  30. /* draw the columns with the bottom at the y value of the point */
  31. y0 = cy;
  32. else
  33. /* center the columns around the y value of the point */
  34. y0 = cy - scale * max * pixel / 2;
  35. /* Left (x0) */
  36. x0 = cx - size * pixel / 2;
  37. bw = size * pixel / ncols;
  38. if (max_reference) {
  39. /* Draw polygon outlining max value in dataset with no fill color */
  40. for (i = 0; i < ncols; i++) {
  41. Vect_reset_line(max_Points);
  42. Vect_append_point(max_Points, x0 + i * bw, y0, 0);
  43. Vect_append_point(max_Points, x0 + (i + 1) * bw, y0, 0);
  44. Vect_append_point(max_Points, x0 + (i + 1) * bw,
  45. y0 + scale * max_reference[i] * pixel, 0);
  46. Vect_append_point(max_Points, x0 + i * bw,
  47. y0 + scale * max_reference[i] * pixel, 0);
  48. Vect_append_point(max_Points, x0 + i * bw, y0, 0);
  49. /* the outline color : default is black */
  50. D_RGB_color(ocolor->r, ocolor->g, ocolor->b);
  51. D_polyline_abs(max_Points->x, max_Points->y, max_Points->n_points);
  52. }
  53. }
  54. /* Draw polygon for each value */
  55. for (i = 0; i < ncols; i++) {
  56. Vect_reset_line(Points);
  57. Vect_append_point(Points, x0 + i * bw, y0, 0);
  58. Vect_append_point(Points, x0 + (i + 1) * bw, y0, 0);
  59. Vect_append_point(Points, x0 + (i + 1) * bw,
  60. y0 + scale * val[i] * pixel, 0);
  61. Vect_append_point(Points, x0 + i * bw, y0 + scale * val[i] * pixel,
  62. 0);
  63. Vect_append_point(Points, x0 + i * bw, y0, 0);
  64. if (!colors[i].none) {
  65. D_RGB_color(colors[i].r, colors[i].g, colors[i].b);
  66. D_polygon_abs(Points->x, Points->y, Points->n_points);
  67. }
  68. D_RGB_color(ocolor->r, ocolor->g, ocolor->b);
  69. D_polyline_abs(Points->x, Points->y, Points->n_points);
  70. }
  71. /* tidy up */
  72. Vect_destroy_line_struct(Points);
  73. Vect_destroy_line_struct(max_Points);
  74. return 0;
  75. }