这个问题已被问过几次,但我无法使用之前提供的答案来完成我的第一个动态Charts.js。我正在尝试构建一个有效的简单示例,一旦我设法做到这一点,我可以扩展它。目前我有以下内容:
1)一个简单的下拉菜单,你可以选择一年:
<form>
<div class="row">
<div class="label"><b>Select Year</b></div>
<select id="year" name="year" style = "width:100%">
<option value=2017>2017</option>
<option value=2018>2018</option>
</select>
</div>
</form>
<br>
2)我用来调用MySQL查询的一些javascript:.change:
<script type="text/javascript">
jQuery(document).ready( function($) {
var valueCheck;
jQuery('#year').on( 'change', function () {
year = $('#year').val();
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
data: {
action: 'call_chart_data',
year: year,
},
success:function(output){
jQuery('#y_data1').html( output );
}
});
}).change();
});
</script>
3)查询MySQL数据库的PHP:
function get_chart_data(){
global $wpdb;
$year = $_POST['year'];
$myQuery = $wpdb->get_results('SELECT dreamTeamPoints FROM afl_master WHERE player_id = "CD_I270963" AND year = '.$year);
$data = array();
foreach ($myQuery as $result) {
$data[] = $result->dreamTeamPoints;
}
wp_die();
}
add_action('wp_ajax_nopriv_call_chart_data', 'get_chart_data');
add_action('wp_ajax_call_chart_data', 'get_chart_data');
这一点的代码成功返回了一个值数组,如下所示(当选择2017时):
Array ( [0] => 68 [1] => 152 [2] => 139 [3] => 143 [4] => 132 [5] => 155
[6] => 65 [7] => 59 [8] => 111 [9] => 157 [10] => 92 [11] => 62
[12] => 89 [13] => 83 [14] => 105 [15] => 34 [16] => 134 [17] => 47
[18] => 124 [19] => 97 [20] => 153 [21] => 149 [22] => 76 [23] => 97 )
最后,我有一个静态图,我希望通过用MySQL响应替换变量y_data1(以下代码中的第三行)来转换为动态,但是我不知道如何将其提供给javascript。
<script type="text/javascript">
var x_time = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,"QF","SF","PF","GF"];
var y_data1 = [91,115,67,46,,,,,,,,,,,68,111,111,77,95,106,99,93];
var y_data2 = [84,74,64,79,,,,,,,,,,,82,84,80,82,88,87,78,79];
new Chart(document.getElementById("myChart"), {
type: 'bar',
data: {
labels: x_time,
datasets: [{
type:'line',
label: 'Fantasy Points',
fill:false,
backgroundColor: 'orange',
borderColor: 'orange',
data: y_data1,
yAxisID: 'left-axis',
pointRadius: 5,
pointHoverRadius: 7
}, {
label: 'Time On Ground (%)',
fill: true,
backgroundColor: 'black',
borderColor: 'black',
data: y_data2,
yAxisID: 'right-axis'
}]
},
options: {
title: {
display: false},
legend: { display: true },
maintainAspectRatio:true,
responsive: true,
tooltips: {mode: 'index', intersect: false},
hover: {mode: 'nearest', intersect: true},
scales: {
xAxes: [{display: true, stacked:true, scaleLabel: {display: true, labelString: 'Round'}}],
yAxes: [{
type:'linear',
id:'left-axis',
display: true,
position: 'left',
scaleLabel: {display: true, labelString: 'Fantasy Points'},
gridLines: {drawOnChartArea:false},
ticks: {beginAtZero: true}
},{
type:'linear',
id:'right-axis',
display: true,
position: 'right',
stacked:false,
scaleLabel: {display: true, labelString: 'Time On Ground (%)'},
gridLines: {drawOnChartArea:false},
ticks: {beginAtZero: true}
}]
}
}
});
</script>
感谢人们提供的任何帮助。
我将值作为字符串而不是数组返回:
$data = '';
foreach ($myQuery as $result) {
$data .= $result->dreamTeamPoints.',';
}
然后将其分配给成功的ajax帖子上的值:
success:function(output){
//jQuery('#y_data1').html( output );
var out = output;
}
最后将'out'变量放在赋值中:
var y_data1 = [ out ];
您可以更简化它 - 去老鹰队。