Changing data sources with zoomable d3 treemap - javascript

I'm very new to d3 and would like some insight before I invest too much time into this code. This zoomable treemap (http://jsfiddle.net/08agmhej/, which I originally got from http://bost.ocks.org/mike/treemap/) uses d3 transitioning to only show two levels of data hierarchy at a time. The data is provided in a JSON file in the following format:
{
"name": "layout",
"children": [
{"name": "AxisLayout", "value": 6725},
{"name": "BundledEdgeRouter", "value": 3727}}
}
I would like to add a second dataset to the JSON that looks like the below code and allow the user to interact with the dataset via a dropdown such as in this example (http://mbostock.github.io/d3/talk/20111018/treemap.html)
{
"name": "layout",
"children": [
{"name": "AxisLayout", "value": 6725, "othervalue" : 2},
{"name": "BundledEdgeRouter", "value": 3727, "othervalue" : 2}
]
}
Is this possible or is it not since d3 couldn't handle the hidden node levels? For example, if zoomed in what would happen if the data source was changed, then zoomed out.

Related

Editing JSON file at multiple positions without rewriting complete file

I have a JSON file that contains lots of nodes. Each node has a unique id. The following is an example skeleton structure of the JSON file.
{
"node1": {
"id": "1",
"name": "student",
"properties": {
"name": "Ram",
"lastname": "Kumar",
"age": "20"
}
},
"node2": {
"id": "2",
"name": "Teacher",
"properties": {
"name": "Ram",
"subject": "Computers"
}
}
Now, this was just sample data. Suppose, I have to find node 2, provided I have the position of the node in the file. That is it's beginning and the end. I create a read stream from the start position and the end position of the file to get the object within the complete JSON object in the file. Now, using JSON.parse function I am able to create an object within the program after parsing it.
But, now I use a writestream to make the changes in the object at the required positions after computing them.
For example, I have to edit the subject property of the teacher, and the main rule is I cannot completely write the object again. Rather, create a writestream at the position where there is subject property and just edit that value.
So, the question is how to do so? And that to with multiple positions to edit, that is editing multiple properties, without rewriting them all.
I know I might face an issue if I use multi-threading for this to simultaneously edit the multiple properties at once, but I need a work around that too.

Dynamic rendering of a datagrid/table in React given JSON data from API

I have this json data that is gotten from an API end point.
[{
"id": 1,
"style": "A",
"color": "black",
"size": "30",
"quantity": "122.00"
},
{
"id": 2,
"style": "B",
"color": "maroon",
"size": "24",
"quantity": "48.00"
},...]
I know I can use array.map() to loop over the data and give me a table with headers 'style','color','size', and 'quantity' with corresponding data rows below.
What I want to do is transform the rendering into this table format as it is more human-readable and a big plus would be to have the cells editable also (e.g. data ties back to state). Specifically, the sizes are in the column headers. How can I do this using either plain react/javascript logic or even better is there a popular React package that has this capability? Currently looking at react-table and react-virtualized but don't have enough experience on these packages to do something complex. Some code would be highly appreciated.
Here is the image of the table which i created.Hope you are looking for this
And here is the link for the project
https://github.com/AdnanShah/ReactJS-KretaHub-

How to get thing from json in d3 tree

I'm trying to improve this graph visualisation with d3 lib
I'm using json like this
{"name": "START", "children": [{"name": "1", "children": [], "state": ["(free left)", "(free right)"]}], "state": ["(free up)", "(free down)"]}
And I need to show the "state" information about node on click. For now it doesn't matter how, alert() for example.
How should I change click() function? What should I use?

Bilevel Partition D3 create a simple 2 layer

This is the first time I am using D3 and was wondering whether someone could explain how I can create the Bilevel Partition with only two layers.
A complete & not complete section. then the complete section shows avergae corrrect and incorrect.
I've had a look at this code http://bl.ocks.org/mbostock/5944371
but I am not fully aware of what it does.
The code doesn't really care what data you throw at it, as long as it is formatted the way it expects.
Right now, this is the JSON file it is loading in.
Make your own JSON file that looks like
{
"name": "sections",
"children": [
{
"name": "complete",
"children": [
{"name": "correct", "size": 10},
{"name": "incorrect", "size": 30}
]
},
{
"name": "incomplete",
"size": 20
}
]
}
And see if it works when you load that in instead of flare.json. If you load the file locally, use firefox to check out the code, chrome will block requests for local files.
I have it working somewhat on tributary, you can change the data and the code, and fork it if you want to save your edits. here it is

Use javascript to update a json file

I'd like to preface this by saying I know what I'm doing isn't ideal.
With that covered:
I need to update data in a JSON file via javascript.
Here is the JSON file as it currently stands:
{
"nextid" : 3,
"ingredients" : [
{
"id": 1,
"name": "onion",
"kilojoules": 180,
"quantity": 1,
"measurement": "whole",
"nutrition": [
{"value": "Fat Total", "quantity": 110},
{"value": "Saturated Fat", "quantity": 46},
{"value": "Sodium", "quantity": 4},
{"value": "Carbohydrates Total", "quantity": 10000},
{"value": "Sugar", "quantity": 5000},
{"value": "Fibre", "quantity": 2000},
{"value": "Proten", "quantity": 1000}
]},
{
"id" : 2,
"name": "carrot",
"kilojoules": 56,
"quantity": 1,
"measurement": "whole",
"nutrition": [
{"value": "Fat Total", "quantity": 66},
{"value": "Saturated Fat", "quantity": 11},
{"value": "Sodium", "quantity": 26},
{"value": "Carbohydrates Total", "quantity": 3000},
{"value": "Sugar", "quantity": 2000},
{"value": "Fibre", "quantity": 1000},
{"value": "Proten", "quantity": 279}
]}
]
}
Now let's say I want to edit the carrot object. I've built an updated carrot object on my html page and have it sitting as an object in the javascript.
What am I going to need to do to update the source json with my edits?
Like I said before, I know this isn't ideal. I should have used a database for storing and manipulating data, but I've made my bed now and I have to get this working, regardless of how much it might make you all cringe.
Research tells me I'm going to need to use PHP or ASP on the server side to collect the parameters the javascript passes to it, but I have no idea where to begin with that.
I'm working in visual studio 2012 and the parameters of the project prohibit me from using addons. NuGet code libraries yes, but no addons. On that basis, I think it means I can't use PHP. Am I correct in my thinking there?
Note: Yes it's for an assignment, but altering the json file is beyond the scope of the requirements. Being able to process json data dynamically is enough for the project, I'd just REALLY like to be able to put some back.
Thanks in advance
EDIT: Probably should have shared this too. This is the javascript that opens the json file. The result of the opening is stored in a script wide variable called ingJson.
function downloadIngredients() {
$(document).ready(function () {
$.getJSON("/data/ingredientsInfo.js", function (result) {
try
{
ingJson = result;
ingredients = result.ingredients;
ingredients.sort(orderByNameAscending);
buildListBox(ingredients);
}
catch (err) {
alert(err.message);
}
});
});
}
To edit a JSON file:
Open the file with php
Send the contents to the browser (Just dump it as string in a variable in a script tag)
Parse the JSON with JavaScript (onload -> get the string from the script tag)
Edit the object
Convert it to a JSON string again
Send it back to php
Have php overwrite the file.
It's a shame you can't just edit the file directly in php, that'd render steps 2-6 unnecessary, you'd just parse, edit, and encode the JSON in php.
Edit: Since you seem to have the data in JavaScript already, steps 1-3 are accounted for.

Categories