ng-click not working in directive - javascript

so I have this Angular application I'm working on.
I have a controller called visualization and a directive called force-layout.
In the HTML template of the directive I'm creating three buttons and I attach to them three corresponding functions that I define in the directive code:
<div class="panel smallest controls">
<span ng-click="centerNetwork()"><i class="fa fa-arrows-alt" aria-hidden="true"></i></span>
<span ng-click="zoomIn()"><i class="fa fa-search-plus" aria-hidden="true"></i></span>
<span ng-click="zoomOut()"><i class="fa fa-search-minus" aria-hidden="true"></i></span>
</div>
<force-layout ng-if=" config.viewMode == 'individual-force' || config.viewMode == 'individual-concentric' "></force-layout>
The function are defined in the directive like this:
scope.centerNetwork = function() {
console.log("Recenter");
var sourceNode = nodes.filter(function(d) { return (d.id == sourceId)})[0];
svg.transition().duration(750).call(zoom.transform, d3.zoomIdentity.translate(width/2-sourceNode.x, height/2-sourceNode.y));
}
var zoomfactor = 1;
scope.zoomIn = function() {
console.log("Zoom In")
svg.transition().duration(500).call(zoom.scaleBy, zoomfactor + .5);
}
scope.zoomOut = function() {
console.log("Zoom Out")
svg.transition().duration(500).call(zoom.scaleBy, zoomfactor - .25);
}
It does not trigger any error.
It was working before, not it is not and I cannot understand what is causing the problem, any help?
UPDATE: full directive code.
'use strict';
/**
* #ngdoc directive
* #name redesign2017App.directive:forceLayout
* #description
* # forceLayout
*/
angular.module('redesign2017App')
.directive('forceLayout', function() {
return {
template: '<svg width="100%" height="100%"></svg>',
restrict: 'E',
link: function postLink(scope, element, attrs) {
console.log('drawing network the first time');
// console.log(scope.data);
var svg = d3.select(element[0]).select('svg'),
width = +svg.node().getBoundingClientRect().width,
height = +svg.node().getBoundingClientRect().height,
nodes,
links,
degreeSize,
sourceId,
confidenceMin = scope.config.confidenceMin,
confidenceMax = scope.config.confidenceMax,
dateMin = scope.config.dateMin,
dateMax = scope.config.dateMax,
complexity = scope.config.networkComplexity;
var durationTransition = 500;
// A function to handle click toggling based on neighboring nodes.
function toggleClick(d, newLinks, selectedElement) {
// Some code for handling selections cutted out from here
}
svg.append('rect')
.attr('width', '100%')
.attr('height', '100%')
.attr('fill', 'transparent')
.on('click', function() {
// Clear selections on nodes and labels
d3.selectAll('.node, g.label').classed('selected', false);
// Restore nodes and links to normal opacity. (see toggleClick() below)
d3.selectAll('.link')
.classed('faded', false)
d3.selectAll('.node')
.classed('faded', false)
// Must select g.labels since it selects elements in other part of the interface
d3.selectAll('g.label')
.classed('hidden', function(d) {
return (d.distance < 2) ? false : true;
});
// reset group bar
d3.selectAll('.group').classed('active', false);
d3.selectAll('.group').classed('unactive', false);
// update selction and trigger event for other directives
scope.currentSelection = {};
scope.$apply(); // no need to trigger events, just apply
});
// HERE ARE THE FUNCTIONS I ASKED ABOUT
// Zooming function translates the size of the svg container.
function zoomed() {
container.attr("transform", "translate(" + d3.event.transform.x + ", " + d3.event.transform.y + ") scale(" + d3.event.transform.k + ")");
}
var zoom = d3.zoom();
// Call zoom for svg container.
svg.call(zoom.on('zoom', zoomed)); //.on("dblclick.zoom", null);
//Functions for zoom and recenter buttons
scope.centerNetwork = function() {
console.log("Recenter");
var sourceNode = nodes.filter(function(d) {
return (d.id == sourceId) })[0];
svg.transition().duration(750).call(zoom.transform, d3.zoomIdentity.translate(width / 2 - sourceNode.x, height / 2 - sourceNode.y));
// svg.transition().duration(750).call(zoom.transform, d3.zoomIdentity);
}
var zoomfactor = 1;
scope.zoomIn = function() {
console.log("Zoom In")
svg.transition().duration(500).call(zoom.scaleBy, zoomfactor + .5);
}
scope.zoomOut = function() {
console.log("Zoom Out")
svg.transition().duration(500).call(zoom.scaleBy, zoomfactor - .25);
}
// TILL HERE
var container = svg.append('g');
// Toggle for ego networks on click (below).
var toggle = 0;
var link = container.append("g")
.attr("class", "links")
.selectAll(".link");
var node = container.append("g")
.attr("class", "nodes")
.selectAll(".node");
var label = container.append("g")
.attr("class", "labels")
.selectAll(".label");
var loading = svg.append("text")
.attr("dy", "0.35em")
.attr("text-anchor", "middle")
.attr('x', width / 2)
.attr('y', height / 2)
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.text("Simulating. One moment pleaseā€¦");
var t0 = performance.now();
var json = scope.data;
// graph = json.data.attributes;
nodes = json.included;
links = [];
json.data.attributes.connections.forEach(function(c) { links.push(c.attributes) });
sourceId = json.data.attributes.primary_people;
// d3.select('.legend .size.min').text('j')
var simulation = d3.forceSimulation(nodes)
// .velocityDecay(.5)
.force("link", d3.forceLink(links).id(function(d) {
return d.id;
}))
.force("charge", d3.forceManyBody().strength(-75)) //.distanceMax([500]))
.force("center", d3.forceCenter(width / 2, height / 2))
.force("collide", d3.forceCollide().radius(function(d) {
if (d.id == sourceId) {
return 26;
} else {
return 13;
}
}))
// .force("x", d3.forceX())
// .force("y", d3.forceY())
.stop();
for (var i = 0, n = Math.ceil(Math.log(simulation.alphaMin()) / Math.log(1 - simulation.alphaDecay())); i < n; ++i) {
simulation.tick();
}
loading.remove();
var t1 = performance.now();
console.log("Graph took " + (t1 - t0) + " milliseconds to load.")
function positionCircle(nodelist, r) {
var angle = 360 / nodelist.length;
nodelist.forEach(function(n, i) {
n.fx = (Math.cos(angle * (i + 1)) * r) + (width / 2);
n.fy = (Math.sin(angle * (i + 1)) * r) + (height / 2);
});
}
function update(confidenceMin, confidenceMax, dateMin, dateMax, complexity, layout) {
// some code for visualizing a force layout cutted out from here
}
// Trigger update automatically when the directive code is executed entirely (e.g. at loading)
update(confidenceMin, confidenceMax, dateMin, dateMax, complexity, 'individual-force');
// update triggered from the controller
scope.$on('Update the force layout', function(event, args) {
console.log('ON: Update the force layout')
confidenceMin = scope.config.confidenceMin;
confidenceMax = scope.config.confidenceMax;
dateMin = scope.config.dateMin;
dateMax = scope.config.dateMax;
complexity = scope.config.networkComplexity;
update(confidenceMin, confidenceMax, dateMin, dateMax, complexity, args.layout);
});
}
};
});

