Commit f9fef9d01651e31b4d8ff881632845b8ff3c2b50

Authored by hitier
1 parent 68fd83a8

Add tooltip

Showing 1 changed file with 37 additions and 4 deletions   Show diff stats
app/main/static/js/charges.js
... ... @@ -2,7 +2,8 @@ var margin = {top: 30, right: 30, bottom: 130, left: 90},
2 2 width = 1000 - margin.left - margin.right,
3 3 height = 500 - margin.top - margin.bottom;
4 4  
5   -const tooltip_offset = {dx: 300, dy: 120}
  5 +const tooltip_offset = {dx: margin.left+200, dy: margin.right+90}
  6 +
6 7  
7 8 // TODO: set main scales attr here then domain later
8 9 // const x = d3.scaleBand()
... ... @@ -37,12 +38,41 @@ function build_chart(div_selector, data_url) {
37 38 .append("g")
38 39 .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
39 40  
  41 + const tooltip = d3.select(div_selector)
  42 + .append("div")
  43 + .style("opacity", 0)
  44 + .attr("class", "tooltip")
  45 +
  46 + var mousemove = function (e, d) {
  47 + tooltip
  48 + .style("left", (e.pageX - tooltip_offset.dx) + "px")
  49 + .style("top", (e.pageY - tooltip_offset.dy) + "px")
  50 + }
  51 +
  52 + var mouseleave = function (d) {
  53 + tooltip
  54 + .transition()
  55 + .duration(100)
  56 + .style("opacity", 0)
  57 + }
  58 +
  59 + var mouseover = function (e, d) {
  60 + var category_name = d3.select(this.parentNode).datum().key
  61 + var category_charge = d.data[category_name]
  62 + tooltip
  63 + .transition()
  64 + .duration(200)
  65 + .style("opacity", 1);
  66 + tooltip
  67 + .html("<b>Categorie:</b> " + category_name + "<br>" + "<b>Charge:</b> " + category_charge + "%")
  68 + .style("left", (e.pageX - tooltip_offset.dx) + "px")
  69 + .style("top", (e.pageY - tooltip_offset.dy) + "px")
  70 + }
  71 +
40 72 d3.csv(data_url).then(data => {
41 73 var services = data.columns.slice(1)
42 74 var periods = d3.map(data, d => d.period)
43 75  
44   - // console.log(data)
45   -
46 76 var stack = d3.stack()
47 77 .keys(services)
48 78 // .order(d3.stackOrderNone)
... ... @@ -112,7 +142,10 @@ function build_chart(div_selector, data_url) {
112 142 // .attr("width", 5)
113 143 .attr("y", d => y(d[1]))
114 144 .attr("height", d => height - y(d[1] - d[0]))
115   - .attr("stroke", "black");
  145 + .attr("stroke", "black")
  146 + .on("mouseover", mouseover)
  147 + .on("mousemove", mousemove)
  148 + .on("mouseleave", mouseleave);
116 149  
117 150 });
118 151  
... ...