Leveraging JSON URL Data for LinePlusBarChart in NVD3: Mapping Values

I am currently facing an issue while using the NVD3 library to create graphs. I am struggling to integrate a JSON URL correctly with the code provided.

Here is the script for the graph:

d3.json("jsondata3.json",function(error,data){
var chart;
nv.addGraph(function() {
chart = nv.models.linePlusBarChart()
  .x(function(d) { return d.label })
  .y(function(d) { return d.value })
  .margin({top: 30, right: 20, bottom: 50, left: 175})

 chart.y1Axis.tickFormat(d3.format(',f'));
     chart.y2Axis.tickFormat(d3.format(',f'));

  d3.select('#chart svg')
  .datum(data)
  .transition().duration(500)
  .call(chart);

 nv.utils.windowResize(chart.update);

 chart.dispatch.on('stateChange', function(e) { nv.log('New State:', JSON.stringify(e)); });

 return chart;
 });
 });

The format of the JSON data is as follows:

[{"TRANSACTION_DAY":"20130620","MT_ATTEMPTED":4505891,"MT_SUCCESS":83.54,"MO_ATTEMPTED":321857,"MO_SUCCESS":98.9},{"TRANSACTION_DAY":"20130621","MT_ATTEMPTED":6636631,"MT_SUCCESS":81.33,"MO_ATTEMPTED":311954,"MO_SUCCESS":98.66},{"TRANSACTION_DAY":"20130622","MT_ATTEMPTED":2708897,"MT_SUCCESS":90.47,"MO_ATTEMPTED":334279,"MO_SUCCESS":98.95} ]

The example provided includes code that utilizes a "map.series.values" function and has a slightly different formatting compared to my JSON data. I'm unsure how to adjust my JSON file to work seamlessly with this code. Any assistance would be appreciated.

var testdata = [
  {
    "key" : "Quantity" ,
    "bar": true,
    "values" : [ [ 1327986000000 , 690033.0] , [ 1330491600000 , 690033.0] , [ 1333166400000 , 514733.0] , [ 1335758400000 , 514733.0]]
  },
  {
    "key" : "Price" ,
    "values" : [ [ 1322629200000 , 382.2] , [ 1325307600000 , 405.0] , [ 1327986000000 , 456.48] , [ 1330491600000 , 542.44] , [ 1333166400000 , 599.55] , [ 1335758400000 , 583.98] ]
  }
].map(function(series) {
  series.values = series.values.map(function(d) { return {x: d[0], y: d[1] } });
  return series;
});

If anyone can provide guidance on how to successfully align my JSON file with this code structure, it would be greatly appreciated. Thank you.

Answer №1

It appears that your JSON data needs to be formatted exactly like the sample data provided. Make sure to follow the same structure for compatibility.

Note: JavaScript timestamps are in milliseconds, so you need to convert your timestamp (e.g., 20130620) to Unix timestamp and multiply by 1000.


 var testdata = [
  {
    "key" : "MT_ATTEMPTED" ,
    "bar": true,
    "values" : [ [ 4505891 , 20130620] , [ 6636631 , 20130621] , [ 2708897 , 20130622] ]
  },
  {
    "key" : "MT_SUCCESS" ,
    "values" : [ [ 83.54 , 20130620] , [ 81.33 , 20130621] , [ 90.47 , 20130622] ]
  },
  {
    "key" : "MO_ATTEMPTED" ,
    "values" : [ [ 321857 , 20130620] , [ 311954 , 20130621] , [ 334279 , 20130622] ]
  },
  {
    "key" : "MO_SUCCESS" ,
    "values" : [ [ 98.9 , 20130620] , [ 98.66 , 20130621] , [ 98.95 , 20130622] ]
  },
].map(function(series) {
  series.values = series.values.map(function(d) { return {x: d[0], y: d[1] } });
  return series;
});

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

Troubleshooting the issue with reactdom.render() functionality in CodeSandbox

Having issues with ReactDom in CodeSandbox for React. The HTML file includes: <body> <div id="root"></div> </body> <script src="scr/index.js"> The JavaScript file (named index) includes: ReactDOM.rende ...

Is there a way to extract a single row from a database with each button click?

I am working on a system for selecting items and need assistance with iterating through a table one item at a time upon clicking a button. Currently, the button only displays the first item in the table. How can I modify it to cycle through each item one b ...

Having trouble shutting down Metro Bundler on Windows?

While working on my React Native development, I regularly use npm start to get things going. However, I've run into an issue recently when trying to stop the process using Ctrl + c. It seems like I can no longer use npm start smoothly: ERROR Metro ...

