我试图通过google charts显示货币汇率折线图。
基本上我有两种类型的值:
当我试图渲染图表时出现错误:"未捕获(在承诺中)错误:未知标题类型:4.7278“
这是我的代码:
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
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);
}
数据样本:
["Date","Rate","2011-01-03",4.7278,"2011-01-04",4.7301,"2011-01-05",4.6814,"2011-01-06",4.6635]
任何人都可能知道这是什么问题?
非常感谢!
答案 0 :(得分:2)
Google图表需要一系列数组。你似乎在提供一个平面。例如
Array('date', 'value', 1,2,3,4);
应该是
Array(
Array(date, value),
Array(1, 2),
Array(3, 4)
);
答案 1 :(得分:0)
在ASP.Net中遇到的相同问题通过更改此方法得以解决 chartData.Add(new object [] {“ Post”,“ Total”}); 您正在犯的错误是您要提供行,而不是标题/列名
**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])
});
}**
答案 2 :(得分:0)
不要忘了在定义列名之后添加逗号。就我而言,这就是问题所在。
Stock = {1: {'Item Name': "Campbell's Cream of Mushroom",
'Description': 'Canned Food',
'Price': 2.30,
'Stock': 32,
'Expiry Date': '27/05/25'},
2: {'Item Name': "Orecchiette",
'Description': 'Noodles & Pasta',
'Price': 3.45,
'Stock': 23,
'Expiry Date': '27/05/22'},
3: {'Item Name': "Coca Cola",
'Description': 'Drinks',
'Price': 1.50,
'Stock': 25,
'Expiry Date': '27/05/21'},
4: {'Item Name': "Dried Cha Soba",
'Description': 'Noodles & Pasta',
'Price': 3.75,
'Stock': 25,
'Expiry Date': '27/05/22'},
5: {'Item Name': "Chinese Cabbage",
'Description': "Vegetable",
'Price': 1.50,
'Stock': 12,
'Expiry Date': '8/7/2020'}
}
something = []
for i in Stock:
something.append(Stock[i]['Item Name'])
something.append(Stock[i]['Stock'])
print(something)
x = 0
while something != "":
for i in something:
x += 1
newsomething = something[x], something[x+1]
print(newsomething)
答案 3 :(得分:0)
我对angular-google-charts
有相同的问题。
使用空数组初始化该组件,然后在数据准备就绪时将其传递。
我在图表组件上使用* ngIf来解决它,并将标志设置为false
,并在数据准备好时将其设置为true
。