UNPKG

dimple-js

Version:

Dimple is an object-oriented API allowing you to create flexible axis-based charts using [d3.js](http://d3js.org "d3.js").

47 lines 2.11 kB
<div id="chartContainer"> <script src="/lib/d3.v3.4.8.min.js"></script> <script src="/dist/dimple.v2.0.0.js"></script> <script type="text/javascript"> // This is the simple vertical grouped stacked 100% bar example var svg = dimple.newSvg("#chartContainer", 590, 400); d3.tsv("/data/example_data.tsv", function (data) { var myChart = new dimple.chart(svg, data); myChart.setBounds(65, 45, 505, 315) var x = myChart.addCategoryAxis("x", ["Price Tier", "Channel"]); var y = myChart.addPctAxis("y", "Unit Sales"); var s = myChart.addSeries("Owner", dimple.plot.bar); // Using the afterDraw callback means this code still works with animated // draws (e.g. myChart.draw(1000)) or storyboards (though an onTick callback should // also be used to clear the text from the previous frame) s.afterDraw = function (shape, data) { // Get the shape as a d3 selection var s = d3.select(shape), rect = { x: parseFloat(s.attr("x")), y: parseFloat(s.attr("y")), width: parseFloat(s.attr("width")), height: parseFloat(s.attr("height")) }; // Only label bars where the text can fit if (rect.height >= 8) { // Add a text label for the value svg.append("text") // Position in the centre of the shape (vertical position is // manually set due to cross-browser problems with baseline) .attr("x", rect.x + rect.width / 2) .attr("y", rect.y + rect.height / 2 + 3.5) // Centre align .style("text-anchor", "middle") .style("font-size", "10px") .style("font-family", "sans-serif") // Make it a little transparent to tone down the black .style("opacity", 0.6) // Format the number .text(d3.format(",.1f")(data.yValue / 1000) + "k"); } }; myChart.addLegend(200, 10, 380, 20, "right"); myChart.draw(2000); }); </script> </div>