function windDirchart(index, indb = undefined) {
const height = 120;
const margin = { top: 20, right: 30, bottom: 30, left: 40 };
const x = d3
.scaleTime()
.domain(d3.extent(buoyData, (d) => d.date))
.range([margin.left, width - margin.right]);
let y;
if (indb) {
y = d3
.scaleLinear()
.domain([0, d3.max(buoyData, (d) => d[indb])])
.nice()
.range([height - margin.bottom, margin.top]);
} else {
y = d3
.scaleLinear()
.domain([0, d3.max(buoyData, (d) => d[index])])
.nice()
.range([height - margin.bottom, margin.top]);
}
const svg = d3.select(DOM.svg(width, height)).style("width", "100%");
svg
.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x));
svg
.append("g")
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(d3.axisLeft(y).ticks(4))
.call((g) =>
g
.select(".tick:last-of-type text")
.clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text("kts")
);
if (indb) {
svg
.append("path")
.datum(buoyData)
.attr("fill", "none")
.attr("stroke", "#e31a1c")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr(
"d",
d3
.line()
.x((d) => x(d.date))
.y((d) => y(d[indb]))
);
}
svg
.append("path")
.datum(buoyData)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr(
"d",
d3
.line()
.x((d) => x(d.date))
.y((d) => y(d[index]))
);
svg
.selectAll("circle")
.data(buoyData)
.enter()
.append("path")
.attr("d", "M-5,0 L-15,15 L15,0 L-15,-15 Z")
.attr("class", "arrowHead")
.attr("fill", "grey")
.attr("transform", function (d, i) {
return (
"translate(" +
x(d.date) +
"," +
y(d[index]) +
") rotate(" +
getAngle(d["WDIR"]) +
") scale(0.5)"
);
});
svg
.append("g")
.selectAll("text")
.data(buoyData)
.enter()
.append("text")
.text(
(d, i) => getText(d["WDIR"])
)
.attr("x", (d) => x(d.date))
.attr("y", (d) => y(d[index]))
.attr("font-family", "sans-serif")
.attr("font-size", "16px")
.attr("fill", "green");
return svg.node();
}