Exploring JSON data visualization in conjunction with Angular's NVD3 library

My HTML code:

In this section of my HTML code, I am fetching fields (key) from a JSON file and displaying them as options in the selection dropdown. When an option is chosen from the dropdown, it is sent to my Angular script via the nvd3 directive data option (

data="drawData(data.repeatSelect1)"
).

<div ng-controller="chartCtrl">

<label for="repeatSelect1"> Field 1: </label>
<select name="repeatSelect1" id="repeatSelect1" ng-model="data.repeatSelect1">
  <option ng-repeat="(key, val) in jsondata[0]">{{key}}</option>
</select>
<br />
<strong>Selected Field:</strong> {{data.repeatSelect1}}<br />
    <div class="col-md-6">
        <div class="well well-lg">
                <h2><kbd>Chart 1</kbd></h2>
                <nvd3-pie-chart
                      data="drawData(data.repeatSelect1)"
                      showLabels="true"
                      labelType="percent"
                      showValues="false"
                      tooltips="true"
                      x="xFunction()"
                      y="yFunction()"
                      donut="true"
                      donutRatio=".5"
                      labelThreshold = "0.05"
                      showLegend="true"
                      donutLabelsOutside="false"
                      transitionDuration = "500">
                </nvd3-pie-chart>
            </div>
        </div>
    </div>

Here is my Angular script code :

    angular.module('myapp').factory("test",['$http',function($http){
    var obj = {};

    obj.fetchdata=function(){
        return $http.get('http://localhost:8080/data/my_json_file.json');
    }

    return obj;
}]) .controller('chartCtrl',function($scope,test){
        test.fetchdata().success(function(data){
            $scope.jsondata = data;


            $scope.drawData = function(tobeselected){
                                        d3.nest()
                                        .key(function(d)
                                            {
                                                console.log(tobeselected);
                                                return d[tobeselected];
                                            })
                                        .rollup(function(v){return v.length;})
                                        .entries(data)
            }

            $scope.xFunction = function() {
              return function(d) {
                return d.key;
              };
            }
            $scope.yFunction = function() {
              return function(d) {
                return d.values;
              };
            }
        });
})

And here is my JSON file named 'my_json_file.json'

[{"Serial":"ARTUCALA","Location":null,"Date":"2014-10-02T00:00:00","Count":1,"Preset":null},{"Serial":"ZAKDJSJS","Location":null,"Date":"2014-10-02T00:00:00","Count":1,"Preset":null},{"Serial":"ARTUCALA","Location":null,"Date":"2014-10-02T00:00:00","Count":1,"Preset":null},{"Serial":"ARTUCALA","Location":null,"Date":"2014-10-02T00:00:00","Count":1,"Preset":null},{"Serial":"ZAKDJSJS","Location":null,"Date":"2014-10-02T00:00:00","Count":1,"Preset":null},{..}...]}

The code is functioning correctly without any errors.

For example, when I select the "Serial" field from the dropdown, it is properly displayed in the AngularJS script (using console.log(tobeselected)), but the d3.nest option is not working as intended.

If I input it normally like:

