I am a newbee in javascript, i followed an example to create a pie chart. I would like it to update with changes in select list. While the pie works, the legend list simply grows.
I tried exit().remove() and selectAll("g").remove() and I saw in debug that "g" elements have been removed but the variable legend keeps growing in each update instead of removing the old data. Can anyone figure out why the code works with the pie while the legend won't work?
Many thanks!
Here is my code
var app=angular.module("app",[]);
// controller gets average json data and reform by host, produces executions
app.controller("pieCtrl",function($scope,$http){
$http.get("average.json").success(function(data){
//executions- key:hostName, value:[listExecutions]
var executions={};
data.forEach(function(d){
if(!(d.hostName in executions)){
executions[d.hostName]={
hostName:d.hostName,
listExecutions:[]
}
}
executions[d.hostName].listExecutions.push(d);
});
//listHostnames for <select> [["host1"],["host2"]]
$scope.ListHostnames=[];
Object.keys(executions).forEach(function(hostName) {
$scope.ListHostnames.push(executions[hostName]);
});
//default value, bound to <select>
$scope.selectedHostname = $scope.ListHostnames[2];
}).error(function(err){
throw err;
});
});
app.directive("pie",function() {
function link(scope,element,attr) {
var wpie = 300;
var hpie = 300;
var outerRadius = wpie / 2;
var innerRadius = outerRadius*0.5;
var color = d3.scale.category10();
var legendRectSize = 18;
var legendSpacing = 4;
scope.$watch('data', update);
var pie = d3.layout.pie()
.value(function(d) {
return d.executionNum;
})
.sort(null);//prevent values from being sorted;
var arc = d3.svg.arc()
.outerRadius(outerRadius)
.innerRadius(innerRadius);
//svg must be outside update(), otherwise multiple charts will be created instead of updating one chart
var svg = d3.select("#chart").append("svg")
.attr("width", wpie*2)
.attr("height", hpie*1.5)
.append("g")
.attr("transform", "translate(" + wpie / 2 + "," + hpie / 2 + ")");
//tooltip
var tooltip = d3.select('pie')
.append('div')
.attr('class', 'tooltip');
tooltip.append('div')
.attr('class', 'date');
tooltip.append('div')
.attr('class', 'num');
function update(){
//clear what is left
svg.selectAll('path').remove();
//input data
if (!scope.$parent.selectedHostname){ return }
var data = scope.$parent.selectedHostname.listExecutions;
//bind data
var arcs = svg.selectAll("path")
.data(pie(data));
//append path for shapes
arcs.enter().append("path")
.attr("d", arc)
.attr("fill", function(d) {
return color((d.data.day));
})
.each(function(d){this._current=d;});// animation
//mouseover tooltip to show contents
arcs.on('mouseover', function(d) {
var num=(d.enabled)? d.data.executionNum :0;
tooltip.select('.date').html('Date: '+d.data.day+' / '+d.data.month);
tooltip.select('.num').html('Number of executions: '+num);
tooltip.style('display', 'block');
});
arcs.on('mouseout', function() {
tooltip.style('display', 'none');
});
//add labels
arcs.enter().append("text")
.attr("transform", function(d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function(d) {
return d.data.executionNum;
})
.attr("fill","black");
svg.selectAll(".legend").remove();
svg.selectAll("g").remove();
svg.selectAll("g.legend").remove();
var legend = svg.selectAll('.legend')
.data(color.domain());
legend.exit().remove();
legend.enter()
.append('g')
.attr('class', 'legend')
.attr('transform', function(d, i) {
var height = legendRectSize + legendSpacing;
var offset = height * color.domain().length / 2;
var horz = 10 * legendRectSize;
var vert = i * height - offset;
return 'translate(' + horz + ',' + vert + ')';
});
legend.append('circle')
.attr('cx', legendRectSize)
.attr('cy', legendRectSize)
.attr('r',8)
.style('fill', color)
.style('stroke', color)
.attr('class', 'legend');
legend.append('text')
.attr('x', legendRectSize + 4*legendSpacing)
.attr('y', legendRectSize +4)
.text(function(d) { return d; })
.attr('class', 'legend');
legend.exit().remove();
}
}
return{
link: link,
restrict: 'E',
scope: { data : '='}
};
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>dynamic pie</title>
</head>
<body ng-app="app" ng-controller="pieCtrl">
<h1> Dynamic pie chart </h1>
<div id="list">
<select ng-model="selectedHostname" ng-options="hostName as hostName.hostName for hostName in ListHostnames"></select>
</div>
<div id="chart">
<pie data="selectedHostname"></pie>
</div>
</body>
</html>
I'm using an angular controller to query the data and d3 code is in the pie directive. update function is called to refresh the chart. original data is a json file.
[{"durationSec":515,"month":2,"day":15.0,"executionNum":8,"hostName":"monitoring02"},{"durationSec":515,"month":2,"day":17.0,"executionNum":3,"hostName":"monitoring02"},{"durationSec":521,"month":2,"day":16.0,"executionNum":5,"hostName":"monitoring02"},{"durationSec":515,"month":2,"day":18.0,"executionNum":6,"hostName":"monitoring02"},{"durationSec":739,"month":2,"day":18.0,"executionNum":7,"hostName":"apple-pc"},{"durationSec":1140,"month":2,"day":19.0,"executionNum":7,"hostName":"apple-pc"},{"durationSec":1117,"month":2,"day":20.0,"executionNum":6,"hostName":"apple-pc"},{"durationSec":1125,"month":2,"day":21.0,"executionNum":3,"hostName":"monitoring01"},{"durationSec":1169,"month":2,"day":22.0,"executionNum":5,"hostName":"monitoring01"}]
Related
I created a copy of the csv file in my local folder because i wanted to mess around with the data a little bit. When i get rid of the link and replace it with the name of my local csv file, the graph doesnt render for some reason. I have added a picture that shows my local file structure. it is in the same folder. What am i doing wrong?
import React, {Component, useRef, useEffect} from 'react';
import * as d3 from "d3";
import { select } from 'd3-selection'
import { extent, max, min } from "d3-array";
class Linechart extends Component {
constructor(props){
super(props)
this.createBarChart = this.createBarChart.bind(this)
}
componentDidMount() {
this.createBarChart()
}
componentDidUpdate() {
this.createBarChart()
}
createBarChart() {
var margin = {top: 30, right: 30, bottom: 30, left: 60},
width = 960 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
var node = this.node
var divObj = select(node)
var svgObj = divObj
.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 + ")");
//Read the data
// when i replace the line below wit this line of code, it doesnt read it d3.csv("5_OneCatSevNumOrdered.csv""), function(data) {
d3.csv("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/5_OneCatSevNumOrdered.csv", function(data) {
// group the data: I want to draw one line per group
var sumstat = d3.nest() // nest function allows to group the calculation per level of a factor
.key(function(d) { return d.name;})
.entries(data);
// Define the div for the tooltip
var tooltip = divObj
.append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.text("I AM A TOOLTIP BOOM SHAKALAKA!! BOOM SHAKALAKA!!");
// Add X axis --> it is a date format
var x = d3.scaleLinear()
.domain(d3.extent(data, function(d) { return d.year; }))
.range([ 0, width ]);
svgObj.append("g")
.attr("transform", "translate(0," + height + ")")
.attr("stroke-width","0.3")
.call(d3.axisBottom(x).tickSize(-height).tickFormat('').ticks(5));
//ticks
svgObj.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).ticks(5));
// Add Y axis
var y = d3.scaleLinear()
.domain([0, d3.max(data, function(d) { return +d.n; })])
.range([ height, 0 ]);
svgObj.append("g")
.attr("stroke-width","0.3")
.call(d3.axisLeft(y).tickSize(-width).tickFormat('').ticks(5));
//ticks
svgObj.append("g")
.call(d3.axisLeft(y).ticks(5));
// color palette
var res = sumstat.map(function(d){ return d.key }) // list of group names
console.log(res)
var color = d3.scaleOrdinal()
.domain(res)
.range(['#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00','#ffff33','#a65628','#f781bf','#999999'])
// Draw the line
svgObj.selectAll(".line")
.data(sumstat)
.enter()
.append("path")
.attr("fill", "none")
.attr("stroke", function(d){ return color(d.key) })
.attr("stroke-width", 2.5)
.attr("d", function(d){
return d3.line()
.x(function(d) { return x(d.year); })
.y(function(d) { return y(+d.n); })
(d.values)
})
.on("mouseover", function(){return tooltip.style("visibility", "visible");})
.on("mousemove", function(){return tooltip.style("top", (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");})
.on("mouseout", function(){return tooltip.style("visibility", "hidden");})
})
}
render() {
return <div ref={node => this.node = node} className="example_div"> </div>
}
}
export default Linechart;
d3.csv is part of the d3-fetch library. That library uses the native fetch method to obtain a file from somewhere on the internet. It's unfortunately not able to deal with files on your hard drive.
You'll need to make the file available on localhost:<some port>, just like you're probably doing with your react code. Depending on what you use, you might need to change your webpack/gulp/rollup configuration. Otherwise, if you have a server-side API running with Python/C#/Ruby just serve it from there as a static file.
In any case, the file name/directory will never work, try serving it through localhost.
Edit: serving the file you put on Github
d3.csv("https://raw.githubusercontent.com/QamarFarooq/data-for-testing/main/5_OneCatSevNumOrdered.csv").then((data) => {
console.log(data);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
I notice that in much of the D3 documentation the charts, graphs, margins and so on are often hard coded. The xAxis is 500px, etc.. This isn't very helpful for me. So I'm trying to think of how I can accomplish a dynamic approach to rendering D3 content in React.
For example, in the following code I am simply rendering a line based on some time-series stock-price data. I have some D3 code in componentDidMount but given the way D3 works it wants concrete width and height values. But in componentDidMount I don't have those values yet. Lets say that this single line plot is one of 100 other plots each in a div in a grid layout.
So how can I get the width/height of the div/svg and only then compute my D3 code and render svgs?
componentDidMount() {
console.log("componentDidMount")
const data = this.props.data;
const selectX = this.props.selectX;
const selectY = this.props.selectY;
console.log(data)
const xScale = d3ScaleTime()
.domain(d3ArrayExtent(data, selectX))
.range([0, 1]);
const yScale = d3ScaleTime()
.domain(d3ArrayExtent(data, selectY))
.range([1, 0]);
const xAxis = d3AxisBottom()
.scale(xScale)
.ticks(data.length / 8);
const yAxis = d3AxisLeft()
.scale(yScale)
.ticks(3);
const selectScaledX = datum => xScale(selectX(datum));
const selectScaledY = datum => yScale(selectY(datum));
const sparkLine = d3Line()
.x(selectScaledX)
.y(selectScaledY);
const linePath = sparkLine(data);
console.log(linePath);
this.setState({
linePath: linePath
});
}
I tried to imitate your issue in the demo below.
class Chart extends React.Component {
componentDidMount() {
var data = this.props.data;
var containerDOMElementWidth = ReactDOM.findDOMNode(this).getBoundingClientRect().width
var chartHeight = containerDOMElementWidth / 2;
this.drawChart(data, containerDOMElementWidth, chartHeight);
}
drawChart(data, chartWidth, chartHeight) {
var margin = { top: 30, right: 20, bottom: 30, left: 50 };
var width = chartWidth - margin.left - margin.right;
var height = chartHeight - margin.top - margin.bottom;
var parseDate = d3.timeParse("%d-%b-%y");
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var xAxis = d3.axisBottom().scale(x)
.ticks(2);
var yAxis = d3.axisLeft().scale(y)
.ticks(2);
var valueline = d3.line()
.x(function (d) {
return x(d.date);
})
.y(function (d) {
return y(d.close);
});
var svg = d3.select("body")
.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 + ")");
data.forEach(function (d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
// Scale the range of the data
x.domain(d3.extent(data, function (d) {
return d.date;
}));
y.domain([0, d3.max(data, function (d) {
return d.close;
})]);
svg.append("path").attr('class', 'line-chart') // Add the valueline path.
.attr("d", valueline(data));
svg.append("g") // Add the X Axis
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g") // Add the Y Axis
.attr("class", "y axis")
.call(yAxis);
}
render() {
return <div></div>;
}
}
function getRandomData() {
return [{
date: "1-May-12",
close: Math.random() * 90
}, {
date: "30-Apr-12",
close: Math.random() * 90
}, {
date: "27-Apr-12",
close: Math.random() * 90
}, {
date: "26-Apr-12",
close: Math.random() * 90
}, {
date: "25-Apr-12",
close: Math.random() * 90
}];
}
ReactDOM.render(
<div className="charts-container">
<div className="chart-wrapper">
<Chart data={getRandomData()} />
</div>
<div className="chart-wrapper">
<Chart data={getRandomData()} />
</div>
</div>,
document.getElementById('container')
);
.line-chart {
fill: none;
stroke: blue
}
.charts-container {
display: flex;
}
.chart-wrapper {
width: 100%;
}
<script src="https://unpkg.com/react#16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom#16/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>
<div id="container">
</div>
Here we draw two charts one by one and calculate their width and height this way:
componentDidMount() {
var data = this.props.data;
// gets the width of container div element with ReactDOM.findDOMNode
var containerDOMElementWidth = ReactDOM.findDOMNode(this).getBoundingClientRect().width
// chart height have to be a half of width
var chartHeight = containerDOMElementWidth / 2;
// pass width and height as an arguments
this.drawChart(data, containerDOMElementWidth, chartHeight);
}
Our drawChart method look like:
drawChart(data, chartWidth, chartHeight) {
var margin = { top: 30, right: 20, bottom: 30, left: 50 };
var width = chartWidth - margin.left - margin.right;
var height = chartHeight - margin.top - margin.bottom;
... // code for the chart drawing
render:
ReactDOM.render(
<div className="charts-container">
<div className="chart-wrapper">
<Chart data={getRandomData()} />
</div>
<div className="chart-wrapper">
<Chart data={getRandomData()} />
</div>
</div>,
document.getElementById('container')
);
If we will render only one chart it also works fine without any code changing because of we calculate the width of the chart as width of parent div element:
class Chart extends React.Component {
componentDidMount() {
var data = this.props.data;
var containerDOMElementWidth = ReactDOM.findDOMNode(this).getBoundingClientRect().width
var chartHeight = containerDOMElementWidth / 2;
this.drawChart(data, containerDOMElementWidth, chartHeight);
}
drawChart(data, chartWidth, chartHeight) {
var margin = { top: 30, right: 20, bottom: 30, left: 50 };
var width = chartWidth - margin.left - margin.right;
var height = chartHeight - margin.top - margin.bottom;
var parseDate = d3.timeParse("%d-%b-%y");
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var xAxis = d3.axisBottom().scale(x)
.ticks(2);
var yAxis = d3.axisLeft().scale(y)
.ticks(2);
var valueline = d3.line()
.x(function (d) {
return x(d.date);
})
.y(function (d) {
return y(d.close);
});
var svg = d3.select("body")
.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 + ")");
data.forEach(function (d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
// Scale the range of the data
x.domain(d3.extent(data, function (d) {
return d.date;
}));
y.domain([0, d3.max(data, function (d) {
return d.close;
})]);
svg.append("path").attr('class', 'line-chart') // Add the valueline path.
.attr("d", valueline(data));
svg.append("g") // Add the X Axis
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g") // Add the Y Axis
.attr("class", "y axis")
.call(yAxis);
}
render() {
return <div></div>;
}
}
function getRandomData() {
return [{
date: "1-May-12",
close: Math.random() * 90
}, {
date: "30-Apr-12",
close: Math.random() * 90
}, {
date: "27-Apr-12",
close: Math.random() * 90
}, {
date: "26-Apr-12",
close: Math.random() * 90
}, {
date: "25-Apr-12",
close: Math.random() * 90
}];
}
ReactDOM.render(
<div className="charts-container">
<div className="chart-wrapper">
<Chart data={getRandomData()} />
</div>
</div>,
document.getElementById('container')
);
.line-chart {
fill: none;
stroke: blue
}
.charts-container {
display: flex;
}
.chart-wrapper {
width: 100%;
}
<script src="https://unpkg.com/react#16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom#16/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>
<div id="container">
</div>
The same thing for three charts:
class Chart extends React.Component {
componentDidMount() {
var data = this.props.data;
var containerDOMElementWidth = ReactDOM.findDOMNode(this).getBoundingClientRect().width
var chartHeight = containerDOMElementWidth / 2;
this.drawChart(data, containerDOMElementWidth, chartHeight);
}
drawChart(data, chartWidth, chartHeight) {
var margin = { top: 30, right: 20, bottom: 30, left: 50 };
var width = chartWidth - margin.left - margin.right;
var height = chartHeight - margin.top - margin.bottom;
var parseDate = d3.timeParse("%d-%b-%y");
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var xAxis = d3.axisBottom().scale(x)
.ticks(2);
var yAxis = d3.axisLeft().scale(y)
.ticks(2);
var valueline = d3.line()
.x(function (d) {
return x(d.date);
})
.y(function (d) {
return y(d.close);
});
var svg = d3.select("body")
.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 + ")");
data.forEach(function (d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
// Scale the range of the data
x.domain(d3.extent(data, function (d) {
return d.date;
}));
y.domain([0, d3.max(data, function (d) {
return d.close;
})]);
svg.append("path").attr('class', 'line-chart') // Add the valueline path.
.attr("d", valueline(data));
svg.append("g") // Add the X Axis
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g") // Add the Y Axis
.attr("class", "y axis")
.call(yAxis);
}
render() {
return <div></div>;
}
}
function getRandomData() {
return [{
date: "1-May-12",
close: Math.random() * 90
}, {
date: "30-Apr-12",
close: Math.random() * 90
}, {
date: "27-Apr-12",
close: Math.random() * 90
}, {
date: "26-Apr-12",
close: Math.random() * 90
}, {
date: "25-Apr-12",
close: Math.random() * 90
}];
}
ReactDOM.render(
<div className="charts-container">
<div className="chart-wrapper">
<Chart data={getRandomData()} />
</div>
<div className="chart-wrapper">
<Chart data={getRandomData()} />
</div>
<div className="chart-wrapper">
<Chart data={getRandomData()} />
</div>
</div>,
document.getElementById('container')
);
.line-chart {
fill: none;
stroke: blue
}
.charts-container {
display: flex;
}
.chart-wrapper {
width: 100%;
}
<script src="https://unpkg.com/react#16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom#16/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.11.0/d3.min.js"></script>
<div id="container">
</div>
I am new to d3.js, can anyone guide me about drawing zoomable sunburst.
Thanks in advance !
You might want to check out the D3 gallery of examples, specifically the zoomable sunburst:
http://bl.ocks.org/mbostock/4348373
Code from their example:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
stroke: #fff;
fill-rule: evenodd;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 700,
radius = Math.min(width, height) / 2;
var x = d3.scale.linear()
.range([0, 2 * Math.PI]);
var y = d3.scale.sqrt()
.range([0, radius]);
var color = d3.scale.category20c();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + (height / 2 + 10) + ")");
var partition = d3.layout.partition()
.value(function(d) { return d.size; });
var arc = d3.svg.arc()
.startAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x))); })
.endAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx))); })
.innerRadius(function(d) { return Math.max(0, y(d.y)); })
.outerRadius(function(d) { return Math.max(0, y(d.y + d.dy)); });
d3.json("/mbostock/raw/4063550/flare.json", function(error, root) {
if (error) throw error;
var path = svg.selectAll("path")
.data(partition.nodes(root))
.enter().append("path")
.attr("d", arc)
.style("fill", function(d) { return color((d.children ? d : d.parent).name); })
.on("click", click);
function click(d) {
path.transition()
.duration(750)
.attrTween("d", arcTween(d));
}
});
d3.select(self.frameElement).style("height", height + "px");
// Interpolate the scales!
function arcTween(d) {
var xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),
yd = d3.interpolate(y.domain(), [d.y, 1]),
yr = d3.interpolate(y.range(), [d.y ? 20 : 0, radius]);
return function(d, i) {
return i
? function(t) { return arc(d); }
: function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); return arc(d); };
};
}
</script>
I'm new to D3. Trying to show "outside" labels with lines, in D3, similar to Bostock's pie chart here http://bl.ocks.org/dbuezas/9306799 I can't get the labels or lines to show up like Bostock's pie. The pie is working well though! Any help with my code, mainly the directive, would be appreciated!
HTML:
<div ng-controller="myControl">
<d3-pie-dir data="d3Data"></d3-pie-dir>
</div>
CSS:
path.slice{
stroke-width:2px;
}
polyline{
opacity: .3;
stroke: black;
stroke-width: 2px;
fill: none;
}
Controller:
inAng.controller('myControl', function ( $scope, $http ) {
var stack = [];
$http.get("/getAPIData").
success(function (data, status, headers, config) {
// formatting for easier D3 consumption
for(var i in data)
stack.push(data [i]);
$scope.d3Data = stack;
// looks something like $scope.d3Data = [{ name: 'Bill', score: 25}, { name: 'Pete', score: 50}]
}).
error(function (data, status, headers, config) {
$scope.stack = 'Error!';
});
});
And the problem is in the directive:
inAng.directive('d3PieDir', function () {
return {
restrict: 'E',
scope: {
data: '='
},
link: function (scope, element, attrs) {
scope.$watch('data', function(values) {
if(values) {
console.log('values from directive: ', values);
var width = 960,
height = 500,
radius = Math.min(width, height) / 2;
var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(0);
var pie = d3.layout.pie()
.sort(null)
.value(function(d) {
return d.score;
});
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
values.forEach(function(d) {
d.score = +d.score;
});
var g = svg.selectAll(".arc")
.data(pie(values))
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function(d) { return color(d.data.name); });
// **** Below is where it stops working! ****
var key = function(d){ return d.data.name; };
/* ------- PIE SLICES -------*/
var slice = svg.select(".slices").selectAll("path.slice")
.data(pie(values), key);
slice.enter()
.insert("path")
.style("fill", function(d) { return color(d.data.name); })
.attr("class", "slice");
slice
.transition().duration(1000)
.attrTween("d", function(d) {
this._current = this._current || d;
var interpolate = d3.interpolate(this._current, d);
this._current = interpolate(0);
return function(t) {
return arc(interpolate(t));
};
})
slice.exit()
.remove();
/* ------- TEXT LABELS -------*/
var text = svg.select(".labels").selectAll("text")
.data(pie(values), key);
text.enter()
.append("text")
.attr("dy", ".35em")
.text(function(d) {
return d.data.name;
});
function midAngle(d){
return d.startAngle + (d.endAngle - d.startAngle)/2;
}
text.transition().duration(1000)
.attrTween("transform", function(d) {
this._current = this._current || d;
var interpolate = d3.interpolate(this._current, d);
this._current = interpolate(0);
return function(t) {
var d2 = interpolate(t);
var pos = outerArc.centroid(d2);
pos[0] = radius * (midAngle(d2) < Math.PI ? 1 : -1);
return "translate("+ pos +")";
};
})
.styleTween("text-anchor", function(d){
this._current = this._current || d;
var interpolate = d3.interpolate(this._current, d);
this._current = interpolate(0);
return function(t) {
var d2 = interpolate(t);
return midAngle(d2) < Math.PI ? "start":"end";
};
});
text.exit()
.remove();
/* ------- SLICE TO TEXT POLYLINES -------*/
var polyline = svg.select(".lines").selectAll("polyline")
.data(pie(values), key);
polyline.enter()
.append("polyline");
polyline.transition().duration(1000)
.attrTween("points", function(d){
this._current = this._current || d;
var interpolate = d3.interpolate(this._current, d);
this._current = interpolate(0);
return function(t) {
var d2 = interpolate(t);
var pos = outerArc.centroid(d2);
pos[0] = radius * 0.95 * (midAngle(d2) < Math.PI ? 1 : -1);
return [arc.centroid(d2), outerArc.centroid(d2), pos];
};
});
polyline.exit()
.remove();
}
})}
}});
You have two problems the first one is that you forgot to append these elements to your svg
svg.append("g")
.attr("class", "slices");
svg.append("g")
.attr("class", "labels");
svg.append("g")
.attr("class", "lines");
these are the g elements that d3 draws lines and labels on , so you need to put them after
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
,, the second issue is that you forgot to define outer arc ,,
var outerArc = d3.svg.arc()
.innerRadius(radius * 0.9)
.outerRadius(radius * 0.9);
which you use in your calculation for creating lines
The data is in an array of objects where the
Data = [object{name:"FIRST TECH", value:2477}];
The technology names change and I am trying to place them to the left of the bar they are associated with.
I'm not sure if I'm doing this in the best practice, but when I get the text appended to the side it is invisible. I've tried changing the class name, changing the color, z-index, position:absolute, it always remains hidden.
What do I need to do in order to get the label correctly on the side?
Code:
<style>
h2{
text-align:center;
margin-left: auto;
margin-right: auto;
}
.abcxyz{
fill:black;
color:#000;
text-anchor: end;
}
.chart {
width:800px;
display:block;
margin:auto;
background-color:#c7d9e8;
}
.chart rect {
fill: steelblue;
}
.chart rect:hover{
fill:brown;
}
.values {
fill: white;
font: 12px sans-serif;
text-anchor: end;
}
</style>
<script>
$(document).ready(function(){
var return_data;
$.ajax({
url: '<?php echo site_url('metrics/getTechDocs') ?>',
type: 'GET',
success: function(data){
data = JSON.parse(data);
set_return_data(data);
drawGraph(data);
},
error: function(data){
console.log("error");
}
});
});
function set_return_data(data){
return_data = data;
console.log (return_data);
}
function formatGraphData(data){
var newData = new Array();
for(x in data){
tmp = {name: x , value: data[x].length};
newData.push(tmp);
}
newData.sort(function(a,b){return b.value - a.value} );
return newData;
}
function drawGraph(rawData){
rawData = formatGraphData(rawData);
var width = 800,
barHeight = 30;
var x = d3.scale.linear()
.domain([0, rawData[0].value])
.range([10, width]);
var y = d3.scale.linear()
.domain(rawData.map(function(d){return d.name}))
.range([0, barHeight * rawData.length]);
var chart = d3.select(".chart")
.attr("height", barHeight * rawData.length);
var bar = chart.selectAll("g")
.data(rawData)
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * barHeight + ")"; });
bar.append("text")
.attr("class", "abcxyz")
.attr("y", barHeight / 2 )
.text(function(d){return d.name});
bar.append("rect")
.attr("height", barHeight - 1)
.attr("width", function(d){return x(d.value);});
bar.append("text")
.attr("class", "values")
.attr("x", function(d) { return x(d.value) - 3; })
.attr("y", barHeight / 2)
.attr("dy", ".35em")
.text(function(d){return d.value;});
}
</script>
<div>
<h2>Frequency of Technology Terms</h2>
<svg class="chart"></svg>
</div>
SVG doesn't know z-order or anything similar. The elements are displayed in the order they are added in. So to make the text appear on top of the bars, draw the bars first and then the text.