Tips on how to use dynamic data
-
The short answer is using the shortcode directly on template and populate its values with the query or function of choice.
Pasting this to your template code will print the chart with the hardwired dataset.
<?php echo do_shortcode( '[wp_charts title="mypie" type="pie" align="alignright" margin="5px 20px" data="10,32,50,25,5"]' ); ?>
To use dynamic data, you have to replace the values from the data for a variable or a function, using concatenation.
A simple example with a number array:
<?php $chartData = implode(',', array(10,32,50,25,5)); // Print the chart echo do_shortcode('[wp_charts title="poepie" type="pie" align="alignright" margin="5px 20px" data="'.$chartData.'"]'); ?>
An example using actual WordPress data:
<?php // Assign the number of posts published or drafted to an array $count_posts = wp_count_posts(); $draft_posts = $count_posts->draft; $published_posts = $count_posts->publish; $countPost = array($draft_posts,$published_posts); $chartNewData = implode(',', $countPost); // Print the chart echo do_shortcode('[wp_charts title="poebar" type="bar" align="alignright" margin="5px 20px" data="'.$draft_posts.'"]'); ?>
Sure enough you can embelish the charts with more parameters, but I hope you get the idea.
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘Tips on how to use dynamic data’ is closed to new replies.