So I think I found the problem.
The directive was invoked by the <force-layout> tag, which presented the ng-if='some-condition' directive.
For some reason, while the controller code is evaluated, that condition doesn't prove to be true, and the directive as a whole, meaning its HTML and its JS, is simply skipped: the ng-click is then obviously not able to attach the click event to a function that does not exists yet.
Replacing ng-if with ng-show solved the issue: apparently in this case the code is executed immediately and the directive exists hidden, with all its declared properties.

Related

How to fix "Maximum Call Stack Exceeded" without using a base case for the function that is causing it

I am adding a hover event to an element. This event triggers a function which inside contains a loop and it also calls itself again when the loop finishes, because I want the function to be running as long as there is a hover event.
I am getting the following error Maximum call stack size exceeded.
I know this error means that a function is calling itself without a base case, consequently creating an infinite loop.
I do not want to add a base case because I want the function to keep running as long as there is a hover event.
How can I fix this? Thanks!
function arrowAnimation(){
let array = ['0.1', '0.1', '1', '0.6'];
var arrowLoop = function (array) {
for (let i = 0; i < arrows.length; i++) {
arrows[i].style.opacity = array[i];
if (i === array.length - 1) {
let lastElement = array.pop();
array = [lastElement].concat(array);
arrowLoop(array);
}
}
}
arrowLoop(array);
}
function arrowAnimatiom() {
t = setInterval(arrowAnimation, 100);
}
function stopArrowAnimation() {
clearInterval(t);
}
var hover = function (d) {
arrowAnimatiom();
var barElementCoords = document.getElementById(d.scaleKeys).getBoundingClientRect();;
var div = document.getElementById('tooltip');
div.style.left = barElementCoords.x + 'px';
div.style.top = barElementCoords.y - 45 + 'px';
div.innerHTML = d.valuesExplanations;
div.style.width = d.valuesExplanations.length + 80 + 'px'
};
var hoverOut = function (d) {
stopArrowAnimation();
var div = document.getElementById('tooltip');
div.style.top = -150 + 'px';
div.style.left = -150 + 'px';
}
svg_.selectAll(".bar")
.data(chartData_)
.enter().append("rect")
.attr("class", "bar__")
.attr("id", function (d) { return d.scaleKeys })
.attr("x", function (d) { return x(d.scaleKeys); })
.attr("width", x.bandwidth())
.attr("y", function (d) { return y(d.scaleValues); })
.attr("height", function (d) { return height_ - y(d.scaleValues); })
.style('fill', function (d) { return setBarColor(d.scaleValues) })
.on("mouseover", hover)
.on('mouseout', hoverOut)
You could do something like
Promise.resolve().then(() => arrowLoop(array));
which will clear the call stack and start fresh.

