main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /****************************************************************************
  2. *
  3. * MODULE: d.vect.chart
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. *
  7. * PURPOSE: Display charts
  8. *
  9. * COPYRIGHT: (C) 2001-2008 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <grass/gis.h>
  19. #include <grass/raster.h>
  20. #include <grass/display.h>
  21. #include <grass/vector.h>
  22. #include <grass/colors.h>
  23. #include <grass/symbol.h>
  24. #include <grass/dbmi.h>
  25. #include <grass/glocale.h>
  26. #include "global.h"
  27. int main(int argc, char **argv)
  28. {
  29. char *p;
  30. int y_center;
  31. int i, j, ret, type, field, ctype, ncols;
  32. COLOR ocolor, *colors;
  33. int r, g, b;
  34. int size;
  35. double scale, *max_reference;
  36. struct GModule *module;
  37. struct Option *map_opt;
  38. struct Option *type_opt, *ctype_opt;
  39. struct Option *size_opt, *scale_opt, *max_reference_opt;
  40. struct Option *field_opt;
  41. struct Option *ocolor_opt, *colors_opt;
  42. struct Option *columns_opt, *sizecol_opt;
  43. struct Flag *y_center_flag, *legend_flag;
  44. /* struct Flag *horizontal_bar_flag; */
  45. struct Map_info Map;
  46. char **tokens;
  47. int ntokens; /* number of tokens */
  48. COLOR defcols[] = { {0, 0, 0, 255}, /* blue */
  49. {0, 0, 255, 255}, /* cyan */
  50. {0, 0, 255, 0}, /* green */
  51. {0, 255, 255, 0}, /* yellow */
  52. {0, 255, 0, 0}, /* red */
  53. {0, 255, 0, 255}, /* magenta */
  54. {-1, 0, 0, 0} /* END */
  55. };
  56. module = G_define_module();
  57. G_add_keyword(_("display"));
  58. G_add_keyword(_("cartography"));
  59. G_add_keyword(_("chart maps"));
  60. module->description =
  61. _("Displays charts of vector data in the active frame "
  62. "on the graphics monitor.");
  63. map_opt = G_define_standard_option(G_OPT_V_MAP);
  64. type_opt = G_define_standard_option(G_OPT_V_TYPE);
  65. type_opt->answer = "point,line,boundary,centroid";
  66. type_opt->guisection = _("Selection");
  67. field_opt = G_define_standard_option(G_OPT_V_FIELD);
  68. field_opt->guisection = _("Selection");
  69. ctype_opt = G_define_option();
  70. ctype_opt->key = "ctype";
  71. ctype_opt->type = TYPE_STRING;
  72. ctype_opt->required = NO;
  73. ctype_opt->multiple = NO;
  74. ctype_opt->answer = "pie";
  75. ctype_opt->options = "pie,bar";
  76. ctype_opt->description = _("Chart type");
  77. ctype_opt->guisection = _("Chart properties");
  78. columns_opt = G_define_standard_option(G_OPT_DB_COLUMNS);
  79. columns_opt->required = YES;
  80. columns_opt->description = _("Attribute columns containing data");
  81. sizecol_opt = G_define_option();
  82. sizecol_opt->key = "sizecol";
  83. sizecol_opt->type = TYPE_STRING;
  84. sizecol_opt->required = NO;
  85. sizecol_opt->description = _("Column used for pie chart size");
  86. sizecol_opt->guisection = _("Chart properties");
  87. size_opt = G_define_option();
  88. size_opt->key = "size";
  89. size_opt->type = TYPE_INTEGER;
  90. size_opt->answer = "40";
  91. size_opt->description =
  92. _("Size of chart (diameter for pie, total width for bar)");
  93. size_opt->guisection = _("Chart properties");
  94. scale_opt = G_define_option();
  95. scale_opt->key = "scale";
  96. scale_opt->type = TYPE_DOUBLE;
  97. scale_opt->answer = "1";
  98. scale_opt->description = _("Scale for size (to get size in pixels)");
  99. scale_opt->guisection = _("Chart properties");
  100. ocolor_opt = G_define_option();
  101. ocolor_opt->key = "ocolor";
  102. ocolor_opt->type = TYPE_STRING;
  103. ocolor_opt->answer = DEFAULT_FG_COLOR;
  104. ocolor_opt->description = _("Outline color");
  105. ocolor_opt->gisprompt = "old_color,color,color";
  106. ocolor_opt->guisection = _("Chart properties");
  107. colors_opt = G_define_option();
  108. colors_opt->key = "colors";
  109. colors_opt->type = TYPE_STRING;
  110. colors_opt->required = NO;
  111. colors_opt->multiple = YES;
  112. colors_opt->description = _("Colors used to fill charts");
  113. colors_opt->gisprompt = "old_color,color,color";
  114. colors_opt->guisection = _("Chart properties");
  115. y_center_flag = G_define_flag();
  116. y_center_flag->key = 'c';
  117. y_center_flag->description =
  118. _("Center the bar chart around a data point");
  119. y_center_flag->guisection = _("Chart properties");
  120. max_reference_opt = G_define_option();
  121. max_reference_opt->key = "max_ref";
  122. max_reference_opt->type = TYPE_DOUBLE;
  123. max_reference_opt->required = NO;
  124. max_reference_opt->multiple = YES;
  125. max_reference_opt->description =
  126. _("Maximum value used for bar plot reference");
  127. legend_flag = G_define_flag();
  128. legend_flag->key = 'l';
  129. legend_flag->description =
  130. _("Create legend information and send to stdout");
  131. /*
  132. horizontal_bar_flag = G_define_flag();
  133. horizontal_bar_flag->key = 'h';
  134. horizontal_bar_flag->description = _("Create a horizontal bar chart from left to right");
  135. */
  136. G_gisinit(argv[0]);
  137. if (G_parser(argc, argv))
  138. exit(EXIT_FAILURE);
  139. /* Center the barchart around the y coordinate? */
  140. if (y_center_flag->answer)
  141. y_center = 1; /* center the bar graphs around the y_coord of a point */
  142. else
  143. y_center = 0; /* do not center the bar graphs around the y_coord of a point */
  144. /* Read options */
  145. type = Vect_option_to_types(type_opt);
  146. field = atoi(field_opt->answer);
  147. /* Outline color */
  148. ret = G_str_to_color(ocolor_opt->answer, &r, &g, &b);
  149. if (ret == 1) {
  150. ocolor.none = 0;
  151. ocolor.r = r;
  152. ocolor.g = g;
  153. ocolor.b = b;
  154. }
  155. else if (ret == 2) { /* none */
  156. ocolor.none = 1;
  157. }
  158. /* Count input columns */
  159. p = columns_opt->answer;
  160. ncols = 1;
  161. while ((p = strchr(p, ',')) != NULL) {
  162. ncols++;
  163. p++;
  164. }
  165. G_debug(3, "ncols = %d", ncols);
  166. /* Fill colors */
  167. colors = (COLOR *) G_malloc(ncols * sizeof(COLOR));
  168. /* Fill max_reference values */
  169. max_reference = (double *)G_malloc(ncols * sizeof(double));
  170. /* default colors */
  171. j = 0;
  172. for (i = 0; i < ncols; i++) {
  173. if (defcols[j].none == -1)
  174. j = 0;
  175. colors[i].none = 0;
  176. colors[i].r = defcols[j].r;
  177. colors[i].g = defcols[j].g;
  178. colors[i].b = defcols[j].b;
  179. j++;
  180. }
  181. /* user colors */
  182. if (colors_opt->answers != NULL) {
  183. for (i = 0; i < ncols; i++) {
  184. if (colors_opt->answers[i] == NULL)
  185. break;
  186. ret = G_str_to_color(colors_opt->answers[i], &r, &g, &b);
  187. if (ret == 1) {
  188. colors[i].none = 0;
  189. colors[i].r = r;
  190. colors[i].g = g;
  191. colors[i].b = b;
  192. }
  193. else if (ret == 2) { /* none */
  194. colors[i].none = 1;
  195. }
  196. }
  197. }
  198. if (legend_flag->answer) {
  199. tokens = G_tokenize(columns_opt->answer, ",");
  200. ntokens = G_number_of_tokens(tokens);
  201. for (i = 0; i < ntokens; i++) {
  202. fprintf(stdout, "%d|%s|%d:%d:%d\n",
  203. i + 1, tokens[i], colors[i].r, colors[i].g, colors[i].b);
  204. }
  205. }
  206. size = atoi(size_opt->answer);
  207. scale = atof(scale_opt->answer);
  208. /* open vector */
  209. Vect_set_open_level(2);
  210. Vect_open_old(&Map, map_opt->answer, "");
  211. ctype = CTYPE_PIE;
  212. if (ctype_opt->answer[0] == 'b')
  213. ctype = CTYPE_BAR;
  214. if (D_open_driver() != 0)
  215. G_fatal_error(_("No graphics device selected. "
  216. "Use d.mon to select graphics device."));
  217. /* should we plot the maximum reference on bar plots? */
  218. if (max_reference_opt->answer != NULL) {
  219. /* loop through the given values */
  220. for (i = 0; i < ncols; i++) {
  221. if (max_reference_opt->answers[i] == NULL)
  222. break;
  223. max_reference[i] = atof(max_reference_opt->answers[i]); /* remember to convert to float */
  224. }
  225. }
  226. D_setup(0);
  227. ret = plot(ctype, &Map, type, field,
  228. columns_opt->answer, ncols,
  229. sizecol_opt->answer, size, scale,
  230. &ocolor, colors, y_center, max_reference);
  231. D_save_command(G_recreate_command());
  232. D_close_driver();
  233. Vect_close(&Map);
  234. exit(EXIT_SUCCESS);
  235. }