Prompt Title: DB in HTML and JS

Created 1 year ago
Votes 0
Views 195 times
0

Prompt Detail:

To create a pie chart on your website based on data stored in an SQL database, you can use a charting library like Chart.js, which allows you to easily create and customize different types of charts including pie charts.

Here's a basic example of how to use Chart.js with PHP to create a pie chart:

  1. Connect to the database and retrieve the data you want to display in the pie chart:
php
// Connect to the database $conn = mysqli_connect('localhost', 'username', 'password', 'database_name'); // Query the database to retrieve the data you want to display in the pie chart $sql = "SELECT category, value FROM table_name"; $result = mysqli_query($conn, $sql); // Fetch the data $data = mysqli_fetch_all($result, MYSQLI_ASSOC); // Close the database connection mysqli_close($conn);
  1. Convert the data into a format that can be used by Chart.js. For a pie chart, you need an array of objects with two properties: "label" and "value".
php
// Convert the data into a format that can be used by Chart.js $chart_data = []; foreach ($data as $row) { $chart_data[] = [ 'label' => $row['category'], 'value' => $row['value'] ]; } // Convert the data into a JSON string $chart_data_json = json_encode($chart_data);
  1. Create an HTML canvas element where the pie chart will be displayed:
html
<canvas id="myChart"></canvas>
  1. Include the Chart.js library and create a JavaScript script to generate the pie chart using the data you retrieved from the database:
html
<!-- Include the Chart.js library --> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <!-- Create a JavaScript script to generate the pie chart --> <script> var chart_data = <?php echo $chart_data_json; ?>; var ctx = document.getElementById('myChart').getContext('2d'); var myChart = new Chart(ctx, { type: 'pie', data: { labels: chart_data.map(function(item) { return item.label; }), datasets: [{ data: chart_data.map(function(item) { return item.value; }), backgroundColor: [ 'rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)' ], borderColor: [ 'rgba(255, 99, 132, 1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)' ], borderWidth: 1 }] }, options: { responsive: true, legend: { position: 'right' } } }); </script>

In this example, Chart.js is used to create a pie chart with the data you retrieved from the database, and the chart is displayed in an HTML canvas element with the ID "myChart". The chart is customized using various options provided

avatar
JC.
Shared 1 prompt
Created 1 year ago

Leave a Comment

Related Tag Prompts

113
0
HTML & CSS Hero
1 year ago 2023-02-03 13:01:47 AIPRM
0
0
0
0
Html para Reac.JS
1 year ago 2023-03-09 23:26:32 Tomás Sarmento
0
0
HTML page's meta headings
1 year ago 2023-03-10 10:57:15 imen
0
0
html
1 year ago 2023-03-11 03:41:32 Cool Kiwi
0
0
0
0
Fix HTML Console Errors
1 year ago 2023-03-24 11:13:56 Vaibhav
0
0
HTML Programming: Hyperlinking Words
1 year ago 2023-03-29 19:26:43 Guillaume
0
0
Convert to HTML
1 year ago 2023-04-17 02:38:44 Remy
0
0
Ebook html
1 year ago 2023-04-21 05:07:11 darrat
0
0
Help with AssistanceHTML.
1 year ago 2023-04-22 05:05:32 Chittibabu
0
0
Переклад HTML на укр.
1 year ago 2023-04-26 11:55:10 kiddy007
0
0
HTML Assistance Provided.
11 months ago 2023-05-05 05:54:22 chatgpt
0
0
HTML Course Content.
9 months ago 2023-07-04 16:49:34 saksham
0
0
保存HTML中的SVG为图片
8 months ago 2023-07-31 03:00:46 Niko