D3- Drawing line on leaflet map through mouse events

I am trying to implement this example. but I am using d3 v4 and leaflet version 1. In the mouse move on the svg function, I am styling the lines but it throws me an error Uncaught TypeError: Cannot read property 'style' of null I could form a lasso but it is all black which means neither the circles are getting styled nor the lines. I don't know why is the line variable null? This is my code--
svgLayer = L.svg({clickable:true});
svgLayer.addTo(map)
// assigning SVG
svg = d3.select('#map').select('svg').attr("pointer-events", "auto");
pointsG = svg.select('g').attr('class', 'leaflet-zoom-hide');
map.dragging.disable();
map.touchZoom.disable();
map.doubleClickZoom.disable();
map.scrollWheelZoom.disable();
if (map.tap) map.tap.disable();
function project(ll) {
//console.log(ll);
var point = map.latLngToLayerPoint(ll.LatLng);
//console.log(point)
return point;
}
d3.queue()
.defer(d3.csv, 'dots.csv', function(row) {
return {LatLng: [+row['lat'], +row['lng']]};
})
.await(readyToDraw);
function readyToDraw(error,data){
//console.log(data);
var points = pointsG.selectAll(".points")
.data(data);
var pointsEnter = points.enter().append("circle")
.attr("class", "points")
.attr("r", 6)
.style("fill-opacity", 0.4)
.style("fill","black")
.attr("pointer-events","visible");
var lassoPoints = [];
var lassoClosed = false;
var dragging = false;
svg.on("click.lasso", function() {
if(dragging) return;
var p = d3.mouse(this);
//console.log(p)
var ll = map.containerPointToLatLng(L.point([p[0],p[1]]))
//console.log(ll)
if(lassoPoints.length) {
var fp = project(lassoPoints[0])
// console.log(lassoPoints[0])
var dist2 = (fp.x - p[0])*(fp.x - p[0]) + (fp.y - p[1])*(fp.y-p[1])
if(dist2 < 100) {
lassoClosed = true;
renderLasso();
pointsG.selectAll("line.lasso").remove();
return;
}
}
if(lassoClosed) {
/*
lassoClosed = false;
g.selectAll(".lasso").remove();
lassoPoints = [];
return render();
*/
return;
};
lassoPoints.push(ll);
updateLayers();
});
svg.on("mousemove", function() {
// we draw a guideline for where the next point would go.
var lastPoint = lassoPoints[lassoPoints.length-1];
var p = d3.mouse(this);
var ll = map.containerPointToLatLng(L.point([p[0],p[1]]));
//console.log(lastPoint)
var line = pointsG.selectAll("line.lasso").data([lastPoint])
//console.log(line)
line.enter().append("line").classed("lasso", true)
if(lassoPoints.length && !lassoClosed) {
//console.log(project(lastPoint))
line.attr('x1', project(lastPoint).x)
.attr('y1', project(lastPoint).y)
.attr('x2', project(ll).x)
.attr('y2', project(ll).y)
.style('stroke', "#111")
.style("stroke-dasharray", "5 5");
} else {
line.remove();
}
})
var path = d3.line()
.x(function(d) { return project(d).x})
.y(function(d) { return project(d).y})
function renderLasso() {
// render our lasso
//console.log(lassoPoints)
var lassoPath = pointsG.selectAll("path.lasso").data([lassoPoints])
lassoPath.enter().append("path").classed("lasso", true)
.on("click", function() {
if(lassoClosed) {
lassoClosed = false;
pointsG.selectAll(".lasso").remove();
lassoPoints = [];
d3.event.stopPropagation();
return updateLayers();
};
})
//console.log(lassoPath)
lassoPath.attr("d", function(d) {
var str = path(d)
if(lassoClosed) str += "Z"
return str;
})
.style('stroke', '#010')
.style('fill', "#010")
.style("fill-opacity", 0.1);
var drag = d3.drag()
.on("drag", function(d) {
if(!lassoClosed) return;
dragging = true;
var p = d3.mouse(svg.node())
var ll = map.containerPointToLatLng(L.point([p[0],p[1]]));
d.lat = ll.lat;
d.lng = ll.lng;
renderLasso();
}).on("end", function() {
setTimeout(function() {
dragging = false;
}, 100)
})
//console.log(lassoPoints)
var lasso = pointsG.selectAll("circle.lasso")
.data(lassoPoints)
lasso.enter().append("circle").classed("lasso", true)
.call(drag);
//console.log(lasso)
lasso.attr('cx', function(d) { return project(d).x;})
.attr('cy', function(d) { return project(d).y;})
.attr('r',8)
.style('stroke','#010')
.style('fill','#b7feb7')
.style("fill-opacity",0.9);
var projected = lassoPoints.map(function(d){
return project(d)
})
//console.log(projected)
pointsG.selectAll(".points").style('fill', function(d) {
//console.log(d);
var isInside = inside(project(d), projected);
//console.log(project(d), isInsid
//console.log(isInside)
if(isInside) {
return "#ff8eec";
} else {
return "#0082a3";
}
})
}
function updateLayers(){
pointsG.selectAll('.points')
.attr('cx', function(d){ return map.latLngToLayerPoint(d.LatLng).x})
.attr('cy', function(d){return map.latLngToLayerPoint(d.LatLng).y})
renderLasso();
};
map.on('zoomend', updateLayers);
updateLayers();
}
function inside(point, vs) {
var x = point.x, y = point.y;
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i].x, yi = vs[i].y;
var xj = vs[j].x, yj = vs[j].y;
var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
};
Without the line where the error is it is hard to give an explanation.
Better to put all the style stuff in the style tag.
You are inconsistent in the use of the bound data to the points
Here you use d
pointsG.selectAll(".points").style({
fill: function(d) {
var isInside = inside(project(d), projected);
//console.log(project(d), isInside)
if(isInside) {
return "#ff8eec";
} else {
return "#0082a3"
}
}
})
Here you use d.LatLng
function updateLayers(){
pointsG.selectAll('.points')
.attr('cx', function(d){ return map.latLngToLayerPoint(d.LatLng).x})
.attr('cy', function(d){ return map.latLngToLayerPoint(d.LatLng).y})
renderLasso();
};