d3.nest()
.key(function(d){return d.Serial;}
.rollup(function(v){return v.length;})

I get the chart correctly. Please assist me in solving this issue.

Answer â„–1

To utilize a dynamic property name, it is necessary to implement the associative array syntax

For example, if selecteditem is the property name, then you should use data[selecteditem]

Therefore, opt for return data[selecteditem] rather than data.selecteditem

When using data.selecteditem, JavaScript will search for an actual property named selecteditem on the data object (which does not exist), instead of a property name equal to the value of the variable selecteditem

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

What is the best way to access and verify data from an array that has been passed through props

I am working with a component that takes an array as a prop <Component runners={['1','2']}/> Inside this functional component, my goal is to generate an element for each value in the array. Here's my logic: const { runners ...

Navigating a JSON object in d3 after it has been loaded

After loading a JSON object using d3.json and assigning it to a global variable, I encountered an issue where the console returned 'undefined' when printing the global variable. However, typing the global variable in the Chrome console returned t ...

AngularJS and the use of template tables

I need help creating an HTML table based on a specific model. Here is what I am trying to accomplish: Student | competence 1 | | subject 1 | subject 2| | exam 1 | exam2 | avera ...

Determine the status of all jqXHR requests within an array to confirm completion

My goal is to execute a block of code once all the jqXHR elements in an array have completed, whether they have succeeded or failed. For the full code, you can check it out here: http://jsfiddle.net/Lkjcrdtz/4/ Essentially, I am anticipating the use of t ...

Error in building Ionic project: Unable to run the specified command "env.runcmd" due

While developing an ionic application, I encountered the following error message: https://i.sstatic.net/Oka4s.png Is there anyone who can assist me? ...

Encountering a syntax error while attempting to send Node.js data to MySQL database

I am facing a problem with an SQL error that I just can't seem to figure out. Here is the error message: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual for MySQL server version for correct syntax near 'x Disney â ...

Looking to automatically scroll to the bottom by clicking on text using javascript/jquery?

I am currently working on a website project where I have a specific requirement. I need the webpage to scroll towards the bottom when a particular text is clicked, using JavaScript/jQuery. <p class="ml-2 mb-2 d-flex view_profile_options"><a hre ...

Retrieving information from a JSON file and dynamically displaying it on an HTML page using JavaScript

I've been working on pulling data from a JSON file to display on my website. Following this helpful guide at: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON, but unfortunately, I'm facing an issue where nothing is showing ...

The implementation of pushing inside a foreach loop is not correctly adding elements to

I have encountered an issue with running a foreach loop and adding values to an array in my code. The first foreach loop works as expected, adding values properly to the array. However, the second foreach loop seems to be malfunctioning as none of its valu ...

Determine the numerical value of the attribute instead of treating it as a string when using

I'm fairly new to AngularJS and I'm not sure if what I want to achieve is possible. I am using an angular directive from this link within a custom directive template, and I need to set certain attributes based on isolated scope properties. The is ...

Maximum opacity in Three.js fog

My Current Endeavor: I am in the process of developing a lightweight GIS application with topography. One feature I want to implement is atmosphere haze. The Code I'm Working With: (Please bear with me as I have multiple scenes) fogColor = new T ...

Is it possible to use npm to install Fabric.js for a PHP website?

Currently collaborating on a project with two team members, we are in the process of developing a small website that enables users to upload a photo and add custom text on top as an overlay. In my exploration, I discovered that Fabric.js is capable of ful ...

Is there a way to effectively incorporate react-native with php and ensure that the data returned by the fetch function is

I'm struggling to make my return value work in this code. Has anyone had success using react-native with php and fetch json? Any tips or advice would be greatly appreciated. PHP $myArray = array(); $myArray['lat'] = $_POST['lat']; ...

Delete the initial image from the opening list item using jQuery

Here is an example of some HTML code: <ul class="products"> <li> <a href="#" class="product-images"> <span class="featured-image"> <img src="img1.jpg"/> <img src="img ...

Integrate @azure/msal-node for user authentication to securely access resources in Azure Vault

Hey there, I've set up a test azure instance and am currently attempting to access a secret stored in a vault. The vault has been created, and my goal is to retrieve access using an electron application without storing any client secrets within the ...

The React Callservice script is failing to fetch the required data from the Node.js script responsible for making the API call

Recently, I decided to create a basic webpage using React.js to display data fetched from an API. Although the project is intended to be straightforward, my lack of recent development experience has led to a perplexing issue that I can't seem to resol ...

What is the best way to extract an object by its specific id using json-server?

I am attempting to retrieve an object by its id using json-server. If I only return a single variable (one JSON) in the module.exports of the database file, everything works fine. However, if I try to return an object with multiple variables, I encounter a ...

Node's MongoDB client

Currently tackling the challenge of connecting MongoDB in Node.js and passing the link to the client as an outer variable. I am looking to develop a module that can yield the result of the .find() method. How should I proceed with this? const MongoClient ...

The value of the view variable in AngularJS remains stagnant and does not update

Hello there, I am just starting to explore Angularjs. Here is the code snippet that I have been working on : .html file : <!DOCTYPE html> <html ng-app="showcase.angularWay.dataChange"> <head> <script src='js/jquery-1 ...

Executing the calling statement once all function statements have been completed

I am currently working on a lengthy function that involves retrieving data from multiple levels of a database. getResult() { this.appService .getCountries() .subscribe( (countries: Country[]) => { this.countries = co ...