diff --git a/lib/displayAsMarkdown.js b/lib/displayAsMarkdown.js index b7a710d..bc1158f 100644 --- a/lib/displayAsMarkdown.js +++ b/lib/displayAsMarkdown.js @@ -16,12 +16,12 @@ if (!String.prototype.replaceAll) { }; } -export function DisplayAsMarkdown({markdowntext}){ +export function DisplayAsMarkdown({markdowntext, className}){ //console.log(markdowntext) markdowntext = markdowntext.replaceAll("\n", "\n\n") return( ) } \ No newline at end of file diff --git a/lib/findPaths.js b/lib/findPaths.js index e240d39..977a781 100644 --- a/lib/findPaths.js +++ b/lib/findPaths.js @@ -43,6 +43,8 @@ function findPaths({sourceElementId, targetElementId, links, nodes}){ function findDistance({sourceElementId, targetElementId, nodes, links}){ let paths = findPaths({sourceElementId, targetElementId, nodes, links}) + console.log(targetElementId) + console.log(paths) let weights = [] for(let path of paths){ let currentSource = sourceElementId @@ -70,20 +72,22 @@ function findDistancesForAllElements({nodes, links}){ let referenceElements = nodes.filter(x => x.isReferenceValue) let midpoint = Math.round(nodes.length/2) let referenceElement = referenceElements.length > 0 ? referenceElements[0] : nodes[midpoint] - - let distances = nodes.map(element => - element.isReferenceValue ? [1] : findDistance({sourceElementId: referenceElement.id, targetElementId: element.id, nodes, links}) + // console.log(nodes) + let distances = nodes.map(node => + node.isReferenceValue || (node.id == referenceElement.id) ? [1] : findDistance({sourceElementId: referenceElement.id, targetElementId: node.id, nodes, links}) ) return distances } -export function CreateTableWithDistances({isListOrdered, nodes, links}){ - if(!isListOrdered){ +export function CreateTableWithDistances({isListOrdered, orderedList, listOfElements, links}){ + if(!isListOrdered || orderedList.length < listOfElements.length){ return (
{""}
) } else { + let nodes = orderedList.map(i => listOfElements[i]) let distances = findDistancesForAllElements({nodes, links}) let rows = nodes.map((element, i) => ({id: element.id, name: element.name, distances: distances[i]})) - + console.log("rows@CreateTableWithDistances") + console.log(rows) return(
diff --git a/lib/labeledgraph.js b/lib/labeledgraph.js index 7e3012a..85cc8a4 100644 --- a/lib/labeledgraph.js +++ b/lib/labeledgraph.js @@ -1,33 +1,50 @@ import React, { useState, useEffect } from "react"; import * as d3 from 'd3'; -function getlength(number) { - return number.toString().length; +let getlength = (number) => number.toString().length; +export function removeOldSvg(){ + d3.select("#graph").select("svg").remove(); } function drawGraphInner({nodes, links}){ - // Build the d3 graph + // List of node ids for convenience + var nodeids = nodes.map(node => node.id) + var positionById = {} + nodeids.forEach((nodeid, i) => positionById[nodeid]=i) + console.log("NodeIds/positionById") + console.log(nodeids) + console.log(positionById) + + // Calculate the dimensions let margin = {top: 0, right: 30, bottom: 20, left: 30}; let width = 900 - margin.left - margin.right; - let height = 400 - margin.top - margin.bottom; + var x = d3.scalePoint() + .range([0, width]) + .domain(nodeids) + let heights = links.map(link => { + let start = x(positionById[link.source]) + let end = x(positionById[link.target]) + return Math.abs(start-end)/2+70 // Magic constant. + }) + console.log(heights) + let maxheight = Math.max(...heights) + let height = maxheight - margin.top - margin.bottom; + console.log(`height: ${height}`) + + // Build the d3 graph + + removeOldSvg() var svg = d3.select("#graph") .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 + ")"); - - // List of node ids - var nodeids = nodes.map(function(d){return d.id}) - var positionById = {} - nodeids.forEach((nodeid, i) => positionById[nodeid]=i) // A linear scale to position the nodes on the X axis - var x = d3.scalePoint() - .range([0, width]) - .domain(nodeids) + // Add the circle for the nodes svg @@ -58,9 +75,9 @@ function drawGraphInner({nodes, links}){ .enter() .append('path') .attr('d', function (d) { - let start = x(positionById[d.source]) + let start = x(d.source) // X position of start node on the X axis - let end = x(positionById[d.target]) + let end = x(d.target) // X position of end node return ['M', start, @@ -85,9 +102,9 @@ function drawGraphInner({nodes, links}){ .enter() .append("text") .attr("x", function(d){ - let start = x(positionById[d.source]) + let start = x(d.source) // X position of start node on the X axis - let end = x(positionById[d.target]) + let end = x(d.target) // X position of end node return start + (end-start)/2 -4*getlength(d.distance) }) @@ -100,18 +117,13 @@ function drawGraphInner({nodes, links}){ }) .text(function(d){ return(`${d.distance}`)}) .style("text-anchor", "top") - - - - } -export function DrawGraph({isListOrdered, nodes, links}) { - +export function DrawGraph({isListOrdered, orderedList, listOfElements, links}) { if(isListOrdered){ + let nodes = orderedList.map(i => listOfElements[i]) drawGraphInner({ nodes, links}); } - return (
diff --git a/lib/slider.js b/lib/slider.js index 4d74700..4a4e33d 100644 --- a/lib/slider.js +++ b/lib/slider.js @@ -123,3 +123,22 @@ export function SliderElement({ onChange, value, displayFunction }) { ) } + +export function SubmitSliderButton({posList, binaryComparisons, sliderValue, toComparePair, nextStepSlider}){ + // This element didn't necessarily have to exist, but it made it easier for debugging purposes + let onClick = (event) => { + //event.preventDefault(); + let obj = {posList, binaryComparisons, sliderValue, element1: toComparePair[1], element2: toComparePair[0]} + // + console.log("input@SubmitSliderButton") + console.log(obj) + nextStepSlider(obj) + } + + return( ) + +} \ No newline at end of file diff --git a/lib/textAreaForJson.js b/lib/textAreaForJson.js new file mode 100644 index 0000000..3e2be6d --- /dev/null +++ b/lib/textAreaForJson.js @@ -0,0 +1,103 @@ +import React, { useState } from 'react'; + +export function TextAreaForJson({handleSubmit}){ + let [value, setValue] = useState(`[ + { + "name": "Some element. The name field is necessary", + "url": "http://www.example.com", + "somethirdfield": "a" + }, + { + "name": "Another element", + "url": "http://www.example1.com", + "somethirdfield": "b" + }, + { + "name": "A third element", + "url": "http://www.example2.com", + "isReferenceValue": true, + "somethirdfield": "c" + } + ]`) + const [displayingDoneMessage, setDisplayingDoneMessage] = useState(false) + const [displayingDoneMessageTimer, setDisplayingDoneMessageTimer] = useState(null) + + let handleChange = (event) => { + setValue(event.target.value) + } + + let handleSubmitInner = (event) => { + clearTimeout(displayingDoneMessageTimer) + event.preventDefault(); + //console.log(event) + console.log("value@handleSubmitInner@TextAreaForJson") + //console.log(typeof(value)); + console.log(value); + try{ + let newData = JSON.parse(value) + //console.log(typeof(newData)) + //console.log(newData) + handleSubmit(newData) + if(!newData.length || newData.length < 2){ + throw Error("Not enough objects") + } + setDisplayingDoneMessage(true) + let timer = setTimeout(() => setDisplayingDoneMessage(false), 3000); + setDisplayingDoneMessageTimer(timer) + }catch(error){ + setDisplayingDoneMessage(false) + //alert(error) + //console.log(error) + let substituteText = `Error: ${error.message} + +Try something like: +[ + { + "name": "Some element. The name field is necessary", + "url": "http://www.example.com", + "somethirdfield": "a" + }, + { + "name": "Another element", + "url": "http://www.example1.com", + "somethirdfield": "b" + }, + { + "name": "Another element", + "url": "http://www.example2.com", + "isReferenceValue": true, + "somethirdfield": "c" + } +] + +Your old input was: ${value}` + setValue(substituteText) + } + + + } + return ( +
+
+
+