I'm trying to create a shortcode for charts on wordpress posts. So far I've got the shortcode working and I can see the shortcode values are being passed through, but what I need to know is how to pass through those values to the so Google Charts will work correctly. Here's the code that I have so far:
function chart_shortcode($atts) {
$a = shortcode_atts( array(
'value1' => '',
'value2' => '',
'value3' => ''
), $atts );
return '<div id="donut" style="width: 745px; height: 500px;"></div>';
}
add_shortcode('chart', 'chart_shortcode');
function header_metadata() {
// Post object if needed
// global $post;
// Page conditional if needed
// if( is_page() ){}
?>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Effort', 'Amount given'],
['value1', 75],
['value2', 20],
['value3', 5],
]);
var options = {
title: 'Energy Breakdown',
pieHole: 0.4,
pieSliceTextStyle: {
color: 'white',
},
};
var chart = new google.visualization.PieChart(document.getElementById('donut'));
chart.draw(data, options);
}
</script>
<?php
}
add_action( 'wp_head', 'header_metadata' );
?>
So what I am after is a way to tell the chart the values of value1,2,3 here instead of 75, 20 and 5:
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Effort', 'Amount given'],
['value1', 75],
['value2', 20],
['value3', 5],
]);
The shortcode that works is:
[chart value1="75" value2="20" value3="5"]
[chart values="75,20,5"]