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

Google visualization Google图表Google.visualization.arrayToDataTable:如何传递格式化字符串,google-visualization,Google Visualization,我已经用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"

我已经用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)"]
并希望在谷歌图表数据中使用相同的功能

我在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作为字符串传递

// 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);
在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(...

您不应该尝试将json作为字符串传递。请分享生成字符串的php好吗?@WhiteHat用创建上述字符串输出的代码块更新了我的问题