Changeset 959466
- Timestamp:
- 08/03/2014 08:55:03 AM (12 years ago)
- Location:
- wp-custom-field-chart/trunk
- Files:
-
- 1 deleted
- 4 edited
-
ChartJs.php (modified) (5 diffs)
-
Field.php (modified) (4 diffs)
-
README.md (deleted)
-
readme.txt (modified) (4 diffs)
-
wp-custom-field-chart.php (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wp-custom-field-chart/trunk/ChartJs.php
r959428 r959466 40 40 } 41 41 42 function gen_html($ attr, $data, $options)42 function gen_html($fields, $data, $options) 43 43 { 44 return '<div class="' . $ attr['class']. '">' .45 $this->gen_canvas($ attr) .46 $this->gen_script($ attr, $data, $options) . '</div>';44 return '<div class="' . $fields->getValue('class') . '">' . 45 $this->gen_canvas($fields) . 46 $this->gen_script($fields, $data, $options) . '</div>'; 47 47 } 48 48 49 function gen_canvas($ attr)49 function gen_canvas($fields) 50 50 { 51 51 $out = '<canvas id="' . $this->sid . '" '; … … 54 54 'height' 55 55 ) as $key) { 56 $out .= $key . '="' . $ attr[$key]. '" ';56 $out .= $key . '="' . $fields->getValue($key) . '" '; 57 57 } 58 58 $out .= "/>\n"; … … 60 60 } 61 61 62 function gen_script($ attr, $data, $options = Null)62 function gen_script($fields, $data, $options = Null) 63 63 { 64 64 $vadata = 'null'; 65 65 $varopt = 'null'; 66 if ( key_exists('js_data', $attr)) {67 $vardata = $ attr['js_data'];66 if (!$fields->isNull('js_data')) { 67 $vardata = $fields->getValue('js_data'); 68 68 } 69 if ( key_exists('js_options', $attr)) {70 $varopt = $ attr['js_options'];69 if (!$fields->isNull('js_options')) { 70 $varopt = $fields->getValue('js_options'); 71 71 } 72 72 $varobj = $this->sid . 'Object'; 73 73 $out = "<script>\n"; 74 74 $out .= 'jQuery(window).load(function() {' . "\n"; 75 $out .= $this->gen_data($ attr, $data);75 $out .= $this->gen_data($fields, $data); 76 76 77 77 $out .= "var ctx = document.getElementById(\"" . 78 78 $this->sid . "\").getContext(\"2d\");\n"; 79 $out .= "var $varobj = new Chart(ctx)." . $ attr['kind'].79 $out .= "var $varobj = new Chart(ctx)." . $fields->getValue('kind') . 80 80 "($vardata, $varopt);\n"; 81 if (key_exists('js_hook', $attr)) {82 $out .= $attr['js_hook'] . "($varobj);\n";83 }84 81 $out .= "});</script>\n"; 85 82 return $out; 86 83 } 87 84 88 function gen_data($ attr, $data)85 function gen_data($fields, $data) 89 86 { 90 $vardata = $ attr['js_data'];91 $ fields = split(',', $attr['fields']);87 $vardata = $fields->getValue('js_data'); 88 $keys = $fields->getValue('fields'); 92 89 $out = $vardata . ".labels=[" . 93 90 join(',', … … 95 92 $data['labels'])) . 96 93 "];\n"; 97 foreach ($ fields as $idx => $name) {94 foreach ($keys as $idx => $name) { 98 95 $out .= $vardata . ".datasets[$idx].data=["; 99 96 foreach ($data['datasets'][$idx] as $key => $value) { … … 105 102 } 106 103 107 function gen_options($ attr, $options = Null)104 function gen_options($fields, $options = Null) 108 105 { 109 if ( is_null($options) || $options == '') {106 if ($fields->isNull('js_options')) { 110 107 return ''; 111 108 } 112 $vardata = $ attr['js_options'];109 $vardata = $fields->getValue('js_options'); 113 110 return "var $vardata=$options;\n"; 114 111 } -
wp-custom-field-chart/trunk/Field.php
r959428 r959466 37 37 } 38 38 39 function getValue($name) { 40 return $this->get($name)->getValue(); 41 } 42 43 function isNull($name) { 44 return $this->get($name)->isNull(); 45 } 46 39 47 function getPool() { 40 48 return $this->pool; … … 65 73 } 66 74 75 function getValue() { 76 return $this->value; 77 } 78 79 function isNull() { 80 return is_null($this->value); 81 } 82 67 83 function validate($value) 68 84 { … … 81 97 $value = $this->default; 82 98 } 99 if (!is_null($this->match)) { 100 if (!preg_match('/' . $this->match . '/i', $value)) { 101 throw new ErrorInvalidValue($this->name); 102 } 103 } 83 104 $cb = $this->callback; 84 105 if (!is_null($cb)) { … … 86 107 } 87 108 $this->value = $value; 88 if (!is_null($this->match)) {89 if (!preg_match('/' . $this->match . '/i', $value)) {90 throw new ErrorInvalidValue($this->name);91 }92 }93 109 $this->is_valid = True; 94 110 return $value; -
wp-custom-field-chart/trunk/readme.txt
r959434 r959466 5 5 Requires at least: 3.9.1 6 6 Tested up to: 3.9.1 7 Stable tag: 0.0. 37 Stable tag: 0.0.4 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 36 36 37 37 == Changelog == 38 = 0.0.4 = 39 * Add *round* and *dump* attribute 40 * Using *Field* class everywhere 38 41 = 0.0.3 = 39 42 * More attribute validation and default … … 118 121 = Notes = 119 122 1. js_data and js_options must reflect name given to your javascript variable. 120 2. Look a shttp://chartjs.org/ for documentation123 2. Look at http://chartjs.org/ for documentation 121 124 3. You don't need to supply labels and data (like in chartjs.org example) :) 122 125 123 126 124 == Tag attribute ==127 == Tag attributes == 125 128 = Required = 126 129 1. *fields*: Custom field separate by comma … … 137 140 1. *to_date*: Current date by default, post date if 'post' specified else value supplied 138 141 1. *dump*: Dumping attributes for debug (default: False) 142 1. *round*: Rounding data with specified precision 139 143 140 144 == Note == -
wp-custom-field-chart/trunk/wp-custom-field-chart.php
r959430 r959466 4 4 Plugin URI: http://wordpress.org/extend/plugins/wp-custom-field-chart 5 5 Description: Add Chart.js and graphs to your WordPress site based on tallies of any numeric custom field over time. Visualize progress toward any goal by day, week, month, or year. 6 Version: 0.0. 36 Version: 0.0.4 7 7 Author: Joachim Basmaison 8 8 Note: Based on Dylan Kuhn Tally wordpress plugin (GPLv2) … … 51 51 'Javascript variable holding our chart options'), 52 52 new Field('fields', true, Null, '^\w[\w\d,_-]+$', 53 'Comma separated wordpress custom field that we want to plot'), 53 'Comma separated wordpress custom field that we want to plot', function($v) { 54 return array_map(trim, explode(',', $v)); 55 }), 54 56 new Field('method', true, 'track', '^(track|cumulative|delta)', 55 57 'Aggregation method: track, cumulative, delta'), … … 60 62 'Class used for our HTML div element'), 61 63 new Field('to_date', false), 62 new Field('dump', false, false, null, 'Dumping attributes', 63 function($e) { 64 $e = strtolower($e); 65 if ($e == 'true' || $e == '1') { 66 return True; 67 } 68 return False; 69 } 70 ), 64 new Field('dump', false, false, null, 'Dumping attributes', function($e) { 65 $e = strtolower($e); 66 if ($e == 'true' || $e == '1') { 67 return True; 68 } 69 return False; 70 }), 71 new Field('round', false, null, '^\d+$', 72 'Rounding data with specified precision', function($e) { 73 return (int)$e; 74 }) 71 75 )); 72 76 … … 76 80 function custom_field_chart($atts) { 77 81 global $CFC_FIELDS; 78 $atts = wp_parse_args($atts);79 82 $chartJs = new WpCustomFieldChart\ChartJs(); 80 83 add_action('wp_enqueue_script', $chartJs->enqueue_script()); 81 84 try { 82 cfc_validate_attributes( $atts);85 cfc_validate_attributes(wp_parse_args($atts)); 83 86 } catch (WpCustomFieldChart\ErrorMissingAttribute $e) { 84 87 return cfc_get_field_error($e); … … 86 89 return cfc_get_field_error($e); 87 90 } 88 $data = cfc_collect_data($ atts);89 return $chartJs->gen_html($ atts, $data, $options);91 $data = cfc_collect_data($CFC_FIELDS); 92 return $chartJs->gen_html($CFC_FIELDS, $data, $options); 90 93 } 91 94 … … 130 133 } 131 134 132 function cfc_collect_data($ atts)135 function cfc_collect_data($fields) 133 136 { 134 137 global $wp_query; 135 $keys = split(',', $atts['fields']); 138 136 139 $end_time = time(); 137 if (isset($atts['to_date'])) { 138 if ($atts['to_date'] == 'post') { 140 if (!$fields->isNull('to_date')) { 141 $to_date = $fields->getValue('to_date'); 142 if ($to_date == 'post') { 139 143 $end_time = strtotime($wp_query->posts[0]->post_date); 140 144 } else { 141 145 $end_time = strtotime($atts['to_date']); 142 146 } 143 if (! $end_time) { 144 return 'Tally Graph: couldn\'t read the to_date ' . $atts['to_date']; 145 } 146 } 147 if (isset($atts['interval'])) { 148 $cfc_interval = $atts['interval']; 149 } else { 150 $cfc_interval = 'month'; 151 } 152 if (isset($atts['label_interval'])) { 153 $label_interval = $atts['label_interval']; 154 } else { 155 $label_interval = $cfc_interval; 156 } 157 $method = CFC_GRAPH_CUMULATIVE_METHOD; 158 if (isset($atts['method'])) { 159 $method = $atts['method']; 160 if (! in_array($method, array( 161 CFC_GRAPH_CUMULATIVE_METHOD, 162 CFC_GRAPH_DELTA_METHOD, 163 CFC_GRAPH_TRACK_METHOD 164 ))) { 165 return 'Tally Graph: Unknown method "' . $method . '"'; 166 } 167 } 168 $interval_count = $atts['interval_count']; 169 147 if (!$end_time) { 148 return 'WpCustomFieldChart: couldn\'t read the to_date: $to_date'; 149 } 150 } 151 $cfc_interval = $fields->getValue('interval'); 152 $label_interval = $cfc_interval; 153 $method = $fields->getValue('method'); 154 $interval_count = $fields->getValue('interval_count'); 155 $keys = $fields->getValue('fields'); 170 156 list ($index_gnu_format, $index_mysql_format, $first_day_suffix) = 171 157 cfc_graph_interval_settings($cfc_interval); 172 // Always start on the first day of the starting interval 173 // Always end on the first day after the ending interval 158 /* 159 * Always start on the first day of the starting interval 160 * Always end on the first day after the ending interval 161 */ 174 162 $start_time = strtotime('-' . $interval_count . ' ' . $cfc_interval, 175 163 $end_time); … … 188 176 $end_time = strtotime($next_date_prefix . $first_day_suffix); 189 177 } 190 191 // Tally ho192 178 $key_counts = array(); 193 179 $key_labels = array(); … … 195 181 $key_counts[$index] = cfc_graph_get_counts($key, $start_time, 196 182 $end_time, $cfc_interval, $method, $key_labels); 197 } 198 183 if (!$fields->isNull('round')) { 184 $round = $fields->getValue('round'); 185 foreach($key_counts[$index] as $idx => $value) { 186 $key_counts[$index][$idx] = round($value, $round); 187 } 188 } 189 } 199 190 $first_index = null; 200 191 $label_array = Array();
Note: See TracChangeset
for help on using the changeset viewer.