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

Add overridable filter function, isNodeExcluded #28

Merged
merged 1 commit 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
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ function flameGraph (opts) {

var clickHandler = (opts.clickHandler === undefined) ? defaultClickHandler : opts.clickHandler || function (target) { return target || nodes ? nodes[0] : null }

var isNodeExcluded = (opts.isNodeExcluded === undefined) ? defaultIsNodeExcluded : opts.isNodeExcluded || function () { return false }

onresize()

function onresize () {
Expand Down Expand Up @@ -181,7 +183,7 @@ function flameGraph (opts) {
if (data.children && (data.children.length > 0)) {
data.children.forEach(filter)
data.children.forEach(function (child) {
if (~filterTypes.indexOf(child.data.type)) {
if (isNodeExcluded(child, filterTypes)) {
child.data.hide = true
} else {
child.data.hide = false
Expand Down Expand Up @@ -957,7 +959,10 @@ function flameGraph (opts) {
return chart
}

// This function can be overridden by passing a function to opts.colorHash
function defaultIsNodeExcluded (node, filterTypes) {
return ~filterTypes.indexOf(node.data.type)
}

function defaultColorHash (d, perc, allSamples, tiers) {
if (!d.name) {
return perc ? 'rgb(127, 127, 127)' : 'rgba(0, 0, 0, 0)'
Expand Down
9 changes: 8 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ Pass in options as an object, including optional overrides for the following bui
- `colorHash` - Applying calculated colours using a built-in orange-to-red scale based on time spent at stack top
- `renderTooltip` - Creating and updating a tooltip giving basic frame information
- `renderLabel` - Writing frame information on the frames themselves in Canvas
- `renderStackFrameBox` - Drawing the frame rectangles in Canvas, including redrawing on hover
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this spurious?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so, it's one of the functions that can be passed in. It was added before this PR, but it should be included in the list. Maybe I should have included it in a separate commit for clarity.

Looking back, it looks like I missed this section of the readme in my last PR ( #24 6.10.0)

- `clickHandler` - Zooming in on nodes when clicked on, or zooming out when clicking outside any node
- `isNodeExcluded` - Checking if a frame should be hidden based on its node data and the current exclusion filters

```js
require('d3-flamegraph')({
Expand All @@ -66,6 +68,7 @@ require('d3-flamegraph')({
element, // Existing DOM reference. Do not pass a d3 selection, use d3.select(...).node()

// Optional:
exclude, // Iterable or Array, containing type strings used to filter and hide frames
timing, // Boolean, if passed as true logs times to console
categorizer: function (data, index, children) { // Function that determines the category for a given
// stack frame. e.g. "app", "core"
Expand Down Expand Up @@ -130,11 +133,15 @@ require('d3-flamegraph')({
state // Number, see STATE_HOVER, STATE_UNHOVER and STATE_IDLE above
} = locals
rect // Object, numeric { x, y, width, height } values for this frame's rectangle
}
},
clickHandler: function (target) { // Responds to clicks on the canvas, before calling dispatch
target // Null or Object, a d3-fg node representing the frame clicked on
this // The DOM object (in this case, the Canvas)
return // Returns target or all-stacks frame
},
isNodeExcluded: function (node, filterTypes) { // Used in filtering to set nodes' .hide property
node // Object, a d3-fg node representing the frame being filtered
filterTypes // Array, based on `exclude` if it is passed as an option
}
})
```
Expand Down