D3 - Transition Arcs in Sunburst Chart

I have a sunburst chart made in D3. Each 'petal' represents a subset of data. When a user clicks on one of the 'petals', I would like it to transition, fanning out to only show that subset (see image):
I'm having trouble getting the code to properly transition.
On click, all 'petals' (besides the selected one) should disappear and the remain paths should animate along the circle (using attrTween, arcTween, and interpolate?). The primary value that would be changing is the angleSize (var angleSize = (2 * Math.PI) / theData.length;).
I've tried using this, this, this, and this as reference without much success. What's the best way to handle the animation?
Thanks for your time!
--> See Plunker Here. <--
Code is below:
var colors = {
'Rank1' : '#3FA548',
'Rank2' : '#00B09E',
'Rank3' : '#8971B3',
'Rank4' : '#DFC423',
'Rank5' : '#E74341'
};
var $container = $('.chart'),
m = 40,
width = $container.width() - m,
height = $container.height() - m,
r = Math.min(width, height) / 2;
var study = null;
var arc = d3.svg.arc();
d3.csv('text.csv', ready);
function ready(err, data) {
if (err) console.warn('Error', err);
var svg = d3.select('.chart')
.append('svg')
.attr({
'width' : (r + m) * 2,
'height' : (r + m) * 2,
'class' : 'container'
})
.append('g')
.attr('transform', 'translate(' + (width / 4) + ', ' + (height / 2) + ' )');
var slice = svg.selectAll('.slice');
function updateChart(study) {
if (study) {
var theData = data.filter(function(d) {
return d.study_name === study;
});
} else {
var theData = data;
}
slice = slice.data(theData);
slice.enter()
.append('g')
.attr('class', 'slice');
var angleSize = (2 * Math.PI) / theData.length;
var startRadArr = [],
endRadArr = [];
for ( var i = 0; i < data.length; i++ ) {
var startRadius = (width / 20),
endRadius = startRadius;
for ( var x = 0; x < 4; x++ ) {
startRadArr.push(startRadius);
if ( x == 0 ) {
endRadius += Number(data[i].group1_score) * (width / 500);
} else if ( x == 1 ) {
endRadius += Number(data[i].group2_score) * (width / 500);
} else if ( x == 2 ) {
endRadius += Number(data[i].group3_score) * (width / 500);
} else {
endRadius += Number(data[i].group4_score) * (width / 500);
}
endRadArr.push(endRadius);
startRadius = endRadius + 0.3;
}
}
var startRadGroup = [],
endRadGroup = [];
for (i = 0; i < startRadArr.length; i += 4) {
startRadGroup.push(startRadArr.slice(i, i + 4));
}
for (i = 0; i < endRadArr.length; i += 4) {
endRadGroup.push(endRadArr.slice(i, i + 4));
}
slice.selectAll('path')
.remove();
for ( var x = 0; x < 4; x++ ) {
slice.append('path')
.attr({
'class' : function(d, i) {
if ( x == 0 ) {
return d.group1_class;
} else if ( x == 1 ) {
return d.group2_class;
} else if ( x == 2 ) {
return d.group3_class;
} else {
return d.group4_class;
}
},
'company' : function(d, i) {
return d.brand_name;
},
'cat' : function(d, i) {
if ( x == 0 ) {
return 'Group1';
} else if ( x == 1 ) {
return 'Group2';
} else if ( x == 2 ) {
return 'Group3';
} else {
return 'Group4';
}
},
'study' : function(d, i) {
return d.study_name;
},
'companyid' : function(d, i) {
return d.brand_id;
},
'startradius' : function(d, i) {
return startRadGroup[i][x];
},
'endradius' : function(d, i) {
return endRadGroup[i][x];
},
'startangle' : function(d, i) {
return angleSize * i;
},
'endangle' : function(d, i) {
return angleSize * (i + 1);
}
})
.on('click', selectStudy);
}
slice.exit()
.remove();
slice.selectAll('path')
.attr({
'd' : function(d) {
return arc({
innerRadius : +d3.select(this)[0][0].attributes.startradius.nodeValue,
outerRadius : +d3.select(this)[0][0].attributes.endradius.nodeValue,
startAngle : +d3.select(this)[0][0].attributes.startangle.nodeValue,
endAngle : +d3.select(this)[0][0].attributes.endangle.nodeValue
})
}
});
}
function selectStudy(d) {
study = $(this).attr('study');
updateChart(study);
}
updateChart();
}
EDIT
Updated the code (based on this) to include a properly working enter, update, and exit pattern. Still unsure about the transition however. Most of the examples I've linked to use something similar to d3.interpolate(this._current, a);, tweening between differing data.
In this chart, this._current and a are the same, angleSize (var angleSize = (2 * Math.PI) / theData.length;), startAngle, and endAngle are the only thing that changes.
Your problem is that you are not really binding data to the elements, and therefore the transition is not possible. I mangled your code a little bit so the data contains all the nested information about the starting and ending angles, so that it can be bound to the paths inside each slice.
Take a look at this Plunker: https://plnkr.co/edit/a7cxRplzy66Pc1arM2a9?p=preview
Here's the listing of the modified version:
var colors = {
Rank1: '#3FA548',
Rank2: '#00B09E',
Rank3: '#8971B3',
Rank4: '#DFC423',
Rank5: '#E74341'
};
// Configuration
var $container = $('.chart'),
m = 40,
width = $container.width() - m,
height = $container.height() - m,
r = Math.min(width, height) / 2;
var study = null;
var arc = d3.svg.arc();
// Load data
d3.csv('text.csv', ready);
// Data loaded callback
function ready(err, data) {
if (err) console.warn('Error', err);
var svg = d3.select('.chart')
.append('svg')
.attr({
'width': (r + m) * 2,
'height': (r + m) * 2,
'class': 'container'
})
.append('g')
.attr('transform', 'translate(' + (width / 4) + ', ' + (height / 2) + ' )');
var slices = svg.selectAll('.slice');
function updateChart(study) {
var theData = data;
if (study) {
theData = data.filter(function (d) {
return d.study_name === study;
});
}
var angleSize = (2 * Math.PI) / theData.length;
theData.forEach(function (item, i) {
var startRadius = (width / 20),
endRadius = startRadius,
groupName;
item.paths = [];
for (var g = 0; g < 4; g++) {
item.paths[g] = {};
item.paths[g].startRadius = startRadius;
groupName = 'group' + (g + 1) + '_score';
endRadius += Number(item[groupName]) * (width / 500);
item.paths[g].endRadius = endRadius;
startRadius = endRadius + 0.3;
}
});
// Set the data
slices = slices.data(theData);
// Enter
slices.enter()
.append('g')
.attr('class', 'slice');
// Exit
slices.exit()
.remove();
// Update
slices
.transition()
.duration(750)
.each(function (dSlice, iSlice) {
var slice = d3.select(this);
var paths = slice.selectAll('path');
// Set data
paths = paths.data(dSlice.paths);
// Exit
paths.exit()
.remove();
// Enter
paths.enter()
.append('path')
.attr('class', 'path');
// Update
paths
.transition()
.attr({
'class': function (d, i) {
return dSlice['group' + (i + 1) + '_class'];
},
'company': dSlice.brand_name,
'cat': function (d, i) {
return 'Group' + (i + 1);
},
'study': function (d, i) {
return dSlice.study_name;
},
'companyid': function (d, i) {
return dSlice.brand_id;
},
'startradius': function (d, i) {
return d.startRadius;
},
'endradius': function (d, i) {
return d.endRadius;
},
'startangle': function (d, i) {
return angleSize * iSlice;
},
'endangle': function (d, i) {
return angleSize * (iSlice + 1);
},
'd': function (d) {
return arc({
innerRadius: +d.startRadius,
outerRadius: +d.endRadius,
startAngle: +angleSize * iSlice,
endAngle: +angleSize * (iSlice + 1)
})
}
})
.duration(750);
paths.on('click', selectStudy);
});
function selectStudy(d, i) {
study = $(this).attr('study');
updateChart(study);
}
}
updateChart();
}
As you can see, the key is correctly preparing the data (let's say the format in your example .tsv file is not the best choice, but sometimes we can't choose our data sources...)
Then afterwards, by putting the code for the paths generation inside the .each call on the slices, the data can be accessed from the function (d, i) { ... } callbacks and every element happens to receive the corresponding data.
Another trick is using the slices data (accessed inside the .each function via the dSlice and iSlice vars) on the paths' callbacks. This way the paths can consume this data for their own purposes. In this case, the company and study_name properties.
Now in order to tweak the transition and make it more accurate, the starting attributes can change. You can try by setting up some attributes for the paths in the .enter() phase.