Combine the selected values of two dropdowns and display the result in an input field using Angular

I am working on a component that consists of 2 dropdowns. Below is the HTML code snippet for this component: <div class="form-group"> <label>{{l("RoomType")}}</label> <p-dropdown [disabled] = "!roomTypes.length" [options]= ...

Guide on accessing a nested array within a JSON object using JavaScript

I'm currently faced with a json object that contains nested arrays and names with spaces like Account ID. My goal is to only display the Account ID's within my Vue.js application. While I can access the entire response.data json object, I'm ...

The Jquery ajax get method is malfunctioning

I've been trying out this code but it doesn't seem to be working. Apologies for the basic question, but I'm curious to understand why it's not functioning as expected. $(document).ready(function(){ $("button").click(function(){ ...

"Utilizing a dynamic global variable in Node.js, based on the variable present in

I'm looking to achieve the following: var userVar={} userVar[user]=["Values"]; function1(user){ //calculations with userVar[user] } function2(user){ //calcula ...

Navigating Hierarchical Data with Angular Dropdowns

I need help with organizing my folder structure in an html select using angular. Currently, the select only displays the top level of folders. Is there a way to show all folders with proper indentation to reflect the structure? I am considering adding a ...

Guide to displaying a task within a table cell after a user submits the form

<?php $servername = "localhost"; $username = "u749668864_PandaHost"; $password = "Effy1234"; $dbname = "u749668864_PandaHost"; $q = $_REQUEST["task"]; $task = $q; echo $task; $conn->close(); ?> Exploring the world of ajax has left me scratching ...

The file name is not compatible with 'eslint' functionality

I attempted to use the solution provided in this Stack Overflow thread for the error message 'eslint' is not recognized as an internal or external command. I successfully created a .eslintrc.json file with the following content: { "extends" ...

Is there any variation in the Stripe payments Workflow when utilizing the Connect-API?

I have a question about simplifying the implementation of the Stripe API for multiple products on a single page. Currently, I have a webpage with 20 different items and I am utilizing Stripe Connect. Instead of creating individual forms for each product i ...

interaction between modernizr and ajax causing loading issues

I am currently utilizing modernizr.js and a customized ajax function on my page to refresh the content every x seconds. Below is the snippet of code for the functions being triggered. function reloadSlides() { jQuery.ajax({ url: document.locat ...

The data retrieved from an NSDictionary in iOS appears to be empty when sourced from JSON

When transitioning from my CatViewController to my TopicViewController using a push segue, I encounter an issue with JSON data being fetched into a dictionary in the CatViewController and displayed in a UITableView. The goal is for the selected row to extr ...

Error encountered: The use of 'import' and 'export' is limited to 'sourceType: module' when attempting to install Tweakpane

I am currently learning JavaScript and have been following a course on Domestika that requires me to install the Tweakpane package. I usually use Git Bash for installing packages, and I have successfully installed others this way before. Here is the proces ...

Sending information from a Bootstrap modal form

I'm experiencing an issue with my form that triggers a modal for confirmation before submission. The modal contains the submit button and is located outside of the <form> tag. Surprisingly, it works perfectly fine on all browsers except for Inte ...

Using the ternary operator in a jQuery event

I am looking for a solution to have a button trigger different functions based on the value of a variable. It appears that the ternary operator does not work with a jQuery event trigger. var color = "blue"; $(document).ready(function(){ $('#bot ...

Modify the JSON file stored on the local server

I am currently working on a project that is being hosted on a local server. My main objective is to be able to edit and save a JSON file that will contain the configurations for this project. I have succeeded in reading the file and accessing it using axio ...

Having trouble with your jQuery code not loading properly?

Currently working on debugging jQuery code and facing an issue where a particular section of the code is not being triggered upon clicking. The HTML/PHP in question: $cartlink .= "<a class='add_to_cart button' data-id='{$product->id} ...

Encountering the error message "Unable to locate module '.nextserverpages-manifest.json'" while attempting to include `babel.config.js` in a Next.js application

During the process of setting up testing for my current next app, we incorporated some new dependencies including jest, babel-jest, @babel/preset-env, @babel/preset-react, and react-test-renderer. We also created a babel.config.js file to configure Babel s ...

Understanding how to access a variable outside of a function in Node.js can be crucial for successfully passing

I am trying to figure out how to access the TRADE_OUT variable outside of a specific function in my application. Despite my efforts, I haven't been successful so far. I need the value of TRADE_OUT to be globally accessible within the entire applicatio ...