Published
Edited
Apr 21, 2021
Fork of D3Search
Insert cell
md`# D3Search`
Insert cell
Insert cell
Insert cell
Insert cell
dataconpos=fetch(`https://hdnlab1.isti.cnr.it/fuseki/commedia/sparql?query=${encodeURIComponent(completeq)}`, {headers: myauthHeaders}).then(response => {
if (!response.ok) throw new Error(response.status);
return response.json();
})
.then(e =>(e.results.bindings))
.then(f => f.map(x => Object.assign({},{Posizione:x.pos.value,Verso:x.sc.value, IRI_Verso:x.ver.value.replace('http://hdn.dantenetwork.it/resource/work/commedia/cantica/', 'hdn:'), IRI_Categoria: x.categ.value.replace('http://dantenetwork.it/resource/ontology/ORL/', 'orl:'), IRI_Forma: x.form.value.replace('http://dantenetwork.it/resource/ontology/ORL/', 'orl:')})))
Insert cell
function mydataconpos(){
const mydata=dataconpos;
for (let i = 0; i < mydata.length; ++i) {
mydata[i]["x"]=100
mydata[i]["y"]=70*(i+1)
const myForma=mydata[i]["IRI_Forma"].slice(mydata[i]["IRI_Forma"].indexOf("_")+1);
if (!mydata[i]["Verso"].includes("tspan class="))
mydata[i]["Verso"]=mydata[i]["Verso"].replace(myForma, "<tspan class='formaf'>"+myForma+"</tspan>");
const tmpstr=mydata[i]['IRI_Verso'];
//myForma=myForma.slice(myForma.indexOf("_")+1);
if (tmpstr.slice(4, 5)=='1')
mydata[i]["rif"]="(Inferno, canto "+tmpstr.slice(12).replace('/verso/', ', verso ')+')';
if (tmpstr.slice(4, 5)=='2')
mydata[i]["rif"]="(Purgatorio, canto "+tmpstr.slice(12).replace('/verso/', ', verso ')+')';
if (tmpstr.slice(4, 5)=='3')
mydata[i]["rif"]="(Paradiso, canto "+tmpstr.slice(12).replace('/verso/', ', verso ')+')';
//<tspan class="highlight">
}
return mydata
}
Insert cell
showdata=mydataconpos()
Insert cell
svg_v1 = {
const svg = d3.select(DOM.svg(width, showdata.length*90));
// draw a number of text elements
svg.selectAll("text")
.data(showdata).enter()
.append("text")
.attr("id", (d, i) => `node${i}`)
.attr("x", d => d.x)
.attr("y", d => d.y)
.style("font-size", "1em")
.style("text-anchor", "start")
.html(d => d.Verso+' '+d.rif);
svg.selectAll("text")
.each(function(d, i) { wrap_text_nchar(d3.select(this), 40) });
return svg.node();
}
Insert cell
wrap_text_nchar = (text_element, max_width, line_height, unit = "em") => {
// use a default line_height if not provided
if (!line_height) line_height = 1.1;
// wrap the text based on how many characters per line
const text_array = wrap_text_array(text_element.text(), max_width);
// append a tspan element for each line of text_array
text_element.text(null)
.selectAll("tspan")
.data(text_array).enter()
.append("tspan")
.attr("x", text_element.attr("x"))
.attr("y", text_element.attr("y"))
.attr("dy", (d, i) => `${i * line_height}${unit}`)
.text(d => d);
}
Insert cell
wrap_text_array = (text, max_width) => {
// split the text around spaces (to get individual words)
const words = text.split(/\s+/).reverse();
// define vars to hold individual words, lines, and all lines
let word,
lines = [ ],
line = [ ];
// add words to a line until we exceed the max_width (in characters)
// when we reach width, add the line to lines and start a new line
while (word = words.pop()) {
line.push(word);
if (line.join(" ").length > max_width || word[0]=='(') {
line.pop()
lines.push(line.join(" "));
line = [word];
}
}
lines.push(line.join(" "));
return lines;
}
Insert cell
categorie=['Verbo volgare', 'Sostantivo volgare', 'Aggettivo volgare', 'Pronome volgare', 'Articolo volgare', 'Avverbio volgare', 'Preposizione volgare', 'Congiunzione volgare', 'Interiezione volgare', 'Onomastica volgare', 'Citazione volgare']
Insert cell
tiporicerca=['Sottostringa', 'Prefisso', 'Suffisso', 'Espressione regolare', 'Tutte le occorrenze']
Insert cell
oggettoricerca=['Forma', 'Lemma']
Insert cell
import {form} from "@mbostock/form-input"
Insert cell
function writesearchValue(form) {
return `Cerco: ${form.oggetto} ${form.query}, ${form.tipo}, nella categoria ${form.categoria}`
}
Insert cell
queryform.query
Insert cell
querytxt=writesearchValue(queryform)+' pippo'
Insert cell
Insert cell
sp_js=require('https://bundle.run/sparqljs@3.4.1')
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
function gimmespqfromjson(){
const sparqlgen = sp_js.Generator;
const generator = new sparqlgen({orl: "http://www.w3.org/2000/01/rdf-schema#" })
const parsedquery=gimmespq(form)
if (queryform.categoria!='all')
parsedquery.where[0].triples.push(JSON.parse(data21)[queryform.categoria]);
if (queryform.query!="")
switch(queryform.tipo) {
case 'parola':
parsedquery.where[1].expression.args[1].value="^"+queryform.query+"$";
break;
case 'sottostringa':
parsedquery.where[1].expression.args[1].value=queryform.query;
break;
case 'prefisso':
parsedquery.where[1].expression.args[1].value=queryform.query;
break;
case 'suffisso':
parsedquery.where[1].expression.args[1].value=queryform.query+"$";
break;
default:
parsedquery.where[1].expression.args[1].value="^"+queryform.query+"$";
};
return generator.stringify(parsedquery);
//return (parsedquery.where[0].triples)

}
Insert cell
Insert cell
restable=table(
dataconpos,
{
nully: () => '<span style="color: red">No data</span>',
limit: 100,
enableFilter: false,
enableCSVDownload: true,
columns: [{
key: 'IRI_Forma',
verso: 'Verso',
//render: val => `<a href="${val}" target="_blank">${val}</a>`
},
'IRI_Categoria',
'Verso',
'IRI_Verso'
]
}
)
Insert cell
showmytable= restable
Insert cell
Insert cell
Insert cell
credentials=`spqrl:lXlZybf4IgvIv9U2Reg`
Insert cell
myauthHeaders = new Headers({"Authorization": `Basic ${base64.encode(credentials)}`});
Insert cell
data=fetch(`https://hdnlab1.isti.cnr.it/fuseki/commedia/sparql?query=${encodeURIComponent(sparql)}`, {headers: myauthHeaders}).then(response => {
if (!response.ok) throw new Error(response.status);
return response.json();
})
Insert cell
data1=data.results.bindings
Insert cell
data2= data1.map(x => Object.assign({},{iri:x.Verbo.value,rappresentazione:x.Rappresentazione.value,funzione:x.Funzione.value,occorrenza:x.occ.value,}))
Insert cell
table(
data2,
{
nully: () => '<span style="color: red">No data</span>',
limit: 500,
enableFilter: false,
enableCSVDownload: true,
columns: [{
key: 'iri',
rappresentazione: 'rappresentazione',
render: val => `<a href="${val}" target="_blank">${val}</a>`
},
'rappresentazione',
'funzione',
'occorrenza'
]
}
)
Insert cell
import { table } from "@cesare/table"
Insert cell
import {Button, Checkbox, Toggle, Radio, Range, Select, Text, Textarea, Search, Table} from "@observablehq/inputs"
Insert cell
data21=(await FileAttachment("categorie.json").text())
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more