Вот весь код, который использует функцию sam()
. Когда вы выполните его, он выведет последовательность дат и чисел. Когда вы скопируете и вставите этот вывод в строку кода рядом с нижней частью, помеченную "data" : [ ]
, он выведет симпатичный график с использованием дат и чисел. Мне нужна функция, которая может сделать то же самое.
<html>
<head>
<script data-require="jquery@2.1.4" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script data-require="highstock@0.0.1" data-semver="0.0.1" src="http://code.highcharts.com/stock/highstock.js"></script>
<script data-require="highstock@0.0.1" data-semver="0.0.1" src="http://code.highcharts.com/stock/modules/exporting.js"></script>
<title>
How to get the array of dates between
two dates in JavaScript ?
</title>
</head>
<body style = "text-align:center;">
<div id="myData"></div>
<div id="demo"></div>
<div id="demo1"></div>
<script>var tday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var tmonth=["-01-","-02-","-03-","-04-","-05-","-06-","-07-","-08-","-09-","-10-","-11-","-12-"];
function GetClock(){
var d=new Date();
var nday=d.getDay(),nmonth=d.getMonth(),ndate=d.getDate(),nyear=d.getFullYear();
var nhour=d.getHours(),nmin=d.getMinutes(),nsec=d.getSeconds(),ap;
if(nhour==0){ap=" AM";nhour=12;}
else if(nhour<12){ap=" AM";}
else if(nhour==12){ap=" PM";}
else if(nhour>12){ap=" PM";nhour-=12;}
if(nmin<=9) nmin="0"+nmin;
if(nsec<=9) nsec="0"+nsec;
var clocktext=""+nyear +tmonth[nmonth] +ndate +"";
document.getElementById('clockbox').innerHTML= clocktext;
}
GetClock();
setInterval(GetClock,1000);</script>
<script>
function heavy(){
var today = new Date("2014-10-01"); //YYYY-MM-DD
var startDate = new Date("2014-10-01");
var endDate = new Date("2016-10-10" );
//YYYY-MM-DD
var getDateArray = function(start, end) {
var arr = new Array();
while (startDate < end) {
arr.push(new Date(startDate));
startDate.setDate(startDate.getDate() + 1);
}
return arr.reverse().toLocaleString().replace(new RegExp('/', 'g'), '-').replace(new RegExp("8:", 'g'), '').replace(new RegExp("00", 'g'), '').replace(new RegExp("PM", 'g'), '').replace(new RegExp("AM", 'g'), '').replace(new RegExp(",", 'g'), '').replace(new RegExp(":", 'g'), '');
}
var dateArr = getDateArray(startDate, endDate);
// Output
for (var i = 0; i < dateArr.length; i++) {
document.write( dateArr[i] );
}
}
</script>
<script>
function heavy1(){
var cars = [["2020-02-13"], ["2020-02-12"]];
var text = "";
var i;
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
document.getElementById("demo").innerHTML = text;}
</script>
<script>
function sam(){
fetch('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&outputsize=full&apikey=demo')
.then(function (response) {
return response.json();
})
.then(function (data) {
appendData(data);
})
.catch(function (err) {
console.log('error: ' + err);
});
function appendData(data) {
var mainContainer = document.getElementById("myData");
var arrw = ["2020-02-14",
"2020-02-13",
"2020-02-12",
"2020-02-11",
"2020-02-10",
"2020-02-07",
"2020-02-06",
"2020-02-05",
"2020-02-04",
"2020-02-03",
"2020-01-31",
"2020-01-30",
"2020-01-29",
"2020-01-28",
"2020-01-27",
"2020-01-24",
"2020-01-23",
"2020-01-22",
"2020-01-21",];
for (var i = 0; i < arrw.length; i++) {
var joe = '[' +"\""+ arrw[i]+ "\"" + "," + " " + data['Time Series (Daily)'][arrw[i]]['1. open']+"," + " " +data['Time Series (Daily)'][arrw[i]]['2. high']+"," + " " +data['Time Series (Daily)'][arrw[i]]['3. low']+","+ " " +data['Time Series (Daily)'][arrw[i]]['4. close']+","+ " " +data['Time Series (Daily)'][arrw[i]]['5. volume' ] + ']' + "," ;
document.write(joe);
}}}
</script>
<div id="container"></div>
<script>
var json = {
"dataset": {
"data": [
sam()
],
}
};
var hiJson = json.dataset.data.map(function(d) {
return [new Date(d[0]).getTime(), d[4]]
});
// Create the chart
$('#container').highcharts('StockChart', {
rangeSelector: {
selected: 1
},
title: {
text: 'MS Stock Price'
},
series: [{
name: 'MS',
data: hiJson,
tooltip: {
valueDecimals: 2
}
}]
});
</script>
</body>
</html>