Skip to content

Commit

Permalink
Merge pull request #340 from alercebroker/fix/various-arrangements
Browse files Browse the repository at this point in the history
Fix various arrangements
  • Loading branch information
Demurest authored Nov 11, 2024
2 parents 7cce28b + d7eab3b commit a5da65b
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 41 deletions.
70 changes: 70 additions & 0 deletions lightcurve/src/probability_api/static/customToolTip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
export function customToolTip(context) {
let tooltipEl = document.getElementById('chartjs-tooltip');

if(!tooltipEl){
tooltipEl = document.createElement('div')
tooltipEl.id = 'chartjs-tooltip';
tooltipEl.innerHTML = '<table></table>';
document.body.appendChild(tooltipEl);
}

let tooltipModel = context.tooltip;

if(tooltipModel.opacity === 0){
tooltipEl.style.opacity = 0;
return;
}

tooltipEl.classList.remove('above', 'below', 'no-transform');
if(tooltipModel.yAlign){
tooltipEl.classList.add(tooltipModel.yAlign);
} else {
tooltipEl.classList.add('no-transform')
}

function getBody(bodyItem) {
return bodyItem.lines;
}

// set Text
if(tooltipModel.body){
let titleLines = tooltipModel.title || [];
let bodyLines = tooltipModel.body.map(getBody);

let innerHtml = '<thead>';

titleLines.forEach(function(title) {
innerHtml += '<tr><th style="color: #fff; width: 140px; height: 12px;padding: 4px;">' + title + ' (score)' + '</th></tr>';
});

innerHtml += '</thead><tbody>';

/** invertir objecto */
innerHtml += '<tr><td style="color: #fff; height: 12px; max-width: 100%;">' + bodyLines[0][0] + '</td></tr>'

let style = ""
for (let index = bodyLines.length - 1; index >= 1; index--){
style = ' color: #fff;';
style = style + ' max-width: 100%;';
style = style + ' height: 12px;';
innerHtml += '<tr><td style="' + style + '">' + bodyLines[index][0] + '</td></tr>';
}

innerHtml += '</tbody>';

let tableRoot = tooltipEl.querySelector('table');
tableRoot.innerHTML = innerHtml;
}

let position = context.chart.canvas.getBoundingClientRect();
let bodyFont = Chart.helpers.toFont(tooltipModel.options.bodyFont);

// Display, position, and set styles for font
tooltipEl.style.opacity = 1;
tooltipEl.style.backgroundColor = 'rgba(50, 50, 50, 0.7)'
tooltipEl.style.position = 'absolute';
tooltipEl.style.left = position.left + window.scrollX + tooltipModel.caretX + 'px';
tooltipEl.style.bottom = (window.innerHeight - (position.top + window.scrollY + tooltipModel.caretY)) + 'px';
tooltipEl.style.font = bodyFont.string;
tooltipEl.style.pointerEvents = 'none';
}
74 changes: 37 additions & 37 deletions lightcurve/src/probability_api/static/probability.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { customToolTip } from "./customToolTip.js"

let probability_data_aux
let mychart

let data = {
labels: [],
Expand Down Expand Up @@ -58,32 +59,10 @@ let config = {
display: false
},
tooltip: {
enabled: true,
mode: 'index',
enabled: false,
mode: 'dataset',
position: 'nearest',
titleFont: {
weight: 'bold'
},
displayColors: false,
callbacks: {
title: function(){
return "Probabilities (score)"
},
label: function(context){
let tooltipText = [];
let length = context.dataset.data.length - 1
let data = context.dataset.data
let labels = context.chart.data.labels

tooltipText.push(`${labels[0]}: ${data[0]}`)

for (let index = length; index >= 1; index--){
tooltipText.push(`${labels[index]}: ${data[index]}`)
}

return tooltipText
}
}
external: customToolTip,
}
},

