This data is from UCI Machine Learning Repository: Car Evaluation Data Set. Inspired by syntagmatic's block: Abalone. forked from syntagmatic's block: Abalone
xxxxxxxxxx
<meta charset="utf-8">
<title>car</title>
<style>
svg {
font: 13px sans-serif;
}
.foreground path {
fill: none;
stroke: #222;
stroke-opacity: 0.35;
pointer-events: none;
stroke-width: 1.5px;
}
.axis .title {
font-size: 11px;
font-weight: bold;
text-transform: uppercase;
}
.axis line,
.axis path {
fill: none;
stroke: black;
stroke-width: 2px;
fill-opacity:0.1
}
.brush .extent {
fill-opacity: .3;
stroke: #fff;
stroke-width: 1px;
}
pre {
width: 900px;
margin: 10px 30px;
tab-size: 15;
font-size: 15px;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var margin = {top: 50, right: 60, bottom: 20, left: 50},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var types = {
"Number": {
name: "Number",
coerce: function(d) { return +d; },
extent: d3.extent,
within: function(d, extent) { return extent[0] <= d && d <= extent[1]; }
},
"String": {
name: "String",
coerce: String,
extent: function (data) { return data.sort(); },
within: function(d, extent, dim) { return extent[0] <= dim.scale(d) && dim.scale(d) <= extent[1]; }
}
};
var dimensions = [
{
name: "buying",
scale: d3.scale.ordinal().rangePoints([0, height]),
type: types["String"],
domain: ["vhigh", "high", "med","low"]
},
{
name: "maint",
scale: d3.scale.ordinal().rangePoints([0, height]),
type: types["String"],
domain: ["vhigh", "high", "med","low"]
},
{
name: "doors",
scale: d3.scale.ordinal().rangePoints([0, height]),
type: types["String"],
domain: ["2", "3", "4","5more"]
},
{
name: "persons",
scale: d3.scale.ordinal().rangePoints([0, height]),
type: types["String"],
domain: ["2", "4","more"]
},
{
name: "lug_boot",
scale: d3.scale.ordinal().rangePoints([0, height]),
type: types["String"],
domain: ["small", "med", "big"]
},
{
name: "safety",
scale: d3.scale.ordinal().rangePoints([0, height]),
type: types["String"],
domain: ["high", "med","low"]
},
{
name: "class_name",
scale: d3.scale.ordinal().rangePoints([0, height]),
type: types["String"],
domain: ["unacc", "acc","good","vgood"]
},
];
var color = d3.scale.ordinal()
.range(["blue","red","yellow","purple"]);
var x = d3.scale.ordinal()
.domain(dimensions.map(function(dim) { return dim.name; }))
.rangePoints([0, width]);
var line = d3.svg.line()
.defined(function(d) { return !isNaN(d[1]); });
var yAxis = d3.svg.axis()
.orient("left");
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var output = d3.select("body").append("pre");
var dimension = svg.selectAll(".dimension")
.data(dimensions)
.enter().append("g")
.attr("class", "dimension")
.attr("transform", function(d) { return "translate(" + x(d.name) + ")"; });
var colordimension = dimensions[6];
d3.csv("data.csv", function(error, rawdata) {
if (error) throw error;
// take subset of data
data = d3.shuffle(rawdata);
data.forEach(function(d) {
dimensions.forEach(function(p) {
d[p.name] = p.type.coerce(d[p.name]);
});
});
dimensions.forEach(function(dim) {
if (!("domain" in dim)) {
// detect domain using dimension type's extent function
dim.domain = d3.functor(dim.type.extent)(data.map(function(d) { return d[dim.name]; }));
}
dim.scale.domain(dim.domain);
});
color.domain(colordimension.scale.domain());
svg.append("g")
.attr("class", "foreground")
.selectAll("path")
.data(data)
.enter().append("path")
.attr("d", draw)
//.attr('fill-opacity', 0.8)
.style("stroke", function(d) { return color(d[colordimension.name]); });
dimension.append("g")
.attr("class", "axis")
.each(function(d) { d3.select(this).call(yAxis.scale(d.scale)); })
.append("text")
.attr("class", "title")
.attr("text-anchor", "middle")
.attr("y", -9)
.text(function(d) { return d.name; });
// Add and store a brush for each axis.
dimension.append("g")
.attr("class", "brush")
.each(function(d) {
d3.select(this).call(d.brush = d3.svg.brush()
.y(d.scale)
.on("brushstart", brushstart)
.on("brush", brush));
})
.selectAll("rect")
.attr("x", -8)
.attr("width", 16);
//output.text(d3.tsv.format(data));
function draw(d) {
return line(dimensions.map(function(dim) {
return [x(dim.name), dim.scale(d[dim.name])];
}));
}
function brushstart() {
d3.event.sourceEvent.stopPropagation();
}
// Handles a brush event, toggling the display of foreground lines.
function brush() {
var actives = dimensions.filter(function(p) { return !p.brush.empty(); }),
extents = actives.map(function(p) { return p.brush.extent(); });
var selected = [];
d3.selectAll(".foreground path").style("display", function(d) {
if (actives.every(function(dim, i) {
// test if point is within extents for each active brush
return dim.type.within(d[dim.name], extents[i], dim);
})) {
selected.push(d);
return null;
}
return "none";
});
//output.text(d3.tsv.format(selected));
}
});
</script>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js