drag n drop utility for tree (nodes and connector) ,need to have handle of nodes and connector to generate eventson them. - angular JS - angularjs

drag n drop utility: need to develop a tree structures with nodes and connectors. Nodes and connectors are to be manually drawn using the tool bar(manually created). On the nodes and connectors need to generate events.Using angular js. Please provide sample code.
Once clicked on the nodes the node gets created in one division and the connector can be used graphically to connect between nodes.

I have achieved this with d3 library which seems to be very useful.
Below is answer:
<div id="drawArea" class="division" ></div>
<script type="text/javascript">
// Create a svg canvas
var svg = d3.select("#drawArea")
.append("svg")
.attr("width", 700)
.attr("height", 500);
//Drag nodes
var drag = d3.behavior.drag()
.on("dragstart", function() {
d3.event.sourceEvent.stopPropagation()
})
.on("drag", dragmove);
//First circle
var g1 = svg.append("g")
.attr("transform", "translate(" + 150 + "," + 100 + ")")
.attr("class", "first")
.call(drag)
.append("circle").attr({
r: 20,
})
.style("fill", "#FFFF00")
//Second circle
var g2 = svg.append("g")
.attr("transform", "translate(" + 250 + "," + 300 + ")")
.attr("class", "second")
.call(drag)
.append("circle").attr({
r: 20,
})
.style("fill", "#00FF00")
svg.on('dblclick', function() {
var coords = d3.mouse(this);
console.log(coords);
drawCircle(coords[0], coords[1]);
});
function drawCircle(x, y) {
var g2 = svg.append("g")
.attr("transform", "translate(" + x + "," + y + ")")
.attr("class", "third")
.call(drag)
.append("circle").attr({
r: 20,
})
.style("fill", "#00F");
}
//Drag handler
function dragmove(d) {
var x = d3.event.x;
var y = d3.event.y;
d3.select(this).attr("transform", "translate(" + x + "," + y + ")");
if(d3.select(this).attr("class") == "first") {
// line.attr("x1", x);
// line.attr("y1", y);
d3.select(this).attr("cx", x);
d3.select(this).attr("cy", y);
} else {
d3.select(this).attr("cx", x);
d3.select(this).attr("cy", y);
//line.attr("x2", x);
//line.attr("y2", y);
}
}
</script>

Related

x-axis horizontal line and y-axis labels are not displaying in D3 Grouped bar chart

