edaross

ChatGPT pie chart quiz 2

Feb 26th, 2023
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. D3.js Pie Chart with Button
  2.  
  3.  
  4. Edward Ross
  5. Can you please help me create a web page that displays a pie chart using D3.js to show the chemical composition of Ethanol with one of the labels as a question mark, and the answer to the question mark only appears when a button is clicked?
  6.  
  7. Certainly! Here's an example code that you can use as a starting point.
  8.  
  9. HTML Code:
  10.  
  11. html
  12. Copy code
  13. <!DOCTYPE html>
  14. <html>
  15. <head>
  16. <meta charset="utf-8">
  17. <title>Ethanol Chemical Composition</title>
  18. <script src="https://d3js.org/d3.v6.min.js"></script>
  19. <style>
  20. #chart {
  21. height: 400px;
  22. width: 400px;
  23. }
  24.  
  25. .hidden {
  26. display: none;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <h1>Ethanol Chemical Composition</h1>
  32. <div id="chart"></div>
  33. <button id="reveal">Reveal</button>
  34. <p id="answer" class="hidden">The question mark label represents oxygen (O).</p>
  35.  
  36. <script src="script.js"></script>
  37. </body>
  38. </html>
  39. In the HTML code above, we have included the D3.js library and created a container for the pie chart. We have also added a button that will reveal the answer to the question mark label when clicked. The answer is initially hidden using the .hidden class.
  40.  
  41. JavaScript Code (in script.js file):
  42.  
  43. javascript
  44. Copy code
  45. // Define the data
  46. const data = [
  47. { name: 'Carbon (C)', value: 2 },
  48. { name: 'Hydrogen (H)', value: 6 },
  49. { name: 'Question Mark (?)', value: 1 },
  50. { name: 'Oxygen (O)', value: 1 }
  51. ];
  52.  
  53. // Set up the SVG element and its dimensions
  54. const svg = d3.select('#chart')
  55. .append('svg')
  56. .attr('width', 400)
  57. .attr('height', 400);
  58.  
  59. // Set up the pie chart
  60. const pie = d3.pie()
  61. .value(d => d.value);
  62.  
  63. // Define the colors to use for each segment
  64. const colors = d3.scaleOrdinal()
  65. .domain(data.map(d => d.name))
  66. .range(d3.schemeSet2);
  67.  
  68. // Add the pie chart segments
  69. const segments = svg.selectAll('path')
  70. .data(pie(data))
  71. .enter()
  72. .append('path')
  73. .attr('d', d3.arc()
  74. .innerRadius(0)
  75. .outerRadius(150)
  76. )
  77. .attr('fill', d => colors(d.data.name));
  78.  
  79. // Add labels to the pie chart
  80. const labels = svg.selectAll('text')
  81. .data(pie(data))
  82. .enter()
  83. .append('text')
  84. .text(d => d.data.name)
  85. .attr('transform', d => `translate(${d3.arc().centroid(d)})`)
  86. .style('text-anchor', 'middle');
  87.  
  88. // Add event listener to reveal the answer
  89. const revealButton = document.getElementById('reveal');
  90. revealButton.addEventListener('click', () => {
  91. const answer = document.getElementById('answer');
  92. answer.classList.remove('hidden');
  93. });
  94. In the JavaScript code above, we define the data for the pie chart and set up the SVG element and its dimensions. We also set up the pie chart using D3's pie() function and define the colors to use for each segment.
  95.  
  96. We then add the pie chart segments and labels using D3's selectAll(), data(), and enter() methods. We use d3.arc() to define the size and shape of each segment, and d3.arc().centroid() to position each label at the center of its respective segment.
  97.  
  98. Finally, we add an event listener to the reveal button that removes the .hidden class from the answer paragraph when clicked.
  99.  
  100. You can customize this code to suit your needs, including changing the data to
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. ChatGPT Feb 13 Version. Free Research Preview. Our goal is to make AI systems more natural and safe to interact with. Your feedback will help us improve.
  108.  
Advertisement
Add Comment
Please, Sign In to add comment