Treatments Plot: Difference between revisions

From Glioblastoma Treatments
Jump to navigationJump to search
(Created page with "== Glioblastoma Treatments == <div id="categories"></div> <div id="plot"></div> <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> <script> // Fetch the JSON data from the query result fetch('/index.php?title=Special:Ask&format=json&q=Category:Treatments|%3FHas+treatment+name=Treatment|%3FHas+Effectiveness=Effectiveness|%3FHas+Toxicity=Toxicity|%3FHas+Category=Category|format=json|link=none|headers=hide|mainlabel=-') .then(response => resp...")
 
Tag: Replaced
Line 1: Line 1:
== Glioblastoma Treatments ==
{{#ask: [[Category:Treatments]]
 
|?Has treatment name=Drug Name
<div id="categories"></div>
|?Has Usefulness Rating=Effectiveness
<div id="plot"></div>
|?Has Toxicity Level=Toxicity
 
|?Has Category=Category
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
|format=json
<script>
|link=none
    // Fetch the JSON data from the query result
|headers=hide
    fetch('/index.php?title=Special:Ask&format=json&q=[[Category:Treatments]]|%3FHas+treatment+name=Treatment|%3FHas+Effectiveness=Effectiveness|%3FHas+Toxicity=Toxicity|%3FHas+Category=Category|format=json|link=none|headers=hide|mainlabel=-')
|mainlabel=-
    .then(response => response.json())
}}
    .then(data => {
        const treatments = data.results.map(item => ({
            treatment: item.printouts['Treatment'][0],
            effectiveness: parseInt(item.printouts['Effectiveness'][0]),
            toxicity: parseInt(item.printouts['Toxicity'][0]),
            category: item.printouts['Category'][0]
        }));
 
        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);
        }
    });
</script>

Revision as of 16:09, 29 May 2024