Published
Edited
Aug 28, 2020
Insert cell
md`# Weather Line Plot With Custom Links`
Insert cell
import {dataset} from "@chungmasterflex/weather-line-plot"
Insert cell
import {select} from "@jashkenas/inputs"
Insert cell
import {color} from "@jashkenas/inputs"

Insert cell
d3 = require ("d3")
Insert cell
md` ## Accessor `
Insert cell
data_fetched_columns = ["temperatureMax", "temperatureMin", "how"]
Insert cell
viewof filter_on_column = html`
<select>
<option></option>
${data_fetched_columns.map(col=>
{return `<option>${col}</option>`})}
</select>`
Insert cell
yAccessor = d => d.temperatureMin
// yAccessor = d => d.temperatureMax
Insert cell
// if filter_on_column === "temperatureMax"
// {
// yAccessor = d => d.temperatureMax
// }
// else
// {
// yAccessor = d => d.temperatureMin
// }

Insert cell
dateParse = d3.timeParse("%Y-%m-%d")
Insert cell
xAccessor = d => dateParse(d.date)
Insert cell
md `## 1. Bounding Box`
Insert cell
width = 1000
Insert cell
height = 500
Insert cell
margin = ({ top: 20, right: 20, bottom: 20, left: 20})
Insert cell
md `## 2. Data`
Insert cell
json_data = dataset.json()
Insert cell
md `## 3. Create X and Y axes`
Insert cell
// dateParser = d3.timeParse("%Y-%m-%d")
Insert cell
xScale = d3.scaleTime()
.domain(d3.extent(json_data, xAccessor)) //Min to Max - that is extent
.range([margin.left, width - margin.right])
Insert cell
// xScale = d3.scaleTime()
// .domain(d3.extent(json_data, d => d.date)) //Min to Max - that is extent
// .range([margin.left, width - margin.right])
Insert cell
// .domain(d3.extent(json_data, d => d.date))
Insert cell
// yScale = d3.scaleLinear()
// .domain(d3.extent(json_data, d=>d.temperatureMax))
// .range([height - margin.bottom, margin.top])
Insert cell
yScale = d3.scaleLinear()
.domain(d3.extent(json_data, yAccessor))
//.domain([0, d3.max(json_data, (d)=>d.value)]).nice() // Left to the Right
.range([height - margin.bottom, margin.top]) //Bottom to the Top
Insert cell
xAxis = (g) => g
.attr(`transform`, `translate(0, ${height - margin.bottom}`)
.call(d3.axisBottom(xScale))
Insert cell
yAxis = (g) => g
.attr(`transform`, `translate(${margin.left}, 0)`)
.call(d3.axisLeft(yScale))
Insert cell
md `## Step 4: Define the line function`
Insert cell
// line = d3.line()
// .defined(d => !isNaN(d.temperatureMax))
// .x(d => xScale(dateParser(d.date)))
// .y(d => yScale(d.value))
Insert cell
// line = d3.line()
// .defined(d => !isNaN(d.temperatureMax))
// .x(d => xScale(dateParser(d.date)))
// .y(d => yScale(d.value))
Insert cell
// line = d3.line()
// .defined(d => !isNaN(d.temperatureMax))
// .x(d => xScale(d.date))
// .y(d => yScale(d.value))
Insert cell
// line = d3.line()
// .defined(d => !isNaN(d.value))
// .x(d => xScale(d.date))
// .y(d => yScale(d.value))
Insert cell
// lineGenerator = d3.line()
// .defined(d => !isNaN(d.temperatureHigh))
// .x(d => xScale(d.date))
// .y(d => yScale(d.temperatureHigh))
Insert cell
lineGenerator = d3.line()
.x(d => xScale(xAccessor(d)))
.y(d => yScale(yAccessor(d)))
Insert cell
md `## Step 5. Draw the SVG`
Insert cell
viewof graph_color = color('#88a1d8')
Insert cell
import {slider} from "@jashkenas/inputs"

Insert cell
viewof line_size_slider = slider({
min: 0,
max: 25,
step: 1,
value: 10,
title: "Integers",
description: "Integers from zero through 100"
})
Insert cell
viewof a4 = slider({
min: 0,
max: 10,
// precision: 3,
description: "A high precision slider example"
})
Insert cell
{
// const yAccessor = d => d.temperatureMax
// const dateParser = d3.timeParse("%Y-%m-%d")
// const xAccessor = d => dateParser(d.date)
// Create an empty SVG with specified width and height.
const svg = d3.select(DOM.svg(width, height));
// Draw the x and y axes.
svg.append('g').call(xAxis)
svg.append('g').call(yAxis)
// Draw the line.
svg.append('path')
.datum(json_data)
// .attr('d', line);
.attr('d', lineGenerator);
const line = svg.append("path")
.attr("d", lineGenerator(json_data))
.attr("fill", "none")
// .attr("stroke", "#553232")
// .attr("stroke", "purple")
.attr("stroke", graph_color)
// .attr("stroke-width", 5)
.attr("stroke-width", line_size_slider)
.attr("stroke-width", a4)
return svg.node();
}
Insert cell
html`
<style>
path {
fill: none;
stroke-linejoin: miter;
stroke-linecap: miter;
}

g {
fill: none;
stroke: salmon;
stroke-width: .1;
stroke-linejoin: miter;
stroke-linecap: miter;
}
</style>`
Insert cell
// stroke: purple;
stroke-width: 1;


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