dimple/examples/advanced_lollipop_with_hove...

133 lines
4.6 KiB
HTML
Raw Normal View History

2013-09-12 13:53:11 +04:00
<!----------------------------------------------------------------->
<!-- AUTOMATICALLY GENERATED CODE - PLEASE EDIT TEMPLATE INSTEAD -->
<!----------------------------------------------------------------->
2013-05-21 15:42:17 +04:00
<div id="chartContainer">
2013-09-11 22:06:40 +04:00
<script src="/lib/d3.v3.min.js"></script>
2014-04-02 22:46:49 +04:00
<script src="/dist/dimple.v1.2.0.js"></script>
2013-05-21 15:42:17 +04:00
<script type="text/javascript">
2013-05-29 19:30:08 +04:00
2013-05-21 15:42:17 +04:00
var svg = dimple.newSvg("#chartContainer", 590, 400);
d3.tsv("/data/example_data.tsv", function (data) {
2013-05-29 19:30:08 +04:00
2013-06-15 02:22:24 +04:00
// Focus on the top 6 brands
data = dimple.filterData(data, "Brand", ["Theta", "Omicron", "Kappa", "Beta", "Lambda", "Alpha"]);
2013-05-29 19:30:08 +04:00
// The main chart
2013-05-21 15:42:17 +04:00
var myChart = new dimple.chart(svg, data);
2013-05-29 19:30:08 +04:00
// The chart to show in a tooltip
var tipChart = null;
// The other popup shapes
var popup = null;
// Position the main chart
2013-08-29 14:20:34 +04:00
myChart.setBounds(60, 40, 500, 320);
2013-05-29 19:30:08 +04:00
// Add the main chart axes
myChart.addCategoryAxis("x", "Brand");
myChart.addMeasureAxis("y", "Unit Sales");
myChart.addMeasureAxis("z", "Sales Value");
2013-05-30 04:52:46 +04:00
// Draw bubbles by SKU colored by brand
var s = myChart.addSeries(["SKU", "Brand"], dimple.plot.bubble);
2013-05-29 19:30:08 +04:00
// Handle the hover event - overriding the default behaviour
2013-05-21 15:42:17 +04:00
s.addEventHandler("mouseover", onHover);
2013-05-29 19:30:08 +04:00
// Handle the leave event - overriding the default behaviour
2013-05-21 15:42:17 +04:00
s.addEventHandler("mouseleave", onLeave);
2013-05-29 19:30:08 +04:00
// Draw the main chart
2013-05-21 15:42:17 +04:00
myChart.draw();
2013-05-29 19:30:08 +04:00
// Event to handle mouse enter
function onHover(e) {
// Get the properties of the selected shape
var cx = parseFloat(e.selectedShape.attr("cx")),
cy = parseFloat(e.selectedShape.attr("cy")),
r = parseFloat(e.selectedShape.attr("r")),
fill = e.selectedShape.attr("fill"),
stroke = e.selectedShape.attr("stroke");
// Set the size and position of the popup
var width = 150,
height = 100,
2013-05-30 04:52:46 +04:00
x = (cx + r + width + 10 < svg.attr("width") ?
cx + r + 10 :
cx - r - width - 20);
y = (cy - height / 2 < 0 ?
15 :
cy - height / 2);
2013-05-29 19:30:08 +04:00
// Fade the popup fill mixing the shape fill with 80% white
var popupFillColor = d3.rgb(
d3.rgb(fill).r + 0.8 * (255 - d3.rgb(fill).r),
d3.rgb(fill).g + 0.8 * (255 - d3.rgb(fill).g),
d3.rgb(fill).b + 0.8 * (255 - d3.rgb(fill).b)
);
// Create a group for the popup objects
popup = svg.append("g");
// Add a rectangle surrounding the chart
popup
.append("rect")
.attr("x", x + 5)
.attr("y", y - 5)
.attr("width", width)
.attr("height", height)
.attr("rx", 5)
.attr("ry", 5)
.style("fill", popupFillColor)
.style("stroke", stroke)
.style("stroke-width", 2);
// Add the series value text
popup
.append("text")
.attr("x", x + 10)
.attr("y", y + 10)
2013-05-30 04:52:46 +04:00
.text(e.seriesValue[0])
2013-05-29 19:30:08 +04:00
.style("font-family", "sans-serif")
.style("font-size", 10)
.style("fill", stroke);
// Filter the data for the selected brand and sku
2013-06-15 02:22:24 +04:00
var hoverData = dimple.filterData(data, "Brand", e.xValue);
2013-05-29 19:30:08 +04:00
hoverData = dimple.filterData(hoverData, "SKU", e.seriesValue);
2013-05-21 15:42:17 +04:00
2013-05-29 19:30:08 +04:00
// Create a new mini chart of Unit Sales over Months
tipChart = new dimple.chart(svg, hoverData);
tipChart.setBounds(x + 10, y + 30, width - 10, height - 40);
2013-06-15 02:22:24 +04:00
tipChart.addCategoryAxis("x", "Date").hidden = true;
2013-05-29 19:30:08 +04:00
tipChart.addMeasureAxis("y", "Unit Sales").hidden = true;
// Add a bar series, this can be changed to a line, area or bubble
// by changing the plot parameter accordingly.
var popUpSeries = tipChart.addSeries("SelectedSeries", dimple.plot.bar);
// Set the gap to 80% - just a style preference
popUpSeries.barGap = 0.8;
// Set the color to the stroke color of the selected node
tipChart.assignColor("SelectedSeries", stroke, stroke);
// Draw the mini chart
tipChart.draw();
};
// Event to handle mouse exit
function onLeave(e) {
// Remove the chart
if (tipChart !== null) {
tipChart._group.remove();
}
// Remove the popup
if (popup !== null) {
popup.remove();
}
};
2013-05-21 15:42:17 +04:00
2013-05-29 19:30:08 +04:00
});
2013-05-21 15:42:17 +04:00
</script>
</div>