d3 circle .on("click") event not firing

I understand it should be as simple as selecting the element you need and applying the call to it, but in my case, nothing is happening when I do so.
In my case, I need to re draw circles based on a zoom level of a map.
If the zoom level is < a certain number, use dataset A for the circles.
If the zoom level is > the number use dataset B to draw the circles.
I can draw the circles fine, and they do change on changing of the zoom level, but when I add an .on("click") event to these though, nothing happens.
Here is a Codepen link showing the lack of click event working CODEPEN LINK
Here is the code I am using, I have a feeling I am doing something wrong in the update() function, and the way I am using the .remove() function:
L.mapbox.accessToken = 'pk.eyJ1Ijoic3RlbmluamEiLCJhIjoiSjg5eTMtcyJ9.g_O2emQF6X9RV69ibEsaIw';
var map = L.mapbox.map('map', 'mapbox.streets')
.setView([53.4072, -2.9821], 14);
var data = {
"largeRadius": [{
"coords": [53.3942, -2.9785],
"name": "Jamaica Street"
}, {
"coords": [53.4073, -2.9824],
"name": "Hood Street"
}],
"smallRadius": [{
"coords": [53.4075, -2.9936],
"name": "Chapel Street"
}, {
"coords": [53.4073, -2.9824],
"name": "Hood Street"
}]
};
// Sort data for leaflet LatLng conversion
data.largeRadius.forEach(function(d) {
d.LatLng = new L.LatLng(d.coords[0], d.coords[1]);
});
data.smallRadius.forEach(function(d) {
d.LatLng = new L.LatLng(d.coords[0], d.coords[1]);
});
var svg = d3.select(map.getPanes().overlayPane).append("svg");
var g = svg.append("g").attr("class", "leaflet-zoom-hide");
var circles = g.selectAll("circle")
.data(data.smallRadius)
.enter().append("circle");
function update() {
circles.remove();
translateSVG();
var dataInstance;
var radius;
if (map.getZoom() < 17) {
dataInstance = data.largeRadius;
radius = 0.008;
} else {
dataInstance = data.smallRadius;
radius = 0.001;
}
dataInstance.forEach(function(d) {
d.LatLng = new L.LatLng(d.coords[0], d.coords[1]);
});
circles = g.selectAll("circle")
.data(dataInstance)
.enter().append("circle")
.attr("id", function(d) {
return d.name
})
.attr("cx", function(d) {
return map.latLngToLayerPoint(d.LatLng).x
})
.attr("cy", function(d) {
return map.latLngToLayerPoint(d.LatLng).y
})
.attr("r", function() {
return radius * Math.pow(2, map.getZoom())
});
}
function translateSVG() {
var width = window.innerWidth;
var height = window.innerHeight;
var regExp = /\(([^)]+)\)/;
var translateString = regExp.exec(document.querySelector(".leaflet-map-pane").attributes[1].nodeValue);
var translateX = parseInt(translateString[1].split(" ")[0]);
var translateY = parseInt(translateString[1].split(" ")[1]);
if (translateX < 0) {
translateX = Math.abs(translateX);
} else {
translateX = -translateX;
}
if (translateY < 0) {
translateY = Math.abs(translateY);
} else {
translateY = -translateY;
}
svg.attr("width", width);
svg.attr("height", height);
svg.attr("viewBox", function() {
return translateX + " " + translateY + " " + width + " " + height;
});
svg.attr("style", function() {
return "transform: translate3d(" + translateX + "px, " + translateY + "px, 0px);";
});
}
// THIS IS THE CLICK EVENT THAT DOES NOT WORK
circles.on("click", function () {
alert("clicked");
})
map.on("moveend", update);
update();
I'm not sure if this fixes your issue completely, mostly because I'm not sure I fully understand what you're trying to achieve, but if you move the 'click' code:
circles.on("click", function () {
alert("clicked");
});
Inside your update, then you'll rebind that after you've done the destroy and re-create, so your update function becomes this:
function update() {
circles.remove();
translateSVG();
var dataInstance;
var radius;
if (map.getZoom() < 17) {
dataInstance = data.largeRadius;
radius = 0.008;
} else {
dataInstance = data.smallRadius;
radius = 0.001;
}
dataInstance.forEach(function(d) {
d.LatLng = new L.LatLng(d.coords[0], d.coords[1]);
});
circles = g.selectAll("circle")
.data(dataInstance)
.enter().append("circle")
.attr("id", function(d) {
return d.name
})
.attr("cx", function(d) {
return map.latLngToLayerPoint(d.LatLng).x
})
.attr("cy", function(d) {
return map.latLngToLayerPoint(d.LatLng).y
})
.attr("r", function() {
return radius * Math.pow(2, map.getZoom())
});
circles.on("click", function () {
alert("clicked");
});
}
and you then remove the circles.on("click") part from the bottom. It may also be worth making sure you're releasing that bind each time, i'm unsure if it'll be overwriting the memory or adding to it each update.
Here's a fork of yours where it seems to be working as I imagine it: http://codepen.io/anon/pen/waqJqB?editors=101

