我正在尝试为wordpress帖子上的图表创建一个快捷代码。到目前为止,我已经使用了短代码,我可以看到短代码值正在被传递,但我需要知道的是如何将这些值传递到Google图表,以便正确工作。以下是我目前掌握的代码:
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\' );
?>
所以我想要的是告诉图表这里的值1,2,3,而不是75,20和5的值:
function drawChart() {
var data = google.visualization.arrayToDataTable([
[\'Effort\', \'Amount given\'],
[\'value1\', 75],
[\'value2\', 20],
[\'value3\', 5],
]);
有效的短代码是:
[chart value1="75" value2="20" value3="5"]