From 7d47a76d2139c54db697ea85a4f2ac321edbcdc5 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Wed, 6 Oct 2021 16:05:11 +0200 Subject: [PATCH] fix: graph display --- lib/labeledgraph.js | 11 ++++++++--- lib/utils.js | 7 ++++++- pages/index.js | 6 +----- pages/listOfResearchOutputs.json | 3 +-- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/labeledgraph.js b/lib/labeledgraph.js index 85cc8a4..3f18d29 100644 --- a/lib/labeledgraph.js +++ b/lib/labeledgraph.js @@ -1,7 +1,9 @@ import React, { useState, useEffect } from "react"; import * as d3 from 'd3'; +import { toLocale, truncateValueForDisplay } from "../lib/utils.js" let getlength = (number) => number.toString().length; + export function removeOldSvg(){ d3.select("#graph").select("svg").remove(); } @@ -106,7 +108,7 @@ function drawGraphInner({nodes, links}){ // X position of start node on the X axis let end = x(d.target) // X position of end node - return start + (end-start)/2 -4*getlength(d.distance) + return start + (end-start)/2 //-4*getlength(d.distance) }) .attr("y", function(d){ let start = x(d.source) @@ -115,8 +117,11 @@ function drawGraphInner({nodes, links}){ // X position of end node return height-32-(Math.abs(start-end)/2)//height-30 }) - .text(function(d){ return(`${d.distance}`)}) - .style("text-anchor", "top") + .text(function(d){ + return(truncateValueForDisplay(Number(d.distance))) + //return(Number(d.distance).toPrecision(2).toString()) + }) + .style("text-anchor", "middle") } export function DrawGraph({isListOrdered, orderedList, listOfElements, links}) { diff --git a/lib/utils.js b/lib/utils.js index c244f71..431b068 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,3 +1,8 @@ import crypto from "crypto" -export const hashString = (string) => crypto.createHash('md5').update(string).digest('hex'); \ No newline at end of file +export const hashString = (string) => crypto.createHash('md5').update(string).digest('hex'); + +export const transformSliderValueToActualValue = value => 10 ** value //>= 2 ? Math.round(10 ** value) : Math.round(10 * 10 ** value) / 10 +export const toLocale = x => Number(x).toLocaleString() +export const truncateValueForDisplay = value => value > 10 ? Number(Math.round(value).toPrecision(2)) : Math.round(value * 10) / 10 +export const transformSliderValueToPracticalValue = value => truncateValueForDisplay(transformSliderValueToActualValue(value)) diff --git a/pages/index.js b/pages/index.js index abcec67..ab22fc3 100644 --- a/pages/index.js +++ b/pages/index.js @@ -14,6 +14,7 @@ import { DisplayAsMarkdown } from '../lib/displayAsMarkdown' import { CreateTableWithDistances } from '../lib/findPaths' import { TextAreaForJson } from "../lib/textAreaForJson" import { pushToMongo } from "../lib/pushToMongo.js" +import { toLocale, transformSliderValueToPracticalValue } from "../lib/utils.js" /* Helpers */ let increasingList = (n) => Array.from(Array(n).keys()) @@ -36,11 +37,6 @@ let checkIfListIsOrdered = (arr, binaryComparisons) => { return isOrdered } -let transformSliderValueToActualValue = value => 10 ** value //>= 2 ? Math.round(10 ** value) : Math.round(10 * 10 ** value) / 10 -let toLocale = x => Number(x).toLocaleString() -let truncateValueForDisplay = value => value > 10 ? Number(Math.round(value)).toPrecision(2) : Math.round(value * 10) / 10 -let transformSliderValueToPracticalValue = value => truncateValueForDisplay(transformSliderValueToActualValue(value)) - let displayFunctionSlider = (value) => { let result if (value >= 0) { diff --git a/pages/listOfResearchOutputs.json b/pages/listOfResearchOutputs.json index 6dcdbcd..3ae5223 100644 --- a/pages/listOfResearchOutputs.json +++ b/pages/listOfResearchOutputs.json @@ -13,8 +13,7 @@ }, { "name": "Categorizing Variants of Goodhart's Law", - "url": "https://arxiv.org/abs/1803.04585", - "isReferenceValue": true + "url": "https://arxiv.org/abs/1803.04585" }, { "name": "The Vulnerable World Hypothesis",