Why do some cells not move entirely

I have set up this jsfiddle : http://jsfiddle.net/386er/dhzq6q6f/14/
var moveCell = function(direction) {
var cellToBeMoved = pickRandomCell();
var currentX = cellToBeMoved.x.baseVal.value;
var currentY = cellToBeMoved.y.baseVal.value;
var change = getPlusOrMinus() * (cellSize + 1 );
var newX = currentX + change;
var newY = currentY + change;
var selectedCell = d3.select(cellToBeMoved);
if (direction === 'x') {
selectedCell.transition().duration(1500)
.attr('x', newX );
} else {
selectedCell.transition().duration(1500)
.attr('y', newY );
}
}
In the moveCell function, I pick a random cell, request its current x and y coordinates and then add or subtract its width or height, to move it to an adjacent cell.
What I am wondering about: If you watch the cells move, some will only move partially to the next cell. Can anoyne tell me, why this is so ?
The first thing to do in this situation is put .each("interrupt", function() { console.log("interrupted!") }); on your transitions. Then you will see the problem.
Its supposed to fix it if you name the transitions like selection.transition("name"), but that doesn't fix it.
That means you have to do as suggested by #jcuenod and exclude the ones that are moving. One way to do that which is idiomatic is like this...
if (direction === 'x') {
selectedCell.transition("x").duration(1500)
.attr('x', newX)
.each("start", function () { lock.call(this, "lockedX") })
.each("end", function () { unlock.call(this, "lockedX") });
} else {
selectedCell.transition("y").duration(1500)
.attr('y', newY)
.each("start", function () { lock.call(this, "lockedX") })
.each("end", function () { unlock.call(this, "lockedX") });
}
function lock(lockClass) {
var c = { cell: false }; c[lockClass] = true;
d3.select(this).classed(c)
};
function unlock(lockClass) {
var c = { cell: this.classList.length == 1 }; c[lockClass] = false;
d3.select(this).classed(c);
};
Here is a fiddle to prove the concept.
Pure and idiomatic d3 version
Just for completeness here is the d3 way to do it.
I've tried to make it as idiomatic as possible. The main points being...
Purely data-driven
The data is updated and the viz manipulation left entirely to d3 declarations.
Use d3 to detect and act on changes to svg element attributes
This is done by using a composite key function in the selection.data() method and by exploiting the fact that changed nodes (squares where the x, y or fillattributes don't match the updated data) are captured by the exit selection.
Splice changed elements into the data array so d3 can detect changes
Since a reference to the data array elements is bound to the DOM elements, any change to the data will also be reflected in the selection.datum(). d3 uses a key function to compare the data values to the datum, in order to classify nodes as update, enter or exit. If a key is made, that is a function of the data/datum values, changes to data will not be detected. By splice-ing changes into the data array, the value referenced by selection.datum() will be different from the data array, so data changes will flag exit nodes.
By simply manipulating attributes and putting transitions on the exit selection and not removing it, it essentially becomes a 'changed' selection.
this only works if the data values are objects.
Concurrent transitions
Named transitions are used to ensure x and y transitions don't interrupt each other, but it was also necessary to use tag class attributes to lock out transitioning elements. This is done using transition start and end events.
Animation frames
d3.timer is used to smooth animation and marshal resources. d3Timer calls back to update the data before the transitions are updated, before each animation frame.
Use d3.scale.ordinal() to manage positioning
This is great because you it works every time and you don't even have to thin about it.
$(function () {
var container,
svg,
gridHeight = 800,
gridWidth = 1600,
cellSize, cellPitch,
cellsColumns = 100,
cellsRows = 50,
squares,
container = d3.select('.svg-container'),
svg = container.append('svg')
.attr('width', gridWidth)
.attr('height', gridHeight)
.style({ 'background-color': 'black', opacity: 1 }),
createRandomRGB = function () {
var red = Math.floor((Math.random() * 256)).toString(),
green = Math.floor((Math.random() * 256)).toString(),
blue = Math.floor((Math.random() * 256)).toString(),
rgb = 'rgb(' + red + ',' + green + ',' + blue + ')';
return rgb;
},
createGrid = function (width, height) {
var scaleHorizontal = d3.scale.ordinal()
.domain(d3.range(cellsColumns))
.rangeBands([0, width], 1 / 15),
rangeHorizontal = scaleHorizontal.range(),
scaleVertical = d3.scale.ordinal()
.domain(d3.range(cellsRows))
.rangeBands([0, height]),
rangeVertical = scaleVertical.range(),
squares = [];
rangeHorizontal.forEach(function (dh, i) {
rangeVertical.forEach(function (dv, j) {
var indx;
squares[indx = i + j * cellsColumns] = { x: dh, y: dv, c: createRandomRGB(), indx: indx }
})
});
cellSize = scaleHorizontal.rangeBand();
cellPitch = {
x: rangeHorizontal[1] - rangeHorizontal[0],
y: rangeVertical[1] - rangeVertical[0]
}
svg.selectAll("rect").data(squares, function (d, i) { return d.indx })
.enter().append('rect')
.attr('class', 'cell')
.attr('width', cellSize)
.attr('height', cellSize)
.attr('x', function (d) { return d.x })
.attr('y', function (d) { return d.y })
.style('fill', function (d) { return d.c });
return squares;
},
choseRandom = function (options) {
options = options || [true, false];
var max = options.length;
return options[Math.floor(Math.random() * (max))];
},
pickRandomCell = function (cells) {
var l = cells.size(),
r = Math.floor(Math.random() * l);
return l ? d3.select(cells[0][r]).datum().indx : -1;
};
function lock(lockClass) {
var c = { cell: false }; c[lockClass] = true;
d3.select(this).classed(c)
};
function unlock(lockClass) {
var c = { cell: this.classList.length == 1 }; c[lockClass] = false;
d3.select(this).classed(c);
};
function permutateColours() {
var samples = Math.min(50, Math.max(~~(squares.length / 50),1)), s, ii = [], i, k = 0,
cells = d3.selectAll('.cell');
while (samples--) {
do i = pickRandomCell(cells); while (ii.indexOf(i) > -1 && k++ < 5 && i > -1);
if (k < 10 && i > -1) {
ii.push(i);
s = squares[i];
squares.splice(i, 1, { x: s.x, y: s.y, c: createRandomRGB(), indx: s.indx });
}
}
}
function permutatePositions() {
var samples = Math.min(20, Math.max(~~(squares.length / 100),1)), s, ss = [], d, m, p, k = 0,
cells = d3.selectAll('.cell');
while (samples--) {
do s = pickRandomCell(cells); while (ss.indexOf(s) > -1 && k++ < 5 && s > -1);
if (k < 10 && s > -1) {
ss.push(s);
d = squares[s];
m = { x: d.x, y: d.y, c: d.c, indx: d.indx };
m[p = choseRandom(["x", "y"])] = m[p] + choseRandom([-1, 1]) * cellPitch[p];
squares.splice(s, 1, m);
}
}
}
function updateSquares() {
//use a composite key function to transform the exit selection into
// an attribute update selection
//because it's the exit selection, d3 doesn't bind the new data
// that's done manually with the .each
var changes = svg.selectAll("rect")
.data(squares, function (d, i) { return d.indx + "_" + d.x + "_" + d.y + "_" + d.c; })
.exit().each(function (d, i, j) { d3.select(this).datum(squares[i]) })
changes.transition("x").duration(1500)
.attr('x', function (d) { return d.x })
.each("start", function () { lock.call(this, "lockedX") })
.each("end", function () { unlock.call(this, "lockedX") })
changes.transition("y").duration(1500)
.attr('y', function (d) { return d.y })
.each("start", function () { lock.call(this, "lockedY") })
.each("end", function () { unlock.call(this, "lockedY") });
changes.attr("stroke", "white")
.style("stroke-opacity", 0.6)
.transition("fill").duration(800)
.style('fill', function (d, i) { return d.c })
.style("stroke-opacity", 0)
.each("start", function () { lock.call(this, "lockedFill") })
.each("end", function () { unlock.call(this, "lockedFill") });
}
squares = createGrid(gridWidth, gridHeight);
d3.timer(function tick() {
permutateColours();
permutatePositions();
updateSquares();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<div class="svg-container"></div>
NOTE: requires d3 version 3.5.5 for the position transitions to run.
EDIT: fixed a problem with lock and un-lock. Would probably better to tag the data rather than write classes to the DOM but, anyway... this way is fun.

Categories