A simple layout with a title, input fields for births and population, buttons for calculation and reset, a display for results, and a section for displaying calculation history.
CSS Styles:
Basic styling for the calculator’s appearance, including colors, transitions, and hover effects. The styles are applied for a dark theme with a toggle feature.
JavaScript Functionality:
Theme Toggle: Switches between light and dark modes.
Calculate CBR: Computes the crude birth rate based on user inputs and displays results.
Live Calculation: Updates the CBR in real-time as the user inputs data.
History Management: Stores calculation history and displays it dynamically.
Charting: Uses Chart.js to visualize the history of calculations.
File Download: Allows users to download their history as a text or CSV file.
Local Storage: Saves and loads history from the browser’s local storage to persist data across sessions.
Copy
<!DOCTYPEhtml><htmllang="en"><head> <metacharset="UTF-8"> <metaname="viewport"content="width=device-width, initial-scale=1.0"> <title>Advanced Crude Birth Rate Calculator</title> <style> /* Basic Styling */ body { font-family: Arial, sans-serif; background-color: #1e1f26; color: #fff; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; padding: 0 20px; flex-direction: column; text-align: center; transition: background-color 0.5s; } .calculator-container { background-color: #282a36; border-radius: 10px; box-shadow: 0px 0px 15px rgba(0,255,255,0.3); padding: 30px; width: 350px; margin-bottom: 20px; transition: transform 0.3s; } .calculator-container:hover { transform: scale(1.02); } h1 { font-size: 24px; color: #50fa7b; margin-bottom: 10px; text-shadow: 0px 0px 10px rgba(80,250,123,0.7); } .info-text { font-size: 14px; color: #bbb; margin-bottom: 20px; } label { font-size: 14px; color: #f8f8f2; display: block; margin-top: 10px; } input[type="number"] { width: 100%; padding: 8px; margin-top: 5px; font-size: 16px; border: 1px solid #44475a; border-radius: 4px; background-color: #44475a; color: #f8f8f2; text-align: center; transition: box-shadow 0.3s; } input[type="number"]:focus { box-shadow: 0px 0px 8px rgba(80,250,123,0.8); outline: none; } .btn { background-color: #ff79c6; color: #fff; border: none; padding: 10px; margin-top: 20px; font-size: 16px; cursor: pointer; border-radius: 4px; width: 100%; transition: box-shadow 0.3s, transform 0.2s; } .btn:hover { box-shadow: 0px 0px 10px rgba(255,121,198,0.7); transform: scale(1.05); } .result, .history, .download-section { margin-top: 20px; font-size: 18px; color: #50fa7b; text-shadow: 0px 0px 10px rgba(80,250,123,0.7); } .history-container { max-height: 150px; overflow-y: auto; margin-top: 10px; background-color: #3c3f4f; padding: 10px; border-radius: 5px; animation: fadeIn 0.5s; } .history-entry { font-size: 14px; color: #8be9fd; margin: 5px 0; } .tooltip { position: relative; display: inline-block; cursor: pointer; color: #ff79c6; font-weight: bold; } .tooltip .tooltiptext { visibility: hidden; width: 200px; background-color: #555; color: #fff; text-align: center; border-radius: 5px; padding: 5px; position: absolute; z-index: 1; bottom: 125%; left: 50%; margin-left: -100px; opacity: 0; transition: opacity 0.3s; } .tooltip:hover .tooltiptext { visibility: visible; opacity: 1; } /* Chart styling */ #myChart { margin-top: 20px; max-width: 400px; width: 100%; } /* Animations */ @keyframes fadeIn { from {opacity:0; } to {opacity:1; } } /* Theme Toggle */ .theme-toggle { margin-top: 20px; cursor: pointer; font-size: 16px; background-color: transparent; color: #ff79c6; border: none; padding: 5px; transition: transform 0.3s; } .theme-toggle:hover { transform: scale(1.05); } </style></head><body> <divclass="calculator-container"> <h1>Crude Birth Rate Calculator</h1> <pclass="info-text"> Calculate the Crude Birth Rate (CBR), which is the number of births per 1,000 people in a given population. <spanclass="tooltip">[?] <spanclass="tooltiptext">CBR = (Number of Births / Population) * 1000</span> </span> </p> <labelfor="births">Number of Births:</label> <inputtype="number"id="births"placeholder="Enter number of births"oninput="liveCalculateCBR()"> <labelfor="population">Population:</label> <inputtype="number"id="population"placeholder="Enter population"oninput="liveCalculateCBR()"> <buttonclass="btn"onclick="calculateCBR()">Calculate CBR</button> <buttonclass="btn"onclick="resetCalculator()">Reset</button> <divclass="result"id="result"></div> <h2class="history">Calculation History</h2> <divclass="history-container"id="history-container"> <!-- Calculation history will be shown here --> </div> <divclass="download-section"> <buttonclass="btn"onclick="downloadHistory()">Download History</button> <buttonclass="btn"onclick="exportToCSV()">Export to CSV</button> </div> <buttonclass="theme-toggle"onclick="toggleTheme()">Toggle Light/Dark Mode</button> <canvasid="myChart"></canvas> </div> <scriptsrc="https://cdn.jsdelivr.net/npm/chart.js"></script> <script> let historyEntries = []; // Array to store history entries let isDarkMode = true; // Track theme mode // Function to toggle theme function toggleTheme() { isDarkMode =!isDarkMode; document.body.style.backgroundColor = isDarkMode ?'#1e1f26':'#f0f0f0'; document.body.style.color = isDarkMode ?'#fff':'#000'; document.querySelector('.calculator-container').style.backgroundColor = isDarkMode ?'#282a36':'#ffffff'; document.querySelectorAll('input[type="number"]').forEach(input =>{ input.style.backgroundColor = isDarkMode ?'#44475a':'#e0e0e0'; input.style.color = isDarkMode ?'#f8f8f2':'#000';}); document.querySelectorAll('.btn').forEach(btn =>{ btn.style.backgroundColor = isDarkMode ?'#ff79c6':'#007bff';}); } // Function to calculate Crude Birth Rate (CBR) function calculateCBR() { const births = document.getElementById('births').value; const population = document.getElementById('population').value;if(births >0&& population >0){ const cbr=(births/population)*1000; const resultText=`Crude Birth Rate (CBR): ${cbr.toFixed(2)} per 1000 people`; document.getElementById('result').textContent = resultText; addHistoryEntry(births, population, cbr.toFixed(2)); updateChart(); saveHistoryToLocal(); } else { document.getElementById('result').textContent = 'Please enter valid numbers for births and population.'; } }// Live calculation on input function liveCalculateCBR() { const births = document.getElementById('births').value; const population = document.getElementById('population').value; const cbrDisplay = document.getElementById('result'); if (births > 0 && population > 0) { const cbr = (births / population) * 1000; cbrDisplay.textContent = `Estimated CBR: ${cbr.toFixed(2)} per 1000 people`; } else { cbrDisplay.textContent = ''; } }// Function to reset calculator function resetCalculator() { document.getElementById('births').value = ''; document.getElementById('population').value = ''; document.getElementById('result').textContent = ''; document.getElementById('history-container').innerHTML = ''; resetChart(); historyEntries = []; // Clear history saveHistoryToLocal(); }// Function to add entry to history function addHistoryEntry(births, population, cbr) { const entry = { births, population, cbr }; historyEntries.push(entry); const historyContainer = document.getElementById('history-container'); const entryDiv = document.createElement('div'); entryDiv.classList.add('history-entry'); entryDiv.textContent = `Births: ${births}, Population: ${population}, CBR: ${cbr}`; historyContainer.appendChild(entryDiv); }// Function to update chart function updateChart() { const ctx = document.getElementById('myChart').getContext('2d'); const labels = historyEntries.map(entry => `Births: ${entry.births}`); const data = historyEntries.map(entry => entry.cbr); new Chart(ctx, { type:'bar',data:{labels:labels,datasets:[{label:'Crude Birth Rate (CBR)',data:data,backgroundColor:'rgba(80, 250, 123, 0.6)',borderColor:'rgba(80, 250, 123, 1)',borderWidth:1}]},options:{scales:{y:{beginAtZero:true,title:{display:true,text:'CBR per 1000 people'}}}}}); } // Function to reset chart function resetChart() { const ctx = document.getElementById('myChart').getContext('2d'); ctx.clearRect(0,0, ctx.canvas.width, ctx.canvas.height); // Clear the chart } // Function to save history to local storage function saveHistoryToLocal() { localStorage.setItem('cbrHistory',JSON.stringify(historyEntries)); } // Function to download history as a text file function downloadHistory() { const blob =newBlob([JSON.stringify(historyEntries,null,2)],{type:'text/plain'}); const url =URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download ='cbr_history.txt'; document.body.appendChild(a); a.click(); document.body.removeChild(a);URL.revokeObjectURL(url); } // Function to export history as a CSV file function exportToCSV() { const csvRows =[]; csvRows.push(['Births','Population','CBR']); // Header historyEntries.forEach(entry =>{ csvRows.push([entry.births, entry.population, entry.cbr]);}); const csvString = csvRows.map(row => row.join(',')).join('\n'); const blob =newBlob([csvString],{type:'text/csv'}); const url =URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download ='cbr_history.csv'; document.body.appendChild(a); a.click(); document.body.removeChild(a);URL.revokeObjectURL(url); } // Load history from local storage on page load window.onload = function() { const storedHistory = localStorage.getItem('cbrHistory');if(storedHistory){ historyEntries= JSON.parse(storedHistory); historyEntries.forEach(entry=>addHistoryEntry(entry.births,entry.population,entry.cbr));updateChart();} }; </script></body></html>
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok