This commit is contained in:
philipp 2023-10-01 14:19:23 +02:00
parent fa66efa361
commit 22642e3867

View File

@ -42,94 +42,59 @@
import * as d3 from "https://cdn.jsdelivr.net/npm/d3@7/+esm"; import * as d3 from "https://cdn.jsdelivr.net/npm/d3@7/+esm";
// Declare the chart dimensions and margins. const data = [
const width = 928; { date: '2023-01-01', km: 5 },
const height = 500; { date: '2023-02-01', km: 24 },
const marginTop = 20; { date: '2023-08-30', km: 1340 },
const marginRight = 30; ];
const marginBottom = 30;
const marginLeft = 40;
// Declare the x (horizontal position) scale. const margin = { top: 20, right: 20, bottom: 50, left: 50 },
const parseTime = d3.timeParse("%d-%b-%y"); width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
const parseTime = d3.timeParse('%Y-%m-%d');
data.forEach(d => {
d.date = parseTime(d.date);
d.km = +d.km;
});
const x = d3.scaleTime() const x = d3.scaleTime()
.rangeRound([0, width]); .domain(d3.extent(data, d => d.date))
.range([0, width]);
const y = d3.scaleLinear() const y = d3.scaleLinear()
.rangeRound([height, 0]); .domain([0, d3.max(data, d => d.km)])
.range([height, 0]);
// Declare the line generator.
const line = d3.line() const line = d3.line()
.x(d => x(d.date)) .x(d => x(d.date))
.y(d => y(d.km)); .y(d => y(d.km));
// Create the SVG container. const svg = d3.select('#container')
const svg = d3.create("svg") .append('svg')
.attr("width", width) .attr('width', width + margin.left + margin.right)
.attr("height", height) .attr('height', height + margin.top + margin.bottom)
.attr("viewBox", [0, 0, width, height]) .append('g')
.attr("style", "max-width: 100%; height: auto; height: intrinsic;"); .attr('transform', `translate(${margin.left},${margin.top})`);
// Add the x-axis. svg.append('path')
svg.append("g") .data([data])
.attr("transform", `translate(0,${height - marginBottom})`) .attr('class', 'line')
.call(d3.axisBottom(x).ticks(width / 80).tickSizeOuter(0)); .attr('d', line);
// Add the y-axis, remove the domain line, add grid lines and a label. svg.append('g')
svg.append("g") .attr('transform', `translate(0,${height})`)
.attr("transform", `translate(${marginLeft},0)`) .call(d3.axisBottom(x));
.call(d3.axisLeft(y).ticks(height / 40))
.call(g => g.select(".domain").remove())
.call(g => g.selectAll(".tick line").clone()
.attr("x2", width - marginLeft - marginRight)
.attr("stroke-opacity", 0.1))
.call(g => g.append("text")
.attr("x", -marginLeft)
.attr("y", 10)
.attr("fill", "currentColor")
.attr("text-anchor", "start")
.text("↑ Daily km ($)"));
d3.csv("../public/csv/stats.csv").then(function(data) { svg.append('g')
data.forEach(function(d) { .call(d3.axisLeft(y));
d.date = d3.timeParse("%Y-%m-%d")(d.date);
d.km = +d.km;
console.log(d);
});
}, function(error, data) {
if (error) throw error;
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.km; }));
g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.select(".domain")
.remove();
g.append("g")
.call(d3.axisLeft(y))
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Price ($)");
g.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5)
.attr("d", line);
});
container.append(svg.node());
</script> </script>
<style>
.line {
fill: none;
stroke: steelblue;
stroke-width: 2px;
}
</style>
{% endblock content%} {% endblock content%}