Published
Edited
Feb 25, 2021
Insert cell
Insert cell
vl
.markLine()
.data(tideFormated)
.encode(
vl.x().fieldT("date"), // .timeUnit('utcmonthdate'),
vl.y().fieldQ("sea_surface_elevation")
)
.render()
Insert cell
tideFormated = {
const startDates = [
new Date(new Date().setDate(new Date().getDate() - 7)),
new Date()
];

const startDatesFormat = startDates.map(day1 => {
return {
date: day1,
year: day1.toLocaleDateString("en-GB", {
year: "numeric"
}),
month: day1.toLocaleDateString("en-GB", {
month: "2-digit"
}),
day: day1.toLocaleDateString("en-GB", {
day: "2-digit"
})
};
});

// fetchData is an array of promises
const fetchData = startDatesFormat.map(x =>
tabletojson
.convertUrl(
"https://mb-hakai.herokuapp.com/http://tides.gc.ca/eng/station?type=0&date=" +
x.year +
"%2F" +
x.month +
"%2F" +
x.day +
"&sid=7955&tz=PST&pres=1"
)
.then(function(tablesAsJson) {
const weekly = tablesAsJson[7];
const tides = [];
weekly.forEach((w, i) => {
i === 0
? (w.date = x.date)
: (w.date = new Date(new Date().setDate(x.date.getDate() + i)));
tides.push(w);
});
return tides;
})
);

return Promise.all(fetchData).then(values => {
const result = values[0].concat(values[1]); // flatten 2d array
const hours = [
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"00",
"01",
"02",
"03",
"04",
"05",
"06",
"07",
"08",
"09",
"10"
];

const outdata = [];
// const ll = [51.5529, -128.0056]

result.forEach(d => {
let temp = hours.forEach((h, i) => {
let obj = {};

let time = new Date(new Date(d.date).setHours(h, 0, 0, 0));
const sun = suncalc.getTimes(new Date(time), 51.5529, -128.0056);

// let day;
if (d3.isoParse(sun.sunrise) < time && time < d3.isoParse(sun.sunset)) {
d.day = 0;
} else {
d.day = 10;
}
obj["day"] = d.day;
obj["date"] = time;
obj["sea_surface_elevation"] = Number(Object.values(d)[i]);
outdata.push(obj);
});
});
outdata.sort((a, b) => a.date - b.date);

// return result;
return outdata;
});
}
Insert cell
suncalc = require("suncalc")
Insert cell
d3 = require('d3@6')
Insert cell
tabletojson = require("https://bundle.run/tabletojson@1.0.4")
Insert cell
import { vl } from "@vega/vega-lite-api"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more