Expand All @@ -101,10 +80,11 @@ export function init(){

reverseData(raw_tax)


let mychart = new Chart(ctx, config);
probability_data_aux = []
mychart = new Chart(ctx, config);

updateMyChart(initial_value, raw_tax, raw_group_prob_dict)
updateMyChart(mychart, initial_value, raw_tax, raw_group_prob_dict)

custom_select.addEventListener('click', () => {
custom_select.querySelector('.select').classList.toggle('open');
Expand All @@ -117,7 +97,7 @@ export function init(){
option.classList.add('selected');
option.closest('.select').querySelector('.select__trigger span').textContent = option.textContent;

updateMyChart(option.getAttribute("data-value"), raw_tax, raw_group_prob_dict)
updateMyChart(mychart, option.getAttribute("data-value"), raw_tax, raw_group_prob_dict)
}
})
}
Expand Down Expand Up @@ -161,7 +141,7 @@ function getScale(max_value){
return (max_value/3)
}

function updateMyChart(classifier_name, raw_tax, raw_group_prob_dict){
function updateMyChart(mychart, classifier_name, raw_tax, raw_group_prob_dict){
let max_value
let classes_arr = raw_tax[classifier_name].classes
let classifier_data = Object.values(raw_group_prob_dict[classifier_name])[0]
Expand All @@ -170,13 +150,13 @@ function updateMyChart(classifier_name, raw_tax, raw_group_prob_dict){

max_value = maxValue(classifier_data)

removeDataChart()
updateDataChart(classes_arr, max_value)
removeDataChart(mychart)
updateDataChart(mychart, classes_arr, max_value)

isDark();
isDark(mychart);
}

function removeDataChart(){
function removeDataChart(mychart){
mychart.data.labels.length = 0;
mychart.data.datasets.forEach((dataset) => {
dataset.data.length = 0;
Expand All @@ -186,7 +166,7 @@ function removeDataChart(){
mychart.update();
}

function updateDataChart(labels, max_value){
function updateDataChart(mychart, labels, max_value){
mychart.data.labels.push(...labels);
mychart.data.datasets.forEach((dataset) => {
dataset.data.push(...probability_data_aux);
Expand All @@ -198,7 +178,7 @@ function updateDataChart(labels, max_value){
mychart.update();
}

function isDark(){
function isDark(mychart){
if(document.getElementById("probabilities-app").classList.contains("tw-dark")){
mychart.config.options.scales.r.backgroundColor = 'rgba(245, 245, 245, 0.2)'
mychart.config.options.scales.r.angleLines.color = '#F5F5F5'
Expand All @@ -207,4 +187,24 @@ function isDark(){

mychart.update();
}
}
}

export function elementReady(selector) {
return new Promise((resolve, reject) => {
const el = document.querySelector(selector);
if (el) {
resolve(el);
}

new MutationObserver((mutationRecords, observer) => {
Array.from(document.querySelectorAll(selector)).forEach(element => {
resolve(element);
observer.disconnect();
});
})
.observe(document.documentElement, {
childList: true,
subtree: true
});
});
}
24 changes: 20 additions & 4 deletions lightcurve/src/probability_api/templates/prob.html.jinja
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script id="htmx-script" src="https://unpkg.com/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script type="module" src="{{API_URL}}/static/probability.js"></script>
<link rel="stylesheet" href="{{API_URL}}/static/probability.css">

Expand Down Expand Up @@ -39,16 +38,33 @@

</div>

<div class="md:tw-w-[80%] md:tw-h-[80%] sm:tw-w-[100%] sm:tw-h-[100%] tw-mx-auto tw-relative">
<div class="md:tw-w-[365px] md:tw-h-[250px] sm:tw-w-[100%] sm:tw-h-[100%] tw-mx-auto tw-relative">
<canvas id="myChart"></canvas>
</div>
</div>
</div>



<script type="module">
import { init } from "{{API_URL}}/static/probability.js";
init();
import { init, elementReady } from "{{API_URL}}/static/probability.js";
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
loadScript("https://cdn.jsdelivr.net/npm/chart.js")
.then(() => elementReady('#probabilities-app'))
.then((el) => {
init();
})
.catch((error) => console.error('Failed to load Chart.js:', error));
</script>

<script id="probabilities-data" type="application/json">
Expand Down

0 comments on commit a5da65b

Please sign in to comment.