Published
Edited
Aug 18, 2020
Importers
Insert cell
md`# Weather Line Plot`
Insert cell
md`#You kinda need this: https://observablehq.com/@observablehq/file-attachments`
Insert cell
dataset = FileAttachment("my_weather_data.json")
Insert cell
json_data = FileAttachment("my_weather_data.json").json()
Insert cell
json_data[0].time
Insert cell
json_data[0].summary
Insert cell
json_data[0].icon
Insert cell
d3 = require("d3") //d3 = require("d3-dsv@1")

Insert cell
text = FileAttachment("example.csv").text()
Insert cell
d3.csvParse(text, d3.autoType)
Insert cell
csv = d3.csvParse(await FileAttachment("example.csv").text(), d3.autoType)
Insert cell
image = FileAttachment("example.png").image()
Insert cell
success2 =
{

// 1. Access data
//const dataset = await d3.json("./../../my_weather_data.json")
const dataset = json_data

const yAccessor = d => d.temperatureMax
const dateParser = d3.timeParse("%Y-%m-%d")
const xAccessor = d => dateParser(d.date)

// 2. Create chart dimensions

let dimensions = {
width: window.innerWidth * 0.9,
height: 400,
margin: {
top: 15,
right: 15,
bottom: 40,
left: 60,
},
}
dimensions.boundedWidth = dimensions.width
- dimensions.margin.left
- dimensions.margin.right
dimensions.boundedHeight = dimensions.height
- dimensions.margin.top
- dimensions.margin.bottom

// 3. Draw canvas

const svg = selection.create('svg')

const foreignObject = svg.append('foreignObject')
.attr('width', dimensions.width)
.attr('height', dimensions.height)
const canvas = foreignObject.node().appendChild(document.createElement('canvas'));

const node = canvas.nodeName
const constructor = canvas.constructor.name

const context = canvas.getContext('2d')
//TEMP DELETED - THIS WAS WORKING
// context.fillStyle = 'blue';
// context.fillRect(0, 0, dimensions.width, dimensions.height);
// const wrapper = d3.select("#wrapper")
// .append("svg")
// .attr("width", dimensions.width)
// .attr("height", dimensions.height)
// const bounds = wrapper.append("g")
// .style("transform", `translate(${
// dimensions.margin.left
// }px, ${
// dimensions.margin.top
// }px)`)

const bounds = foreignObject.append("g")
.style("transform", `translate(${
dimensions.margin.left
}px, ${
dimensions.margin.top
}px)`)
// 4. Create scales

const yScale = d3.scaleLinear()
.domain(d3.extent(dataset, yAccessor))
.range([dimensions.boundedHeight, 0])

const freezingTemperaturePlacement = yScale(32)
const freezingTemperatures = bounds.append("rect")
.attr("x", 0)
.attr("width", dimensions.boundedWidth)
.attr("y", freezingTemperaturePlacement)
.attr("height", dimensions.boundedHeight
- freezingTemperaturePlacement)
.attr("fill", "#e0f3f3")

const xScale = d3.scaleTime()
.domain(d3.extent(dataset, xAccessor))
.range([0, dimensions.boundedWidth])

// 5. Draw data

const lineGenerator = d3.line()
.x(d => xScale(xAccessor(d)))
.y(d => yScale(yAccessor(d)))

const line = bounds.append("path")
.attr("d", lineGenerator(dataset))
.attr("fill", "none")
.attr("stroke", "#553232")
.attr("stroke-width", 5)

// 6. Draw peripherals

const yAxisGenerator1 = d3.axisLeft()
.scale(yScale)

const yAxisDraw1 = bounds.append("g")
.call(yAxisGenerator1)

const yAxisGenerator = d3.axisRight()
.scale(yScale)

const yAxisDraw = bounds.append("g")
.call(yAxisGenerator)
.style("transform", `translateX(${
dimensions.boundedWidth
}px)`)

const xAxisGenerator = d3.axisBottom()
.scale(xScale)

const xAxisDraw = bounds.append("g")
.call(xAxisGenerator)
// return { svg, node, constructor };
return { svg, node, constructor }
}
Insert cell
success2.svg.node();
Insert cell
// success = {
// const svg = selection.create('svg')

// const foreignObject = svg.append('foreignObject')
// .attr('width', width)
// .attr('height', height);

// const canvas = foreignObject.node().appendChild(document.createElement('canvas'));

// const node = canvas.nodeName
// const constructor = canvas.constructor.name

// const context = canvas.getContext('2d')

// context.fillStyle = 'blue';
// context.fillRect(0, 0, width, height);

// return { svg, node, constructor };
// }
Insert cell
selection = require('d3-selection')
Insert cell
width = 100

Insert cell
height = 100

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