color_scale = {
const margin = {top: 10, right: 50, bottom: 20, left: 550};
const width = 300;
const height = 30;
const key = "Value";
const colorScale = d3.scaleSequential()
.domain([0, 0.4])
.interpolator(d3.interpolateViridis);
const svg = d3.create("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
const g = svg.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const gradient = g.append("linearGradient")
.attr("id", "gradient")
.attr("x1", "0%")
.attr("y1", "100%")
.attr("x2", "100%")
.attr("y2", "100%")
.attr("spreadMethod", "pad");
for (let i = 0; i <= 1; i += 0.01) {
gradient.append("stop")
.attr("offset", i * 100 + "%")
.attr("stop-color", colorScale(i * 0.4))
.attr("stop-opacity", 1);
}
g.append("rect")
.attr("width", width)
.attr("height", height)
.style("fill", "url(#gradient)")
.attr("transform", `translate(0,0)`);
const axisScale = d3.scaleLinear()
.domain([0, 0.4])
.range([0, width]);
const axisBottom = g => g
.attr("class", `x-axis`)
.attr("transform", `translate(0,${height})`)
.call(d3.axisBottom(axisScale).ticks(5))
.call(g => g.select(".domain").remove());
g.append("g")
.call(axisBottom);
return svg.node();
}