class-easy-charts.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. <?php
  2. /**
  3. * The file that defines the core plugin class
  4. *
  5. * A class definition that includes attributes and functions used across both the
  6. * public-facing side of the site and the admin area.
  7. *
  8. * @link https://kiranpotphode.com/
  9. * @since 1.0.0
  10. *
  11. * @package Easy_Charts
  12. * @subpackage Easy_Charts/includes
  13. */
  14. /**
  15. * The core plugin class.
  16. *
  17. * This is used to define internationalization, admin-specific hooks, and
  18. * public-facing site hooks.
  19. *
  20. * Also maintains the unique identifier of this plugin as well as the current
  21. * version of the plugin.
  22. *
  23. * @since 1.0.0
  24. * @package Easy_Charts
  25. * @subpackage Easy_Charts/includes
  26. * @author Kiran Potphode <kiranpotphode15@gmail.com>
  27. */
  28. class Easy_Charts {
  29. /**
  30. * The loader that's responsible for maintaining and registering all hooks that power
  31. * the plugin.
  32. *
  33. * @since 1.0.0
  34. * @access protected
  35. * @var Easy_Charts_Loader $loader Maintains and registers all hooks for the plugin.
  36. */
  37. protected $loader;
  38. /**
  39. * The unique identifier of this plugin.
  40. *
  41. * @since 1.0.0
  42. * @access protected
  43. * @var string $plugin_name The string used to uniquely identify this plugin.
  44. */
  45. protected $plugin_name;
  46. /**
  47. * The current version of the plugin.
  48. *
  49. * @since 1.0.0
  50. * @access protected
  51. * @var string $version The current version of the plugin.
  52. */
  53. protected $version;
  54. /**
  55. * Define the core functionality of the plugin.
  56. *
  57. * Set the plugin name and the plugin version that can be used throughout the plugin.
  58. * Load the dependencies, define the locale, and set the hooks for the admin area and
  59. * the public-facing side of the site.
  60. *
  61. * @since 1.0.0
  62. */
  63. public function __construct() {
  64. $this->plugin_name = 'easy-charts';
  65. $this->version = '1.0.0';
  66. $this->load_dependencies();
  67. $this->set_locale();
  68. $this->define_admin_hooks();
  69. $this->define_public_hooks();
  70. }
  71. /**
  72. * Load the required dependencies for this plugin.
  73. *
  74. * Include the following files that make up the plugin:
  75. *
  76. * - Easy_Charts_Loader. Orchestrates the hooks of the plugin.
  77. * - Easy_Charts_i18n. Defines internationalization functionality.
  78. * - Easy_Charts_Admin. Defines all hooks for the admin area.
  79. * - Easy_Charts_Public. Defines all hooks for the public side of the site.
  80. *
  81. * Create an instance of the loader which will be used to register the hooks
  82. * with WordPress.
  83. *
  84. * @since 1.0.0
  85. * @access private
  86. */
  87. private function load_dependencies() {
  88. /**
  89. * The class responsible for orchestrating the actions and filters of the
  90. * core plugin.
  91. */
  92. require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-easy-charts-loader.php';
  93. /**
  94. * The class responsible for defining internationalization functionality
  95. * of the plugin.
  96. */
  97. require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-easy-charts-i18n.php';
  98. /**
  99. * The class responsible for defining all actions that occur in the admin area.
  100. */
  101. require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-easy-charts-admin.php';
  102. /**
  103. * The class responsible for defining all actions that occur in the public-facing
  104. * side of the site.
  105. */
  106. require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-easy-charts-public.php';
  107. $this->loader = new Easy_Charts_Loader();
  108. }
  109. /**
  110. * Define the locale for this plugin for internationalization.
  111. *
  112. * Uses the Easy_Charts_i18n class in order to set the domain and to register the hook
  113. * with WordPress.
  114. *
  115. * @since 1.0.0
  116. * @access private
  117. */
  118. private function set_locale() {
  119. $plugin_i18n = new Easy_Charts_i18n();
  120. $this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );
  121. }
  122. /**
  123. * Register all of the hooks related to the admin area functionality
  124. * of the plugin.
  125. *
  126. * @since 1.0.0
  127. * @access private
  128. */
  129. private function define_admin_hooks() {
  130. $plugin_admin = new Easy_Charts_Admin( $this->get_plugin_name(), $this->get_version() );
  131. $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
  132. $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
  133. $this->loader->add_action( 'init', $plugin_admin, 'init' );
  134. $this->loader->add_action( 'add_meta_boxes', $plugin_admin, 'add_meta_boxes' );
  135. $this->loader->add_action( 'save_post', $plugin_admin, 'easy_charts_save_meta_box_data' );
  136. $this->loader->add_action( 'admin_head', $plugin_admin, 'easy_charts_add_insert_chart_button' );
  137. $this->loader->add_action( 'admin_print_scripts', $plugin_admin, 'admin_print_scripts' );
  138. $this->loader->add_action( 'admin_menu', $plugin_admin, 'add_options_menu' );
  139. }
  140. /**
  141. * Register all of the hooks related to the public-facing functionality
  142. * of the plugin.
  143. *
  144. * @since 1.0.0
  145. * @access private
  146. */
  147. private function define_public_hooks() {
  148. $plugin_public = new Easy_Charts_Public( $this->get_plugin_name(), $this->get_version() );
  149. $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
  150. $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
  151. $this->loader->add_action( 'init', $plugin_public, 'init' );
  152. $this->loader->add_action( 'wp_ajax_easy_charts_save_chart_data', $plugin_public, 'init' );
  153. $this->loader->add_action( 'wp_ajax_easy_charts_get_published_charts', $plugin_public, 'init' );
  154. }
  155. /**
  156. * Run the loader to execute all of the hooks with WordPress.
  157. *
  158. * @since 1.0.0
  159. */
  160. public function run() {
  161. $this->loader->run();
  162. }
  163. /**
  164. * The name of the plugin used to uniquely identify it within the context of
  165. * WordPress and to define internationalization functionality.
  166. *
  167. * @since 1.0.0
  168. * @return string The name of the plugin.
  169. */
  170. public function get_plugin_name() {
  171. return $this->plugin_name;
  172. }
  173. /**
  174. * The reference to the class that orchestrates the hooks with the plugin.
  175. *
  176. * @since 1.0.0
  177. * @return Easy_Charts_Loader Orchestrates the hooks of the plugin.
  178. */
  179. public function get_loader() {
  180. return $this->loader;
  181. }
  182. /**
  183. * Retrieve the version number of the plugin.
  184. *
  185. * @since 1.0.0
  186. * @return string The version number of the plugin.
  187. */
  188. public function get_version() {
  189. return $this->version;
  190. }
  191. /**
  192. * Create dropdown of available chart types.
  193. *
  194. * @param array $args An array of arguments to create chart options dropdown.
  195. * @access private
  196. * @return string HTML string of output.
  197. */
  198. private function easy_chart_dropdown( $args = '' ) {
  199. $defaults = array(
  200. 'selected' => 0,
  201. 'echo' => 1,
  202. 'name' => 'ec_dropdown',
  203. 'id' => '',
  204. 'options' => array(),
  205. 'show_option_none' => '',
  206. 'show_option_no_change' => '',
  207. 'option_none_value' => '',
  208. );
  209. $r = wp_parse_args( $args, $defaults );
  210. $output = '';
  211. // Back-compat with old system where both id and name were based on $name argument.
  212. if ( empty( $r['id'] ) ) {
  213. $r['id'] = $r['name'];
  214. }
  215. if ( ! empty( $r['options'] ) ) {
  216. $output = "<select name='" . esc_attr( $r['name'] ) . "' id='" . esc_attr( $r['id'] ) . "' class='ec-dropdown-select'>\n";
  217. if ( $r['show_option_no_change'] ) {
  218. $output .= "\t<option value=\"-1\" " . selected( $r['selected'], -1, 0 ) . '>' . $r['show_option_no_change'] . "</option>\n";
  219. }
  220. if ( $r['show_option_none'] ) {
  221. $output .= "\t<option value=\"" . esc_attr( $r['option_none_value'] ) . '" ' . selected( $r['selected'], esc_attr( $r['option_none_value'] ), 0 ) . '>' . $r['show_option_none'] . "</option>\n";
  222. }
  223. foreach ( $r['options'] as $key => $value ) {
  224. $output .= "\t<option value=\"" . esc_attr( $key ) . '" ' . selected( $r['selected'], esc_attr( $key ), 0 ) . '>' . $value . "</option>\n";
  225. }
  226. $output .= "</select>\n";
  227. }
  228. /**
  229. * Filter the HTML output.
  230. *
  231. * @since 1.0.0
  232. *
  233. * @param string $output HTML output.
  234. */
  235. $html = apply_filters( 'easy_chart_dropdown', $output );
  236. if ( $r['echo'] ) {
  237. echo $html;
  238. }
  239. return $html;
  240. }
  241. /**
  242. * Helper to transpose any two dimensional array.
  243. *
  244. * @since 1.0.0
  245. * @access private
  246. *
  247. * @param array $array An 2D array to transpose.
  248. * @param integer|string $selectkey Key on which transpose the array Optional Default us false.
  249. *
  250. * @return array Transposed array.
  251. */
  252. private function array_transpose( $array, $selectkey = false ) {
  253. if ( ! is_array( $array ) ) {
  254. return false;
  255. }
  256. $return = array();
  257. foreach ( $array as $key => $value ) {
  258. if ( ! is_array( $value ) ) {
  259. return $array;
  260. }
  261. if ( $selectkey ) {
  262. if ( isset( $value[ $selectkey ] ) ) {
  263. $return[] = $value[ $selectkey ];
  264. }
  265. } else {
  266. foreach ( $value as $key2 => $value2 ) {
  267. $return[ $key2 ][ $key ] = $value2;
  268. }
  269. }
  270. }
  271. return $return;
  272. }
  273. /**
  274. * Get available font Family
  275. *
  276. * @since 1.0.3
  277. *
  278. * @return array Array of font-family.
  279. */
  280. public function get_font_family_array() {
  281. $font_family = array();
  282. $font_family = array( 'Arial' => 'Arial', 'Impact' => 'Impact', 'Palatino Linotype' => 'Palatino Linotype', 'Tahoma' => 'Tahoma', 'Century Gothic' => ' Century Gothic', 'Lucida Sans Unicode' => 'Lucida Sans Unicode', 'Arial Black' => 'Arial Black', 'Times New Roman' => 'Times New Roman', 'Arial Narrow' => 'Arial Narrow', 'Verdana' => 'Verdana', 'Lucida Console' => 'Lucida Console', 'Gill Sans' => 'Gill Sans', 'Trebuchet MS' => 'Trebuchet MS', 'Courier New' => 'Courier New', 'Georgia' => 'Georgia' );
  283. /**
  284. * Filter to add font family.
  285. *
  286. * @since 1.0.3
  287. *
  288. * @param array $font_family Array of font familly.
  289. */
  290. return apply_filters( 'easy_charts_fonts', $font_family );
  291. }
  292. /**
  293. * Get chart data.
  294. *
  295. * @since 1.0.0
  296. *
  297. * @param integer|null $chart_id ID of chart to retrieve data Default is null.
  298. *
  299. * @return array Array of data.
  300. */
  301. public function get_ec_chart_data( $chart_id = null ) {
  302. if ( null === $chart_id ) {
  303. return;
  304. }
  305. $ec_chart_data = array();
  306. $ec_chart_categories = array();
  307. $ec_chart_configuration = array();
  308. $ec_chart_type = get_post_meta( $chart_id, '_ec_chart_type', true );
  309. switch ( $ec_chart_type ) {
  310. case 'ec_bar_chart':
  311. $ec_chart_type = 'Bar';
  312. break;
  313. case 'ec_area_chart':
  314. $ec_chart_type = 'Area';
  315. break;
  316. case 'ec_stacked_bar_chart':
  317. $ec_chart_type = 'StackedBar';
  318. break;
  319. case 'ec_stacked_area_chart':
  320. $ec_chart_type = 'StackedArea';
  321. break;
  322. case 'ec_percent_bar_chart':
  323. $ec_chart_type = 'PercentBar';
  324. break;
  325. case 'ec_percent_area_chart':
  326. $ec_chart_type = 'PercentArea';
  327. break;
  328. case 'ec_pie_chart':
  329. $ec_chart_type = 'Pie';
  330. break;
  331. case 'ec_donut_chart':
  332. $ec_chart_type = 'Donut';
  333. break;
  334. case 'ec_step_up_bar_chart':
  335. $ec_chart_type = 'StepUpBar';
  336. break;
  337. case 'ec_waterfall_chart':
  338. $ec_chart_type = 'Waterfall';
  339. break;
  340. case 'ec_line_chart':
  341. $ec_chart_type = 'Line';
  342. break;
  343. case 'ec_polar_area_chart':
  344. $ec_chart_type = 'PolarArea';
  345. break;
  346. default:
  347. $ec_chart_type = 'Bar';
  348. break;
  349. }
  350. $ec_chart_dataset = json_decode( get_post_meta( $chart_id, '_easy_charts_chart_data', true ) );
  351. if ( null === $ec_chart_dataset ) {
  352. return;
  353. }
  354. if ( 'Pie' === $ec_chart_type || 'Donut' === $ec_chart_type || 'PolarArea' === $ec_chart_type || 'StepUpBar' === $ec_chart_type ) {
  355. $ec_chart_dataset = $this->array_transpose( $ec_chart_dataset );
  356. }
  357. $ec_chart_categories = array_shift( $ec_chart_dataset );
  358. array_shift( $ec_chart_categories );
  359. $translated_dataset = array();
  360. foreach ( $ec_chart_categories as $key => $ec_chart_category ) {
  361. $translated_dataset[ $ec_chart_category ] = array();
  362. foreach ( $ec_chart_dataset as $data_key => $data_value ) {
  363. $translated_dataset[ $ec_chart_category ][] = array(
  364. 'name' => $data_value[0],
  365. 'value' => floatval( $data_value[ $key + 1 ] ),
  366. );
  367. }
  368. }
  369. $ec_chart_graph = $this->ec_get_chart_configuration( $chart_id, 'graph' );
  370. $ec_chart_meta = $this->ec_get_chart_configuration( $chart_id, 'meta' );
  371. $ec_chart_dimension = $this->ec_get_chart_configuration( $chart_id, 'dimension' );
  372. $ec_chart_margin = $this->ec_get_chart_configuration( $chart_id, 'margin' );
  373. $ec_chart_frame = $this->ec_get_chart_configuration( $chart_id, 'frame' );
  374. $ec_chart_axis = $this->ec_get_chart_configuration( $chart_id, 'axis' );
  375. $ec_chart_label = $this->ec_get_chart_configuration( $chart_id, 'label' );
  376. $ec_chart_legend = $this->ec_get_chart_configuration( $chart_id, 'legend' );
  377. $ec_chart_scale = $this->ec_get_chart_configuration( $chart_id, 'scale' );
  378. $ec_chart_tooltip = $this->ec_get_chart_configuration( $chart_id, 'tooltip' );
  379. $ec_chart_caption = $this->ec_get_chart_configuration( $chart_id, 'caption' );
  380. $ec_chart_subcaption = $this->ec_get_chart_configuration( $chart_id, 'subcaption' );
  381. $ec_chart_bar = $this->ec_get_chart_configuration( $chart_id, 'bar' );
  382. $ec_chart_line = $this->ec_get_chart_configuration( $chart_id, 'line' );
  383. $ec_chart_area = $this->ec_get_chart_configuration( $chart_id, 'area' );
  384. $ec_chart_pie = $this->ec_get_chart_configuration( $chart_id, 'pie' );
  385. $ec_chart_donut = $this->ec_get_chart_configuration( $chart_id, 'donut' );
  386. $ec_chart_data = array(
  387. 'chart_type' => $ec_chart_type,
  388. 'chart_data' => $translated_dataset,
  389. 'chart_categories' => $ec_chart_categories,
  390. 'chart_configuration' => array(
  391. 'graph' => $ec_chart_graph,
  392. 'meta' => $ec_chart_meta,
  393. 'frame' => $ec_chart_frame,
  394. 'axis' => $ec_chart_axis,
  395. 'dimension' => $ec_chart_dimension,
  396. 'label' => $ec_chart_label,
  397. 'legend' => $ec_chart_legend,
  398. 'scale' => $ec_chart_scale,
  399. 'tooltip' => $ec_chart_tooltip,
  400. 'caption' => $ec_chart_caption,
  401. 'subCaption' => $ec_chart_subcaption,
  402. 'bar' => $ec_chart_bar,
  403. 'line' => $ec_chart_line,
  404. 'area' => $ec_chart_area,
  405. 'pie' => $ec_chart_pie,
  406. 'donut' => $ec_chart_donut,
  407. ),
  408. );
  409. /**
  410. * Filter for get data of chart require to render chart.
  411. *
  412. * @since 1.0.3
  413. *
  414. * @param array $ec_chart_data All chart related data.
  415. * @param int $chart_is Chart id.
  416. */
  417. $ec_chart_data = apply_filters( 'easy_charts_get_chart_data', $ec_chart_data, $chart_id );
  418. return $ec_chart_data;
  419. }
  420. /**
  421. * Render easy chart.
  422. *
  423. * @since 1.0.0
  424. *
  425. * @param integer|null $chart_id Chart id which is to be rendered.
  426. *
  427. * @return string html markup for chart container.
  428. */
  429. public function ec_render_chart( $chart_id = null ) {
  430. $chart_html = '';
  431. if ( $chart_id ) {
  432. $chart = get_post( $chart_id );
  433. if ( 'easy_charts' !== $chart->post_type ) {
  434. return;
  435. }
  436. $ec_chart_data = $this->get_ec_chart_data( $chart_id );
  437. if ( null !== $ec_chart_data ) {
  438. if ( is_admin() ) {
  439. wp_localize_script( 'easy-charts-admin-js', 'ec_chart_data', $ec_chart_data );
  440. } else {
  441. wp_localize_script( 'easy-charts-public-js', 'ec_object_'.$chart_id, $ec_chart_data );
  442. }
  443. }
  444. $chart_html = '<div class="ec-uv-chart-container uv-div-'.$chart_id.'" data-object="ec_object_'.$chart_id.'"></div>';
  445. /**
  446. * Filter to replace html content of chart.
  447. *
  448. * @since 1.0.3
  449. *
  450. * @param string $chart_html HTML of chart to render.
  451. * @param int $chart_id Chart ID.
  452. */
  453. $chart_html = apply_filters( 'easy_charts_render_chart', $chart_html, $chart_id );
  454. return $chart_html;
  455. }
  456. return;
  457. }
  458. /**
  459. * Get chart configuration options.
  460. *
  461. * @since 1.0.0
  462. *
  463. * @param integer|null $chart_id Chart id.
  464. * @param string $meta_key Meta key of configuration.
  465. *
  466. * @return array Array of configuration.
  467. */
  468. public function ec_get_chart_configuration( $chart_id = null, $meta_key = '' ) {
  469. $ec_chart_option = get_post_meta( $chart_id, '_ec_chart_'.$meta_key, true );
  470. if ( '' === $ec_chart_option ) {
  471. switch ( $meta_key ) {
  472. case 'meta':
  473. $ec_chart_option = array(
  474. 'position' => '#uv-div',
  475. 'caption' => '',
  476. 'subcaption' => '',
  477. 'hlabel' => '',
  478. 'hsublabel' => '',
  479. 'vlabel' => '',
  480. 'vsublabel' => '',
  481. 'isDownloadable' => 0,
  482. 'downloadLabel' => 'Download',
  483. );
  484. break;
  485. case 'graph':
  486. $ec_chart_option = array(
  487. 'palette' => 'Brink',
  488. 'responsive' => 1,
  489. 'bgcolor' => '#ffffff',
  490. 'orientation' => 'Horizontal',
  491. 'opacity' => 1,
  492. );
  493. break;
  494. case 'dimension':
  495. $ec_chart_option = array(
  496. 'width' => 400,
  497. 'height' => 400,
  498. );
  499. break;
  500. case 'margin':
  501. $ec_chart_option = array(
  502. 'top' => 50,
  503. 'bottom' => 150,
  504. 'left' => 100,
  505. 'right' => 100,
  506. );
  507. break;
  508. case 'frame':
  509. $ec_chart_option = array(
  510. 'bgcolor' => '#ffffff',
  511. );
  512. break;
  513. case 'axis':
  514. $ec_chart_option = array(
  515. 'opacity' => 0.1,
  516. 'ticks' => 8,
  517. 'subticks' => 2,
  518. 'padding' => 5,
  519. 'strokecolor' => '#000000',
  520. 'minor' => -10,
  521. 'fontfamily' => 'Arial',
  522. 'fontsize' => 11,
  523. 'fontweight' => 700,
  524. 'showticks' => 1,
  525. 'showsubticks' => 1,
  526. 'showtext' => 1,
  527. );
  528. break;
  529. case 'label':
  530. $ec_chart_option = array(
  531. 'fontfamily' => 'Arial',
  532. 'fontsize' => 11,
  533. 'fontweight' => 700,
  534. 'strokecolor' => '#000000',
  535. 'showlabel' => 1,
  536. 'precision' => 2,
  537. 'prefix' => '',
  538. 'suffix' => '',
  539. );
  540. break;
  541. case 'legend':
  542. $ec_chart_option = array(
  543. 'position' => 'bottom',
  544. 'fontfamily' => 'Arial',
  545. 'fontsize' => '11',
  546. 'fontweight' => 'normal',
  547. 'color' => '#000000',
  548. 'strokewidth' => 0.15,
  549. 'textmargin' => 15,
  550. 'symbolsize' => 10,
  551. 'inactivecolor' => '#DDD',
  552. 'legendstart' => 0,
  553. 'legendtype' => 'categories',
  554. 'showlegends' => true,
  555. );
  556. break;
  557. case 'scale':
  558. $ec_chart_option = array(
  559. 'type' => 'linear',
  560. 'ordinality' => 0.2,
  561. );
  562. break;
  563. case 'tooltip':
  564. $ec_chart_option = array(
  565. 'show' => 1,
  566. 'format' => '%c [%l] : %v',
  567. );
  568. break;
  569. case 'caption':
  570. $ec_chart_option = array(
  571. 'fontfamily' => 'Arial',
  572. 'fontsize' => 11,
  573. 'fontweight' => 700,
  574. 'textdecoration' => 'none',
  575. 'strokecolor' => '#000000',
  576. 'cursor' => 'pointer',
  577. );
  578. break;
  579. case 'subcaption':
  580. $ec_chart_option = array(
  581. 'fontfamily' => 'Arial',
  582. 'fontsize' => 11,
  583. 'fontweight' => 700,
  584. 'textdecoration' => 'none',
  585. 'strokecolor' => '#000000',
  586. 'cursor' => 'pointer',
  587. );
  588. break;
  589. case 'bar':
  590. $ec_chart_option = array(
  591. 'fontfamily' => 'Arial',
  592. 'fontsize' => 10,
  593. 'fontweight' => 700,
  594. 'strokecolor' => 'none',
  595. 'textcolor' => '#000000',
  596. );
  597. break;
  598. case 'line':
  599. $ec_chart_option = array(
  600. 'interpolation' => 'linear',
  601. );
  602. break;
  603. case 'area':
  604. $ec_chart_option = array(
  605. 'interpolation' => 'linear',
  606. 'opacity' => 0.2,
  607. 'offset' => 'zero',
  608. );
  609. break;
  610. case 'pie':
  611. $ec_chart_option = array(
  612. 'fontfamily' => 'Arial',
  613. 'fontsize' => 11,
  614. 'fontweight' => 700,
  615. 'fontvariant' => 'small-caps',
  616. 'fontfill' => '#000000',
  617. 'strokecolor' => '#ffffff',
  618. 'strokewidth' => 1,
  619. );
  620. break;
  621. case 'donut':
  622. $ec_chart_option = array(
  623. 'fontfamily' => 'Arial',
  624. 'fontsize' => 11,
  625. 'fontweight' => 700,
  626. 'fontvariant' => 'small-caps',
  627. 'fontfill' => '#000000',
  628. 'strokecolor' => '#ffffff',
  629. 'strokewidth' => 1,
  630. 'factor' => 0.4,
  631. );
  632. break;
  633. default:
  634. break;
  635. }
  636. }
  637. /**
  638. * Filter to get configuration options for selected meta key.
  639. *
  640. * @since 1.0.3
  641. *
  642. * @param array $ec_chart_option Chart configuration options.
  643. * @param int $chart_id ID of chart.
  644. * @param string $meta_key Meta key for which to get options.
  645. */
  646. $ec_chart_option = apply_filters( 'easy_charts_get_chart_configurations', $ec_chart_option, $chart_id, $meta_key );
  647. return $ec_chart_option;
  648. }
  649. /**
  650. * Render input field.
  651. *
  652. * @since 1.0.0
  653. *
  654. * @param string $field_type Type of input field.
  655. * @param string $field_name Name of field to refer in form.
  656. * @param string $field_label Label to display along with input field.
  657. * @param string $field_value Value of input field.
  658. * @param array $field_options Optional array of options for input field.
  659. */
  660. public function ec_render_field( $field_type, $field_name, $field_label, $field_value, $field_options = array() ) {
  661. switch ( $field_type ) {
  662. case 'text': ?>
  663. <div class="field">
  664. <table>
  665. <tbody>
  666. <tr>
  667. <td class="ec-td-label">
  668. <label><?php esc_html_e( $field_label ); ?> :</label>
  669. </td>
  670. <td class="ec-td-field">
  671. <input type="text" name="<?php echo esc_attr( $field_name ); ?>" value="<?php echo esc_attr( $field_value ); ?>" />
  672. </td>
  673. </tr>
  674. </tbody>
  675. </table>
  676. </div>
  677. <?php break;
  678. case 'number': ?>
  679. <div class="field">
  680. <table>
  681. <tbody>
  682. <tr>
  683. <td class="ec-td-label">
  684. <label><?php esc_html_e( $field_label ); ?> :</label>
  685. </td>
  686. <td class="ec-td-field">
  687. <input type="number" name="<?php echo esc_attr( $field_name ); ?>" min="<?php echo esc_attr( isset( $field_options['min'] ) ? $field_options['min'] : '' ); ?>" max="<?php echo esc_attr( isset( $field_options['max'] ) ? $field_options['max'] : '' ); ?>" step="<?php echo esc_attr( isset( $field_options['step'] ) ? $field_options['step'] : '' ); ?>" value="<?php echo esc_attr( $field_value ); ?>" class="ec-field-number"/>
  688. </td>
  689. </tr>
  690. </tbody>
  691. </table>
  692. </div>
  693. <?php break;
  694. case 'radio': ?>
  695. <div class="field">
  696. <table>
  697. <tbody>
  698. <tr>
  699. <td class="ec-td-label">
  700. <label><?php esc_html_e( $field_label ); ?> :</label>
  701. </td>
  702. <td class="ec-td-field">
  703. <div class="ec-field-buttonset">
  704. <?php foreach ( $field_options as $key => $value ) { ?>
  705. <input name="<?php echo esc_attr( $field_name ); ?>" id="<?php echo esc_attr( $field_name.$value ); ?>" type="radio" value="<?php echo esc_attr( $value ); ?>" <?php checked( $value, $field_value ); ?> /><label for="<?php echo esc_attr( $field_name.$value ); ?>"><?php echo esc_html( $key ); ?></label>
  706. <?php } ?>
  707. </div>
  708. </td>
  709. </tr>
  710. </tbody>
  711. </table>
  712. </div>
  713. <?php break;
  714. case 'slider': ?>
  715. <div class="field">
  716. <table>
  717. <tbody>
  718. <tr>
  719. <td class="ec-td-label">
  720. <label><?php esc_html_e( $field_label ); ?> :</label>
  721. </td>
  722. <td class="ec-td-field">
  723. <input type="text" name="<?php echo esc_attr( $field_name ); ?>" class="ec-field-slider-attach <?php echo esc_attr( $field_name ); ?>" readonly value="<?php echo esc_attr( $field_value ); ?>" />
  724. <div class="ec-field-slider" data-attach=".<?php echo esc_attr( $field_name ); ?>"></div>
  725. </td>
  726. </tr>
  727. </tbody>
  728. </table>
  729. </div>
  730. <?php break;
  731. case 'color-picker' : ?>
  732. <div class="field">
  733. <table>
  734. <tbody>
  735. <tr>
  736. <td class="ec-td-label">
  737. <label><?php esc_html_e( $field_label ); ?> :</label>
  738. </td>
  739. <td class="ec-td-field">
  740. <input type="text" name="<?php echo esc_attr( $field_name ); ?>" class="ec-color-picker" value="<?php echo esc_attr( $field_value ); ?>" />
  741. </td>
  742. </tr>
  743. </tbody>
  744. </table>
  745. </div>
  746. <?php break;
  747. case 'dropdown' : ?>
  748. <div class="field">
  749. <table>
  750. <tbody>
  751. <tr>
  752. <td class="ec-td-label">
  753. <label><?php esc_html_e( $field_label ); ?> :</label>
  754. </td>
  755. <td class="ec-td-field">
  756. <?php $args = array(
  757. 'options' => $field_options,
  758. 'selected' => $field_value,
  759. 'id' => $field_name,
  760. 'name' => $field_name,
  761. 'echo' => 1,
  762. );
  763. $this->easy_chart_dropdown( $args );
  764. ?>
  765. </td>
  766. </tr>
  767. </tbody>
  768. </table>
  769. </div>
  770. <?php break;
  771. default:
  772. break;
  773. }
  774. }
  775. }