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, *chart3d_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 = "chart_type";
  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_standard_option(G_OPT_DB_COLUMN);
  82. sizecol_opt->key = "size_column";
  83. sizecol_opt->required = NO;
  84. sizecol_opt->description = _("Column used for pie chart size");
  85. sizecol_opt->guisection = _("Chart properties");
  86. size_opt = G_define_option();
  87. size_opt->key = "size";
  88. size_opt->type = TYPE_INTEGER;
  89. size_opt->answer = "40";
  90. size_opt->description =
  91. _("Size of chart (diameter for pie, total width for bar)");
  92. size_opt->guisection = _("Chart properties");
  93. scale_opt = G_define_option();
  94. scale_opt->key = "scale";
  95. scale_opt->type = TYPE_DOUBLE;
  96. scale_opt->answer = "1";
  97. scale_opt->description = _("Scale for size (to get size in pixels)");
  98. scale_opt->guisection = _("Chart properties");
  99. ocolor_opt = G_define_standard_option(G_OPT_C);
  100. ocolor_opt->key = "outline_color";
  101. ocolor_opt->label = _("Outline color");
  102. ocolor_opt->guisection = _("Chart properties");
  103. colors_opt = G_define_standard_option(G_OPT_C);
  104. colors_opt->key = "colors";
  105. colors_opt->multiple = YES;
  106. colors_opt->label = _("Colors used to fill charts");
  107. colors_opt->guisection = _("Chart properties");
  108. y_center_flag = G_define_flag();
  109. y_center_flag->key = 'c';
  110. y_center_flag->description =
  111. _("Center the bar chart around a data point");
  112. y_center_flag->guisection = _("Chart properties");
  113. max_reference_opt = G_define_option();
  114. max_reference_opt->key = "max_ref";
  115. max_reference_opt->type = TYPE_DOUBLE;
  116. max_reference_opt->required = NO;
  117. max_reference_opt->multiple = YES;
  118. max_reference_opt->description =
  119. _("Maximum value used for bar plot reference");
  120. legend_flag = G_define_flag();
  121. legend_flag->key = 'l';
  122. legend_flag->description =
  123. _("Create legend information and send to stdout");
  124. chart3d_flag = G_define_flag();
  125. chart3d_flag->key = '3';
  126. chart3d_flag->description =
  127. _("Create 3D charts");
  128. chart3d_flag->guisection = _("Chart properties");
  129. /*
  130. horizontal_bar_flag = G_define_flag();
  131. horizontal_bar_flag->key = 'h';
  132. horizontal_bar_flag->description = _("Create a horizontal bar chart from left to right");
  133. */
  134. G_gisinit(argv[0]);
  135. if (G_parser(argc, argv))
  136. exit(EXIT_FAILURE);
  137. /* Center the barchart around the y coordinate? */
  138. if (y_center_flag->answer)
  139. y_center = 1; /* center the bar graphs around the y_coord of a point */
  140. else
  141. y_center = 0; /* do not center the bar graphs around the y_coord of a point */
  142. /* Read options */
  143. type = Vect_option_to_types(type_opt);
  144. field = atoi(field_opt->answer);
  145. /* Outline color */
  146. ret = G_str_to_color(ocolor_opt->answer, &r, &g, &b);
  147. if (ret == 1) {
  148. ocolor.none = 0;
  149. ocolor.r = r;
  150. ocolor.g = g;
  151. ocolor.b = b;
  152. }
  153. else if (ret == 2) { /* none */
  154. ocolor.none = 1;
  155. }
  156. /* Count input columns */
  157. p = columns_opt->answer;
  158. ncols = 1;
  159. while ((p = strchr(p, ',')) != NULL) {
  160. ncols++;
  161. p++;
  162. }
  163. G_debug(3, "ncols = %d", ncols);
  164. /* Fill colors */
  165. colors = (COLOR *) G_malloc(ncols * sizeof(COLOR));
  166. /* Fill max_reference values */
  167. max_reference = NULL;
  168. /* default colors */
  169. j = 0;
  170. for (i = 0; i < ncols; i++) {
  171. if (defcols[j].none == -1)
  172. j = 0;
  173. colors[i].none = 0;
  174. colors[i].r = defcols[j].r;
  175. colors[i].g = defcols[j].g;
  176. colors[i].b = defcols[j].b;
  177. j++;
  178. }
  179. /* user colors */
  180. if (colors_opt->answers != NULL) {
  181. for (i = 0; i < ncols; i++) {
  182. if (colors_opt->answers[i] == NULL)
  183. break;
  184. ret = G_str_to_color(colors_opt->answers[i], &r, &g, &b);
  185. if (ret == 1) {
  186. colors[i].none = 0;
  187. colors[i].r = r;
  188. colors[i].g = g;
  189. colors[i].b = b;
  190. }
  191. else if (ret == 2) { /* none */
  192. colors[i].none = 1;
  193. }
  194. }
  195. }
  196. if (legend_flag->answer) {
  197. tokens = G_tokenize(columns_opt->answer, ",");
  198. ntokens = G_number_of_tokens(tokens);
  199. for (i = 0; i < ntokens; i++) {
  200. fprintf(stdout, "%d|%s|%d:%d:%d\n",
  201. i + 1, tokens[i], colors[i].r, colors[i].g, colors[i].b);
  202. }
  203. }
  204. size = atoi(size_opt->answer);
  205. scale = atof(scale_opt->answer);
  206. /* open vector */
  207. Vect_set_open_level(2);
  208. if (Vect_open_old(&Map, map_opt->answer, "") < 0)
  209. G_fatal_error(_("Unable to open vector map <%s>"), map_opt->answer);
  210. ctype = CTYPE_PIE;
  211. if (ctype_opt->answer[0] == 'b')
  212. ctype = CTYPE_BAR;
  213. D_open_driver();
  214. /* should we plot the maximum reference on bar plots? */
  215. if (max_reference_opt->answer != NULL) {
  216. max_reference = (double *)G_malloc(ncols * sizeof(double));
  217. /* loop through the given values */
  218. for (i = 0; i < ncols; i++) {
  219. if (max_reference_opt->answers[i] == NULL)
  220. break;
  221. max_reference[i] = atof(max_reference_opt->answers[i]); /* remember to convert to float */
  222. }
  223. }
  224. D_setup(0);
  225. ret = plot(ctype, &Map, type, field,
  226. columns_opt->answer, ncols,
  227. sizecol_opt->answer, size, scale,
  228. &ocolor, colors, y_center, max_reference,
  229. chart3d_flag->answer);
  230. D_save_command(G_recreate_command());
  231. D_close_driver();
  232. Vect_close(&Map);
  233. exit(EXIT_SUCCESS);
  234. }