我的莫里斯图表有问题。 我尝试了多次,并以多种不同的方式从位于 HTML 表中的数据创建了一个morris区域图表,但我失败了。
这是我用来从表中提取数据的方式:
$('.tabella_dati').click(function () {
var table = $("#datatbl tbody");
var $data = table.find('tr').each(function (i, tr) {
var righe = $('td', tr).map(function (i, td) {
return $(td).text();
});
var $ciao = JSON.stringify(righe);
$.post('charts', {data: $ciao}, function (data) {
alert(data);
});
}).text();
我尝试使用和不使用post方法。 使用post方法,我尝试以这种方式在 PHP 函数中创建图表:
function morris() {
$data = Input::get('data');
echo '<script>Morris.Area({element: "morris-area-chart",'
. 'data: '.$data.'}, xkey: '.$data.', ykeys: '.$data.')</script>';
xkey
和ykeys
错了,我知道。
对不起,我的英语不好,谢谢。
function buildTable($arrFilled, $arrDates) {
$mesi = array("1" => "January",
"2" => "February",
"3" => "March",
"4" => "April",
"5" => "May",
"6" => "June",
"7" => "July",
"8" => "August",
"9" => "September",
"10" => "October",
"11" => "November",
"12" => "December");
echo '<table id="datatbl" class="table">';
echo '<thead>';
echo '<th class="text-center">Country</th>';
foreach ($arrFilled as $k => $v) {
$arr = explode("_", $k);
echo '<th class="text-center">' . $mesi[$arr[1]] . "<br>" . $arr[0] . '</th>';
}
echo '</thead>';
echo '<tbody>';
$i = 0;
$countries = array_keys($arrDates);
foreach ($countries as $c) {
echo '<tr>';
echo '<td class = "text-center">' . $countries[$i] . '</td>';
foreach ($arrFilled as $k => $v) {
$valore = isset($arrDates[$c][$k]) ? $arrDates[$c][$k] : $v;
if ($valore != 0) {
echo '<td class="text-center" bgcolor="#ADD8E6">' . $valore . '</td>';
} else {
echo '<td class="text-center">' . $valore . '</td>';
}
}
echo '</tr>';
$i++;
}
echo '</tbody>';
echo '</table>';
}
function table() {
$mesi = Input::get('month');
$array = [];
for ($i = 0; $i <= $mesi - 1; $i++) {
$arrayMesi[$i] = $i;
$array[date("Y_n", strtotime("-$arrayMesi[$i] month"))] = 0;
}
$countries = DB::table('countries')->get();
$country_index = 0;
$arrDates = [];
foreach ($countries as $c) {
$arrCountires[] = $c->country_name;
$subscription = DB::table('subscription')->select('quantity', 'country_id', DB::raw('concat(year(date), "_" , month(date)) as meseAnno'))
->where('country_id', '=', $c->country_id)
->groupBy('meseAnno')->get();
foreach ($subscription as $s) {
$arrDates[$arrCountires[$country_index]][$s->meseAnno] = $s->quantity;
}
$country_index++;
}
return $this->buildTable($array, $arrDates);
}