JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://d3js.org/d3.v4.min.js"></script>
<div class="chart-container">
<div class="js-chart-containerX">
<form class="controls">
Scale X:
<label><input type="radio" name="x-scale" value="power2" checked>Power2</label>
<label><input type="radio" name="x-scale" value="linear">Linear</label>
<label><input type="radio" name="x-scale" value="sqrt">SquareRoot</label>
<label><input type="radio" name="x-scale" value="log2">Log2</label>
<label><input type="radio" name="x-scale" value="log10">Log10</label>
</form>
</div>
<div class="js-chart-containerY">
<form class="controls">
Scale Y:
<label><input type="radio" name="y-scale" value="power2" checked>Power2</label>
<label><input type="radio" name="y-scale" value="linear">Linear</label>
<label><input type="radio" name="y-scale" value="sqrt">SquareRoot</label>
<label><input type="radio" name="y-scale" value="log2">Log2</label>
<label><input type="radio" name="y-scale" value="log10">Log10</label>
</form>
</div>
<svg class="chart js-chart"></svg>
</div>
CSS
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.chart-container {
position: relative;
}
.js-chart-containerY{
position: relative;
top: 24px;
}
.js-chart-containerX{
position: relative;
top: 0px;
}
.chart-container .controls {
position: absolute;
top: 14px;
left: 18px;
}
.chart path,
.chart line,
.chart rect {
shape-rendering: crispEdges;
}
.chart .axis path,
.chart .axis line {
fill: none;
stroke: #000;
}
.chart .linear .point {
fill: steelblue;
}
.chart .pow .point {
fill: #CD4638;
}
JavaScript
"use strict";
var chart = {
margin: { top: 60, right: 25, bottom: 20, left: 25 },
animationDuration: 400,
scales: {
power2: d3.scalePow().exponent(2),
linear: d3.scaleLinear(),
sqrt: d3.scalePow().exponent(0.5),
log2: d3.scaleLog().base(2),
log10: d3.scaleLog().base(10)
},
init: function (container, data) {
this.el = d3.select(".js-chart")
.attr("width", container.width)
.attr("height", container.height);
this.width = container.width - this.margin.left - this.margin.right;
this.height = container.height - this.margin.top - this.margin.bottom;
this.adaptScalesX();
this.adaptScalesY();
this.setXScale();
this.setYScale();
this.draw(data);
d3.selectAll(".js-chart-containerX input").on("click", this.changeXScale.bind(this));
d3.selectAll(".js-chart-containerY input").on("click", this.changeYScale.bind(this));
},
draw: function (data) {
var mainGroup, series;
mainGroup = this.el.append("g")
.attr("transform", "translate(" + this.margin.left + "," + this.margin.top + ")");
series = mainGroup.selectAll(".series").data(data)
.enter().append("g")
.attr("class", function (d) { return "series " + d.name; });
this.points = series.selectAll(".point").data(data)
.enter().append("circle")
.attr("class", "point")
.attr('r',2)
.attr("cx", function(d) { return d.x;}.xScale)
.attr("cy", function(d) { return d.y;}.yScale)
this.xAxis = d3.axisBottom()
.scale(this.xScale);
this.domXAxis = mainGroup.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + this.height + ")")
.call(this.xAxis);
this.yAxis = d3.axisLeft()
.scale(this.yScale);
...