Google 图表 google.visualization.arrayToDataTable: 如何传递格式化字符串

Google Chart google.visualization.arrayToDataTable: How can I pass a formatted string

我在 PHP

中创建了以下字符串
["Month", "Points", {role: "style"}, "Goal", {role: "annotation"}],
["JAN", 3, "#4a7dae", 6.5, ""],
["FEB", 2, "#4a7dae", 6.5, ""],
["MAR", 3, "#4a7dae", 6.5, ""],
["APR", 1, "#4a7dae", 6.5, ""],
["MAY", 2, "#4a7dae", 6.5, ""],
["JUN", 1, "#4a7dae", 6.5, "Goal (6.5)"]

并希望在 Google 图表数据中使用相同的内容

上面的字符串我在JavaScript变量(str_data)中抓取并尝试这样

var data = google.visualization.arrayToDataTable([str_data]);

但出现以下错误:

jsapi_compiled_default_module.js:23 Uncaught (in promise) Error: First row is not an array.
    at gvjs_rba (jsapi_compiled_default_module.js:23)
    at Object.gvjs_Tl [as arrayToDataTable] (jsapi_compiled_default_module.js:25)
    at prev_year_chart_callback_function (evd-all.js?ver=1.0:212)

更新(PHP代码) 以下代码在循环内运行并一次创建一行。

$model_str = '["Month", "Points", {role: "style"}, "Goal", {role: "annotation"}],';
if ( $row_index === count( $assoc_array ) - 1 ) {
    $model_str .= '["' . $unix_month_start_formatted . '", ' . $assoc_array[ $key ] . ', "#4a7dae", ' . $prior_season_goal_point . ', "Goal (' . $prior_season_goal_point . ')"],';
} else {
    $model_str .= '["' . $unix_month_start_formatted . '", ' . $assoc_array[ $key ] . ', "#4a7dae", ' . $prior_season_goal_point . ', ""],';
}

而不是尝试将 json 作为字符串传递

在 php 中构建 json,然后将编码的 json 作为字符串传递

// create column headings
$model_str = [];
$columns = [];
$columns[] = "Month";
$columns[] = "Points";
$columns[] = ["role" => "style"];
$columns[] = "Goal";
$columns[] = ["role" => "annotation"];
$model_str[] = $columns;


// create rows
$row = [];
if ( $row_index === count( $assoc_array ) - 1 ) {
    $row[] = $unix_month_start_formatted;
    $row[] = $assoc_array[$key];
    $row[] = "#4a7dae";
    $row[] = $prior_season_goal_point;
    $row[] = "Goal (" . $prior_season_goal_point . ")";
} else {
    $row[] = $unix_month_start_formatted;
    $row[] = $assoc_array[$key];
    $row[] = "#4a7dae";
    $row[] = $prior_season_goal_point;
    $row[] = "";
}
$model_str[] = $row;

// return json
echo json_encode($model_str);

然后,假设您使用 ajax 获取数据,将类型设置为 json...

$.ajax({
  url: '...',
  dataType: 'json'
}).done(...