为什么我在从D3 v3升级到v5后收到错误“无法读取null的属性(阅读'transition')”

lsmepo6l  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(138)

我已经在一个简单的视觉上将d3从v3升级到v5。我已经做了大部分的调整,包括d3.scv.arc和d3.layout.pie,但我得到以下错误消息:
“无法读取null的属性(阅读'transition')”
下面是v3的代码:https://jsfiddle.net/bentham7246/b06wLcm8/141/以下是到目前为止包含错误的v5代码:https://jsfiddle.net/bentham7246/hmnyu7bs/1/
有人能帮忙吗?
这是当前的v5代码

  1. // set the dimensions and margins of the graph
  2. var margin = {
  3. top: 50,
  4. right: 30,
  5. bottom: 10,
  6. left: 50
  7. },
  8. width = 900 - margin.left - margin.right,
  9. height = 150 - margin.top - margin.bottom;
  10. // append the svg object to the body of the page
  11. var svg = d3.select("#chart")
  12. .append("svg")
  13. .attr("width", width + margin.left + margin.right)
  14. .attr("height", height + margin.top + margin.bottom)
  15. .append("g")
  16. .attr("transform",
  17. "translate(" + margin.left + "," + margin.top + ")");
  18. var outerRadius = 45;
  19. var innerRadius = outerRadius - 5;
  20. var color = ['#ec1561', '#2a3a46', '#ec1561'];
  21. var data = [
  22. {
  23. "value": 90,
  24. "variance": 6
  25. }
  26. ]
  27. var percent = d3.max(data, function(d) {
  28. return d.value
  29. })
  30. var pie = d3.pie()
  31. .value(function(d) {
  32. return d
  33. })
  34. .sort(null);
  35. var arc = d3.arc()
  36. .innerRadius(30)
  37. .outerRadius(35)
  38. .startAngle(0)
  39. .endAngle(2 * Math.PI);
  40. var arc2 = d3.arc()
  41. .innerRadius(30)
  42. .outerRadius(35)
  43. .startAngle(0)
  44. .endAngle(2 * Math.PI);
  45. //The circle is following this
  46. var arcDummy = d3.arc()
  47. .innerRadius((outerRadius - innerRadius) / 2 + innerRadius)
  48. .outerRadius((outerRadius - innerRadius) / 2 + innerRadius)
  49. .startAngle(0);
  50. var arcLine = d3.arc()
  51. .innerRadius(innerRadius)
  52. .outerRadius(outerRadius)
  53. // .cornerRadius(10)
  54. .startAngle(0);
  55. //background
  56. var path = svg.append('path')
  57. .attr({
  58. d: arc,
  59. fill: color[1],
  60. opacity: 0.2
  61. });
  62. var path2 = svg.append('path')
  63. .attr({
  64. d: arc2,
  65. fill: color[1],
  66. opacity: 0.2
  67. });
  68. var pathForeground = svg.append('path')
  69. .datum({
  70. endAngle: 0
  71. })
  72. .attr({
  73. d: arcLine,
  74. fill: "#137A76"
  75. });
  76. //Dummy Arc for Circle
  77. var pathDummy = svg.append('path')
  78. .datum({
  79. endAngle: 0
  80. })
  81. .attr({
  82. d: arcDummy,
  83. fill: color[0]
  84. });
  85. var endCircle = svg.append('circle')
  86. .attr({
  87. r: 0,
  88. transform: 'translate(0,' + (-outerRadius + 7) + ')',
  89. stroke: color[0],
  90. 'stroke-width': 5,
  91. fill: color[2]
  92. });
  93. var middleTextCount = svg.append('text')
  94. .datum(0)
  95. .text(function(d) {
  96. return d + '%';
  97. })
  98. .attr({
  99. class: 'middleText',
  100. 'text-anchor': 'middle',
  101. dy: 0,
  102. dx: 75,
  103. fill: '#ec1561',
  104. 'font-size': '20px'
  105. });
  106. svg
  107. .data(data)
  108. .append("svg:image")
  109. .attr("y", 20)
  110. .attr("x", 50)
  111. .attr("width", 8)
  112. .attr("xlink:href", function(d) {
  113. if (d.variance < 0 ) {
  114. return "https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/Red_Arrow_Down.svg/2048px-Red_Arrow_Down.svg.png"
  115. } else {
  116. return "https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/Green_Arrow_Up.svg/1200px-Green_Arrow_Up.svg.png"
  117. }
  118. })
  119. svg
  120. .append("svg:image")
  121. .attr("y", -25)
  122. .attr("x", -25)
  123. .attr("width", 50)
  124. .attr("xlink:href", function(d) {
  125. return "https://cdn-icons-png.flaticon.com/512/2879/2879567.png"
  126. })
  127. //add icon
  128. svg
  129. .append("text")
  130. .attr("class", "title")
  131. .attr("y", 15)
  132. .attr("x", 50)
  133. .text("Positive Feedback")
  134. svg
  135. .data(data)
  136. .append("text")
  137. .attr("class", "varweek")
  138. .attr("y", 27)
  139. .attr("x", 62)
  140. .text(function(d) {
  141. return d.variance + '% vs last week';
  142. })
  143. var gradient = svg.append("svg:defs")
  144. .append("svg:linearGradient")
  145. .attr("id", "gradient")
  146. .attr("x1", "0%")
  147. .attr("y1", "0%")
  148. .attr("x2", "0%")
  149. .attr("y2", "100%")
  150. .attr("spreadMethod", "pad");
  151. gradient.append("svg:stop")
  152. .attr("offset", "0%")
  153. .attr("stop-color", "#DA0D91")
  154. .attr("stop-opacity", 0.05);
  155. gradient.append("svg:stop")
  156. .attr("offset", "100%")
  157. .attr("stop-color", "#DA0D91")
  158. .attr("stop-opacity", 1);
  159. var arcTweenOld = function(transition, percent, oldValue) {
  160. transition.attrTween("d", function(d) {
  161. var newAngle = (percent / 100) * (2 * Math.PI);
  162. var interpolate = d3.interpolate(d.endAngle, newAngle);
  163. var interpolateCount = d3.interpolate(oldValue, percent);
  164. return function(t) {
  165. d.endAngle = interpolate(t);
  166. var pathForegroundCircle = arcLine(d);
  167. middleTextCount.text(Math.floor(interpolateCount(t)) + '%');
  168. var pathDummyCircle = arcDummy(d);
  169. var coordinate = pathDummyCircle.split("L")[1].split("A")[0];
  170. endCircle.attr('transform', 'translate(' + coordinate + ')');
  171. return pathForegroundCircle;
  172. };
  173. });
  174. };
  175. var oldValue = 0;
  176. var animate = function() {
  177. pathForeground.transition()
  178. .duration(750)
  179. .ease('cubic')
  180. .call(arcTweenOld, percent, oldValue);
  181. oldValue = percent;
  182. //percent=(Math.random() * 60) + 20;
  183. // setTimeout(animate,3000);
  184. };
  185. setTimeout(animate, 0);
  1. @import url('https://fonts.googleapis.com/css2?family=Georama:ital@1&display=swap');
  2. body {
  3. background-color: #1B1F2A;
  4. width: 100%;
  5. font-family: 'Georama', sans-serif;
  6. height: 100%;
  7. }
  8. .widget {
  9. margin: 0 auto;
  10. width:350px;
  11. margin-top:50px;
  12. background-color: #222D3A;
  13. border-radius: 5px;
  14. box-shadow: 0px 0px 1px 0px #06060d;
  15. }
  16. .header{
  17. background-color: #29384D;
  18. height:40px;
  19. color:#929DAF;
  20. text-align: center;
  21. line-height: 40px;
  22. border-top-left-radius: 7px;
  23. border-top-right-radius: 7px;
  24. font-weight: 400;
  25. font-size: 1.5em;
  26. text-shadow: 1px 1px #06060d;
  27. }
  28. .chart-container{
  29. padding:25px;
  30. }
  31. .shadow {
  32. -webkit-filter: drop-shadow( 0px 3px 3px rgba(0,0,0,.1) );
  33. filter: drop-shadow( 0px 3px 3px rgba(0,0,0,.1) );
  34. }
  35. .title {
  36. font-family: 'Georama', sans-serif;
  37. fill: white;
  38. font-size: 11px;
  39. }
  40. .varweek
  41. {
  42. fill: white;
  43. font-size: 9px;
  44. opacity: 0.25
  45. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
  2. <div id="chart" class="chart-container">
  3. </div>
g0czyy6m

g0czyy6m1#

我想你应该看看方法的返回值
比如你写的

  1. var pathForeground = svg.append('path')
  2. .datum({
  3. endAngle: 0
  4. })
  5. .attr({
  6. d: arcLine,
  7. fill: "#137A76"
  8. });

字符串
我没有查它,但看起来.attr没有返回对象,所以现在pathForeground变成了null。您可以将其拆分到定义部分并设置属性部分。

  1. var pathForeground = svg.append('path');
  2. // new line
  3. pathForeground
  4. .datum({
  5. endAngle: 0
  6. })
  7. .attr({
  8. d: arcLine,
  9. fill: "#137A76"
  10. });


所有的财产。这就是导致你的错误。我没看更新日志。

展开查看全部

相关问题