MediaWiki:InteractivePlot.js

From Glioblastoma Treatments
Revision as of 20:00, 29 May 2024 by Lazy (talk | contribs) (Created page with "$(document).ready(function() { // Fetch the JSON data from the query result page fetch('https://glioblastomatreatments.wiki/index.php?title=Special:Ask/-5B-5BHas-20Usefulness-20Rating::%2B-5D-5D/-3FHas-20treatment-20name%3DTreatment-20Name/-3FHas-20Usefulness-20Rating%3DUsefulness-20Rating/-3FHas-20Toxicity-20Level%3DToxicity-20Level/-3FCategory/mainlabel%3D-2D/limit%3D100/offset%3D0/format%3Djson') .then(response => response.json()) .then(data => {...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
$(document).ready(function() {
    // Fetch the JSON data from the query result page
    fetch('https://glioblastomatreatments.wiki/index.php?title=Special:Ask/-5B-5BHas-20Usefulness-20Rating::%2B-5D-5D/-3FHas-20treatment-20name%3DTreatment-20Name/-3FHas-20Usefulness-20Rating%3DUsefulness-20Rating/-3FHas-20Toxicity-20Level%3DToxicity-20Level/-3FCategory/mainlabel%3D-2D/limit%3D100/offset%3D0/format%3Djson')
    .then(response => response.json())
    .then(data => {
        const treatments = [];
        for (const [key, value] of Object.entries(data.results)) {
            treatments.push({
                treatment: value.printouts['Treatment'][0].fulltext,
                effectiveness: parseInt(value.printouts['Effectiveness'][0].fulltext),
                toxicity: parseInt(value.printouts['Toxicity'][0].fulltext),
                category: value.printouts['Category'][0].fulltext.replace('Category:', '')
            });
        }

        const categories = [...new Set(treatments.map(t => t.category))];

        // Create checkboxes for categories
        const categoriesDiv = document.getElementById('categories');
        categories.forEach(category => {
            const label = document.createElement('label');
            const checkbox = document.createElement('input');
            checkbox.type = 'checkbox';
            checkbox.value = category;
            checkbox.checked = true;
            checkbox.addEventListener('change', updatePlot);
            label.appendChild(checkbox);
            label.appendChild(document.createTextNode(category));
            categoriesDiv.appendChild(label);
        });

        // Initial plot
        updatePlot();

        function updatePlot() {
            const selectedCategories = Array.from(document.querySelectorAll('#categories input[type="checkbox"]:checked')).map(cb => cb.value);

            const filteredTreatments = treatments.filter(t => selectedCategories.includes(t.category));

            const trace = {
                x: filteredTreatments.map(t => t.effectiveness),
                y: filteredTreatments.map(t => t.toxicity),
                mode: 'markers',
                type: 'scatter',
                text: filteredTreatments.map(t => t.treatment),
                marker: { color: filteredTreatments.map(t => categories.indexOf(t.category)) }
            };

            const layout = {
                title: 'Glioblastoma Treatments',
                xaxis: { title: 'Effectiveness' },
                yaxis: { title: 'Toxicity', autorange: 'reversed' }
            };

            Plotly.newPlot('plot', [trace], layout);
        }
    })
    .catch(error => console.error('Error fetching data:', error));
});