You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中基于HTML实现学生成绩交互式绘图的技术方案咨询

Hey there! Let me share a complete, working example to build this student grade visualization feature using vanilla HTML, JavaScript, and Chart.js (a lightweight, easy-to-use library for charts). This setup will let you display a student list, pop up a subject selector on click, and generate a time-series chart for their selected subject's grades.

Step 1: Basic HTML Structure

First, we'll set up the core page layout—student list, a modal for subject selection, and a container for the chart:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Student Grade Tracker</title>
    <!-- Include Chart.js via CDN (you can also host it locally) -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        .student-list {
            list-style: none;
            padding: 0;
            margin: 20px 0;
        }
        .student-item {
            padding: 10px;
            background: #f0f0f0;
            margin: 5px 0;
            cursor: pointer;
            border-radius: 4px;
            transition: background 0.2s;
        }
        .student-item:hover {
            background: #e0e0e0;
        }
        .modal {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(0,0,0,0.2);
            z-index: 1000;
        }
        .chart-container {
            width: 80%;
            margin: 20px auto;
            height: 400px;
        }
    </style>
</head>
<body>
    <h1>Student Grade List</h1>
    <ul class="student-list" id="studentList">
        <!-- Student names will populate via JavaScript -->
    </ul>

    <!-- Subject Selection Modal -->
    <div class="modal" id="subjectModal">
        <h3>Choose a Subject</h3>
        <select id="subjectSelect"></select>
        <button onclick="generateChart()">View Grades</button>
        <button onclick="closeModal()">Cancel</button>
    </div>

    <!-- Chart Display Container -->
    <div class="chart-container">
        <canvas id="gradeChart"></canvas>
    </div>

    <script>
        // JavaScript logic will go here
    </script>
</body>
</html>

Step 2: Sample Data Setup

Inside the <script> tag, define sample grade data to mimic your dataset. You can replace this with real data from a backend later:

// Sample student grade data (replace with your actual data)
const studentGrades = {
    "Peter W": {
        maths: [
            { date: "2024-01", score: 85 },
            { date: "2024-02", score: 88 },
            { date: "2024-03", score: 92 },
            { date: "2024-04", score: 89 }
        ],
        sciences: [
            { date: "2024-01", score: 78 },
            { date: "2024-02", score: 82 },
            { date: "2024-03", score: 80 },
            { date: "2024-04", score: 85 }
        ]
    },
    "Andreas Q": {
        maths: [
            { date: "2024-01", score: 75 },
            { date: "2024-02", score: 79 },
            { date: "2024-03", score: 83 },
            { date: "2024-04", score: 81 }
        ],
        sciences: [
            { date: "2024-01", score: 90 },
            { date: "2024-02", score: 88 },
            { date: "2024-03", score: 92 },
            { date: "2024-04", score: 95 }
        ]
    },
    "Lisa W": {
        maths: [
            { date: "2024-01", score: 90 },
            { date: "2024-02", score: 93 },
            { date: "2024-03", score: 95 },
            { date: "2024-04", score: 94 }
        ],
        sciences: [
            { date: "2024-01", score: 85 },
            { date: "2024-02", score: 87 },
            { date: "2024-03", score: 89 },
            { date: "2024-04", score: 91 }
        ]
    },
    "Renate S": {
        maths: [
            { date: "2024-01", score: 82 },
            { date: "2024-02", score: 80 },
            { date: "2024-03", score: 84 },
            { date: "2024-04", score: 86 }
        ],
        sciences: [
            { date: "2024-01", score: 72 },
            { date: "2024-02", score: 76 },
            { date: "2024-03", score: 79 },
            { date: "2024-04", score: 83 }
        ]
    }
};

// Track the currently selected student
let selectedStudent = "";

Step 3: Add Interactive Logic

Now add the functions to populate the student list, open the subject modal, and generate the chart:

// Populate student list when page loads
window.onload = function() {
    const studentList = document.getElementById("studentList");
    Object.keys(studentGrades).forEach(student => {
        const listItem = document.createElement("li");
        listItem.className = "student-item";
        listItem.textContent = student;
        listItem.onclick = () => openSubjectModal(student);
        studentList.appendChild(listItem);
    });
};

// Open subject modal and populate options for the selected student
function openSubjectModal(student) {
    selectedStudent = student;
    const subjectSelect = document.getElementById("subjectSelect");
    // Clear existing options
    subjectSelect.innerHTML = "";
    // Add subjects for the selected student
    Object.keys(studentGrades[student]).forEach(subject => {
        const option = document.createElement("option");
        option.value = subject;
        option.textContent = subject.charAt(0).toUpperCase() + subject.slice(1);
        subjectSelect.appendChild(option);
    });
    // Show the modal
    document.getElementById("subjectModal").style.display = "block";
}

// Close the subject modal
function closeModal() {
    document.getElementById("subjectModal").style.display = "none";
}

// Generate the time-series grade chart
function generateChart() {
    const selectedSubject = document.getElementById("subjectSelect").value;
    const grades = studentGrades[selectedStudent][selectedSubject];
    
    // Extract dates and scores for the chart
    const dates = grades.map(item => item.date);
    const scores = grades.map(item => item.score);
    
    // Destroy existing chart if it exists to avoid duplicates
    if (window.gradeChartInstance) {
        window.gradeChartInstance.destroy();
    }
    
    // Create new line chart
    const ctx = document.getElementById("gradeChart").getContext("2d");
    window.gradeChartInstance = new Chart(ctx, {
        type: "line", // Perfect for showing trends over time
        data: {
            labels: dates,
            datasets: [{
                label: `${selectedStudent}'s ${selectedSubject.charAt(0).toUpperCase() + selectedSubject.slice(1)} Grades`,
                data: scores,
                borderColor: "rgb(75, 192, 192)",
                tension: 0.1,
                fill: false
            }]
        },
        options: {
            scales: {
                y: {
                    beginAtZero: false,
                    min: 0,
                    max: 100,
                    title: {
                        display: true,
                        text: "Score"
                    }
                },
                x: {
                    title: {
                        display: true,
                        text: "Date"
                    }
                }
            }
        }
    });
    
    // Close modal after chart is generated
    closeModal();
}

Customization & Enhancement Tips

  • Data Integration: Replace the studentGrades object with data fetched from a backend using fetch() or a library like Axios if you're storing grades in a database.
  • Chart Styles: Swap type: "line" with bar or radar if you prefer different visualization styles—Chart.js supports all these natively.
  • Validation: Add checks to handle edge cases, like a student having no grades for a subject or incomplete date/score data.
  • UI Tweaks: Adjust the CSS to match your website's design—modify colors, spacing, or modal appearance as needed.

This example works right out of the box—save it as an HTML file and open it in any browser to test the functionality.

内容的提问来源于stack exchange,提问作者momo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:14:52