score:1
I have written the following that should do what you need it to. If not you can adjust it for your needs:
function convertToJsonTree(lines) {
// create the base
let parent = {};
// iterate through the lines
for (let i = 0; i < lines.length; i++) {
let _s = lines[i].split(',');
let _name = _s[0];
let _parent = _s[1];
// if the parents name is null, we must be at the base
if (_parent === 'null') {
// set the name
parent.name = _name;
// get rid of the current line
delete lines[i];
let subLines = lines.filter(function(el) {
return el != null;
});
// check for the first set of children
for (let j = 0; j < subLines.length; j++) {
let __s = subLines[j].split(',');
let __name = __s[0];
let __parent = __s[1];
// if there's a child that has the current level as the parent
if (parent && parent.name === __parent) {
// if the children array is undefined, create an array
if (!parent.children)
parent.children = [];
// create the child
let child = {
name: __name
};
// add it to the parents children
parent.children.push(child);
// remove the line that was already checked
delete subLines[j];
let _subLines = subLines.filter(function(el) {
return el != null;
});
// check if the child has any children
getChildren(child, _subLines);
}
}
}
}
return parent;
}
function getChildren(parent, lines) {
for (let i = 0; i < lines.length; i++) {
let _s = lines[i].split(',');
let _name = _s[0];
let _parent = _s[1];
// if there's a child that has the current level as the parent
if (parent && parent.name === _parent) {
// if the children array is undefined, create an array
if (!parent.children)
parent.children = [];
// create the child
let child = {
name: _name
};
// add it to the parents children
parent.children.push(child);
// remove the line that was already checked
delete lines[i];
let subLines = lines.filter(function(el) {
return el != null;
});
// check if the child has any children
getChildren(child, subLines);
}
}
}
Its use:
let csv = `name,parent
Level 2: A,Top Level
Top Level,null
Son of A,Level 2: A
Daughter of A,Level 2: A
Level 2: B,Top Level`;
let lines = csv.split(/\r\n|\n/);
let treeData = convertToJsonTree(lines);
console.log(treeData);
JSFiddle: https://jsfiddle.net/dbheale/Lrhsxw9e/
EDIT:
To read in your CSV from a file:
function loadDiagramFromCsvFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var csv = rawFile.responseText;
let lines = csv.split(/\r\n|\n/);
let treeData = convertToJsonTree(lines);
// Do what you need with your treeData here
// Maybe create a method that generates the diagram?
createDiagram(treeData);
}
}
}
rawFile.send(null);
}
Source: stackoverflow.com
Related Query
- How to create an interactive collapsible tree diagram from csv with D3 and javascript?
- How to create d3.js Collapsible force layout with non tree data?
- How to use d3.layout.cloud.js to create a word cloud with a csv containing both words and their weight?
- How to extract data from a csv file and create a line chart from it in D3.js
- How do I remove all children elements from a node and then apply them again with different color and size?
- D3.js Zooming and panning a collapsible tree diagram
- How to create floor plans that work nice with mobile and desktop browsers?
- How to load data from an internal JSON array rather than from an external resource / file for a collapsible tree in d3.js?
- How to pull data from mysql database and visualize with D3.JS?
- D3: How to create a circular flow / Sankey diagram with 2 arcs
- Creating a D3.js Collapsible Tree from CSV data
- d3.js how to generate tree hierarchy from csv or table
- How to make an interactive tree chart with d3?
- Create tree hierarchy from csv in d3.js
- Drawing voronoi diagram from a csv file with d3.js
- Create an interactive 3d scatter plot with D3.js and Three.js with json data
- How to create a d3 radial-tree from a list of nodes and links?
- D3: Tree diagram with images and circles as nodes
- d3.js, collapsible tree - how to connect different parents with same child
- How to summarize an array with group and rollup from d3-array?
- how to get data with tsv or csv to array in d3.js from a txt file?
- d3.js, how can i create an axis with custom labels and customs ticks?
- How to add links from CSV file to SVG elements generated with D3?
- How to retrieve Vector Tiles from Mapbox with d3.js and convert to geojson?
- How to create left axis with variable tick interval in tick values and has same distance between tick value
- Javascript D3 How to access and manipulate dataset from CSV
- How to make a mouseover interactive line graph with multiple data series and 2 y axes?
- How do I create a D3.js tree layout with custom child nodes?
- How do I convert strings from csv in d3.js and be able to use them as a dataset?
- D3.js V4 : How to create X axis with time and %H:%M:%S format
More Query from same tag
- If else statement that disables a button
- How to keep the order of slices fixed in a pie chart? [d3]
- how to use Promise.all instead of queue in d3
- Line not visible in line chart when having same value in the domain() - using D3
- Trying to append a click function to the node text in d3
- Format of json data for D3 bundle layout
- How to Create Blank Lower 48 Map in Django JS
- Cannot wait until DOM rendering has finished in Angular/Jasmine unit test
- How to color DC js bubble chart based on column value?
- dc.js geoChoroplethChart doesn't display legend
- D3 - Stacked Bar Chart - Only use certain columns CSV - Counting distinct elements
- d3.js not being able to visualiza a large dataset
- Sweep direction of arc in ring diagram
- Using d3j to select an element in an embedded SVG file
- D3 selectAll multiple classes AND or OR
- Change domain in bar chart
- d3.js not working in chrome
- D3 Collapsible tree: Scrollable container for tree
- Upload Javascript Object (svgCrowbar output) to Laravel 5.6 Server
- Floating line disturbs the mousemove function
- Accessing JSON values in D3
- How to throttle function call on mouse event with D3.js
- D3 How to zoom in on SVG text that stays within an SVG Rectangle?
- Changing position of label base D3
- D3.js animation
- How to plot a straight line in d3.js by user interaction
- Error when providing svg v1 path parameter d
- div element is getting appended outside the body
- How to fix a multiple brush chart in d3
- How to create a new div dynamically everytime a element in data is introduced