[英]Google chart: “Uncaught (in promise) Error: Unknown header type: 4.7278” error
I'm trying to display a line chart of currency rates via google charts .我正在尝试通过google charts显示货币汇率折线图。
Basically i have 2 type of values:基本上我有两种类型的值:
when i'm trying to render the chart i get an error: "Uncaught (in promise) Error: Unknown header type: 4.7278”当我尝试渲染图表时,出现错误:“未捕获(承诺)错误:未知标头类型:4.7278”
Here is my code:这是我的代码:
PHP array making: PHP数组制作:
$xml=simplexml_load_file('https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/usd.xml') or die("Error: Cannot create object");
$arrayForChart[] = ["Date","Rate"];
foreach ($xml->DataSet->Series->Obs as $key => $value) {
$dateIso8601Format=(string)$value['TIME_PERIOD'];
$rateForDate=(string)$value['OBS_VALUE'][0];
$rateForDate=(float)$rateForDate;
$arrayForChart[] = [$dateIso8601Format,$rateForDate];
}
$arrayForChart = json_encode($arrayForChart);
Javascript Javascript
var arrayForChart;
$.ajax({
type: "POST",
url: ajaxUrl,
//data: {configuration: Config },
success: function (data) {
arrayForChart = data;
arrayForChart = $.map(arrayForChart, function (el) {
return el;
});//converting js object to js array
},
cache: false
});
google.charts.load("current", {packages: ["corechart", "line"]});
google.charts.setOnLoadCallback(drawLineColors);
function drawLineColors() {
var data = google.visualization.arrayToDataTable([arrayForChart]);
var options = {
hAxis: {
title: "Rate",
id: "Rate",
label: "Rate",
type: "number"
},
vAxis: {
title: "Date",
id: "Date",
label: "Date",
type: "string"
},
colors: ["#a52714", "#097138"]
};
var chart = new google.visualization.LineChart(document.getElementById("chart_div"));
chart.draw(data, options);
}
Sample of data:数据样本:
["Date","Rate","2011-01-03",4.7278,"2011-01-04",4.7301,"2011-01-05",4.6814,"2011-01-06",4.6635]
Anybody might know what is the problem?有人可能知道是什么问题吗?
Many thanks!非常感谢!
Google charts expects an array of arrays.谷歌图表需要一个数组数组。 You appear to be providing it with one flat away.
你似乎给它提供了一套公寓。 Eg
例如
Array('date', 'value', 1,2,3,4);
Should be应该是
Array(
Array(date, value),
Array(1, 2),
Array(3, 4)
);
Faced Same issue in ASP.Net resolved by changing this chartData.Add(new object[] { "Post", "Total" });通过更改此图表解决了 ASP.Net 中的相同问题。Add(new object[] { "Post", "Total" }); Mistake you are doing is you are giving rows but not header/column name
您正在做的错误是您提供的是行而不是标题/列名称
**List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"Post", "Total"
});
foreach (DataRow dtrow in dt.Rows)
{
chartData.Add(new object[]
{
dtrow[0].ToString(), Convert.ToInt32(dtrow[1])
});
}**
don't forget to add a comma right after you define the column names.不要忘记在定义列名称后立即添加逗号。 That was the problem in my case.
这就是我的问题。
function drawChart1() {
var data = new google.visualization.arrayToDataTable([
['Month', 'Total claims'],
<?php
if(mysqli_num_rows($sqlclaims) > 0){
while ($row = mysqli_fetch_assoc($sqlclaims)){
echo "['".$row['FINANCIAL_']."', ".$row['tot_claims']."],";
}
}
?>
]);
I had the same issue with angular-google-charts
.我对
angular-google-charts
有同样的问题。 The component was initialized with empty array, then it was passed when the data is ready.组件用空数组初始化,然后在数据准备好时传递。
I solved it using *ngIf on the chart component with a flag set to false
, and set it to true
when the data is ready.我在图表组件上使用 *ngIf 解决了这个问题,并将标志设置为
false
,并在数据准备好时将其设置为true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.