Du kan teste her.
demo2.html
https://borres.hiof.no/wep/svg/norge/k2/demo2.html
, og inspisere kildekoden.
SVG-formatet er slik ( se modulen
Norge
)
<svg
version="1.2"
width="600px"
height="600px"
viewBox="600.15482 40.73736 1076.0438 2876.6739"
id="NO">
<g
id="01"
label="Østfold">
<path
label="Halden"
style="fill:#999999;fill-opacity:1;stroke:none"
id="0101"
d="m 800.69641,2717.5047 c -0.29632,-0.2395 -1.18938,-1.0938 -1.98449,-1.8986 ... z"
/>
...
</g>
....
</svg>
Et kommunedata-objekt ser slik ut:
{
"id":"0101",
"name":"Halden",
"center":"Halden",
"fylke":"Østfold",
"fylkesId":"01",
"pop":30132,
"area":642.34,
"logo":"0101.svg.png"
}
Et fylkesdata-objekt ser slik ut:
{
"name":"Østfold",
"id":"01",
"center":"Sarpsborg",
"logo":"01.png",
"area":"4182.19",
"pop":284962,
"kcount":18,
"klist":[
{"kname":"Halden","kid":"0101"},
{"kname":"Moss","kid":"0104"},
{"kname":"Sarpsborg","kid":"0105"},
...
]
},
Javaskriptet som handterer det hele ser slik ut
// kommune data
// common part of logo addresses
var kroot=kdata.logoLocation;
var froot=fdata.logoLocation;
// list of kommune objects
var klist=kdata.list;
// list of kommune objects
var flist=fdata.flist;
// all kommune paths in svg
var allPaths=null;
// kommune selection:
var unselected="fill:#eeeeee;fill-opacity:1;stroke:#000000";
var selected="fill:#990000;fill-opacity:1;stroke:none";
var neighbourOn="fill:#eeee00;fill-opacity:1;stroke:#000000";
// selected kommune
var target=null;
// the one and only svg-element
var svg=null;
// the element where we show a kommune
var kommuneElt;
// the element where we show a fylke
var fylkeElt;
//---------------------------------------------
// mark all kommuner in same fylke as neighbours
// as neutral if we want to remove marking
function markFylke(kommune,on){
var owner=kommune.parentNode;
var all=owner.getElementsByTagName("path");
if(on){
for(var ix=0;ix <all.length;ix++){
if(all[ix]!=kommune){
all[ix].setAttribute("style",neighbourOn);
}
else
all[ix].setAttribute("style",selected);
}
}
else{
for(var ix=0;ix <all.length;ix++){
all[ix].setAttribute("style",unselected);
}
}
}
//----------------------------
// when user click a kommune in the map
function kommune_click(evt) {
stopWalk();
clearAll();
if(target){
// turn off
markFylke(target,false);
}
// set new and turn on
// target is a svg path-element
target=evt.target;
markFylke(target,true);
var kname=target.getAttribute("label");
var kid=target.getAttribute("id");
// show data
displayKommune(kid,kname);
}
//----------------------------
// show all about this kommune and its owner (fylke)
function displayKommune(kid,kname){
// find the kommune with data
for(var ix=0;ix < klist.length;ix++)
if(klist[ix].id==kid){
// fill the kommune box
var im='<img class="im" src="'+kroot+klist[ix].logo+'" alt="id"/>'
var N='<div class="kname">'+klist[ix].name+'</div>';
var C='<div class="center">'+klist[ix].center+'</div>';
var P='<div class="pop"> Innbyggere: '+klist[ix].pop+'</div>';
var A='<div class="areal"> Areal: '+klist[ix].area+' km<sup>2</sup></div>';
// admin center present as different from kommune ?
if(klist[ix].name != klist[ix].center)
kommuneElt.innerHTML=im+N+C+P+A;
else
kommuneElt.innerHTML=im+N+P+A;
// the we display alll about the fylke that
// contains the kommune
displayFylke(klist[ix].fylkesId,kid);
return;
}
// Found no data, use the name from svg-file, just dump it
kommuneElt.innerHTML='<div class="kname">'+kname+'</div>';
}
//-------------------------
// show all about this kommune
function displayFylke(fid,kid){
// find the object with data
for(var ix=0;ix < flist.length;ix++)
if(flist[ix].id==fid){
var im='<img class="im" src="'+froot+flist[ix].logo+'" alt=""/>'
var N='<div class="kname">'+flist[ix].name+'</div>';
var C='<div class="center">'+flist[ix].center+'</div>';
var P='<div class="pop"> Innbyggere: '+flist[ix].pop+'</div>';
var kInFList=flist[ix].klist;
var sl='<ul>';
for(var kix=0;kix < kInFList.length; kix++){
var inKname=kInFList[kix].kname;
var inKid=kInFList[kix].kid;
if(inKid==kid)
sl=sl+'<div class="hilited small" >'+inKname+'</div>';
else
sl=sl+'<div class="small" style="cursor:pointer" onclick="makeAClick(\''+inKid+'\')">'+inKname+'</div>';
}
sl=sl+'</ul>';
fylkeElt.innerHTML=im+N+C+P+sl;
return;
}
// Found no data, use the name from svg-file, just dump it
fylkeElt.innerHTML='<div class="kname">'+fname+'</div>';
}
//------------------------------------------------
// simulate a click on a kommune in fylkesliste
function makeAClick(eltid){
var elt=document.getElementById(eltid);
if (elt.fireEvent) {
elt.fireEvent('onClick');
}
else {
var evObj = document.createEvent('Events');
evObj.initEvent('click', true, false);
elt.dispatchEvent(evObj);
}
stopWalk();
}
//-------------------------------------------
// walk the svg-document and plant click function
// and find display boxes
function setUp(){
allPaths=document.getElementsByTagName("path");
for(var ix=0;ix < allPaths.length;ix++){
allPaths[ix].setAttribute("onclick","kommune_click(evt)");
allPaths[ix].setAttribute("style",unselected)
}
//.. and locate message boxes
kommuneElt=document.getElementById("kommune");
fylkeElt=document.getElementById("fylke");
}
//------------------------------------------
// walk norway
var currentPath=0;
var timer;
var wait=1000;
function clearAll(){
for(var ix=0; ix < allPaths.length; ix++)
allPaths[ix].setAttribute("style",unselected)
}
function showAll(targ){
clearAll();
// set new and turn on
// targ is a svg path-element
//targ=evt.target;
markFylke(targ,true);
var kname=targ.getAttribute("label");
var kid=targ.getAttribute("id");
// show data
displayKommune(kid,kname);
}
function walk(){
if (currentPath ==allPaths.length-1){
currentPath=0;
}
showAll(allPaths[currentPath]);
currentPath=currentPath+1;
timer=setTimeout(walk,wait);
}
function startWalk(delay){
if(timer){
// stop to restart from same currentPath
stopWalk();
target=null;
}
else{
// only look for new start when we
// are not in walk/run situation
if(target){
for(var ix=0;ix < allPaths.length;ix++){
if(allPaths[ix]==target)
currentPath=ix;
}
}
}
wait=delay;
clearAll();
walk();
}
function stopWalk(){
if(timer){
clearTimeout(timer);
timer=null;
target=null;
}
}
Stilsettet er veldig enkelt:
#kommune{position:absolute;top:250px; left:450px;width:200px;background-color:#eeeeee;
border-style:solid;border-width:2px;border-color:#990000;
padding:5px}
#fylke{position:absolute;top:10px; left:10px;width:200px;background-color:#eeeeee;
border-style:solid;border-width:3px;border-color:#ffff00;
padding:5px}
#map{position:absolute;top:10px; left:150px;width:600px;
border-style:solid;border-width:thin;border-color:#eeeeee;}
#buttons{position:absolute;top:650px; left:300px;}
#buttons button{margin:5px}
.im{float:left;margin-right:10px}
.kname{font-weight:bold}
.center{font-style: italic;margin-top:2px}
.hilited{color:#990000;font-weight:bold}
.small{font-size:11px}