Skip to content

Instantly share code, notes, and snippets.

@NPashaP
Last active August 7, 2016 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NPashaP/a97b84dd96b9dbb7fd8d1b2acbb5f4ca to your computer and use it in GitHub Desktop.
Save NPashaP/a97b84dd96b9dbb7fd8d1b2acbb5f4ca to your computer and use it in GitHub Desktop.
biPartite -keyPrimary, keySecondary, value
license: gpl-3.0

viz.bP().keyPrimary, viz.bP().keySecondary, viz.bP().value

These methods are used to set the accessor functions for the primary and secondary keys and values for the graph. The primary key determines the groups appearing on the left for vertical orientation and top for horizontal orientation. The secondary is the other part. The value column is used to compute the size of bars.

The left example has the default keyPrimary, keySecondary, and value accessors which are d=>d[0], d=>d[1] and d=>d[2] respectively.

The right example is using d=>d[1] (column with X,Y in data) for primary part, d=>d[0] (column with A,B, C in data) for secondary part, and d=>d[3] for value.

For other options and examples, check VizJS

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.mainBars{
shape-rendering: auto;
fill-opacity: 0;
stroke-width: 0.5px;
stroke: rgb(0, 0, 0);
stroke-opacity: 0;
}
.subBars{
shape-rendering:crispEdges;
}
.edges{
stroke:none;
fill-opacity:0.3;
}
</style>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="http://vizjs.org/viz.v1.1.0.min.js"></script>
<script>
var width = 960, height=700;
var data=[
['A','X', 1, 5]
,['A','Y', 3,4]
,['B','X', 5,1]
,['B','Y', 8,10]
,['C','X', 2,13]
,['C','Y', 9,7]
];
var color = {A:"#3366CC", B:"#DC3912", C:"#FF9900"};
var svg = d3.select("body").append("svg").attr("width", width).attr("height", height);
var bp1=viz.bP().data(data).fill(d=>color[d.primary]);
svg.append("g").attr("transform","translate(50,50)").call(bp1);
var bp2=viz.bP().data(data)
.fill(d=>color[d.secondary])
.keyPrimary(d=>d[1])
.keySecondary(d=>d[0])
.value(d=>d[3]);
svg.append("g").attr("transform","translate(500,50)").call(bp2);
// adjust the bl.ocks frame dimension.
d3.select(self.frameElement).style("height", height+"px").style("width", width+"px");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment