JSFiddle - React, Tailwind, and code Playground
HTML
<input id="clickMe" type="button" value="clickme" />
<div id="chart"></div>
JavaScript
var data = [{
"position": 5,
"name": "Test1",
"shape": true
}, {
"position": 10,
"name": "Test2",
"shape": false
}, {
"position": 15,
"name": "Test3",
"shape": true
}];
var vis;
var gEnter;
var group;
var margin = {
top: 20,
right: 15,
bottom: 15,
left: 200
}, width = 1000 - margin.left - margin.right,
height = 800 - margin.top - margin.bottom;
var yRange = d3.scale.linear().range([0, height]);
var xRange = d3.scale.linear().range([0, width]);
var xAxis = d3.svg.axis().scale(xRange).orient('bottom').ticks(4).tickSize(6, 0, 0);
var yAxis = d3.svg.axis().scale(yRange).orient('left').ticks(4).tickSize(6, 0, 0);
xRange.domain([0, 100]);
yRange.domain([0, 100]);
vis = d3.select("#chart")
.append('svg')
.attr('width', width)
.attr('height', height)
.attr('class', 'chart');
vis.append("g").attr("class", "x axis").attr("transform", "translate(0," + height + ")").call(xAxis);
vis.append("g").attr("transform", "translate(40, 0)").attr("class", "y axis").call(yAxis);
//----------------------------------------------------------------------------------------//
group = vis.selectAll(".rectangle, .circle").data(data); // select the data points and set their data
gEnter = group.enter()
.append("g")
.attr("class", function (d) {
return d.shape ? "rectangle" : "circle";
})
.attr("transform", function (d) {
return "translate(" + xRange(d.position) + "," + yRange(d.position) + ")";
});
vis.selectAll(".rectangle")
.append("rect")
.attr("class", "rectangle");
group.selectAll(".rectangle")
.attr("width", 40)
.attr("height", 18)
.style("fill", "red")
.style("opacity", .5) // set the element opacity
.style("stroke", "black"); // set the line colour;
vis.selectAll(".circle")
.append("circle")
.attr("class", "circ");
group.selectAll(".circ")
.attr("r", 20)
...