How to Calculate Steel Round Bar Weight

Steel Round Bar Weight Calculator & Guide :root { –primary-color: #004a99; –success-color: #28a745; –background-color: #f8f9fa; –text-color: #333; –border-color: #dee2e6; –card-background: #ffffff; –shadow: 0 4px 8px rgba(0, 0, 0, 0.1); } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: var(–background-color); color: var(–text-color); line-height: 1.6; margin: 0; padding: 20px; display: flex; flex-direction: column; align-items: center; } .container { width: 100%; max-width: 960px; background-color: var(–card-background); padding: 30px; border-radius: 8px; box-shadow: var(–shadow); margin-bottom: 40px; } h1, h2, h3 { color: var(–primary-color); text-align: center; margin-bottom: 20px; } h1 { font-size: 2.5em; } h2 { font-size: 1.8em; border-bottom: 2px solid var(–primary-color); padding-bottom: 10px; margin-top: 30px; } h3 { font-size: 1.4em; margin-top: 25px; } .calculator-section { background-color: var(–card-background); padding: 30px; border-radius: 8px; box-shadow: var(–shadow); margin-bottom: 40px; } .input-group { margin-bottom: 20px; display: flex; flex-direction: column; } .input-group label { display: block; margin-bottom: 8px; font-weight: bold; color: var(–primary-color); } .input-group input, .input-group select { width: 100%; padding: 12px; border: 1px solid var(–border-color); border-radius: 4px; box-sizing: border-box; font-size: 1em; } .input-group .helper-text { font-size: 0.85em; color: #6c757d; margin-top: 5px; } .error-message { color: red; font-size: 0.8em; margin-top: 5px; display: none; } .button-group { display: flex; gap: 10px; margin-top: 25px; justify-content: center; flex-wrap: wrap; } button { padding: 12px 25px; border: none; border-radius: 5px; cursor: pointer; font-size: 1em; font-weight: bold; transition: background-color 0.3s ease; } .btn-calculate { background-color: var(–primary-color); color: white; } .btn-calculate:hover { background-color: #003366; } .btn-reset, .btn-copy { background-color: #6c757d; color: white; } .btn-reset:hover, .btn-copy:hover { background-color: #5a6268; } #results-container { margin-top: 30px; padding: 25px; border: 1px solid var(–border-color); border-radius: 8px; background-color: #e9ecef; text-align: center; } #results-container h3 { margin-top: 0; color: var(–primary-color); } .main-result { font-size: 2.2em; font-weight: bold; color: var(–success-color); margin: 15px 0; display: inline-block; padding: 10px 20px; background-color: #ffffff; border-radius: 5px; box-shadow: inset 0 0 5px rgba(0,0,0,0.1); } .intermediate-results { display: flex; justify-content: space-around; flex-wrap: wrap; gap: 20px; margin-top: 20px; padding-top: 20px; border-top: 1px dashed var(–border-color); } .intermediate-value { text-align: center; flex: 1; min-width: 150px; } .intermediate-value strong { display: block; font-size: 1.3em; color: var(–primary-color); } .intermediate-value span { font-size: 0.9em; color: #6c757d; } .formula-explanation { margin-top: 20px; font-size: 0.95em; color: #555; text-align: left; } canvas { max-width: 100%; height: auto; margin-top: 30px; border: 1px solid var(–border-color); border-radius: 4px; } table { width: 100%; border-collapse: collapse; margin-top: 30px; box-shadow: var(–shadow); } th, td { padding: 12px 15px; text-align: left; border: 1px solid var(–border-color); } thead { background-color: var(–primary-color); color: white; } tbody tr:nth-child(even) { background-color: #f2f2f2; } caption { caption-side: top; font-weight: bold; margin-bottom: 10px; color: var(–text-color); font-size: 1.1em; text-align: left; } .article-content { background-color: var(–card-background); padding: 30px; border-radius: 8px; box-shadow: var(–shadow); margin-top: 40px; } .article-content p, .article-content ul, .article-content ol, .article-content li { margin-bottom: 15px; } .article-content h2, .article-content h3 { text-align: left; margin-top: 30px; } .article-content h2 { border-bottom: 2px solid var(–primary-color); padding-bottom: 5px; } .article-content code { background-color: #e9ecef; padding: 2px 5px; border-radius: 3px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; } .article-content table { box-shadow: none; } .article-content th, .article-content td { background-color: transparent; border-color: var(–border-color); } .article-content thead th { background-color: var(–primary-color); color: white; } .article-content tbody tr:nth-child(even) { background-color: #f2f2f2; } a { color: var(–primary-color); text-decoration: none; } a:hover { text-decoration: underline; } .faq-item { margin-bottom: 15px; } .faq-item strong { display: block; color: var(–primary-color); margin-bottom: 5px; } .related-tools ul { list-style: none; padding: 0; } .related-tools li { margin-bottom: 10px; } .related-tools span { font-size: 0.9em; color: #6c757d; display: block; margin-top: 3px; } @media (max-width: 768px) { .container, .calculator-section, .article-content { padding: 20px; } h1 { font-size: 1.8em; } h2 { font-size: 1.5em; } .intermediate-results { flex-direction: column; align-items: center; } .intermediate-value { margin-bottom: 15px; } .button-group { flex-direction: column; align-items: center; } button { width: 80%; } }

How to Calculate Steel Round Bar Weight

Accurately determine the weight of steel round bars for your projects using our comprehensive calculator and guide.

Steel Round Bar Weight Calculator

Enter the diameter of the round bar in millimeters (mm).
Enter the length of the round bar in millimeters (mm).
Standard density for steel is 7850 kg/m³. Adjust if necessary.

Calculation Results

–.– kg
Total Weight
–.– m³ Volume
–.– mm² Cross-Sectional Area
–.– kg/m Weight per Meter
Formula Used:
Weight = Volume × Density
Volume = (π × Diameter² / 4) × Length (in meters)
Weight = (π × (Diameter/1000)² / 4) × (Length/1000) × Density
Simplified: Weight (kg) = (π × Diameter² × Length × Density) / (4 × 1000³)
Where Diameter and Length are in mm, Density is in kg/m³.

Weight vs. Length for Different Diameters

Diameter 1 (mm) Diameter 2 (mm)
Typical Steel Round Bar Weights per Meter
Diameter (mm) Weight per Meter (kg/m) Density Used (kg/m³)
10 0.616 7850
12 0.887 7850
16 1.578 7850
20 2.463 7850
25 3.848 7850

{primary_keyword}

The process of determining how to calculate steel round bar weight is fundamental in structural engineering, fabrication, construction, and manufacturing. Steel round bars, also known as rebar when used in concrete reinforcement or plain round bars for mechanical applications, have a consistent circular cross-section. Their weight is a critical factor for logistics, material cost estimation, structural load calculations, and ensuring material integrity. Understanding how to calculate steel round bar weight accurately allows professionals and DIY enthusiasts alike to manage resources effectively and avoid costly errors in project planning and execution. This guide will break down the process, provide a reliable calculator, and offer insights into related aspects of steel bar usage.

Anyone involved in purchasing, fabricating, or specifying steel round bars needs to understand their weight. This includes:

  • Fabricators and Welders: For material handling, cutting, and structural design.
  • Construction Managers: For budgeting, procurement, and site logistics.
  • Engineers: For structural load calculations and material specifications.
  • Procurement Specialists: For accurate quoting and purchasing.
  • DIY Enthusiasts: For smaller projects involving metalworking or repairs.

A common misconception is that steel density is uniform across all types of steel. While 7850 kg/m³ is a widely accepted standard for carbon steel, alloy steels and stainless steels can have slightly different densities. Furthermore, assuming weight based on visual estimation alone can lead to significant inaccuracies. Precise calculation is always recommended for critical applications. Accurately calculating the weight of steel round bars relies on established physical principles and material properties.

{primary_keyword} Formula and Mathematical Explanation

The fundamental principle behind calculating the weight of any object with a uniform density is:

Weight = Volume × Density

For a steel round bar, we need to calculate its volume first. The volume of a cylinder (which a round bar is) is given by the area of its circular base multiplied by its length.

Volume = Cross-Sectional Area × Length

The cross-sectional area of a circle is calculated using the formula:

Area = π × (Radius)²

Since most measurements are given in diameter, and Radius = Diameter / 2, the formula becomes:

Area = π × (Diameter / 2)² = π × Diameter² / 4

Combining these, the volume of the round bar is:

Volume = (π × Diameter² / 4) × Length

Now, to get the weight, we multiply this volume by the density of steel. It's crucial to ensure consistent units. If diameter and length are in millimeters (mm), we need to convert them to meters (m) for density calculations (typically given in kg/m³).

  • 1 meter = 1000 millimeters
  • Diameter in meters = Diameter (mm) / 1000
  • Length in meters = Length (mm) / 1000

So, the volume in cubic meters (m³) is:

Volume (m³) = (π × (Diameter (mm) / 1000)² / 4) × (Length (mm) / 1000)

Volume (m³) = (π × Diameter (mm)² × Length (mm)) / (4 × 1000³)

Finally, the weight in kilograms (kg) is:

Weight (kg) = Volume (m³) × Density (kg/m³)

Weight (kg) = [ (π × Diameter (mm)² × Length (mm)) / (4 × 1,000,000,000) ] × Density (kg/m³)

This is the formula implemented in the calculator. The calculator simplifies the steps by first calculating the cross-sectional area in mm², then the volume in m³, and finally the total weight.

Variables Table for Steel Round Bar Weight Calculation

Variable Meaning Unit Typical Range/Value
Diameter (D) The diameter of the round steel bar. mm (millimeters) 1 mm to 1000 mm (or more)
Length (L) The length of the steel round bar. mm (millimeters) 1 mm to 12000 mm (or more)
Density (ρ) The mass per unit volume of the steel. kg/m³ (kilograms per cubic meter) ~7850 kg/m³ (for common carbon steel)
π (Pi) Mathematical constant, approximately 3.14159. Unitless 3.14159
Area (A) The cross-sectional area of the bar. mm² (square millimeters) Calculated
Volume (V) The total volume occupied by the bar. m³ (cubic meters) Calculated
Weight (W) The total mass of the steel bar. kg (kilograms) Calculated

Practical Examples (Real-World Use Cases)

Let's illustrate how to calculate steel round bar weight with practical scenarios.

Example 1: Reinforcing Bar for a Concrete Slab

A construction project requires a 12 mm diameter steel round bar that is 6 meters long for reinforcing a concrete slab. We need to estimate the weight for material handling and cost.

  • Input:
    • Bar Diameter: 12 mm
    • Bar Length: 6000 mm (6 meters)
    • Steel Density: 7850 kg/m³
  • Calculation using the calculator:
    • Cross-Sectional Area: π × (12²/4) ≈ 113.1 mm²
    • Volume: (113.1 mm² × 6000 mm) / 1,000,000,000 ≈ 0.0006786 m³
    • Total Weight: 0.0006786 m³ × 7850 kg/m³ ≈ 5.33 kg
  • Interpretation: Each 6-meter length of 12 mm rebar weighs approximately 5.33 kg. This weight is manageable for lifting and placement by workers. Procuring the required quantity involves multiplying this unit weight by the total number of bars needed.

Example 2: Machining Stock for a Shaft

A machine shop needs a piece of 40 mm diameter steel round bar, 1.5 meters long, to be machined into a shaft. They need to know the weight for transportation and inventory.

  • Input:
    • Bar Diameter: 40 mm
    • Bar Length: 1500 mm (1.5 meters)
    • Steel Density: 7850 kg/m³
  • Calculation using the calculator:
    • Cross-Sectional Area: π × (40²/4) ≈ 1256.6 mm²
    • Volume: (1256.6 mm² × 1500 mm) / 1,000,000,000 ≈ 0.001885 m³
    • Total Weight: 0.001885 m³ × 7850 kg/m³ ≈ 14.80 kg
  • Interpretation: A 1.5-meter length of 40 mm steel round bar weighs about 14.80 kg. This helps in planning the lifting equipment required and accurately billing the customer for the raw material used.

How to Use This Steel Round Bar Weight Calculator

Using our Steel Round Bar Weight Calculator is straightforward and designed for speed and accuracy.

  1. Input Diameter: Enter the diameter of your steel round bar in millimeters (mm) into the "Bar Diameter" field. For example, if you have a 3/4 inch bar, convert it to mm (0.75 inches * 25.4 mm/inch ≈ 19.05 mm).
  2. Input Length: Enter the total length of the bar in millimeters (mm) into the "Bar Length" field. If your bar is measured in meters, multiply by 1000 (e.g., 5 meters = 5000 mm).
  3. Verify Steel Density: The calculator defaults to a standard steel density of 7850 kg/m³. This is suitable for most common carbon steels. If you are working with a specific alloy that has a known different density, you can update this value.
  4. Click Calculate: Press the "Calculate Weight" button.

Reading the Results:

  • Total Weight: This is your primary result, displayed prominently in kilograms (kg). It represents the total mass of the steel bar based on your inputs.
  • Volume: Shows the total volume of the bar in cubic meters (m³).
  • Cross-Sectional Area: Displays the area of the circular face of the bar in square millimeters (mm²).
  • Weight per Meter: Indicates the weight of the bar for every meter of its length in kg/m.

Decision-Making Guidance:

  • Use the calculated weight to estimate material costs accurately.
  • Plan for transportation and lifting requirements based on the total weight.
  • Cross-reference the results with supplier specifications to ensure you are ordering the correct material.
  • The "Weight per Meter" is particularly useful for quickly estimating the weight of bars of different lengths.

Don't forget to use the "Reset" button to clear your inputs and start fresh, and the "Copy Results" button to easily transfer the key figures to your documents or spreadsheets.

Key Factors That Affect Steel Round Bar Weight Results

While the calculation itself is straightforward physics, several factors can influence the perceived or actual weight of steel round bars:

  1. Steel Density Variations: As mentioned, different steel alloys (e.g., stainless steel, high-alloy steels) have slightly different densities than standard carbon steel. Using an incorrect density value will directly impact the calculated weight. Always confirm the material specification.
  2. Dimensional Tolerances: Steel bars are manufactured within specified tolerance limits for diameter and length. Minor variations from the nominal dimensions can lead to slight deviations in actual weight compared to theoretical calculations. Reputable suppliers provide details on these tolerances.
  3. Surface Finish and Coatings: Some steel bars may have coatings (like galvanization or paint) or specific surface treatments. These add a small amount of weight. For most structural and mechanical applications, this added weight is negligible compared to the steel's mass.
  4. Temperature Effects: While not typically a practical concern for weight calculations, extreme temperature fluctuations can cause steel to expand or contract, slightly altering its dimensions and thus its volume and weight per unit length. This is usually accounted for in precision engineering applications under varying thermal conditions.
  5. Measurement Accuracy: The precision of your input measurements (diameter and length) directly affects the accuracy of the final weight calculation. Using a reliable measuring tool and taking measurements carefully is essential. Double-check conversions if using imperial units initially.
  6. Units Consistency: A common pitfall is mixing units (e.g., using diameter in inches and length in meters, or density in lbs/ft³). Always ensure all inputs are converted to a consistent set of units (like mm for dimensions and kg/m³ for density) before calculation, as our calculator does internally.

Frequently Asked Questions (FAQ)

Q1: What is the standard density of steel used for weight calculations?

A: The most commonly used density for steel in calculations is 7850 kg/m³. This value is appropriate for most carbon steels and mild steels.

Q2: Does the shape of the steel affect its weight calculation?

A: Yes, the shape is critical. This calculator is specifically for round bars (cylinders). The formula for weight calculation differs for other shapes like flat bars, angle irons, or tubes, as their volume calculation changes.

Q3: Can I use this calculator for metric and imperial measurements?

A: The calculator requires inputs in millimeters (mm) for diameter and length. If you have measurements in inches or feet, you must convert them to millimeters first (1 inch = 25.4 mm, 1 foot = 304.8 mm). The output is in kilograms (kg).

Q4: What if my steel is not exactly 7850 kg/m³?

A: If you know the specific density of your steel alloy (e.g., stainless steel might be around 8000 kg/m³), you can input that value into the "Steel Density" field for a more accurate result.

Q5: How accurate is the calculated weight?

A: The accuracy depends on the precision of your input measurements and the exact density of the steel. The formula itself is precise for a perfect cylinder. Real-world variations (tolerances, surface finish) might cause slight deviations.

Q6: Is the weight calculated by volume or by length?

A: The calculation is fundamentally based on volume (calculated from diameter and length) multiplied by density. The "Weight per Meter" result is derived from this by calculating the weight for a 1-meter length.

Q7: Can I calculate the weight of a hollow steel tube?

A: No, this calculator is designed for solid round bars only. Calculating the weight of a hollow tube requires subtracting the volume of the inner hollow space from the volume of the outer cylinder.

Q8: What is the difference between weight and mass?

A: In common usage, "weight" (measured in kg or lbs) often refers to mass. Technically, weight is the force of gravity on an object (measured in Newtons). This calculator provides the mass of the steel bar in kilograms.

var PI = Math.PI; function isValidNumber(value) { return !isNaN(parseFloat(value)) && isFinite(value); } function validateInput(id, errorId, min, max) { var inputElement = document.getElementById(id); var errorElement = document.getElementById(errorId); var value = parseFloat(inputElement.value); if (inputElement.value.trim() === "") { errorElement.textContent = "This field cannot be empty."; errorElement.style.display = "block"; return false; } else if (!isValidNumber(value)) { errorElement.textContent = "Please enter a valid number."; errorElement.style.display = "block"; return false; } else if (value max) { errorElement.textContent = "Value cannot exceed " + max + "."; errorElement.style.display = "block"; return false; } else { errorElement.textContent = ""; errorElement.style.display = "none"; return true; } } function calculateWeight() { var diameterInput = document.getElementById('barDiameter'); var lengthInput = document.getElementById('barLength'); var densityInput = document.getElementById('steelDensity'); var diameterError = document.getElementById('diameterError'); var lengthError = document.getElementById('lengthError'); var densityError = document.getElementById('densityError'); var diameterValid = validateInput('barDiameter', 'diameterError', 0, 5000); // Max diameter 5m var lengthValid = validateInput('barLength', 'lengthError', 0, 20000); // Max length 20m var densityValid = validateInput('steelDensity', 'densityError', 1000, 15000); // Realistic density range if (!diameterValid || !lengthValid || !densityValid) { document.getElementById('totalWeight').textContent = "–.– kg"; document.getElementById('barVolume').textContent = "–.– m³"; document.getElementById('crossSectionalArea').textContent = "–.– mm²"; document.getElementById('weightPerMeter').textContent = "–.– kg/m"; updateChart(); // Clear or reset chart on error return; } var diameterMM = parseFloat(diameterInput.value); var lengthMM = parseFloat(lengthInput.value); var density = parseFloat(densityInput.value); // Calculations var radiusMM = diameterMM / 2; var areaMM2 = PI * radiusMM * radiusMM; var volumeM3 = (areaMM2 * lengthMM) / 1000000000.0; // Convert mm³ to m³ var totalWeightKG = volumeM3 * density; var weightPerMeterKG = (areaMM2 / 1000000.0) * density; // Area in m² * density // Format results var formattedWeight = totalWeightKG.toFixed(2); var formattedVolume = volumeM3.toFixed(6); var formattedArea = areaMM2.toFixed(2); var formattedWeightPerMeter = weightPerMeterKG.toFixed(3); // Display results document.getElementById('totalWeight').textContent = formattedWeight + " kg"; document.getElementById('barVolume').textContent = formattedVolume + " m³"; document.getElementById('crossSectionalArea').textContent = formattedArea + " mm²"; document.getElementById('weightPerMeter').textContent = formattedWeightPerMeter + " kg/m"; // Update table density if changed var tableDensities = document.querySelectorAll('#weightTableBody td[id^="tableDensity"]'); for (var i = 0; i < tableDensities.length; i++) { tableDensities[i].textContent = density.toFixed(0); } updateChart(); } function resetCalculator() { document.getElementById('barDiameter').value = "20"; document.getElementById('barLength').value = "1000"; document.getElementById('steelDensity').value = "7850"; document.getElementById('diameterError').textContent = ""; document.getElementById('diameterError').style.display = "none"; document.getElementById('lengthError').textContent = ""; document.getElementById('lengthError').style.display = "none"; document.getElementById('densityError').textContent = ""; document.getElementById('densityError').style.display = "none"; // Reset results document.getElementById('totalWeight').textContent = "–.– kg"; document.getElementById('barVolume').textContent = "–.– m³"; document.getElementById('crossSectionalArea').textContent = "–.– mm²"; document.getElementById('weightPerMeter').textContent = "–.– kg/m"; // Reset table densities var tableDensities = document.querySelectorAll('#weightTableBody td[id^="tableDensity"]'); for (var i = 0; i 5) ? diameter1 : 10; // Use input or default to 10mm var diameterB = (diameter1 && diameter1 > 5 && diameter1 10) ? diameter1 * 1.5 : 30; // Use 1.5x input or default to 30mm var dataSeriesA = []; var dataSeriesB = []; var dataSeriesC = []; var step = maxChartLength / 20; // Generate 20 data points if (step < 10) step = 10; // Minimum step size for (var l = step; l 5) ? diameter1 : 10; var diameterB = (diameter1 && diameter1 > 5 && diameter1 10) ? diameter1 * 1.5 : 30; var dataA = []; var dataB = []; var dataC = []; var step = maxChartLength / 20; if (step < 10) step = 10; // min step for (var l = step; l maxYValue) maxYValue = weightA; var areaB = (PI * Math.pow(diameterB / 2, 2)); var weightB = (areaB * l) / 1000000000.0 * density * 1000; dataB.push({ x: l / 1000, y: weightB }); if (weightB > maxYValue) maxYValue = weightB; var areaC = (PI * Math.pow(diameterC / 2, 2)); var weightC = (areaC * l) / 1000000000.0 * density * 1000; dataC.push({ x: l / 1000, y: weightC }); if (weightC > maxYValue) maxYValue = weightC; } // Adjust maxYValue for better visualization maxYValue *= 1.1; if (maxYValue === 0) maxYValue = 100; // Default if no data // Chart parameters var padding = 50; var chartAreaWidth = canvas.width – 2 * padding; var chartAreaHeight = canvas.height – 2 * padding; var xAxisY = canvas.height – padding; var yAxisX = padding; // — Draw Axes — ctx.strokeStyle = '#ccc'; ctx.lineWidth = 1; // X-axis ctx.beginPath(); ctx.moveTo(yAxisX, xAxisY); ctx.lineTo(canvas.width – padding, xAxisY); ctx.stroke(); // Y-axis ctx.beginPath(); ctx.moveTo(yAxisX, padding); ctx.lineTo(yAxisX, xAxisY); ctx.stroke(); // — Draw Labels and Ticks — ctx.fillStyle = '#333′; ctx.font = '12px Arial'; ctx.textAlign = 'center'; // X-axis labels (length in meters) var numXTicks = 5; for (var i = 0; i <= numXTicks; i++) { var xPos = yAxisX + (chartAreaWidth / numXTicks) * i; var labelValue = (maxChartLength / numXTicks / 1000) * i; ctx.fillText(labelValue.toFixed(1) + ' m', xPos, xAxisY + 20); ctx.beginPath(); ctx.moveTo(xPos, xAxisY); ctx.lineTo(xPos, xAxisY – 5); ctx.stroke(); } // Y-axis labels (weight in kg) ctx.textAlign = 'right'; var numYTicks = 5; for (var i = 0; i <= numYTicks; i++) { var yPos = xAxisY – (chartAreaHeight / numYTicks) * i; var labelValue = (maxYValue / numYTicks) * i; ctx.fillText(labelValue.toFixed(0) + ' kg', yAxisX – 10, yPos + 5); ctx.beginPath(); ctx.moveTo(yAxisX, yPos); ctx.lineTo(yAxisX + 5, yPos); ctx.stroke(); } // Title for Y-axis ctx.save(); ctx.translate(padding / 2, canvas.height / 2); ctx.rotate(-Math.PI / 2); ctx.textAlign = 'center'; ctx.fillText('Weight (kg)', 0, 0); ctx.restore(); // Title for X-axis ctx.fillText('Length (meters)', canvas.width / 2, canvas.height – padding / 2 + 10); // — Draw Data Series — function drawLine(data, color) { ctx.strokeStyle = color; ctx.lineWidth = 2; ctx.beginPath(); var firstPoint = true; for (var i = 0; i < data.length; i++) { var xPos = yAxisX + (data[i].x / (maxChartLength / 1000)) * chartAreaWidth; var yPos = xAxisY – (data[i].y / maxYValue) * chartAreaHeight; if (isNaN(xPos) || isNaN(yPos)) continue; if (firstPoint) { ctx.moveTo(xPos, yPos); firstPoint = false; } else { ctx.lineTo(xPos, yPos); } } ctx.stroke(); } drawLine(dataA, '#004a99'); drawLine(dataB, '#28a745'); drawLine(dataC, '#ffc107'); // Legend (simple text for now) var legendHTML = '
'; legendHTML += ' Diameter ' + diameterA.toFixed(1) + ' mm'; legendHTML += ' Diameter ' + diameterB.toFixed(1) + ' mm'; legendHTML += ' Diameter ' + diameterC.toFixed(1) + ' mm'; legendHTML += '
'; var chartContainer = canvas.parentElement; var existingLegend = chartContainer.querySelector('.legend'); if (existingLegend) { existingLegend.innerHTML = legendHTML; } else { canvas.insertAdjacentHTML('afterend', legendHTML); } } // Call updateChart whenever an input changes document.getElementById('barDiameter').addEventListener('input', updateChart); document.getElementById('barLength').addEventListener('input', updateChart); document.getElementById('steelDensity').addEventListener('input', updateChart); // Initial chart render after DOM is ready document.addEventListener('DOMContentLoaded', updateChart);

Leave a Comment