async function lineChart() {
const dataset = await d3.json(
"https://gist.githubusercontent.com/chekos/7ee802ef53ba4bbd10a3b8161116d638/raw/e0d655473a57fae5ba54e648429bfd01ca698e12/tijuana_weather_data.json"
);
let dimensions = {
width: 685,
height: 450,
margin: {
top: 25,
right: 10,
bottom: 50,
left: 50
}
};
dimensions.boundedWidth =
dimensions.width - dimensions.margin.left - dimensions.margin.right;
dimensions.boundedHeight =
dimensions.height - dimensions.margin.top - dimensions.margin.bottom;
const wrapper = d3
.select("#wrapper-lineChart")
.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 dateParser = d3.timeParse("%Y-%m-%d");
const xAccessor = d => dateParser(d.date);
const yAccessor = d => ((d.temperatureMax - 32) * 5) / 9;
const xScale = d3
.scaleTime()
.domain(d3.extent(dataset, xAccessor))
.range([0, dimensions.boundedWidth])
.nice();
const yScale = d3
.scaleLinear()
.domain(d3.extent(dataset, yAccessor))
.range([dimensions.boundedHeight, 0])
.nice();
const lineGenerator = d3
.line()
.x(d => xScale(xAccessor(d)))
.y(d => yScale(yAccessor(d)));
bounds
.append("path")
.attr("d", lineGenerator(dataset))
.attr("fill", "none")
.attr("stroke", selected_color)
.attr("stroke-width", 1.4);
const xAxisGenerator = d3.axisBottom().scale(xScale);
const xAxis = bounds
.append("g")
.call(xAxisGenerator)
.style("transform", `translateY(${dimensions.boundedHeight}px)`);
const xAxisLabel = xAxis
.append("text")
.attr("x", dimensions.boundedWidth / 2)
.attr("y", dimensions.margin.bottom - 10)
.attr("fill", allumaSlate)
.style("font-size", "1.4em")
.html("X axis title")
.attr("class", "axis-title");
const yAxisGenerator = d3
.axisLeft()
.scale(yScale)
.ticks(dimensions.boundedHeight / 50);
const yAxis = bounds.append("g").call(yAxisGenerator);
const yAxisLabel = yAxis
.append("text")
.attr("x", -dimensions.margin.left)
.attr("y", -dimensions.margin.top / 2)
.attr("fill", allumaSlate)
.style("font-size", "1.4em")
.text("Y axis title")
.style("text-anchor", "start")
.attr("class", "axis-title");
}