|
import * as d3 from 'd3'; |
|
import formatXml from 'xml-formatter'; |
|
import css from './index.css'; |
|
|
|
const data = [ |
|
["Jul-73", 36.7], |
|
["Jul-74", 34.9], |
|
["Jul-75", 36], |
|
["Jul-76", 34.5], |
|
["Jul-77", 35], |
|
["Jul-78", 37], |
|
["Jul-79", 35.2], |
|
["Jul-80", 34.3], |
|
["Jul-81", 36.5], |
|
["Jul-82", 33.4], |
|
["Jul-83", 35.6], |
|
["Jul-84", 36.4], |
|
["Jul-85", 36.5], |
|
["Jul-86", 36.1], |
|
["Jul-87", 39.1], |
|
["Jul-88", 35.9], |
|
["Jul-89", 33.7], |
|
["Jul-90", 38.9], |
|
["Jul-91", 37.8], |
|
["Jul-92", 37.5], |
|
["Jul-93", 34], |
|
["Jul-94", 37.9], |
|
["Jul-95", 37.9], |
|
["Jul-96", 38.4], |
|
["Jul-97", 39.9], |
|
["Jul-98", 38.5], |
|
["Jul-99", 37.1], |
|
["Jul-00", 37.6], |
|
["Jul-01", 39.6], |
|
["Jul-02", 38], |
|
["Jul-03", 32.5], |
|
["Jul-04", 39.2], |
|
["Jul-05", 36.2], |
|
["Jul-06", 36.9], |
|
["Jul-07", 35.1], |
|
["Jul-08", 36.2], |
|
["Jul-09", 36.8], |
|
["Jul-10", 38], |
|
["Jul-11", 38.2], |
|
["Jul-12", 37.8], |
|
["Jul-13", 38.3], |
|
["Jul-14", 37], |
|
["Jul-15", 38.2], |
|
["Jul-16", 37.3], |
|
["Jul-17", 36.2], |
|
["Jul-18", 41.1] |
|
]; |
|
|
|
const plot = (data) => { |
|
// Transpose data from |
|
// an array of year-value pairs |
|
// to a pair of years and values. |
|
const tdata = d3.transpose(data); |
|
// Place an SVG. |
|
const svg = d3.select("svg"); |
|
const margin = {top: 40, right: 10, bottom: 80, left: 80}, |
|
width = +svg.attr("width") - margin.left - margin.right, |
|
height = +svg.attr("height") - margin.top - margin.bottom, |
|
root = svg.append("g") |
|
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
// Define x-axis. |
|
const x = d3.scaleBand() |
|
.domain(tdata[0]) |
|
.rangeRound([0, width]) |
|
.padding(0.5); |
|
// Define y-axis. |
|
const y = d3.scaleLinear() |
|
.domain([Math.floor(d3.min(tdata[1]) - 1), Math.ceil(d3.max(tdata[1]) + 1)]) |
|
.range([height, 0]); |
|
// Add a bar chart. |
|
const g = root.append("g"); |
|
g.attr("fill", 'blue'); |
|
const rect = g.selectAll("rect") |
|
.data(data) |
|
.enter().append("rect") |
|
.attr("x", (d) => (x(d[0]))) |
|
.attr("y", height) |
|
.attr("width", x.bandwidth()) |
|
.attr("height", 0); |
|
// Animate the bar chart. |
|
rect.transition() |
|
.delay((d, i) => (i * 10)) |
|
.attr("y", (d) => (y(d[1]))) |
|
.attr("height", (d) => (height - y(d[1]))); |
|
// Show years. |
|
root.append("g") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(d3.axisBottom(x) |
|
.tickSize(0) |
|
.tickPadding(6)) |
|
.selectAll("text") |
|
.style("text-anchor", "end") |
|
.attr("dy", "-.2em") |
|
.attr("dx", "-.2em") |
|
.attr("transform", "rotate(-70)"); |
|
// Show the y-axis label. |
|
root.append("text") |
|
.style("text-anchor", "middle") |
|
.attr("transform", "rotate(-90)") |
|
.attr("dx", -height/2) |
|
.attr("dy", -margin.left/2) |
|
.text("degC"); |
|
// Show the x-axis label. |
|
root.append("text") |
|
.style("text-anchor", "middle") |
|
.attr("x", width/2) |
|
.attr("y", height) |
|
.attr("dy", margin.bottom/2) |
|
.text("Year"); |
|
// Show values. |
|
root.append("g") |
|
.call(d3.axisLeft(y)); |
|
}; |
|
|
|
(() => { |
|
const body = d3.select("body").append("div").style("width", "100%"); |
|
const title = "Highest Temperature of July for Each Year in Kumagaya"; |
|
const header = body |
|
.append("div") |
|
.classed(css.title, true) |
|
.text(title); |
|
body.append("div").append("p") |
|
.text("Data taken from www.jma.go.jp"); |
|
body.append("div").append("svg").attr("width", 600).attr("height", 300); |
|
plot(data); |
|
body.append("button") |
|
.text("Dump") |
|
.on("click", () => ( |
|
d3.select("#svg-dump") |
|
.property("value", |
|
formatXml( |
|
(new XMLSerializer()).serializeToString(d3.select("svg").node()), |
|
{ |
|
indentation: ' ' |
|
} |
|
)) |
|
)); |
|
body.append("div") |
|
.style("width", "100%") |
|
.append("textarea") |
|
.attr("id", "svg-dump") |
|
.style("width", "100%") |
|
.attr("rows", 20); |
|
})(); |
|
|