chart.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. +----------------------------------------------------------------------+
  3. | XlsWriter Extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2017-2018 The Viest |
  6. +----------------------------------------------------------------------+
  7. | http://www.viest.me |
  8. +----------------------------------------------------------------------+
  9. | Author: viest <[email protected]> |
  10. +----------------------------------------------------------------------+
  11. */
  12. #include "include.h"
  13. #include "chart.h"
  14. zend_class_entry *vtiful_chart_ce;
  15. /* {{{ format_objects_new
  16. */
  17. static zend_object_handlers chart_handlers;
  18. static zend_always_inline void *vtiful_char_object_alloc(size_t obj_size, zend_class_entry *ce) {
  19. void *obj = emalloc(obj_size);
  20. memset(obj, 0, obj_size);
  21. return obj;
  22. }
  23. PHP_VTIFUL_API zend_object *chart_objects_new(zend_class_entry *ce)
  24. {
  25. chart_object *format = (chart_object *)vtiful_char_object_alloc(sizeof(chart_object), ce);
  26. zend_object_std_init(&format->zo, ce);
  27. object_properties_init(&format->zo, ce);
  28. format->ptr.chart = NULL;
  29. format->ptr.series = NULL;
  30. format->zo.handlers = &chart_handlers;
  31. return &format->zo;
  32. }
  33. /* }}} */
  34. /* {{{ chart_objects_free
  35. */
  36. static void chart_objects_free(zend_object *object)
  37. {
  38. chart_object *intern = php_vtiful_chart_fetch_object(object);
  39. if (intern->ptr.series != NULL) {
  40. // free by workbook
  41. intern->ptr.series = NULL;
  42. }
  43. if (intern->ptr.chart != NULL) {
  44. // free by workbook
  45. intern->ptr.chart = NULL;
  46. }
  47. zend_object_std_dtor(&intern->zo);
  48. }
  49. /* }}} */
  50. /* {{{ ARG_INFO
  51. */
  52. ZEND_BEGIN_ARG_INFO_EX(chart_construct_arginfo, 0, 0, 2)
  53. ZEND_ARG_INFO(0, handle)
  54. ZEND_ARG_INFO(0, type)
  55. ZEND_END_ARG_INFO()
  56. ZEND_BEGIN_ARG_INFO_EX(chart_series_arginfo, 0, 0, 1)
  57. ZEND_ARG_INFO(0, value)
  58. ZEND_ARG_INFO(0, categories)
  59. ZEND_END_ARG_INFO()
  60. ZEND_BEGIN_ARG_INFO_EX(chart_series_name_arginfo, 0, 0, 1)
  61. ZEND_ARG_INFO(0, value)
  62. ZEND_END_ARG_INFO()
  63. ZEND_BEGIN_ARG_INFO_EX(chart_style_arginfo, 0, 0, 1)
  64. ZEND_ARG_INFO(0, style)
  65. ZEND_END_ARG_INFO()
  66. ZEND_BEGIN_ARG_INFO_EX(chart_axis_name_arginfo, 0, 0, 1)
  67. ZEND_ARG_INFO(0, name)
  68. ZEND_END_ARG_INFO()
  69. ZEND_BEGIN_ARG_INFO_EX(chart_title_name_arginfo, 0, 0, 1)
  70. ZEND_ARG_INFO(0, title)
  71. ZEND_END_ARG_INFO()
  72. /* }}} */
  73. /** {{{ \Vtiful\Kernel\Chart::__construct(resource $handle, int $type)
  74. */
  75. PHP_METHOD(vtiful_chart, __construct)
  76. {
  77. zval *handle;
  78. zend_long type;
  79. chart_object *obj;
  80. xls_resource_t *xls_res;
  81. ZEND_PARSE_PARAMETERS_START(2, 2)
  82. Z_PARAM_RESOURCE(handle)
  83. Z_PARAM_LONG(type)
  84. ZEND_PARSE_PARAMETERS_END();
  85. ZVAL_COPY(return_value, getThis());
  86. xls_res = zval_get_resource(handle);
  87. obj = Z_CHART_P(getThis());
  88. if (obj->ptr.chart == NULL) {
  89. obj->ptr.chart = workbook_add_chart(xls_res->workbook, type);
  90. }
  91. }
  92. /* }}} */
  93. /** {{{ \Vtiful\Kernel\Chart::series(string $value, string $categories)
  94. */
  95. PHP_METHOD(vtiful_chart, series)
  96. {
  97. chart_object *obj;
  98. zend_string *values, *categories = NULL;
  99. ZEND_PARSE_PARAMETERS_START(1, 2)
  100. Z_PARAM_STR(values)
  101. Z_PARAM_OPTIONAL
  102. Z_PARAM_STR(categories)
  103. ZEND_PARSE_PARAMETERS_END();
  104. ZVAL_COPY(return_value, getThis());
  105. obj = Z_CHART_P(getThis());
  106. if (ZEND_NUM_ARGS() == 2) {
  107. obj->ptr.series = chart_add_series(obj->ptr.chart, ZSTR_VAL(categories), ZSTR_VAL(values));
  108. }
  109. if (ZEND_NUM_ARGS() == 1) {
  110. obj->ptr.series = chart_add_series(obj->ptr.chart, NULL, ZSTR_VAL(values));
  111. }
  112. }
  113. /* }}} */
  114. /** {{{ \Vtiful\Kernel\Chart::seriesName(string $value)
  115. */
  116. PHP_METHOD(vtiful_chart, seriesName)
  117. {
  118. chart_object *obj;
  119. zend_string *values;
  120. ZEND_PARSE_PARAMETERS_START(1, 1)
  121. Z_PARAM_STR(values)
  122. ZEND_PARSE_PARAMETERS_END();
  123. ZVAL_COPY(return_value, getThis());
  124. obj = Z_CHART_P(getThis());
  125. chart_series_set_name(obj->ptr.series, ZSTR_VAL(values));
  126. }
  127. /* }}} */
  128. /** {{{ \Vtiful\Kernel\Chart::seriesName(string $value)
  129. */
  130. PHP_METHOD(vtiful_chart, style)
  131. {
  132. chart_object *obj;
  133. zend_long style;
  134. ZEND_PARSE_PARAMETERS_START(1, 1)
  135. Z_PARAM_LONG(style)
  136. ZEND_PARSE_PARAMETERS_END();
  137. ZVAL_COPY(return_value, getThis());
  138. obj = Z_CHART_P(getThis());
  139. chart_set_style(obj->ptr.chart, style);
  140. }
  141. /* }}} */
  142. /** {{{ \Vtiful\Kernel\Chart::axisNameX(string $name)
  143. */
  144. PHP_METHOD(vtiful_chart, axisNameX)
  145. {
  146. chart_object *obj;
  147. zend_string *name;
  148. ZEND_PARSE_PARAMETERS_START(1, 1)
  149. Z_PARAM_STR(name)
  150. ZEND_PARSE_PARAMETERS_END();
  151. ZVAL_COPY(return_value, getThis());
  152. obj = Z_CHART_P(getThis());
  153. chart_axis_set_name(obj->ptr.chart->x_axis, ZSTR_VAL(name));
  154. }
  155. /* }}} */
  156. /** {{{ \Vtiful\Kernel\Chart::axisNameY(string $name)
  157. */
  158. PHP_METHOD(vtiful_chart, axisNameY)
  159. {
  160. chart_object *obj;
  161. zend_string *name;
  162. ZEND_PARSE_PARAMETERS_START(1, 1)
  163. Z_PARAM_STR(name)
  164. ZEND_PARSE_PARAMETERS_END();
  165. ZVAL_COPY(return_value, getThis());
  166. obj = Z_CHART_P(getThis());
  167. chart_axis_set_name(obj->ptr.chart->y_axis, ZSTR_VAL(name));
  168. }
  169. /* }}} */
  170. /** {{{ \Vtiful\Kernel\Chart::title(string $title)
  171. */
  172. PHP_METHOD(vtiful_chart, title)
  173. {
  174. chart_object *obj;
  175. zend_string *title;
  176. ZEND_PARSE_PARAMETERS_START(1, 1)
  177. Z_PARAM_STR(title)
  178. ZEND_PARSE_PARAMETERS_END();
  179. ZVAL_COPY(return_value, getThis());
  180. obj = Z_CHART_P(getThis());
  181. chart_title_set_name(obj->ptr.chart, ZSTR_VAL(title));
  182. }
  183. /* }}} */
  184. /** {{{ \Vtiful\Kernel\Chart::toResource()
  185. */
  186. PHP_METHOD(vtiful_chart, toResource)
  187. {
  188. chart_object *obj = Z_CHART_P(getThis());
  189. RETURN_RES(zend_register_resource(&obj->ptr, le_xls_writer));
  190. }
  191. /* }}} */
  192. /** {{{ chart_methods
  193. */
  194. zend_function_entry chart_methods[] = {
  195. PHP_ME(vtiful_chart, __construct, chart_construct_arginfo, ZEND_ACC_PUBLIC)
  196. PHP_ME(vtiful_chart, series, chart_series_arginfo, ZEND_ACC_PUBLIC)
  197. PHP_ME(vtiful_chart, seriesName, chart_series_name_arginfo, ZEND_ACC_PUBLIC)
  198. PHP_ME(vtiful_chart, style, chart_style_arginfo, ZEND_ACC_PUBLIC)
  199. PHP_ME(vtiful_chart, axisNameY, chart_axis_name_arginfo, ZEND_ACC_PUBLIC)
  200. PHP_ME(vtiful_chart, axisNameX, chart_axis_name_arginfo, ZEND_ACC_PUBLIC)
  201. PHP_ME(vtiful_chart, title, chart_title_name_arginfo, ZEND_ACC_PUBLIC)
  202. PHP_ME(vtiful_chart, toResource, NULL, ZEND_ACC_PUBLIC)
  203. PHP_FE_END
  204. };
  205. /* }}} */
  206. VTIFUL_STARTUP_FUNCTION(chart)
  207. {
  208. zend_class_entry ce;
  209. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Chart", chart_methods);
  210. ce.create_object = chart_objects_new;
  211. vtiful_chart_ce = zend_register_internal_class(&ce);
  212. memcpy(&chart_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  213. chart_handlers.offset = XtOffsetOf(chart_object, zo);
  214. chart_handlers.free_obj = chart_objects_free;
  215. REGISTER_CLASS_CONST_LONG(vtiful_chart_ce, "CHART_LINE", LXW_CHART_LINE)
  216. REGISTER_CLASS_CONST_LONG(vtiful_chart_ce, "CHART_COLUMN", LXW_CHART_COLUMN)
  217. REGISTER_CLASS_CONST_LONG(vtiful_chart_ce, "CHART_AREA", LXW_CHART_AREA)
  218. return SUCCESS;
  219. }