I am creating grouped bar chart using D3 V5 in react.I am able to display y axis but not ticks and text.but in case of x-axis it's completely invisible. i have added d3.min.js to index.html file, but nothing works. any help is appreciated
here I am attaching my code
DrawChart = (data) => {
var w = 450, h = 300, p = 100;
var x0 = d3.scaleBand().range([0, w]).padding(0.4);
var x1 = d3.scaleBand();
var y = d3.scaleLinear().range([h, 0]);
var color = d3.scaleOrdinal().range(["#a85db3", "#95f578"]);
const svg = d3.select("div#predicative")
.append("svg").attr("width", w).attr("height", h)
.attr("padding", p).style("margin-left", 30)
.style("margin-top", 20).style("margin-bottom", 10);
var ageNames = d3.keys(data[0]).filter(function (key) { return key !== "dept"; });
data.forEach(function (d) {
d.ages = ageNames.map(function (name) { return { name: name, value: +d[name] }; });
});
x0.domain(data.map(function (d) { return d.dept; }));
x1.domain(ageNames).rangeRound([0, x0.bandwidth()]);
y.domain([0, (d3.max(data, function (d) { return d3.max(d.ages, function (d) { return d.value; }); })) + 10]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(d3.axisBottom(x0)
.tickSize(-w, 0, 0)
.tickFormat(''));
svg.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Time");
svg:not(:root){
overflow: visible;
}

How to create a bar chart with custom string labels with d3 in angular?

I am using d3 in angular to create a bar chart of feelings from very bad (1) to very good (5) with the feelings as labels on the yAxis. I am running into an error: Argument of type '(d: any, i: any) => any' is not assignable to parameter of type 'string'. I've been able to use "any" to get around similar type errors, but it isn't working in this part: .tickFormat(function(d:any,i:any): any { return tickLabels[i] }) ;
interface Datum {
created_at: string,
decription: string,
feeling: string,
feeling_attachments: any,
feeling_in_number: number,
id: number,
tag_user_ids: string,
tags: any,
visibility: string
}
buildChart2(feels: Array<Datum>){
var feelsData = feels.reverse()
var margin = {top: 20, right: 30, bottom: 30, left: 40},
width = 800 - margin.left - margin.right,
height = 250 - margin.top - margin.bottom;
var ticks = [0,1,2,3,4,5];
var tickLabels = ['','very bad','bad','neutral','good','very good']
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], 0);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickValues(ticks)
.tickFormat(function(d:any,i:any): any { return tickLabels[i] }) ;
var chart = d3.select(".feelsChart")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
y.domain([0, d3.max(feelsData, function(d: any): any { return d.feeling_in_number; })]);
var barWidth = width / feelsData.length;
var bar = chart.selectAll("g")
.data(feelsData)
.enter().append("g")
.attr("transform", function(d, i) { return "translate(" + i * barWidth + ",0)"; });
bar.append("rect")
.attr("y", function(d) { return y(d.feeling_in_number); })
.attr("height", function(d) { return height - y(d.feeling_in_number); })
.attr("width", barWidth - 1);
bar.append("text")
.attr("x", barWidth / 2)
.attr("y", function(d) { return y(d.feeling_in_number) + 3; })
.attr("dy", ".75em");
// .text(function(d) { return d.feeling_in_number; });
chart.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency");
chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
}
I've been trying to work off Mike Bostock's Let's Make A Bar Chart tutorial and a few other stack overflow questions about d3 in angular.
D3.JS change text in axis ticks to custom strings
It looks like you are trying to follow some guides that uses javascript with the older style of declaring functions. Typescript uses the newer ES6 syntax with arrow functions. So the function should be written like this in typescript:
.tickFormat((d:any, i:any): any => return tickLabels[i]);
You can read more about functions in typescript here

How to create dynamic line chart with d3 with data from websocket?

