Published
Edited
Jul 2, 2021
Insert cell
md`# July 2020 temperature data`
Insert cell
d3 = require("d3@7")
Insert cell

chart = {
//draw chart
const svg = d3.create("svg")
.attr("viewBox",[0, 0, width, height]);

//draw x-axis and y-axis
svg.append('g').call(xAxis)
svg.append('g').call(yAxis)
//draw line
svg.append('path')
.datum(series)
.attr('d', line)
.style('stroke', (d, i) => colors(d[i].topic))
//.style('stroke', function(d) {return colors(d.topic)})
.style('fill', 'transparent');
return svg.node();
}
Insert cell
mutable data = FileAttachment("july2020.json").json();
Insert cell
d3.max(data, d => d.value)
Insert cell
d3.extent(data, d => d.time)
Insert cell
temp = data.map(d => d.value)
Insert cell
//function(data){
// data.forEach(function(d){
// d.value = +d.value;
// d.topic = d.topic;
// d.time = d3.isoParse(d.time["$date"]);
// })
// return [
// data.filter(function(d){ return d.topic == "sensor/moisture" }),
// data.filter(function(d){ return d.topic == "sensor/temperature" }),
// data.filter(function(d){ return d.topic == "sensor/humidity" })
// ]
// } , {
// y: " °F",
// conditions: ["sensor/moisture", "sensor/temperature", "sensor/humidity"],
// labels: ["moisture", "temperature", "humidity"],
// colors: ["deepskyblue", "lightskyblue", "lightblue"]


// series = Array.from(
// d3.group(data, d => d.topic)
// );

// function(data){
// data.map(({ time, topic, value }) => ({
// time : d3.isoParse(time["$date"]),
// topic : topic,
// value: value
// }))

series = data.filter(({ topic }) => topic === 'sensor/temperature')

//series2 = data.filter(({ topic }) => topic === 'sensor/temperature')
//series = data.filter(({ topic }) => topic === 'sensor/humidity')
//}

// Group the entries by symbol
// dataNest = Array.from(
// d3.group(data, d => d.symbol), ([key, value]) => ({key, value})
// );
Insert cell
series.map(({time, topic, value}) => ({
time : d3.isoParse(time["$date"]),
topic : topic,
value : value
}))
Insert cell

series2 = data.filter(({ topic }) => topic === 'sensor/temperature')

Insert cell
series2.map(({time, topic, value}) => ({
time : d3.isoParse(time["$date"]),
topic : topic,
value : value
}))
Insert cell
data.filter(({topic}) => topic === 'sensor/moisture')
Insert cell
data.filter(function(d){ return d.topic == "sensor/humidity" })
Insert cell
data[1].topic
Insert cell
d3.isoParse(data[1].time["$date"])
Insert cell
data[1].value
Insert cell
margin = ({ top: 0, right: 0, bottom: 20, left: 30})
Insert cell
d3.extent(data, (d) => d3.isoParse(d.time["$date"]))
Insert cell
xScale = d3.scaleTime()
.domain(d3.extent(data, (d) => d3.isoParse(d.time["$date"])))
.range([margin.left, width - margin.right])
Insert cell
d3.max(data, (d) => d.value)
Insert cell
yScale = d3.scaleLinear()
.domain([0, d3.max(data, (d) => d.value)])
.range([height - margin.bottom, margin.top]);
Insert cell
xAxis = (g) => g
.attr('transform', `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xScale)
.tickFormat(d3.timeFormat("%B %e, %Y")))
Insert cell
yAxis = (g) => g
.attr('transform', `translate(${margin.left},0)`)
.call(d3.axisLeft(yScale))
Insert cell
height = 200;
Insert cell
value = data[0].value
Insert cell
format = d3.isoParse("2020-07-14T13:55:13.226Z")
Insert cell
time = d3.isoParse(data[0].time["$date"])
Insert cell
topic = data[0].topic
Insert cell
line = d3.line()
.x(d => xScale(d3.isoParse(d.time["$date"])))
.y(d => yScale(d.value))
Insert cell
colors = d3.scaleOrdinal(d3.schemeSet1)
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