javascript – 使用D3.js的属性cx =“NaN”的值无效

前端之家收集整理的这篇文章主要介绍了javascript – 使用D3.js的属性cx =“NaN”的值无效前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建一种散点图形式.我有一个自定义的x轴和两个a轴的特定比例.我也为它实现了缩放功能.到目前为止一切都很好,但是当我最终尝试将数据绘制为圆圈时,我得到两个错误

.

我的图表可以在这个网站上查看:http://servers.binf.ku.dk/hemaexplorerbeta/
(圈子很大,因为我想确保在我设计它们之前大致知道它们的位置)

我根据从MysqL服务器读取的数据创建我的圈子.我检查了所有数据,数字是正确的.他们要么错误地绘图,要么我的刻度/缩放有问题.

您也可能会注意到我创建了我的轴并最初使用某些值进行缩放,并在之后的某些函数中更改它们.这是因为我计划在网站上加载一个空图表,用户可以决定加载它的数据集,其中函数必须自定义要加载的数据的比例和轴.

我在下面粘贴了我的源代码

  1. //Setting generic width and height values for our SVG.
  2. var margin = {top: 60,right: 0,bottom: 70,left: 40},genWidth = 1024;
  3. genHeight = 768;
  4.  
  5. width = genWidth - 70 - margin.left - margin.right,height = genHeight - 100 - margin.top - margin.bottom;
  6.  
  7. //Other variable declarations.
  8. var valueY = 0;
  9. var graphData = Array();
  10.  
  11.  
  12. //Creating scales used to scale everything to the size of the SVG.
  13. var xScale = d3.scale.linear()
  14. .domain([0,genWidth])
  15. .range([0,width-margin.right]);
  16.  
  17. var yScale = d3.scale.linear()
  18. .domain([0,genHeight])
  19. .range([height,margin.bottom]);
  20.  
  21. var xAxis = d3.svg.axis()
  22. .scale(xScale)
  23. .orient("bottom");
  24.  
  25. var yAxis = d3.svg.axis()
  26. .scale(yScale)
  27. .orient("left");
  28.  
  29. //Zoom command ...
  30. var zoom = d3.behavior.zoom()
  31. .x(xScale)
  32. .y(yScale)
  33. .scaleExtent([1,10])
  34. .on("zoom",zoomTargets);
  35.  
  36. // The mark '#' indicates an ID. IF '#' isn't included argument expected is a tag such as "svg" or "p" etc..
  37. var SVG = d3.select("#mainSVG")
  38. .attr("class","SVG")
  39. .attr("width",width + margin.left + margin.right)
  40. .attr("height",height + margin.top + margin.bottom)
  41. .attr("pointer-events","all")
  42. .append("g")
  43. .attr("transform","translate(" + margin.left + "," + margin.top + ")");
  44.  
  45. //This creates a body with a clippath inside the svg where all element in the graph will be. This prevents elemnts on the graph to go past the axis.
  46. var SVGbody = SVG.append("g")
  47. .attr("clip-path","url(#clip)")
  48. .call(zoom);
  49.  
  50.  
  51. //Create background. The mouse must be over an object on the graph for the zoom to work. The rectangle will cover the entire graph.
  52. var rect = SVGbody.append("rect")
  53. .attr("width",width)
  54. .attr("height",height);
  55.  
  56. //Showing the axis that we created earlier in the script for both X and Y.
  57. SVG.append("g")
  58. .attr("class","x axis")
  59. .attr("transform","translate(0," + height + ")")
  60. .call(xAxis)
  61. .selectAll("text")
  62. .style("text-anchor","end")
  63. .attr("transform",function(d) {
  64. return "rotate(-30)"
  65. });;
  66.  
  67. SVG.append("g")
  68. .attr("class","y axis")
  69. .call(yAxis);
  70.  
  71.  
  72. d3.json("getdata.PHP?type=load&gene=CCL5&data=human",function(error,data) {
  73. var arrayValues = [];
  74. if(error){ return console.log(error); }
  75.  
  76. data.forEach( function(d) {
  77. arrayValues.push(d.gene_name);
  78. valueY = getValueY(d.gene_data);
  79. var string = JSON.stringify(d.gene_data);
  80. graphData.push(string.split(" "));
  81. });
  82.  
  83. //console.log(graphData);
  84.  
  85. arrayValues = removeDuplicatesInPlace(arrayValues);
  86. updateScaleX(arrayValues.length);
  87. updateAxisX(arrayValues);
  88. //console.log(arrayValues);
  89.  
  90. updateScaleY(valueY);
  91.  
  92. //This selects 4 circles (non-existent,there requires data-binding) and appends them all below enter.
  93. //The amount of numbers in data is the amount of circles to be appended in the enter() section.
  94. for(var i = 0;i <= graphData.length;i++){
  95. var circle = SVGbody
  96. .selectAll("circle")
  97. .data(graphData[i])
  98. .enter()
  99. .append("circle")
  100. .attr("cx",function(d){return xScale((i*100)+100);})
  101. .attr("cy",function(d){return yScale(d)})
  102. .attr("r",20);
  103. }
  104.  
  105.  
  106.  
  107. });
  108.  
  109. //Clipping is defined here used to prevent elements from the graph from going past the axis.
  110. var clip = SVG.append("defs").append("svg:clipPath")
  111. .attr("id","clip")
  112. .append("svg:rect")
  113. .attr("id","clip-rect")
  114. .attr("x","0")
  115. .attr("y","0")
  116. .attr("width",height);
  117.  
  118. //Resets zoom when click on circle object. Zoom work now,should be changed to a button instead of click on circle though.
  119. SVG.selectAll("circle").on("click",function() {
  120. zoom.scale(1);
  121. zoom.translate([0,0]);
  122. zoomTargets();
  123. });
  124.  
  125. //The function handleling the zoom. Nothing is zoomed automatically,every elemnt must me defined here.
  126. function zoomTargets() {
  127.  
  128. var translate = zoom.translate(),scale = zoom.scale();
  129.  
  130. tx = Math.min(0,Math.max(width * (1 - scale),translate[0]));
  131. ty = Math.min(0,Math.max(height * (1 - scale),translate[1]));
  132.  
  133. //This line applies the tx and ty which prevents the graphs from moving out of the limits. This means it can't be moved until zoomed in first.
  134. zoom.translate([tx,ty]);
  135.  
  136. SVG.select(".x.axis").call(xAxis)
  137. .selectAll("text")
  138. .style("text-anchor",function(d) {
  139. return "rotate(-30)"
  140. });
  141. SVG.select(".y.axis").call(yAxis);
  142. SVG.selectAll("circle").attr("cx",function(d){return xScale(d)}).attr("cy",function(d){return yScale(d)});
  143. }
  144.  
  145. function resetZoom() {
  146. zoom.scale(1);
  147. zoom.translate([0,0]);
  148. zoomTargets();
  149. }
  150.  
  151. function updateAxisX(arr) {
  152. var formatAxis = function(d,i) { return arr[i]; }
  153.  
  154. xAxis = d3.svg.axis()
  155. .scale(xScale)
  156. .orient("bottom")
  157. .tickValues(createTickValuesArray(arr.length))
  158. .tickFormat(formatAxis);
  159.  
  160. SVG.select(".x.axis")
  161. .call(xAxis)
  162. .selectAll("text")
  163. .style("text-anchor",function(d) {
  164. return "rotate(-30)"
  165. });
  166. }
  167.  
  168. function updateScaleX(newWidth){
  169.  
  170. genWidth = newWidth;
  171. xScale = d3.scale.linear()
  172. .domain([0,(newWidth*100)+50])
  173. .range([0,width-margin.right]);
  174.  
  175. SVG.selectAll("circle").attr("cx",function(d){return yScale(d)});
  176. zoom.x(xScale);
  177. }
  178.  
  179. function updateScaleY(newHeight){
  180.  
  181. console.log(newHeight);
  182. var yScale = d3.scale.linear()
  183. .domain([0,newHeight])
  184. .range([height,margin.bottom]);
  185.  
  186. yAxis = d3.svg.axis()
  187. .scale(yScale)
  188. .orient("left");
  189.  
  190. SVG.select(".y.axis").call(yAxis);
  191. SVG.selectAll("circle").attr("cx",function(d){return yScale(d)});
  192. zoom.y(yScale);
  193. }
  194.  
  195. function createTickValuesArray(amountOfTicks){
  196. var tickValuesArr = [];
  197. for(var i = 1;i<=amountOfTicks;i++){
  198. tickValuesArr[i-1] = 100*i;
  199. }
  200. return tickValuesArr;
  201. }
  202.  
  203. function getValueY(coordinates){
  204. return d3.max(coordinates,Number);
  205. }
  206.  
  207. //Custom functions used for specific uses.
  208. var removeDuplicatesInPlace = function (arr) {
  209. var i,j,cur,found;
  210. for (i = arr.length - 1; i >= 0; i--) {
  211. cur = arr[i];
  212. found = false;
  213. for (j = i - 1; !found && j >= 0; j--) {
  214. if (cur === arr[j]) {
  215. if (i !== j) {
  216. arr.splice(i,1);
  217. }
  218. found = true;
  219. }
  220. }
  221. }
  222. return arr;
  223. };

解决方法

由于附加引用,graphData中每个数组的第一个和最后一个元素在解析为数字时会导致错误
例如,第七个graphData数组如下所示:
  1. console.log(graphData[6]) // [""5.149230","4.965121""]

造成这种情况的原因似乎是在获取数据时不必要的JSON.stringfiy()调用

  1. d3.json("getdata.PHP?type=load&gene=CCL5&data=human",data) {
  2. var arrayValues = [];
  3. if(error){ return console.log(error); }
  4.  
  5. data.forEach( function(d) {
  6. arrayValues.push(d.gene_name);
  7. valueY = getValueY(d.gene_data);
  8. var string = JSON.stringify(d.gene_data); // <-- this one
  9. graphData.push(string.split(" "));
  10. });

d.gene_data已经是一个字符串,所以当你删除JSON.stringify()时它应该按预期工作

猜你在找的JavaScript相关文章