Changeset 959428
- Timestamp:
- 08/03/2014 03:31:58 AM (12 years ago)
- Location:
- wp-custom-field-chart/trunk
- Files:
-
- 4 edited
-
ChartJs.php (modified) (1 diff)
-
Field.php (modified) (5 diffs)
-
readme.txt (modified) (4 diffs)
-
wp-custom-field-chart.php (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wp-custom-field-chart/trunk/ChartJs.php
r957730 r959428 62 62 function gen_script($attr, $data, $options = Null) 63 63 { 64 $vardata = $attr['js_data']; 65 $varopt = '{}'; 64 $vadata = 'null'; 65 $varopt = 'null'; 66 if (key_exists('js_data', $attr)) { 67 $vardata = $attr['js_data']; 68 } 69 if (key_exists('js_options', $attr)) { 70 $varopt = $attr['js_options']; 71 } 66 72 $varobj = $this->sid . 'Object'; 67 73 $out = "<script>\n"; 68 74 $out .= 'jQuery(window).load(function() {' . "\n"; 69 75 $out .= $this->gen_data($attr, $data); 70 if (key_exists('js_options', $attr)) { 71 // out .= $this->gen_options($attr, $options); 72 $varopt = $attr['js_options']; 73 } 76 74 77 $out .= "var ctx = document.getElementById(\"" . 75 78 $this->sid . "\").getContext(\"2d\");\n"; -
wp-custom-field-chart/trunk/Field.php
r957730 r959428 5 5 namespace WpCustomFieldChart; 6 6 7 class ErrorMissingAttribute extends \ErrorException 8 { 7 class ErrorMissingAttribute extends \ErrorException {}; 8 class ErrorInvalidValue extends \ErrorException {}; 9 class ErrorAddingSameField extends \ErrorException {}; 10 class ErrorNonExistingField extends \ErrorException {}; 11 12 class FieldArray { 13 private $pool = array(); 14 15 function __construct($fields=Null) { 16 $this->populate($fields); 17 } 18 19 function add(Field $field) { 20 if (key_exists($field->name, $this->pool)) { 21 throw new ErrorAddingSameField($field->name); 22 } 23 $this->pool[$field->name] = $field; 24 } 25 26 function get($name) { 27 if (!key_exists($name, $this->pool)) { 28 throw new ErrorNonExistingField($name); 29 } 30 return $this->pool[$name]; 31 } 32 33 function populate($fields) { 34 foreach($fields as $field) { 35 $this->add($field); 36 } 37 } 38 39 function getPool() { 40 return $this->pool; 41 } 9 42 } 10 ;11 43 12 44 class Field … … 19 51 public $info = Null; 20 52 public $callback = Null; 53 public $value = Null; 54 public $is_valid = False; 21 55 22 56 function __construct($name, $required, $default = Null, $match = Null, … … 33 67 function validate($value) 34 68 { 69 $this->is_valid = False; 35 70 if ($value == '') { 36 71 $value = Null; 72 } 73 if (is_null($value) && !$this->required) { 74 $this->value = Null; 75 return Null; 37 76 } 38 77 if (is_null($value) && $this->required) { … … 40 79 throw new ErrorMissingAttribute($this->name); 41 80 } 42 return$this->default;81 $value = $this->default; 43 82 } 44 83 $cb = $this->callback; … … 46 85 $value = $cb($value); 47 86 } 87 $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 $this->is_valid = True; 48 94 return $value; 49 95 } 50 96 51 function make_error_message( )97 function make_error_message($e) 52 98 { 99 $msg = ""; 100 if ($e instanceof ErrorMissingAttribute) { 101 $msg .= '<b>Missing attribute:</b> ' . $this->name . '<br>'; 102 $msg .= '<b>Attribute information:</b> ' . $this->info . ''; 103 } elseif ($e instanceof ErrorInvalidValue) { 104 $msg .= '<b>Invalid value for '. $this->name . ':</b> ' . 105 $this->value . ' (regex: ' . $this->match . ')<br>'; 106 $msg .= '<b>Attribute information:</b> ' . $this->info . ''; 107 } else { 108 $msg .= "<b>Unknown error for attribute:</b> " . $this->name 109 . '<br>'; 110 $msg .= '<b>Attribute information:</b> ' . $this->info . ''; 111 } 53 112 $out = '<div class="cfc-error" style="background-color: black; ' . 54 113 'color:white; padding: 0.5em 1em; font-family: Arial; ' . 55 114 'border-style: solid; border-width:2px; border-color: red">'; 56 115 $out .= '<h2>Wordpress Extension Error / Custom Field Chart</h2>'; 57 $out .= '<b>Missing attribute:</b> ' . $this->name . '<br>'; 58 $out .= '<b>Attribute information:</b> ' . $this->info . ''; 116 $out .= $msg; 59 117 $out .= '</div>'; 60 118 return $out; -
wp-custom-field-chart/trunk/readme.txt
r958396 r959428 5 5 Requires at least: 3.9.1 6 6 Tested up to: 3.9.1 7 Stable tag: 0.0. 27 Stable tag: 0.0.3 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html 10 10 11 Make chart from *custom field* using *Chart.js* library 11 Make chart from custom field attached to your post/page using Chart.js 12 javascript library. 12 13 13 14 == Description == 14 15 This plugin collect data attached to post/article via *custom field* and make 15 This plugin collect data attached to post/article via **custom field** and make 16 16 chart of it. 17 This plugin use * Chart.js* for chart drawing [ChartJs](http://www.chartjs.org/)17 This plugin use **Chart.js** for chart drawing [ChartJs](http://www.chartjs.org/) 18 18 19 19 Data are collected by looking for specific *custom field* attached to your 20 post/page. You can change aggregation method and intervall, chart multiple 21 field in one chart... 20 post/page. You can change aggregation method, intervall... 21 22 See [usage](http://wordpress.org/plugins/wp-custom-field-chart/other_notes/) 22 23 23 24 == Installation == 24 25 25 1. Upload the entire `wp-custom-field-chart` folder to the `/wp-content/plugins/` directory 26 26 2. Activate the plugin through the 'Plugins' menu in WordPress 27 3. include [custom_field_chart] tag in your post/page (See Other Notes)27 3. Include [custom_field_chart] tag in your post/page (See Other Notes) 28 28 29 29 == Frequently Asked Questions == … … 31 31 32 32 == Screenshots == 33 1. One field for each chart 34 2. Combined field in one chart 35 3. Bar char with different period33 1. One field for each chart (Two tags) 34 2. Combined field in one chart (One Tag) 35 3. Bar chart with different period 36 36 37 37 == Changelog == 38 38 = 0.0.3 = 39 * More attribute validation and default 40 * Now as to_date default, introducing post to specify post data as to_date 41 * Better readme, well more informations... 39 42 = 0.0.2 = 40 43 * Uploading some screenshots … … 47 50 Edit your post/page in text mode and put some Javascript and a Wordpress tag 48 51 52 = Minimum = 53 ` 54 <script> 55 var mydata = { datasets: [{}]}; 56 </script> 57 [custom_field_chart fields="humidity" js_data="mydata"] 58 ` 59 For each field you need to put empty {} into datasets. 60 61 For two fields: 62 ` 63 <script> 64 var mydata = {datasets: [{},{}]} 65 </script> 66 [custom_field_chart fields="humidity,temperature" js_data="mydata"] 67 ` 68 But it's pretty useless to put more than one field without different colors :) 69 70 = More = 49 71 ` 50 72 <script> … … 100 122 3. You don't need to supply labels and data (like in chartjs.org example) :) 101 123 102 = Tag options =124 = Tag options (required)= 103 125 1. fields: Custom field separate by comma 104 1. method: Aggregate method (track, delta, cumulative)105 126 1. js_data: Name of javascript variable holding chart datasets 127 = Tag options (optional) 128 1. width: Chart width (default: 400) 129 1. height: Chart Height (default: 200) 130 1. method: Aggregate method track, delta or cumulative (defaul: track) 106 131 1. js_options: Name of javascript variable holding chart options 107 1. kind: Chart type (line or bar) 108 1. width: Chart width 109 1. height: Chart Height 110 132 1. kind: Chart type line or bar (default: line) 133 1. to_date: Current date by default, post date if 'post' specified else value supplied 134 1. dump: Dumping attributes for debug (default: False) 111 135 == Note == 112 136 Beta software... Interface may change. -
wp-custom-field-chart/trunk/wp-custom-field-chart.php
r957731 r959428 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. 16 Version: 0.0.2 7 7 Author: Joachim Basmaison 8 8 Note: Based on Dylan Kuhn Tally wordpress plugin (GPLv2) … … 38 38 39 39 use WpCustomFieldChart\Field as Field; 40 41 $CFC_FIELDS = array( 40 use WpCustomFieldChart\FieldArray; 41 42 43 $CFC_FIELDS = new FieldArray(array( 42 44 new Field('width', true, 400, '^\d+$', 'Chart width'), 43 45 new Field('height', true, 200, '^\d+$', 'Chart height'), … … 46 48 new Field('js_data', true, Null, '^\w[\w\d_]+$', 47 49 'Javascript variable holding our chart datasets'), 48 new Field('js_option ', false, Null, '^\w[\w\d_]+$',50 new Field('js_options', false, Null, '^\w[\w\d_]+$', 49 51 'Javascript variable holding our chart options'), 50 // new Field('period', true, 'month', '^(year|month|day)$', 51 // 'Period: year, month, day'), 52 new Field('fields', true, Null, '^\w[\w\d_-]+$', 52 new Field('fields', true, Null, '^\w[\w\d,_-]+$', 53 53 'Comma separated wordpress custom field that we want to plot'), 54 54 new Field('method', true, 'track', '^(track|cumulative|delta)', 55 55 'Aggregation method: track, cumulative, delta'), 56 new Field('interval', true, ' month', '^(year|month|day)$',56 new Field('interval', true, 'day', '^(year|month|day)$', 57 57 'Period: year, month, day'), 58 new Field('interval_count', true, 6, '^\d+$', 'Interval count'),58 new Field('interval_count', true, 31, '^\d+$', 'Interval count'), 59 59 new Field('class', true, 'cfc-chart', '^[\w\d-_]+$', 60 60 'Class used for our HTML div element'), 61 new Field('to_date', false) 62 ); 61 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 ), 71 )); 63 72 64 73 /************************ … … 73 82 cfc_validate_attributes($atts); 74 83 } catch (WpCustomFieldChart\ErrorMissingAttribute $e) { 75 return cfc_get_field_error($e->getMessage()); 84 return cfc_get_field_error($e); 85 } catch (WpCustomFieldChart\ErrorInvalidValue $e) { 86 return cfc_get_field_error($e); 76 87 } 77 88 $data = cfc_collect_data($atts); … … 79 90 } 80 91 81 function cfc_get_field_error($ name) {92 function cfc_get_field_error($e) { 82 93 global $CFC_FIELDS; 83 foreach($CFC_FIELDS as $field) { 94 $name = $e->getMessage(); 95 foreach($CFC_FIELDS->getPool() as $key => $field) { 84 96 if ($field->name != $name) { 85 97 continue; 86 98 } 87 return $field->make_error_message( );99 return $field->make_error_message($e); 88 100 } 89 101 return "Unknow field $name"; … … 93 105 { 94 106 global $CFC_FIELDS; 95 foreach ($CFC_FIELDS as $field) { 96 $atts[$field->name] = $field->validate($atts[$field->name]); 107 $dump = False; 108 if (key_exists('dump', $atts)) { 109 $dump = $CFC_FIELDS->get('dump')->validate($atts['dump']); 110 } 111 foreach ($CFC_FIELDS->getPool() as $key => $field) { 112 $value = $field->validate($atts[$field->name]); 113 if (is_null($value)) { 114 unset($atts[$field->name]); 115 } else { 116 $atts[$field->name] = $value; 117 } 118 if ($dump) { 119 echo "$key: $value<br>\n"; 120 } 97 121 } 98 122 } … … 109 133 { 110 134 global $wp_query; 111 112 // $atts = wp_parse_args($atts);113 // $defaults = array(114 // 'interval_count' => '6',115 // 'chs' => '200x200',116 // 'cht' => 'bvs'117 // );118 // $atts = array_merge($defaults, $atts);119 // if (! isset($atts['fields']))120 // return 'Tally Graph: required parameter "key" is missing.';121 122 // Extract non-chart variables from attributes123 135 $keys = split(',', $atts['fields']); 124 // unset($atts['fields']); 125 // $use_cache = true; 126 // if (isset($atts['no_cache'])) { 127 // $use_cache = false; 128 // unset($atts['no_cache']); 129 // } 136 $end_time = time(); 130 137 if (isset($atts['to_date'])) { 131 $end_time = strtotime($atts['to_date']); 132 if (! $end_time) { 133 return 'Tally Graph: couldn\'t read the to_date ' . 134 $atts['to_date']; 135 } 136 // unset($atts['to_date']); 137 } else 138 if ($wp_query->post_count > 0) { 138 if ($atts['to_date'] == 'post') { 139 139 $end_time = strtotime($wp_query->posts[0]->post_date); 140 140 } else { 141 $end_time = time(); 142 } 141 $end_time = strtotime($atts['to_date']); 142 } 143 if (! $end_time) { 144 return 'Tally Graph: couldn\'t read the to_date ' . $atts['to_date']; 145 } 146 } 143 147 if (isset($atts['interval'])) { 144 148 $cfc_interval = $atts['interval']; 145 // unset($atts['interval']);146 149 } else { 147 150 $cfc_interval = 'month'; … … 149 152 if (isset($atts['label_interval'])) { 150 153 $label_interval = $atts['label_interval']; 151 // unset( $atts['label_interval'] );152 154 } else { 153 155 $label_interval = $cfc_interval; … … 163 165 return 'Tally Graph: Unknown method "' . $method . '"'; 164 166 } 165 // unset($atts['method']);166 167 } 167 168 $interval_count = $atts['interval_count']; 168 // unset($atts['interval_count']);169 169 170 170 list ($index_gnu_format, $index_mysql_format, $first_day_suffix) = … … 197 197 } 198 198 199 // Build the chart parameters200 199 $first_index = null; 201 200 $label_array = Array();
Note: See TracChangeset
for help on using the changeset viewer.