Support » Plugin: WordPress Charts » 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.

    • This topic was modified 2 years, 9 months ago by  João Miguel.
    • This topic was modified 2 years, 9 months ago by  João Miguel.
Viewing 8 replies - 1 through 8 (of 8 total)
  • A minor correction to the WordPress data chart. Please replace the variables in the shortcode, as below.

    // Print the chart 
    echo do_shortcode('[wp_charts title="poebar" type="bar" align="alignright" margin="5px 20px" data="'.$chartNewData.'"]');

    There seems to be an issue when your suggested code (above) is run on a WordPress Post/Page, in combination with the Allow PHP Execute plug-in.

    Have verified PHP code not containing [wp_charts] shortcode, works as intended, such as:

    Your IP Address is: <?php echo $_SERVER['REMOTE_ADDR']; ?>

    Could you help me with echo do_shortcode. I use this, as you showed with Allow PHP Execute and chart is not displayed. Other shortcodes work, but this with chart does not. There is only a background of chart without a content.

    Code:

    <p>Your IP Address is: <?php echo $_SERVER['REMOTE_ADDR']; ?></p>
    <p><?php
    echo do_shortcode('[mlw_quizmaster quiz=34]');
    ?><br />
    <?php
    echo do_shortcode('[wp_charts title="poebar" type="bar" align="alignright" margin="5px 20px" data="1,2"]');
    ?></p>
    

    looks like that:
    http://prawdziwyskarb.pl/test-do-shortcode/

    Could you help me with that?

    Michalmic, have you tried hardwiring the shortcode to your template file, like I showed in the first example??

    <?php echo do_shortcode( '[wp_charts title="mypie" type="pie" align="alignright" margin="5px 20px" data="10,32,50,25,5"]' ); ?>

    Of course, you have to create the chart with WP Charts first…

    Hi João Miguel, thanks for your help. It works, but I had to use global variables:

    <?php
    $GLOBALS['data'] = "1,2,3";
    echo do_shortcode( '[wp_charts title="mypie" type="pie" align="alignright" margin="5px 20px" data="'.$GLOBALS['data'].'"]' ); 
    ?>

    Thanks for help.

    little confused.

    what codes needs to be added into the template and what code into the page? (for your example)

    this kind of code you put to template:

    <?php
    echo do_shortcode( ‘[wp_charts title=”mypie” type=”pie” align=”alignright” margin=”5px 20px” data=”1,2″]’ );
    ?>

    and this kind into pages or posts:

    [wp_charts title=”mypie” type=”pie” align=”alignright” margin=”5px 20px” data=”1,2″]

    michalmix thx for reply, I forgot to mention that I`m trying to display dynamic data…

    $count_posts = wp_count_posts();
    $draft_posts = $count_posts->draft;
    $published_posts = $count_posts->publish;
    $countPost = array($draft_posts,$published_posts);
    $chartNewData = implode(‘,’, $countPost);

    this for an example. I follow the example but nothing is displayed. I notice that you mention about global variations. care to share a working example?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Tips on how to use dynamic data’ is closed to new replies.