我是编程和特别是javascript的新手。 为了学习javascript,我正在考虑使用" ticker data"来创建一个图表。来自coinmarketcap。为了获得股票代码数据,我创建了以下函数:
function golemPrice() {
//Fetch data
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.coinmarketcap.com/v1/ticker/golem-network-tokens/?convert=EUR");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$rawData = curl_exec($curl);
curl_close($curl);
// decode to array
$data = json_decode($rawData);
//Access array[0] and select value price_eur
$gntPrice = $data[0]->price_eur;
// show data
return $gntPrice;
}
?>
为了将数据放入图表,我想使用chart.js。我创建了以下图表:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.js"></script>
<script>
let priceFromFunction = ["<?php golemPrice();?>"];
let timeFormat = "MM/DD/YYYY HH:mm";
let myChart = document.getElementById('golemChart').getContext('2d');
let lineChart = new Chart(myChart, {
type: 'line',
data: {
datasets: [{
label: 'Golem chart',
borderColor: "rgba(80, 164, 245, 4)",
data: priceFromFunction
}]
},
options: {
scales: {
xAxes: [{
type: "time",
time: {
format: timeFormat,
// round: 'day'
tooltipFormat: 'll HH:mm'
},
scaleLabel: {
display: true,
labelString: 'Date'
}
}, ],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'Price'
}
}]
},
}
});
</script>
不幸的是,我的图表没有显示日期,也没有添加该功能的数据。我尝试了很多不同的东西,但现在我没有想法。
希望你能帮助我。
答案 0 :(得分:0)
您缺少echo函数值,请尝试替换此:
let priceFromFunction = ["<?php echo golemPrice() ?>"];
到
R