Published
Edited
Feb 11, 2021
Importers
Insert cell
Insert cell
viewof chartDistribution = {
var svg = d3
.create("svg")
.attr("width", 100)
.attr("height", 100);

barChart({ node: svg.node(), data: dataDistributionRight });

return svg.node();
}
Insert cell
Insert cell
data = [
{ judge: 'FN', value: 4 },
{ judge: 'TN', value: 6 },
{ judge: 'FP', value: 8 },
{ judge: 'TP', value: 5 }
]
Insert cell
function renderFT(opts = {}) {
// debugger;
var data = opts.data,
node = opts.node,
height = d3.select(node).attr("height"),
width = d3.select(node).attr("width"),
margin = {
left: 7,
right: 1,
top: 1,
bottom: 2
},
x = d3
.scaleBand()
.domain(data.map(d => d.judge))
.range([margin.left, d3.select(node).attr("width") - margin.right])
.padding(0.1),
y = d3
.scaleLinear()
.domain([0, d3.max(data.map(d => d.value))])
.range([d3.select(node).attr("height") - margin.bottom, margin.top]);
var color = d3
.scaleOrdinal()
.domain(['FN', 'TN', 'FP', 'TP'])
.range(['red', 'grey', 'yellow', 'green']);
var svg = d3.select(node);

const g = svg.append('g');
// .attr('transform', `translate(${margin.left}, ${margin.top})`);

g.append("g")
.append("rect")
.attr("x", margin.left)
.attr("y", margin.top)
.attr("width", width - margin.right - margin.left)
.attr("height", height - margin.bottom - margin.top)
.attr("fill", 'none')
.attr("stroke-width", 1)
.attr("stroke", "black");

let bar = g
.selectAll('.bar')
.data(data)
.enter()
.append('g')
.attr('class', 'bar-group');

bar
.append('rect')
.attr('class', '.bar')
.attr('x', d => x(d.judge))
.attr('y', d => y(d.value))
.attr('width', x.bandwidth())
.attr('height', d => height - margin.bottom - y(d.value))
.style('fill', d => color(d.judge));

bar
.append('text')
.text(d => d.judge)
.attr('x', d => x(d.judge) + x.bandwidth() / 2)
.attr('y', height - margin.bottom)
.attr('text-anchor', 'middle')
.style('font-family', 'sans-serif')
// .style('font-size', x.bandwidth())
.style('font-size', 7)
.style('fill', 'black');

bar
.append('text')
.text('0')
.attr('x', margin.left * 0.8)
.attr('y', height - margin.bottom)
.attr('text-anchor', 'end')
.style('font-family', 'sans-serif')
.style('font-size', margin.left)
.style('fill', 'black'); // visualize the minimum axis;

bar
.append('text')
.text(d3.max(data.map(d => d.value)))
.attr('text-anchor', 'end')
.attr('dominant-baseline', d => {
if (d3.max(data.map(d => d.value)) >= 10) {
return 'auto';
} else {
return "hanging";
}
})
.attr("transform", d => {
if (d3.max(data.map(d => d.value)) >= 10) {
return `translate(${margin.left * 0.8},${margin.top})rotate(-90)`;
} else {
return `translate(${margin.left * 0.8},${margin.top})`;
}
})
.style('font-family', 'sans-serif')
.style('font-size', margin.left)
.style('fill', 'black'); // visualize the maximum axis;
}
Insert cell
function barChart(opts = {}) {
// debugger;
var data = opts.data,
node = opts.node,
height = d3.select(node).attr("height"),
width = d3.select(node).attr("width"),
margin = {
left: 7,
right: 1,
top: 1,
bottom: 2
},
x = d3
.scaleBand()
.domain(data.map(d => d.pred))
.range([margin.left, d3.select(node).attr("width") - margin.right])
.padding(0.1),
y = d3
.scaleLinear()
.domain([0, d3.max([d3.max(data, d => d.sum), 2])])
.range([d3.select(node).attr("height") - margin.bottom, margin.top]);
var svg = d3.select(node);

const g = svg.append('g');
// .attr('transform', `translate(${margin.left}, ${margin.top})`);

g.append("g")
.append("rect")
.attr("x", margin.left)
.attr("y", margin.top)
.attr("width", width - margin.right - margin.left)
.attr("height", height - margin.bottom - margin.top)
.attr("fill", 'none')
.attr("stroke-width", 1)
.attr("stroke", "black");

let bar = g
.selectAll('.bar')
.data(data)
.enter()
.append('g')
.attr('class', 'bar-group');

bar
.append('rect')
.attr('class', '.bar')
.attr('x', d => x(d.pred))
.attr('y', d => y(d.sum))
.attr('width', x.bandwidth())
.attr('height', d => height - margin.bottom - y(d.sum))
.style('fill', 'steelblue');

bar
.append('text')
.text(d => d.pred)
.attr('x', d => x(d.pred) + x.bandwidth() / 2)
.attr('y', height - margin.bottom)
.attr('text-anchor', 'middle')
.style('font-family', 'sans-serif')
// .style('font-size', x.bandwidth())
.style('font-size', d =>
Math.min(x.bandwidth(), height - margin.bottom - y(d.sum))
)
.style('fill', 'black');

bar
.append('text')
.text('0')
.attr('x', margin.left * 0.8)
.attr('y', height - margin.bottom)
.attr('text-anchor', 'end')
.style('font-family', 'sans-serif')
.style('font-size', margin.left)
.style('fill', 'black'); // visualize the minimum axis;

bar
.append('text')
.text(d3.max(data.map(d => d.sum)))
.attr('text-anchor', 'end')
.attr("dominant-baseline", d => {
if (d3.max(data.map(d => d.sum)) >= 10) {
return 'auto';
} else {
return "hanging";
}
})
.attr("transform", d => {
if (d3.max(data.map(d => d.sum)) > 10) {
return `translate(${margin.left * 0.8},${margin.top})rotate(-90)`;
} else {
return `translate(${margin.left * 0.8},${margin.top})`;
}
})
.style('font-family', 'sans-serif')
.style('font-size', margin.left)
.style('fill', 'black'); // visualize the maximum axis;
}
Insert cell
classId = unique(dataset1.map(d => d.pred)) // All the names of labels of class, e.g. '1','2','3', etc.
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more