I have a chart built in d3 to display who is speaking and where during the course of a play. The characters are represented by color bars, and I'd like to provide a legend. However, my legend code displays in a single column vertically and displays the same character name multiple times. I know the latter is because it's parsing each record in the json, but I'm not sure how to have it mention each character only once other than providing a list of characters in the same way I provide a list of locations in my code. Is there a way I can do this from the json instead?
Also, how would I organize the legend in a grid so as to utilize the white space in the upper left portion of the chart? I created a fiddle with the code I currently have at https://jsfiddle.net/3f7xrgeb/, and have also pasted it below. My working copy (in case the fiddle does not display) is at http://www.matthewedavis.net/Magdalene_playtext/test_bars.html.
Any help would be appreciated.
var margin = {top: 50, right: 150, bottom: 50, left: 150},
w = 2500 - margin.left - margin.right,
h = 500 - margin.top - margin.bottom;
d3.json("http://www.matthewedavis.net/Magdalene_playtext/test_chart.json", function(json) {
var data = json.items;
var test = function(d) { return d.starting_line + d.duration; };
var stage_directions = ([44, 49, 114, 140, 209, 217, 249, 257, 265, 277, 305, 334, 358, 381, 398, 409, 440, 470, 491, 547, 555, 564, 572, 587, 614, 630, 640, 691, 704, 725, 743, 775, 793, 818, 823, 841, 845, 868, 872, 888, 902, 910, 920, 925, 963, 993, 1005, 1023, 1031, 1047, 1061, 1096, 1125, 1133, 1143, 1178, 1210, 1228, 1281, 1293, 1336, 1349, 1376, 1395, 1439, 1446, 1454, 1538, 1554, 1562, 1578, 1598, 1610, 1618, 1642, 1646, 1716, 1725, 1743, 1781, 1791, 1797, 1843, 1863, 1887, 1915, 1923, 1939 ,1972, 1989, 2019, 2031, 2039, 2045, 2073, 2085, 2101, 2123])
var x = d3.scale.linear()
.domain([0, 2200])
.range([0, w]);
var y = d3.scale.ordinal()
.domain(["The priest's cell","The Cloud","Wilderness","The mountain","The Lodge","Marseilles","The Ship","Heaven","Heathen Temple","Sepulchre","The Stations","Palace of the King of Marseilles","Lazarus' tomb","Simon the Leper's House","Arbor","Jherusalem","Tavern","Hellmouth","The Place","Stage Above Hell","King of Flesh's location","King of the World's location","Pilate's location","Herod's location","Magdalene Castle","Tiberius' Palace"])
.rangeBands([0, h]);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var svg = d3.select("body").append("svg")
.attr("width", w + margin.left + margin.right)
.attr("height", h + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("text")
.attr("transform", "translate(" + (w / 2) + " ," + (h + margin.bottom - 5) +")")
.style("text-anchor", "middle")
.text("Line Number");
var t;
for (t in stage_directions){
svg.append("rect")
.attr("class", "overlay")
.attr("x",stage_directions[t])
.attr("width", 1)
.attr("height", h)
.style ("fill", "black")
.style ("opacity", 0.3);
}
svg.append("rect")
.attr("class", "overlay")
.attr("x",1)
.attr("width", 1133)
.attr("height", h)
.style ("fill", "red")
.style ("opacity", 0.1);
svg.append("rect")
.attr("class", "overlay")
.attr("x",1133)
.attr("width", 857)
.attr("height", h)
.style ("fill", "yellow")
.style ("opacity", 0.1);
svg.append("rect")
.attr("class", "overlay")
.attr("x",1990)
.attr("width", 134)
.attr("height", h)
.style ("fill", "green")
.style ("opacity", 0.1);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
var bars = svg.selectAll(".bar")
.data(data)
.enter()
.append("rect")
.attr("class", function(d, i) {return "bar " + d.character;})
.attr("x", function(d, i) {return d.starting_line;})
.attr("y", function(d, i) {return y(d.location);})
.attr("width", function(d, i) {return d.duration;})
.attr("height", 15)
.style("fill", function(d,i) {return "#" + d.color;});
var legend = svg.selectAll(".legend")
.data(data)
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", 10)
.attr("width", 10)
.attr("height", 10)
.style("fill", function(d,i) {return "#" + d.color;});
legend.append("text")
.attr("x", 22)
.attr("y", 5)
.attr("dy", ".35em")
.text(function(d) { return d.character; });
svg.append("g")
.attr("class", "legend")
.call(legend)
});
Try this:
First, create an array with the list of characters, avoiding duplicates:
var listCharacters = d3.set(data.map(function(d){ return d.character})).values();
Then, use it as the data for your legend:
var legend = svg.selectAll(".legend")
.data(listCharacters)
.enter()
//the test of the code...
And change the text:
.text(function(d,i){ return listCharacters[i]});
For the colours, do the same:
var listColors = d3.set(data.map(function(d){ return d.colour})).values(); //check the spelling in your object
And color the rectangles according to the index:
.attr("fill", function(d,i){ return listColors[i]});
To understand what it does, please read this:
https://github.com/mbostock/d3/wiki/Arrays
I have a list named geneexp which contains dictionaries inside of it as a JSON.
The thing I would like to do is to visualize the fpkm2 values on the right and fpkm1 values on the left as a horizontal bar chart just like here
Does D3.js accept dot in the numerical values or should I change the values by using comma? because I need whole value.
I tried to imitate it by changing datas but it didn't work. Here is what i have tried
var geneexp = [{"chr":"1","end":79110897,"fpkm1":4.50805,"fpkm2":17.1285,"gene":"IFI44L","log2ratio":1.92583,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":79085606},{"chr":"1","end":109749401,"fpkm1":17.2746,"fpkm2":42.2573,"gene":"KIAA1324","log2ratio":1.29055,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":109648572},{"chr":"1","end":149783928,"fpkm1":3.79975,"fpkm2":18.0374,"gene":"FCGR1A","log2ratio":2.24701,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":149754226},{"chr":"1","end":663527,"fpkm1":2.0079,"fpkm2":0,"gene":"RP11-206L10.1","log2ratio":-10000000000,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":661610},{"chr":"1","end":26701013,"fpkm1":7.31716,"fpkm2":22.1062,"gene":"ZNF683","log2ratio":1.5951,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":26688124},{"chr":"1","end":40261668,"fpkm1":87.1441,"fpkm2":0,"gene":"RP1-118J21.24","log2ratio":-10000000000,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":40261514},{"chr":"1","end":68698803,"fpkm1":116.196,"fpkm2":40.7994,"gene":"WLS","log2ratio":-1.50993,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":68167148},{"chr":"1","end":153348125,"fpkm1":334.978,"fpkm2":986.306,"gene":"S100A12","log2ratio":1.55797,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":153346183},{"chr":"1","end":243265046,"fpkm1":2.09012,"fpkm2":8.19733,"gene":"RP11-261C10.3","log2ratio":1.97157,"pvalue":0.0002,"qvalue":0.0396421,"sample1":"q1","sample2":"q2","significant":true,"start":243218159},{"chr":"1","end":243265046,"fpkm1":2.09012,"fpkm2":8.19733,"gene":"RP11-261C10.6","log2ratio":1.97157,"pvalue":0.0002,"qvalue":0.0396421,"sample1":"q1","sample2":"q2","significant":true,"start":243218159},{"chr":"10","end":6277513,"fpkm1":21.6928,"fpkm2":46.1653,"gene":"PFKFB3","log2ratio":1.08959,"pvalue":0.00025,"qvalue":0.047075,"sample1":"q1","sample2":"q2","significant":true,"start":6186880},{"chr":"10","end":1779670,"fpkm1":0.0869107,"fpkm2":4.44899,"gene":"ADARB2","log2ratio":5.6778,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":1228072},{"chr":"11","end":44105772,"fpkm1":11.185,"fpkm2":35.0837,"gene":"ACCS","log2ratio":1.64924,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":44087474},{"chr":"11","end":59634048,"fpkm1":2.72747,"fpkm2":10.5967,"gene":"TCN1","log2ratio":1.95799,"pvalue":0.00025,"qvalue":0.047075,"sample1":"q1","sample2":"q2","significant":true,"start":59620272},{"chr":"11","end":102597781,"fpkm1":2.085,"fpkm2":21.2457,"gene":"MMP8","log2ratio":3.34905,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":102582472},{"chr":"12","end":58212487,"fpkm1":48.7468,"fpkm2":11.4797,"gene":"AVIL","log2ratio":-2.08622,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":58156116},{"chr":"12","end":58212487,"fpkm1":48.7468,"fpkm2":11.4797,"gene":"U6","log2ratio":-2.08622,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":58156116},{"chr":"13","end":53626196,"fpkm1":1.30126,"fpkm2":10.4113,"gene":"OLFM4","log2ratio":3.00017,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":53602467},{"chr":"14","end":21250626,"fpkm1":61.36,"fpkm2":28.3291,"gene":"RNASE6","log2ratio":-1.11501,"pvalue":0.00025,"qvalue":0.047075,"sample1":"q1","sample2":"q2","significant":true,"start":21249209},{"chr":"14","end":106725733,"fpkm1":244.435,"fpkm2":94.9557,"gene":"IGHV3-23","log2ratio":-1.36413,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":106725200},{"chr":"15","end":40569688,"fpkm1":1.13809,"fpkm2":4.07193,"gene":"BUB1B","log2ratio":1.8391,"pvalue":0.00015,"qvalue":0.0309534,"sample1":"q1","sample2":"q2","significant":true,"start":40453223},{"chr":"15","end":40569688,"fpkm1":1.13809,"fpkm2":4.07193,"gene":"PAK6","log2ratio":1.8391,"pvalue":0.00015,"qvalue":0.0309534,"sample1":"q1","sample2":"q2","significant":true,"start":40453223},{"chr":"15","end":40569688,"fpkm1":1.13809,"fpkm2":4.07193,"gene":"RP11-133K1.2","log2ratio":1.8391,"pvalue":0.00015,"qvalue":0.0309534,"sample1":"q1","sample2":"q2","significant":true,"start":40453223},{"chr":"15","end":82577271,"fpkm1":4.60645,"fpkm2":0.0831472,"gene":"FAM154B","log2ratio":-5.79184,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":82555150},{"chr":"15","end":41806085,"fpkm1":12.6574,"fpkm2":3.4051,"gene":"LTK","log2ratio":-1.89421,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":41795835},{"chr":"17","end":20947073,"fpkm1":3.0439,"fpkm2":0,"gene":"AC090774.2","log2ratio":-10000000000,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":20717932},{"chr":"17","end":20947073,"fpkm1":3.0439,"fpkm2":0,"gene":"RP11-344E13.3","log2ratio":-10000000000,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":20717932},{"chr":"17","end":20947073,"fpkm1":3.0439,"fpkm2":0,"gene":"RP11-381P6.1","log2ratio":-10000000000,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":20717932},{"chr":"17","end":20947073,"fpkm1":3.0439,"fpkm2":0,"gene":"RP11-746M1.2","log2ratio":-10000000000,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":20717932},{"chr":"17","end":56358296,"fpkm1":2.22762,"fpkm2":10.5089,"gene":"MPO","log2ratio":2.23803,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":56347216},{"chr":"17","end":62936347,"fpkm1":66.2861,"fpkm2":537.599,"gene":"PLEKHM1P","log2ratio":3.01975,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":62745654},{"chr":"17","end":62936347,"fpkm1":66.2861,"fpkm2":537.599,"gene":"RP11-927P21.4","log2ratio":3.01975,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":62745654},{"chr":"17","end":62936347,"fpkm1":66.2861,"fpkm2":537.599,"gene":"RP11-927P21.5","log2ratio":3.01975,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":62745654},{"chr":"17","end":62936347,"fpkm1":66.2861,"fpkm2":537.599,"gene":"RP11-927P21.6","log2ratio":3.01975,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":62745654},{"chr":"17","end":62936347,"fpkm1":66.2861,"fpkm2":537.599,"gene":"RP13-104F24.1","log2ratio":3.01975,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":62745654},{"chr":"17","end":62936347,"fpkm1":66.2861,"fpkm2":537.599,"gene":"hsa-mir-6080","log2ratio":3.01975,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":62745654},{"chr":"19","end":832017,"fpkm1":2.30561,"fpkm2":12.5563,"gene":"AZU1","log2ratio":2.44519,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":825096},{"chr":"19","end":51308186,"fpkm1":927.54,"fpkm2":5.58845,"gene":"C19orf48","log2ratio":-7.37482,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":51300960},{"chr":"19","end":51308186,"fpkm1":927.54,"fpkm2":5.58845,"gene":"SNORD88B","log2ratio":-7.37482,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":51300960},{"chr":"19","end":51308186,"fpkm1":927.54,"fpkm2":5.58845,"gene":"SNORD88C","log2ratio":-7.37482,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":51300960},{"chr":"19","end":55660722,"fpkm1":0.904421,"fpkm2":4.90537,"gene":"TNNT1","log2ratio":2.4393,"pvalue":0.0002,"qvalue":0.0396421,"sample1":"q1","sample2":"q2","significant":true,"start":55644161},{"chr":"2","end":103069345,"fpkm1":26.8634,"fpkm2":79.7291,"gene":"AC007278.3","log2ratio":1.56946,"pvalue":0.0001,"qvalue":0.0209222,"sample1":"q1","sample2":"q2","significant":true,"start":103034998},{"chr":"2","end":103069345,"fpkm1":26.8634,"fpkm2":79.7291,"gene":"IL18RAP","log2ratio":1.56946,"pvalue":0.0001,"qvalue":0.0209222,"sample1":"q1","sample2":"q2","significant":true,"start":103034998},{"chr":"2","end":119752236,"fpkm1":3.81979,"fpkm2":14.0364,"gene":"MARCO","log2ratio":1.87761,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":119699741},{"chr":"2","end":162841792,"fpkm1":3.96841,"fpkm2":0.485637,"gene":"AC009487.5","log2ratio":-3.03061,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":162272604},{"chr":"2","end":162841792,"fpkm1":3.96841,"fpkm2":0.485637,"gene":"SLC4A10","log2ratio":-3.03061,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":162272604},{"chr":"2","end":162841792,"fpkm1":3.96841,"fpkm2":0.485637,"gene":"TBR1","log2ratio":-3.03061,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":162272604},{"chr":"20","end":24940564,"fpkm1":119.63,"fpkm2":349.159,"gene":"CST7","log2ratio":1.5453,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":24929865},{"chr":"20","end":36965907,"fpkm1":4.81264,"fpkm2":23.3885,"gene":"BPI","log2ratio":2.2809,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":36838889},{"chr":"20","end":43805185,"fpkm1":89.1047,"fpkm2":418.219,"gene":"PI3","log2ratio":2.23068,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":43803516},{"chr":"20","end":3687775,"fpkm1":0.740776,"fpkm2":3.19236,"gene":"SIGLEC1","log2ratio":2.10751,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":3667616},{"chr":"20","end":49493714,"fpkm1":18196.3,"fpkm2":8056.63,"gene":"TMSB4XP6","log2ratio":-1.1754,"pvalue":0.0001,"qvalue":0.0209222,"sample1":"q1","sample2":"q2","significant":true,"start":49411430},{"chr":"20","end":62587775,"fpkm1":17.0093,"fpkm2":292.901,"gene":"MIR1914","log2ratio":4.10602,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":62571185},{"chr":"20","end":62587775,"fpkm1":17.0093,"fpkm2":292.901,"gene":"MIR647","log2ratio":4.10602,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":62571185},{"chr":"20","end":62587775,"fpkm1":17.0093,"fpkm2":292.901,"gene":"UCKL1","log2ratio":4.10602,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":62571185},{"chr":"21","end":42831141,"fpkm1":24.1349,"fpkm2":55.4697,"gene":"MX1","log2ratio":1.20058,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":42792230},{"chr":"22","end":23165787,"fpkm1":40.261,"fpkm2":377.429,"gene":"D86994.1","log2ratio":3.22875,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":23040273},{"chr":"22","end":23165787,"fpkm1":40.261,"fpkm2":377.429,"gene":"D87015.1","log2ratio":3.22875,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":23040273},{"chr":"22","end":23165787,"fpkm1":40.261,"fpkm2":377.429,"gene":"IGLV2-14","log2ratio":3.22875,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":23040273},{"chr":"22","end":23165787,"fpkm1":40.261,"fpkm2":377.429,"gene":"IGLV2-23","log2ratio":3.22875,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":23040273},{"chr":"22","end":23165787,"fpkm1":40.261,"fpkm2":377.429,"gene":"IGLV2-8","log2ratio":3.22875,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":23040273},{"chr":"22","end":23165787,"fpkm1":40.261,"fpkm2":377.429,"gene":"IGLV3-12","log2ratio":3.22875,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":23040273},{"chr":"22","end":23165787,"fpkm1":40.261,"fpkm2":377.429,"gene":"MIR650","log2ratio":3.22875,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":23040273},{"chr":"22","end":23243617,"fpkm1":49.5145,"fpkm2":11.3554,"gene":"IGLC2","log2ratio":-2.12448,"pvalue":0.0001,"qvalue":0.0209222,"sample1":"q1","sample2":"q2","significant":true,"start":23243155},{"chr":"22","end":29564321,"fpkm1":11.9594,"fpkm2":27.9691,"gene":"KREMEN1","log2ratio":1.22568,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":29469065},{"chr":"3","end":48266981,"fpkm1":25.9102,"fpkm2":98.0926,"gene":"CAMP","log2ratio":1.92062,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":48264836},{"chr":"3","end":101489406,"fpkm1":3.09138,"fpkm2":622.79,"gene":"CEP97","log2ratio":7.65435,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":101442768},{"chr":"3","end":46546365,"fpkm1":20.1568,"fpkm2":149.34,"gene":"LTF","log2ratio":2.88926,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":46477135},{"chr":"3","end":146262651,"fpkm1":31.9606,"fpkm2":82.6978,"gene":"PLSCR1","log2ratio":1.37156,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":146232966},{"chr":"4","end":6698897,"fpkm1":92.3842,"fpkm2":24.0847,"gene":"S100P","log2ratio":-1.93953,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":6694795},{"chr":"4","end":79531597,"fpkm1":26.8309,"fpkm2":114.07,"gene":"ANXA3","log2ratio":2.08795,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":79472672},{"chr":"6","end":29865563,"fpkm1":7.44281,"fpkm2":0,"gene":"HLA-T","log2ratio":-10000000000,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":29864430},{"chr":"6","end":29897009,"fpkm1":19.3003,"fpkm2":107.257,"gene":"HLA-K","log2ratio":2.47438,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":29892499},{"chr":"6","end":35992645,"fpkm1":3.06182,"fpkm2":9.49569,"gene":"SLC26A8","log2ratio":1.63288,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":35911290},{"chr":"6","end":49712150,"fpkm1":1.15836,"fpkm2":10.0617,"gene":"CRISP3","log2ratio":3.11871,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":49695096},{"chr":"7","end":10516505,"fpkm1":0,"fpkm2":6.30144,"gene":"AC009945.4","log2ratio":10000000000,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":10516207},{"chr":"7","end":129396922,"fpkm1":12.534,"fpkm2":48.0358,"gene":"NRF1","log2ratio":1.93826,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":129251554},{"chr":"7","end":129396922,"fpkm1":12.534,"fpkm2":48.0358,"gene":"RNA5SP244","log2ratio":1.93826,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":129251554},{"chr":"7","end":142428984,"fpkm1":27.6122,"fpkm2":247.735,"gene":"TRBV28","log2ratio":3.16542,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":142428483},{"chr":"7","end":56119297,"fpkm1":5.02525,"fpkm2":1.00895,"gene":"PSPH","log2ratio":-2.31634,"pvalue":0.0001,"qvalue":0.0209222,"sample1":"q1","sample2":"q2","significant":true,"start":56078743},{"chr":"7","end":150502208,"fpkm1":47.706,"fpkm2":15.0642,"gene":"TMEM176B","log2ratio":-1.66304,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":150488372},{"chr":"8","end":6795860,"fpkm1":6.34889,"fpkm2":30.9513,"gene":"DEFA4","log2ratio":2.28542,"pvalue":0.0002,"qvalue":0.0396421,"sample1":"q1","sample2":"q2","significant":true,"start":6793343},{"chr":"8","end":99306621,"fpkm1":3.74493,"fpkm2":41.811,"gene":"NIPAL2","log2ratio":3.48087,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":99202060},{"chr":"8","end":99306621,"fpkm1":3.74493,"fpkm2":41.811,"gene":"U6","log2ratio":3.48087,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":99202060},{"chr":"9","end":130915734,"fpkm1":16.756,"fpkm2":102.235,"gene":"LCN2","log2ratio":2.60914,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":130911349},{"chr":"9","end":125667813,"fpkm1":13.5883,"fpkm2":47.7743,"gene":"RC3H2","log2ratio":1.81387,"pvalue":0.0001,"qvalue":0.0209222,"sample1":"q1","sample2":"q2","significant":true,"start":125606559},{"chr":"Y","end":7249743,"fpkm1":31.7483,"fpkm2":7.34532,"gene":"PRKY","log2ratio":-2.11178,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":7142012},{"chr":"Y","end":7249743,"fpkm1":31.7483,"fpkm2":7.34532,"gene":"U6","log2ratio":-2.11178,"pvalue":5e-05,"qvalue":0.0115877,"sample1":"q1","sample2":"q2","significant":true,"start":7142012}],
margin = {
top: 30,
right: 10,
bottom: 10,
left: 10
},
width = 1200 - margin.left - margin.right,
height = 1200 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width])
var y = d3.scale.ordinal()
.rangeRoundBands([0, height], .2);
var xAxis = d3.svg.axis()
.scale(x)
.orient("top");
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 + ")");
x.domain([-800,800])
y.domain(data.map(function (d) {return d.gene;}));
svg.selectAll(".bar")
.data(geneexp) //changed as data to geneexp
.enter().append("rect")
.attr("class", "bar")
.attr("x", function (d) {
return x(Math.min(0, d.fpkm2)); //changed as value to fpkm2
})
.attr("y", function (d) {
return y(d.gene); //changed as name to fpmk1
})
.attr("width", function (d) {
return Math.abs(x(d.fpkm2) - x(0));
})
.attr("height", y.rangeBand());
svg.selectAll(".bar2")
.data(geneexp)
.enter().append("rect")
.attr("class", "bar2")
.attr("x", function (d) {
return x(Math.min(0, -d.fpkm1));
})
.attr("y", function (d) {
return y(d.gene);
})
.attr("width", function (d) {
return Math.abs(x(-d.fpkm1) - x(0));
})
.attr("height", y.rangeBand());
svg.append("g")
.attr("class", "x axis")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.append("line")
.attr("x1", x(0))
.attr("x2", x(0))
.attr("y2", height);
function type(d) {
d.value = +d.value;
return d;
}
Any help will be appreciated.
Thanks a lot.
Inspect your console for errors.
In this line:
y.domain(data.map(function (d) {return d.gene;}));
data should be geneexp
Example here.