Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                        <meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
	
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
	<script src="https://d3js.org/d3.v4.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>

<div class="container">
	     <h1>Scholia</h1>
	     <div class="row">
		 <h2>Topic-work matrix</h2>
		 <div class="col-xs-12 col-lg-12" id="topic-work-matrix"></div>
	     </div>
</div>
    
              
            
!

CSS

              
                
              
            
!

JS

              
                function resize() {
  width = document.getElementById("topic-work-matrix").clientWidth - 0;
  d3.select("#topic-work-matrix").attr("width", width);
}

window.onresize = resize;

function makeSparqlQuery(sparql, doneCallback) {
  var endpointUrl = "https://query.wikidata.org/sparql";
  var settings = {
    headers: { Accept: "application/sparql-results+json" },
    data: { query: sparql }
  };
  return $.ajax(endpointUrl, settings).then(doneCallback);
}

sparql = `
	SELECT ?work ?workLabel ?theme ?themeLabel WHERE {
	?work wdt:P50 wd:Q6251482 .
	?work wdt:P921 ?theme .
	OPTIONAL { ?work wdt:P577 ?publication_date }
	SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,cz,da,de,es,fr,hi,jp,nl,nn,ru,sv,zh". }
	}
	ORDER BY ?publication_date
	`;

makeSparqlQuery(sparql, function(response) {
  var data = response.results.bindings;
  var qToLabel = new Object();
  data.forEach(function(item, index) {
    qToLabel[item.theme.value.substring(31)] = item.themeLabel.value || item.theme.value.substring(31);
    qToLabel[item.work.value.substring(31)] = item.workLabel.value || item.work.value.substring(31);
  });

  var works = $.map(data, function(row) {
    return row.work.value.substring(31);
  });
  var themes = $.map(data, function(row) {
    return row.theme.value.substring(31);
  });

  // set the dimensions and margins of the graph
  var margin = { top: 100, right: 1, bottom: 1, left: 0 },
    width =
      document.getElementById("topic-work-matrix").clientWidth -
      margin.left - margin.right,
    height = works.length * 15 - margin.top - margin.bottom;

  // append the svg object to the body of the page
  var svg = d3
    .select("#topic-work-matrix")
    .append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

  // Build X scales and axis:
  var x = d3
    .scaleBand()
    .range([0, width])
    .domain(themes)
    .padding(0.15);
  svg
    .append("g")
    .style("font-size", 15)
    .call(
      d3
        .axisTop(x)
        .tickSize(0)
        .tickFormat(function(d) {
          return qToLabel[d];
        })
    )
    .selectAll("text")
    .style("text-anchor", "start")
    .attr("transform", "rotate(-65)");

  var y = d3
    .scaleBand()
    .range([0, height])
    .domain(works)

  svg
    .append("g")
    .style("font-size", "10px")
    .style("text-anchor", "start")
    .style("opacity", 0.5)
    .call(
      d3
        .axisLeft(y)
        .tickSize(0)
        .tickFormat(function(d) {
          return qToLabel[d]; // .substring(0, 30);
        })
    )
    .select(".domain")
    .remove();

  d3.selectAll(".tick").on("click", function(d, i) {
    console.log(d + i);
  });

  // create a tooltip
  var tooltip = d3
    .select("body")
    .append("div")
    .style("opacity", 0)
    .attr("class", "tooltip")
    .style("background-color", "white")
    .style("border", "solid")
    .style("border-width", "2px")
    .style("padding", "10px")
    .style("font-size", "20px");

  var mouseover = function(d) {
    html =
      "<a href='../work/" +
      d.work.value.substring(31) +
      "'>" +
      d.workLabel.value +
      "</a>" +
      "<br/>" +
      "<a href='https://tools.wmflabs.org/scholia/" +
      d.theme.value.substring(31) +
      "'>" +
      d.themeLabel.value +
      "</a>";
    
    tooltip
      .html(html)
      .style("opacity", 1)
      .style("left", d3.event.pageX + "px")
      .style("top", d3.event.pageY + "px")
      .style("position", "absolute");
  };
  var mouseleave = function(d) {
    tooltip
      //.transition()
      //.delay(5000)
      //.duration(500)
      // .style("transition-delay", 1000)
      .style("opacity", 0);
  };

  // Add elements in matrix
  svg
    .selectAll()
    .data(data, function(d) {
      return true;
    })
    .enter()
    .append("rect")
    .attr("x", function(d) {
      var xIndex = d.theme.value.substring(31);
      var xValue = x(xIndex);
      return xValue;
    })
    .attr("y", function(d) {
      var yValue = y(d.work.value.substring(31));
      return yValue;
    })
    .attr("width", x.bandwidth())
    .attr("height", y.bandwidth())
    .on("mouseover", mouseover)
    // .on("mousemove", mouseover)
    .on("mouseleave", mouseleave);
  //.on("click", mouseover);
});

              
            
!
999px

Console