Parsing and Displaying JSON Data from a Python DataFrame in D3

Trying to create a stock chart, I encountered an issue with parsing the json file output by my python dataframe. The example code from http://bl.ocks.org/mbostock/3884955 does not seem to fit the format of my data:

The json looks like this:

var dataset = [{"1412121600000":{"Long":100.0,"Short":100.0,"Combined":100.0},
                "1412208000000":{"Long":100.7259540416,"Short":99.9845645596,"Combined":100.7105186013},
                "1412294400000":{"Long":100.4436402904,"Short":98.8837985253,"Combined":99.3194897889},
                "1412553600000":{"Long":100.4436402904,"Short":98.9995341224,"Combined":99.4357353278},
                "1412640000000":{"Long":99.5664445414,"Short":100.5277244418,"Combined":100.1022654968},
                "1412726400000":{"Long":101.6333965937,"Short":98.7695530192,"Combined":100.4296105442},
                "1412812800000":{"Long":101.8552090464,"Short":100.7284660335,"Combined":102.6406329748},
                "1412899200000":{"Long":101.5628181559,"Short":101.8782213959,"Combined":103.5175690128},
                "1413158400000":{"Long":100.6352059402,"Short":103.5518262499,"Combined":104.27263855},
                "1413244800000":{"Long":99.5664445414,"Short":103.3915337819,"Combined":103.0038388108},
                "1413331200000":{"Long":98.3464414352,"Short":104.0910952724,"Combined":102.4386550712}}];

I'm unsure how to format the key as the date and access the relevant node correctly. My attempts based on the provided example have been unsuccessful:

d3.json(dataset, function(error, data) {
    data.forEach(function(d) {
        ??
    });

Answer №1

If you're looking to tidy up your data, I suggest starting with reformatting. Each point should follow this structure:

{"date":"1412121600000","values":["Long":100.0,"Short":100.0,"Combined" :100.0]}

Then, you can easily reference them using .date or .value. For instance, if "date" should be on the x-axis, you can use:

.attr('x',function(d){return d.date;})

and maybe utilize Date.parse() function.

Take a look at how Mike deals with it:

data.forEach(function(d) {
d.date = parseDate(d.date);
  });

var parseDate = d3.time.format("%Y%m%d").parse;

He has "date" as a column and accesses it accordingly. Using JSON, you can achieve similar organization.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Simplified jQuery function for multiple div mouseover operations

Uncertain about the accuracy of my title. Due to certain reasons, I need to assign different IDs for the class, as it only detects ID and not class when hovered over. Therefore, I have created a CSS version where the opacity of a specific div will change t ...

An Unexpected Token Leads to a SyntaxError in Jest's CSS-Modules

I have successfully configured my jest to allow the usage of static files by following their detailed documentation. However, despite implementing the instructions correctly, I am still encountering an error: What steps can I take to resolve this issue an ...

Retrieve information from various MongoDB collections

Greetings! I currently have a database with the following collections: db={ "category": [ { "_id": 1, "item": "Cat A", }, { "_id": 2, "item": "Cat B" ...

"Enhance Your Website with Slider Swipe Effects using CSS3 and

After scouring the internet for various types of sliders, including swipe sliders, I am interested in creating a responsive swipe slider specifically for mobile phones. This would be a full page swipe slider where users can swipe left and right seamlessly. ...

Tips for improving performance with ng-repeat directive?

I have encountered some performance issues while using socket.io with the ng-repeat directive in Angular. The application slows down significantly when receiving a large amount of data from the backend, making it impossible to interact with the app. What w ...

Using TypeScript: Implementing array mapping with an ES6 Map

If I have an array of key-value objects like this: const data = [ {key: "object1", value: "data1"}, {key: "object2", value: "data2"}, {key: "object3", value: "data3"}, ] const mappedData = data.map(x => [x.key, x.value]); const ES6Map = n ...

The issue of undefined database columns arises when attempting to transmit data from an HTML form to a MySQL database via Express

My primary objective is to develop a RestAPI using Node.js and test it in a small HTML application. With the guidance of my instructor, I successfully created the RestAPI based on an example and customized it to work with my own MySQL database. Testing ea ...

What are the best practices for implementing optional chaining in object data while using JavaScript?

In my current project, I am extracting singlePost data from Redux and converting it into an array using Object.keys method. The issue arises when the rendering process is ongoing because the singlePost data is received with a delay. As a result, the initi ...

What could be causing errors with my kick command?

Hey everyone, I'm currently working on implementing some admin commands. Right now, I'm focusing on creating a kick command, but I keep running into errors. Making any changes seems to cause issues in other parts of the code. This is where I&apo ...

OutOfBoundsException due to an invalid index value for the string length

I've recently started working with Android and I'm trying to send the value of an editText using JSON to a server. However, I keep encountering an error "StringIndexOutOfBoundsException" and I'm unsure of how to resolve it. Here is my code: ...

Can you identify the nature of the argument(s) used in a styled-component?

Utilizing typescript and react in this scenario. Fetching my variable const style = 'display: inline-block;' Constructing a simple component export const GitHubIcon = () => <i className="fa-brands fa-github"></i> Enh ...

Is there a way to display an alert using JavaScript that only appears once per day?

I've created a website that displays an alert to the user upon logging in. Currently, the alert is shown each time the user logs in, but I'm looking to make it display only once per day at initial page loading. How can I achieve this? Below i ...

Google Maps API - Custom Label for Map Markers

I am trying to implement a custom map on my website, and everything seems to be working fine except for one issue. The red marker on the map needs to have a label, but I don't want to use an additional image as an icon. Is there a way to add a label ...

Tips for effortlessly enlarging an element

When you click on "#sidebarnav>li", the following happens: 1) The second child of it - <ul> element will expand and its class toggles between "collapse" and "collapse in". 2) "#sidebarnav>li" will receive the "active" class. 3) The "aria-ex ...

Does TypeGraphQl have the capability to automatically format SQL queries?

I am utilizing TypeORM in conjunction with TypeGraphQL. I am curious about the SQL query result that TypeGraphQL provides. For instance, if I have a User Table with numerous columns and a simple resolver like this: @Resolver() class UserResolver { @Q ...

Creating unique appbars for different sections on my sidebar in ReactJs

I am trying to implement a responsive drawer and AppBar using material-ui (@material-ui/core). My goal is to display a specific AppBar for each section of the drawer. For instance, when the user navigates to the Timetable section, I want the AppBar label t ...

MVC - transforming a model into a JSON object

In my MVC project, I utilized an Mvc object as my model. My requirement is to stringify my model into a JSON object so that I can use it in JavaScript as needed. Currently, I am implementing something along the lines of: <script type="text/javascript ...

Improving JavaScript Functions: Minimize duplication of helper methods

I have a set of helper functions that check for the presence of specific strings in an array and certain steps before triggering other functions. The reason for keeping them separated is because arrTours must be associated with only those arrSteps. // Help ...

Issue with Angular $compile directive failing to update DOM element

I'm currently working on a project that involves integrating AngularJS and D3 to create an application where users can draw, drag, and resize shapes. I've been trying to use angular binding to update the attributes and avoid manual DOM updates, b ...

Getting PHP Post data into a jQuery ajax request can be achieved by using the `$_POST

I'm struggling to figure out how to pass the blog title into the data field of my ajax call. I've been searching for beginner tutorials on SQL, PHP, and AJAX, but haven't found anything that clarifies this issue. If anyone knows of any usefu ...