Skip to content

Latest commit

 

History

History
28 lines (28 loc) · 1.79 KB

README.md

File metadata and controls

28 lines (28 loc) · 1.79 KB

Python array to d3js hierarchical json flare

For those types of visualization tools:
https://observablehq.com/@d3/gallery
https://observablehq.com/@d3/collapsible-tree
https://observablehq.com/@d3/zoomable-treemap

Python script to convert sorted array of nested data to d3js json flare

array=[['00001', '00004'], ['00001', '00004'], ['00002', '00008'], ['00002', '00008'], ['00002', '00008'], ['00002', '00014'], ['00002', '00014'], ['00003', '00092'], ['00003', '00092'], ['00003', '01004']]

value_is_count=True
value_is_lastColumn=False

  • Either array contains values in its last column, set value_is_lastColumn=True
  • Either array has no provided values but has duplicates to count, set value_is_count=True
  • Either array has single lines, values bill be set to 1

dict=asr_array_to_d3js_flare(array=array, value_is_count=value_is_count, value_is_lastColumn=value_is_lastColumn)

Will give a python dict like this:

{'name': 'flare', 'children': [{'name': '00001', 'children': [{'name': '00004', 'value': 2}]}, {'name': '00002', 'children': [{'name': '00008', 'value': 3}, {'name': '00014', 'value': 2}]}, {'name': '00003','children': [{'name': '00092', 'value': 2}, {'name': '01004', 'value': 1}]}]}

To obtain JSON string:
import json
json.dumps(d)

Will give this string:
{"name": "flare", "children": [{"name": "00001", "children": [{"name": "00004", "value": 2}]}, {"name": "00002", "children": [{"name": "00008", "value": 3}, {"name": "00014", "value": 2}]}, {"name": "00003", "children": [{"name": "00092", "value": 2}, {"name": "01004", "value": 1}]}]}

Et voilà !