main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 = "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. /*
  125. horizontal_bar_flag = G_define_flag();
  126. horizontal_bar_flag->key = 'h';
  127. horizontal_bar_flag->description = _("Create a horizontal bar chart from left to right");
  128. */
  129. G_gisinit(argv[0]);
  130. if (G_parser(argc, argv))
  131. exit(EXIT_FAILURE);
  132. /* Center the barchart around the y coordinate? */
  133. if (y_center_flag->answer)
  134. y_center = 1; /* center the bar graphs around the y_coord of a point */
  135. else
  136. y_center = 0; /* do not center the bar graphs around the y_coord of a point */
  137. /* Read options */
  138. type = Vect_option_to_types(type_opt);
  139. field = atoi(field_opt->answer);
  140. /* Outline color */
  141. ret = G_str_to_color(ocolor_opt->answer, &r, &g, &b);
  142. if (ret == 1) {
  143. ocolor.none = 0;
  144. ocolor.r = r;
  145. ocolor.g = g;
  146. ocolor.b = b;
  147. }
  148. else if (ret == 2) { /* none */
  149. ocolor.none = 1;
  150. }
  151. /* Count input columns */
  152. p = columns_opt->answer;
  153. ncols = 1;
  154. while ((p = strchr(p, ',')) != NULL) {
  155. ncols++;
  156. p++;
  157. }
  158. G_debug(3, "ncols = %d", ncols);
  159. /* Fill colors */
  160. colors = (COLOR *) G_malloc(ncols * sizeof(COLOR));
  161. /* Fill max_reference values */
  162. max_reference = NULL;
  163. /* default colors */
  164. j = 0;
  165. for (i = 0; i < ncols; i++) {
  166. if (defcols[j].none == -1)
  167. j = 0;
  168. colors[i].none = 0;
  169. colors[i].r = defcols[j].r;
  170. colors[i].g = defcols[j].g;
  171. colors[i].b = defcols[j].b;
  172. j++;
  173. }
  174. /* user colors */
  175. if (colors_opt->answers != NULL) {
  176. for (i = 0; i < ncols; i++) {
  177. if (colors_opt->answers[i] == NULL)
  178. break;
  179. ret = G_str_to_color(colors_opt->answers[i], &r, &g, &b);
  180. if (ret == 1) {
  181. colors[i].none = 0;
  182. colors[i].r = r;
  183. colors[i].g = g;
  184. colors[i].b = b;
  185. }
  186. else if (ret == 2) { /* none */
  187. colors[i].none = 1;
  188. }
  189. }
  190. }
  191. if (legend_flag->answer) {
  192. tokens = G_tokenize(columns_opt->answer, ",");
  193. ntokens = G_number_of_tokens(tokens);
  194. for (i = 0; i < ntokens; i++) {
  195. fprintf(stdout, "%d|%s|%d:%d:%d\n",
  196. i + 1, tokens[i], colors[i].r, colors[i].g, colors[i].b);
  197. }
  198. }
  199. size = atoi(size_opt->answer);
  200. scale = atof(scale_opt->answer);
  201. /* open vector */
  202. Vect_set_open_level(2);
  203. if (Vect_open_old(&Map, map_opt->answer, "") < 0)
  204. G_fatal_error(_("Unable to open vector map <%s>"), map_opt->answer);
  205. ctype = CTYPE_PIE;
  206. if (ctype_opt->answer[0] == 'b')
  207. ctype = CTYPE_BAR;
  208. D_open_driver();
  209. /* should we plot the maximum reference on bar plots? */
  210. if (max_reference_opt->answer != NULL) {
  211. max_reference = (double *)G_malloc(ncols * sizeof(double));
  212. /* loop through the given values */
  213. for (i = 0; i < ncols; i++) {
  214. if (max_reference_opt->answers[i] == NULL)
  215. break;
  216. max_reference[i] = atof(max_reference_opt->answers[i]); /* remember to convert to float */
  217. }
  218. }
  219. D_setup(0);
  220. ret = plot(ctype, &Map, type, field,
  221. columns_opt->answer, ncols,
  222. sizecol_opt->answer, size, scale,
  223. &ocolor, colors, y_center, max_reference);
  224. D_save_command(G_recreate_command());
  225. D_close_driver();
  226. Vect_close(&Map);
  227. exit(EXIT_SUCCESS);
  228. }