angularjs directive that constantly reuses the array multiple times

I have been attempting to utilize ng-repeat with Highcharts but I am encountering an issue where only one chart is displayed in each row. Below is the HTML code:

<tr ng-repeat="perspective in perspectives">
                <td>
                    <highcharts-pie class="hc-pie" items="processed"></highcharts-pie>
                </td>
                <td>{{perspective.perfomance}}</td>
                <td>{{perspective.current}}</td>
                <td>{{perspective.previous}}</td>
                <td>{{perspective.variance}}</td>
            </tr>

And this is the data in the controller:

    $scope.perspectives=[
      {perfomance:'A',
        current:'0',
        previous:'1',
        variance:'-1',
        plus:false,
        graphData:[
          {value: 32.4},
          {value: 13.2},
          {value: 84.5},
          {value: 19.7},
          {value: 22.6},
          {value: 65.5},
          {value: 77.4},
          {value: 90.4},
          {value: 17.6},
          {value: 59.1},
          {value: 76.8},
          {value: 21.1}
            ]
      },{perfomance:'B',
          current:'1',
          previous:'0',
          variance:'1',
          plus:true,
          graphData:[
            {value: 22.4},
            {value: 53.2},
            {value: 45.5},
            {value: 67.7},
            {value: 92.6},
            {value: 78.5},
            {value: 71.4},
            {value: 35.4},
            {value: 21.6},
            {value: 34.1},
            {value: 68.8},
            {value: 24.1}
          ]
 }];
  $scope.processed = $scope.perspectives[0].graphData.map(function (elem, i) {
    return [i, elem.value];
  })

This is the directive used:

.directive('hcPie', function () {
  return {
    restrict: 'C',
    replace: true,
    scope: {
      items: '='
    },
    controller: function ($scope, $element) {
    },
    template: '<div id="container">not working</div>',
    link: function (scope, element) {
      var chart = new Highcharts.Chart({
        chart: {
          renderTo: element[0],
          height: 45,
          type: 'column',
          backgroundColor: null
        },
        title: {
          text: null
        },
        subtitle: {
          text: null
        },
        // Rest of the configuration properties...
      });
      scope.$watch("items", function (processed) {
        chart.series[0].setData(processed, true);
        console.log(processed)
      }, true);
    }
  }
});

I have been trying to display a unique chart for each row but it keeps showing the same chart every time. Any ideas on what I might be missing?

Answer №1

$scope.processed is defined only once, ensuring that it will always contain the processed values of the graphData from the initial perspective.

Here's a possible solution:

In your controller:

$scope.process = function(graphData) {
  return graphData.map(function (elem, i) {
    return [i, elem.value];
  });
}

When using the directive:

<tr ng-repeat="perspective in perspectives">
   <td>
     <highcharts-pie class="hc-pie" items="process(perspective.graphData)"></highcharts-pie>
   </td>
   ....
</tr>

Answer №2

Make sure you're iterating over graphData by using

  perspective.graphData

within the ng-repeat directive

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

Error encountered while trying to upload a file using PHP on a hosting platform

Hey everyone, I'm feeling really frustrated right now because I'm facing a file upload error in my PHP code. The issue is that the file is not getting uploaded to my folder and no error messages are being displayed. I've been working on cr ...

Testing with Angular's $httpBackend to ensure that a GET request is being made in unit testing

I want to verify that a component's controller is sending a GET request to a specific URL without worrying about the response. I thought that using httpBackend.expectGET('/some/random/url'); would monitor the http backend and trigger a fa ...

Error encountered during the building of a Java project using Gradle

I ran into an issue with Git Bash error output (build failed). Despite attempting to resolve it by installing Python as suggested, setting the Python environment variable in IntelliJ, and following other recommendations, I still encounter the same build ...

How can I change the displayed value of a 'select' element programmatically in Internet Explorer?

My interactive graphic has a drop-down menu implemented with a <select> element. Everything functions correctly, except when using Internet Explorer. The issue arises when attempting to programmatically change the selected value of the <select> ...

Change the keys of the object in the return statement

Scenario Imagine a scenario where a method called matter is returning an object in the form of return {content, data} Issue There is a conflict when the method is called a second time, as it overwrites any previous variables that were set from the return ...

