Published
Edited
Feb 9, 2021
Insert cell
md `# A bunch of experiments`
Insert cell
Insert cell
Insert cell
viewof f = text()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cell = {
const obj = {}
obj[TYPE.VALUE]=100
obj[TYPE.FUNCTION]=
`{
return "Object Value: " + obj[TYPE.VALUE]
}`
return obj
}

Insert cell
{
const run = new Function('obj', 'TYPE', cell[TYPE.FUNCTION])
return run(cell, TYPE); //need to pass in TYPE as no global TYPE
}

Insert cell
Insert cell
Insert cell
md ` ### Html view. Instantiate a div element with events to render mouse location`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md`### A custom dispatch helper function`
Insert cell
Insert cell
Insert cell
mutable rs=0
Insert cell
Insert cell
Insert cell
myset.entries().next()
Insert cell
Insert cell
md `remove redundent array entries using set`
Insert cell

{let arr = [1, 2, 2, 4, 5, 5];
arr = [...new Set(arr)]; // returns [1, 2, 4, 5]
return arr
}
Insert cell
{
let arr = [1, 2, 2, 4, 5, 5];
let total = 0;
return arr.reduce( (total, num) => total * num);
}
Insert cell
{
let arr = [1, 2, 2, 4, 5, 5];
let total = 0;
return arr.filter( (total, num) => total * num);
}
Insert cell
{
// Modifying each words
let words = ['spray', 'limit', 'exuberant', 'destruction','elite', 'present']

const modifiedWords = words.filter( (word, index, arr) => {
arr[index+1] +=' extra'
return word.length < 15
})
return modifiedWords;
}
Insert cell
Insert cell
settings = {
let defaultsettings = {
speed: 30,
posX:0,
posY:10
}
let settings = {...defaultsettings,
speed:10, //change defaulte
newsettings:{
color:{background:"white",text:"blue"},
size:20,
} //add new
}
return settings
}
Insert cell
treeview.usage();
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md `## Stock Prices using Yahoo Finance`
Insert cell
md ` Company information API https://financialmodelingprep.com/developer/docs/dashboard`
Insert cell
companyinfo = async function(symbol){
const apikey='bfdaaf37d02e001df65432b81ca6d871'
const url = `https://financialmodelingprep.com/api/v3/profile/${symbol}?apikey=${apikey}`
const info = await soFetch(url)
const text = await info.text();
const data = await JSON.parse(text);
return data[0];
}
Insert cell
plotData = async function (symbols, datefrom , dateto) {
let dataArray = [];
symbols.forEach( (value, index, array) => {
debugger;
let p = fetchYahooData(value, '', '1d', datefrom , dateto);
p.then(result=> {
let trace =
{
x: unpack(result.data, 'date'),
y: unpack(result.data, 'close'),
mode: 'lines',
type: 'scatter',
name: result.info.companyName
}
dataArray.push(trace);
});
});
return dataArray;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
t4a = fetchYahooData('ENB.TO', 'Enbridge', '1d', datefrom , dateto) // Delta
Insert cell
Insert cell
Insert cell
Insert cell
t7 = fetchYahooData('AMZN', 'AMZN', '1d', datefrom , dateto)
Insert cell
t8 = fetchYahooData('MSFT', 'MSFT', '1d', datefrom , dateto)
Insert cell
summaryQuote_response = async (SYMBOL) => {
let modules = [ 'assetProfile', 'incomeStatementHistory', 'incomeStatementHistoryQuarterly', 'balanceSheetHistory', 'balanceSheetHistoryQuarterly', 'cashFlowStatementHistory', 'cashFlowStatementHistoryQuarterly', 'defaultKeyStatistics', 'financialData', 'calendarEvents', 'secFilings', 'recommendationTrend', 'upgradeDowngradeHistory', 'institutionOwnership', 'fundOwnership', 'majorDirectHolders', 'majorHoldersBreakdown', 'insiderTransactions', 'insiderHolders', 'netSharePurchaseActivity', 'earnings', 'earningsHistory', 'earningsTrend', 'industryTrend', 'indexTrend', 'sectorTrend' ]
let Summary = JSON.parse((await (await soFetch(`https://query1.finance.yahoo.com/v10/finance/quoteSummary/${SYMBOL}?modules=${modules.join(',')}`)).text())).quoteSummary;
console.log(`https://query1.finance.yahoo.com/v10/finance/quoteSummary/${SYMBOL}?modules=${modules.join(',')}`)
if(Summary.error !== null) return Summary.error.description;
return Summary.result[0];
}
Insert cell
summaryQuote_response('TRP.TO')

Insert cell
html `<div id='table' style='height:400px;overflow:scroll'></div>`
Insert cell
Insert cell
GetNews = async (symbol)=>{
return JSON.parse((await (await soFetch(`https://api.rss2json.com/v1/api.json?rss_url=http://feeds.finance.yahoo.com/rss/2.0/headline?s=${symbol}&region=US&lang=en-US`)).text())).items}
Insert cell
news = GetNews('TRP.TO')
Insert cell
html `<div id='newstable' style='height:400px;overflow:scroll'></div>`
Insert cell
tabulate(d3.select('#newstable'),news,['title', 'pubDate', 'link', 'content'])
Insert cell
Insert cell
Insert cell
Insert cell
tabulate = function (element, data, columns) {
element.html("");
var table = element.append('table')
var thead = table.append('thead')
var tbody = table.append('tbody');

// append the header row
thead.append('tr')
.selectAll('th')
.data(columns).enter()
.append('th')
.text(function (column) { return column; });

// create a row for each object in the data
var rows = tbody.selectAll('tr')
.data(data)
.enter()
.append('tr');

// create a cell in each row for each column
var cells = rows.selectAll('td')
.data(function (row) {
return columns.map(function (column) {
return {column: column, value: row[column]};
});
})
.enter()
.append('td')
.text(function (d) { return d.value; });

return table;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
incomeLevelsData = jsonp(
"https://query1.finance.yahoo.com/v10/finance/quoteSummary/TRP.TO?modules=assetProfile,incomeStatementHistory,incomeStatementHistoryQuarterly,balanceSheetHistory,balanceSheetHistoryQuarterly,cashFlowStatementHistory,cashFlowStatementHistoryQuarterly,defaultKeyStatistics,financialData,calendarEvents,secFilings,recommendationTrend,upgradeDowngradeHistory,institutionOwnership,fundOwnership,majorDirectHolders,majorHoldersBreakdown,insiderTransactions,insiderHolders,netSharePurchaseActivity,earnings,earningsHistory,earningsTrend,industryTrend,indexTrend,sectorTrend",
{
param: "prefix"
}
)
Insert cell
jsonp = require("@tmcw/jsonp@0.2.2")
Insert cell
import { elky } from "@tmcw/elk@260"
Insert cell
import {table} from '@tmcw/tables'
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more