Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tooltips fix #98

Merged
merged 3 commits into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion packages/core/src/bar-chart.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// D3 Imports
import { select } from "d3-selection";
import { select, mouse } from "d3-selection";
import { scaleBand } from "d3-scale";
import { min } from "d3-array";

Expand Down Expand Up @@ -245,4 +245,39 @@ export class BarChart extends BaseAxisChart {

super.resizeChart();
}

addDataPointEventListener() {
const self = this;
const { accessibility } = this.options;

this.svg.selectAll("rect")
.on("click", function(d) {
self.dispatchEvent("bar-onClick", d);
})
.on("mouseover", function(d) {
select(this)
.attr("stroke-width", Configuration.bars.mouseover.strokeWidth)
.attr("stroke", self.colorScale[d.datasetLabel](d.label))
.attr("stroke-opacity", Configuration.bars.mouseover.strokeOpacity);

self.showTooltip(d, this);
self.reduceOpacity(this);
})
.on("mousemove", function(d) {
const tooltipRef = select(self.holder).select("div.chart-tooltip");

const relativeMousePosition = mouse(self.holder as HTMLElement);
tooltipRef.style("left", relativeMousePosition[0] + Configuration.tooltip.magicLeft2 + "px")
.style("top", relativeMousePosition[1] + "px");
})
.on("mouseout", function(d) {
const { strokeWidth, strokeWidthAccessible } = Configuration.bars.mouseout;
select(this)
.attr("stroke-width", accessibility ? strokeWidthAccessible : strokeWidth)
.attr("stroke", accessibility ? self.colorScale[d.datasetLabel](d.label) : "none")
.attr("stroke-opacity", Configuration.bars.mouseout.strokeOpacity);

self.hideTooltip();
});
}
}
56 changes: 2 additions & 54 deletions packages/core/src/base-axis-chart.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// D3 Imports
import {
mouse,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great attention to detail 💯

select
} from "d3-selection";
import { select } from "d3-selection";
import { scaleBand, scaleLinear } from "d3-scale";
import { axisBottom, axisLeft, axisRight } from "d3-axis";
import { min, max } from "d3-array";
Expand Down Expand Up @@ -594,56 +591,7 @@ export class BaseAxisChart extends BaseChart {
/**************************************
* Events & User interactions *
*************************************/

addDataPointEventListener() {
const self = this;
const { accessibility } = this.options;

this.svg.selectAll("rect")
.on("click", function(d) {
self.dispatchEvent("bar-onClick", d);
})
.on("mouseover", function(d) {
select(this)
.attr("stroke-width", Configuration.bars.mouseover.strokeWidth)
.attr("stroke", self.colorScale[d.datasetLabel](d.label))
.attr("stroke-opacity", Configuration.bars.mouseover.strokeOpacity);

self.showTooltip(d, this);
self.reduceOpacity(this);
})
.on("mousemove", function(d) {
const tooltipRef = select(self.holder).select("div.chart-tooltip");

const relativeMousePosition = mouse(self.holder as HTMLElement);
tooltipRef.style("left", relativeMousePosition[0] + Configuration.tooltip.magicLeft2 + "px")
.style("top", relativeMousePosition[1] + "px");
})
.on("mouseout", function(d) {
const { strokeWidth, strokeWidthAccessible } = Configuration.bars.mouseout;
select(this)
.attr("stroke-width", accessibility ? strokeWidthAccessible : strokeWidth)
.attr("stroke", accessibility ? self.colorScale[d.datasetLabel](d.label) : "none")
.attr("stroke-opacity", Configuration.bars.mouseout.strokeOpacity);

self.hideTooltip();
});

this.svg.selectAll("circle.dot")
.on("mouseover", function(d) {
select(this)
.attr("stroke", self.colorScale[d.datasetLabel](d.label))
.attr("stroke-opacity", Configuration.lines.points.mouseover.strokeOpacity);

self.showTooltip(d, this);
self.reduceOpacity(this);
})
.on("mouseout", function(d) {
select(this)
.attr("stroke", self.colorScale[d.datasetLabel](d.label))
.attr("stroke-opacity", Configuration.lines.points.mouseout.strokeOpacity);

self.hideTooltip();
});
console.warn("You should implement your own `addDataPointEventListener()` function.");
}
}