I'm trying d3 for the very first time and I'm trying to understand how to create a dynamic d3 line chart that needs to get updated every time I receive a websocket message from the server with new a new data point.
I have the below code within my angular directive link function:
var data = [];
var margin = { top: 30, right: 20, bottom: 30, left: 50 },
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis().scale(x)
.orient("bottom");
var yAxis = d3.svg.axis().scale(y)
.orient("left");
var valueline = d3.svg.line()
.x(function (d) { return x(d['label']); })
.y(function (d) { return y(d['value']); });
var svg = d3.select(elem[0])
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("path")
.attr("class", "line")
.attr("d", valueline(data));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
Statistics.listen('topic', function(message) {
data.push({
label: message.webEvent.creationTimeStamp,
value: +message.webEvent.value
});
x.domain(d3.extent(data, function (d) { return d.label; }));
y.domain([
d3.min(data, function(d) { return d.value; }),
d3.max(data, function(d) { return d.value; })
]);
var svg = d3.select(elem[0]).transition();
svg.select(".line") // change the line
.duration(0)
.attr("d", valueline(data));
svg.select(".x.axis") // change the x axis
.duration(0)
.call(xAxis);
svg.select(".y.axis") // change the y axis
.duration(0)
.call(yAxis);
}
I'm not sure what the purpose of the "duration" is. The messages do not come through the socket at regular intervals. So I'm not sure if I should set a static duration value which I assume refreshes the graph based on the number given. I want the graph to update as and when I get an update. The graph, over time, should look like a running sine wave.
Right now, the first data that come gets rendered. But the graph stays static after that even though I can see incoming messages on the websocket and the data array is growing.
What am I missing? Any help is greatly appreciated.

Elasticsearch- Nested Aggregations D3

I am trying to create D3 charts based on elasticsearch nested aggregation. While I can plot a chart based on the first aggregation, I am not sure why I cant plot a similar chart on the nested a subsequent aggregation. Can anyone suggest what I should do?
My code:
esClient.search({
index: 'vehicle',
body: {
query:{
match:{
_all:searchTerms
}
},
aggs: {
touchdowns: {
terms: {
field: "country",
size:5
},
aggs: {
corp: {
terms: {
field: "companyName",
size:5
}
}
},
}
},
sort: [sortObject],
from: resultsPage * 10,
}
}).then(function(es_return){
deferred.resolve(es_return);
//// Pie Chart first aggregation
var touchdowns = es_return.aggregations.touchdowns.buckets;
// d3 donut chart
var width = 600,
height = 300,
radius = Math.min(width, height) / 2;
var color = ['#ff7f0e', '#d62728', '#2ca02c', '#1f77b4'];
var arc = d3.svg.arc()
.outerRadius(radius - 60)
.innerRadius(120);
var pie = d3.layout.pie()
.sort(null)
.value(function (d) { return d.doc_count; });
var svg = d3.select("#donut-chart").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width/1.4 + "," + height/2 + ")");
var g = svg.selectAll(".arc")
.data(pie(touchdowns))
.enter()
.append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function (d, i) { return color[i]; });
g.append("text")
.attr("transform", function (d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.style("text-anchor", "middle")
.style("fill", "white")
.text(function (d) { return d.data.key; });
//// Pie Chart
var touchdowns = es_return.aggregations.touchdowns.buckets.corp.buckets;
// d3 donut chart
var width = 600,
height = 300,
radius = Math.min(width, height) / 2;
var color = ['#ff7f0e', '#d62728', '#2ca02c', '#1f77b4'];
var arc = d3.svg.arc()
.outerRadius(radius - 60)
.innerRadius(120);
var pie = d3.layout.pie()
.sort(null)
.value(function (d) { return d.doc_count; });
var svg = d3.select("#donut-chart").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width/1.4 + "," + height/2 + ")");
var g = svg.selectAll(".arc")
.data(pie(touchdowns))
.enter()
.append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function (d, i) { return color[i]; });
g.append("text")
.attr("transform", function (d) { return "translate(" + arc.centroid(d) + ")"; })
.attr("dy", ".35em")
.style("text-anchor", "middle")
.style("fill", "white")
.text(function (d) { return d.assignee.key; });
}, function(error){
deferred.reject(error);
});
return deferred.promise;};

Bar chart in D3.js with AngularJS

I have an error in d3 bar chart, when load on the web page
the error :
Error: Invalid value for <rect> attribute y="NaN" , Error: Invalid value for <rect> attribute height="NaN"
I tried to solve it by edit this code
nothing worked
var countriesData = data.countries;
var datac=[];
for (var key in countriesData) {
datac.push({key: key, value: countriesData[key]});
};
console.log(datac);
var width = 250;
var height = 250;
//console.log(data4);
//x and y Scales
var xScale = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var yScale = d3.scale.linear()
.range([height, 0]);
xScale.domain(datac.map(function(d) { return d.x; }));
yScale.domain([0, d3.max(datac, function(d) { return d.y; })]);
//x and y Axes
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
//.ticks();
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
//.ticks(function(d) { return d.x; });
//create svg container
var svg = d3.select("#barchart").select("svg").remove();
svg = d3.select("#barchart")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
//.transition().duration(2000)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//create bars
svg.selectAll(".bar")
.data(datac)
.enter()
.append("rect")
.attr("class", "bar")
.attr("x", function(d) { return xScale(d.x); })
.attr("width", xScale.rangeBand())
.attr("y", function(d) { return yScale(d.y); })
.attr("height", function(d) { return height - yScale(d.y); });
//drawing the x axis on svg
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
//drawing the y axis on svg
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Reviews Number");
Please help!
Hard to say, but I'm guessing its the definition of the xScale:
xScale.domain(datac.map(function(d) { return d.x; }));
probably should be something like:
xScale.domain(d3.extent(datac, fucntion (d) {return d.x}))

Resources