Utilizing Redux in my ASP.NET MVC application with the help of RequireJS and TypeScript (Issue: Encountering a script error for "redux" dependency)

I'm currently working on integrating the Redux state library with my ASP.NET MVC application using TypeScript and RequireJS. For this simple application, I have set up a new ASP.NET MVC Project and stripped it down to only include the necessary node ...

Challenges with launching a fresh React App

After installing Nodejs(v 10.16.0 LTS), I used Windows Powershell to execute the following commands: npx create-react-app my-app cd my-app npm start However, I am encountering an issue where my code works properly only when the PowerShell ...

The process of incorporating user properties into the output of a Service Bus topic from a Javascript Azure Function

I'm currently developing a TypeScript Azure Function that utilizes an Azure Service Bus topic as its output. Although I am able to send messages successfully, I have encountered difficulties in setting custom metadata properties for the message. In m ...

Utilize Angular2 data binding to assign dynamic IDs

Here is the JavaScript code fragment: this.items = [ {name: 'Amsterdam1', id: '1'}, {name: 'Amsterdam2', id: '2'}, {name: 'Amsterdam3', id: '3'} ]; T ...

Verify if the specified value is present in the dropdown using AngularJS

Utilizing AngularJS, I have implemented an input field with autocomplete functionality. The autocomplete feature pulls data from a JSON file and displays it in a dropdown table format. Users are able to filter the results and select a value from the dropdo ...

The webpage hosted on Heroku displays a blank background, requiring users to refresh the page with an F5 key

I developed a group chat program using Python microframework flask, vanilla JavaScript, and Flask-SocketIO. The program is deployed on heroku and can be accessed here: . After deployment, I encountered the following issue: While the program worked fine o ...

Send a MySQL query and an HTTP request to an SMS gateway through JavaScript

I have a scenario where data is being received by my confirm.php file. In this file, I have the following code and currently facing an issue with the javascript part. Any help and suggestions would be greatly appreciated. The objective of my javascript is ...

The event listener $scope.$on for $locationChangeStart allows the URL to remain the same while

I'm facing a challenge with the code snippet below. It is part of a larger application where the issue lies in the fact that the code runs successfully, preventing the URL from changing as intended. However, even if I don't confirm to leave the p ...

Performing calculations involving both select and checkbox input values, as well as handling negative numbers

Seeking assistance in creating a feature where users can select a billing term from a dropdown and then choose checkboxes to add to the total. While it works fine on JSFIDDLE, I encounter issues on my website - specifically when selecting all checkboxes, r ...

When closing the menu in Bootstrap, the page automatically scrolls back to the top

Whenever I open my menu, click on a link, and scroll down the page using the menu, upon closing the menu I am automatically taken back to the top of the page. I have experimented with the following scripts: let y = window.scrollY; let offcanvas = docume ...

jQuery: Track mouse movement with a delay

I'm looking to create a div that follows cursor movement with a slight delay, similar to the effect seen on this website: In the example link provided, you can observe that the 'follower' has a brief delay in its animation. I attempted to ...

How can I trigger the execution of textarea HTML code with an onClick event using JavaScript?

When my fingers are lifted from the keyboard, some code will be automatically executed. I want to trigger this function when I click a button (onclick) instead of using onkeyup. <div id="result"></div> <textarea ...

Await that's locked within a solo asynchronous function

async function submitForm(e){ e.preventDefault() console.log(e.target) try { const response = await axios.post('/api/<PATH>', {username, password}); console.log(response.data); const token = response.data.token if (t ...

Issue with Angular.js mobile browser app causing freeze on iOS 8 Safari

Our Angular.js Web App is experiencing occasional freezing on iOS8 Safari. When the freeze occurs, the ng-click callback fails to trigger. Interestingly, replacing ng-click with a standard javascript onclick resolves the issue. It's worth noting that ...

Welcome to the interactive homepage featuring multitouch authentication

I am looking to design a homepage that is interactive based on touch input. For instance, if the user uses 5 fingers to touch the screen, similar to the green circles in the image below, they will be redirected to a specific homepage. If they touch in a di ...