success2 =
{
const dataset = json_data
const yAccessor = d => d.temperatureMax
const dateParser = d3.timeParse("%Y-%m-%d")
const xAccessor = d => dateParser(d.date)
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
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 }
}