chart.c 7.4 KB

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