AngularJS: Transferring data from ng-repeat to a bar chart

Utilizing the countBy function from angular-filter to group and tally the number of entries for a specific column. I'm looking to transfer these values into a separate bar chart on the same page. Despite attempting various methods, I haven't been successful in achieving this.

HTML:
<!doctype html>
<html ng-app="app">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-touch.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script>
    <!-- <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-resource.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-route.js"></script> -->
    <script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script>
    <script src="http://ui-grid.info/release/ui-grid.js"></script>
    <script src="http://ui-grid.info/release/ui-grid.css"></script>
    <script src="node_modules/angular-chart.js/node_modules/chart.js/dist/Chart.min.js"></script>
    <script src="node_modules/angular-chart.js/dist/angular-chart.min.js"></script>
    <script src="node_modules/angular-filter/dist/angular-filter.min.js"></script>
    <!-- <script src="/js/app.js"></script>
    <link rel="stylesheet" href="http://ui-grid.info/release/ui-grid.css" type="text/css">
    <link rel="stylesheet" href="/css/main.css" type="text/css">
    <!-- <link rel="stylesheet" href="/webjars/bootstrap/3.3.6/css/bootstrap.css"> -->
</head>
  <body>
<div ng-controller="myController">
      <div id="grid1" ui-grid="{ data: myData }" class="grid"></div>
      <div><canvas id="bar" class="chart chart-bar"chart-data="key" chart-labels="value" chart-series="series"></canvas></div>
      <div><li ng-repeat="(key, value) in myData | countBy: 'col_ky'" >Column Key: {{ key }}, Number of Hits: {{ value }}</li></div>
    </div>
</body>
</html>

Controller:

var app = angular.module('app',['ngTouch', 'ui.grid', 'chart.js', 'angular.filter']);
app.controller('myController',['$scope', '$http','$filter',function($scope, $http, $filter) {

 $scope.barData = [];
 $scope.labels = [];
 $scope.myData = [{"col_ky":"75421","crt_ts":1501365031000},{"col_ky":"75421","crt_ts":1501124681000},{"col_ky":"75421","crt_ts":1501124688000},{"col_ky":"880610","crt_ts":1501127589000},{"col_ky":"880610","crt_ts":1501127715000},{"col_ky":"891733","crt_ts":1501128075000},{"col_ky":"75421","crt_ts":1501128130000},{"col_ky":"880610","crt_ts":1501128181000},{"col_ky":"75421","crt_ts":1501128192000},{"col_ky":"885110","crt_ts":1501128364000},{"col_ky":"880610","crt_ts":1501128369000},{"col_ky":"326083","crt_ts":1501130957000},{"col_ky":"75421","crt_ts":1501131142000},{"col_ky":"863184","crt_ts":1501131949000}];


}]);

In order to utilize the key and value from ng-repeat within the barData and labels variables in the controller, I would be able to populate the bar chart using barData and labels. Can you provide guidance on how I can achieve this?

Answer №1

After some troubleshooting, I successfully managed to get the jsfiddle code working flawlessly. This code demonstrates how to utilize a custom filter function. Hopefully, this is the solution you have been seeking. One more thing to note is that the line responsible for implementing the custom filter looks like this:

$scope.barData = $filter('countBy')($scope.myData, "col_ky");

Jsfiddle Link: https://jsfiddle.net/Kai_Draord/p3exg6rw/9/

Final Result: https://i.stack.imgur.com/8Ibll.png

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 Problem with Ionic Android Emulator

When using my app, I access an external API to retrieve data. The strange thing is that everything works fine on the browser and the built version, but when I try it on the emulator, the request gets rejected. However, when I test the app on an actual devi ...

angular.js watch() method is not functioning properly during a JSON call

I am trying to trigger a method whenever the value of my $http.selectedSong (model value) changes, but for some reason it is not working. Any ideas on why this could be happening?: app.controller('songController', ['$http', function($h ...

Waiting for Angular pages in Selenium with Protractor JavaScript code

I'm currently in the process of testing an AngularJS page and utilizing Selenium (Java) to create automation scripts for it. Below is the code snippet that I've implemented to ensure synchronization with the page before moving on to the next scr ...

What is the best way to send data via the HTTP POST method in AngularJS?

I have a situation where I am using the same HTTP method in different controllers as shown below: Service: var method="sampleMethod" HotalStatisticService.GetReservations = function (data) { return $http({ method: 'POST' ...

Updating an array within a service across different controllers in AngularJS

My goal is to create an array within a service that can be updated from different controllers. The purpose of this setup is to ensure that the array is accessible across all controllers. I need the ability to add new items to the array as well as delete ex ...

Retrieve JSON Data Using Angular in a Wordpress Environment

I need assistance with displaying a JSON Array in <li>'s within a Wordpress Template. Here is the specific JSON file: I am completely unfamiliar with how to achieve this. This is the HTML code I have: <div ng-app="appExtern" ng- ...

Revamping the code within the controller

I am working on an angular application that retrieves data in JSON format. Here is a sample of the data: [ { "Name": "Main", "Class": "class-o", "Base": 150, "Growth": 10 }, { "Name": "Time", "Cl ...

Encountering issues with gulp-angular-templatecache while processing angular templates through pipelining

I've encountered an issue with gulp-angular-templatecache in my gulpfile. Here's the task causing trouble: gulp.task('templates', function() { return gulp.src(paths.angularTemplates) .pipe(templateCache()) ...

Using AngularJS with angular-google-maps to easily add markers through a form and locate your position with one click

I'm attempting to replicate the functionality demonstrated on since it fulfills 90% of my requirements. However, I'm facing some difficulties. While I can retrieve my location and log it in the console, a marker is not appearing on the map. Add ...

steps to retrieve JSON object using Angular service

In the login.js file, I have a module with a method called loginUser structured like this: ...function loginUser(){ var user={ email:loginVM.Email, password:loginVM.pwd }; console.log(user); loginService.login ...

The 'ngMessages' controller essential for the 'ngMessage' directive is nowhere to be found

Greetings! I am currently in the process of developing an app using angularjs (1.5.0) and following jonhpapa's rules for best practices. In addition, I have incorporated angular material 1.0.6 into the project. However, I have encountered an issue wh ...

What could be the reason that Ng Repeat fails to work when a button is triggered from a separate form

I have an HTML table that includes an ng repeat directive and two buttons. The first button opens a modal with a form to create a new user. When I click save, it adds the new user to the list. The second button is within the same form and also adds a user. ...

Creating an object using a string in node.js

I have a string that I am sending from AngularJS to NodeJS in the following format. "{↵obj:{↵one:string,↵two:integer↵}↵}" //request object from browser console To convert this string into an object and access its properties, I am using the serv ...

AngularJS does not recognize Model as a date object in the input

I am attempting to utilize AngularJS to showcase a date using an input tag with the type attribute set to date: <input ng-model="campaign.date_start" type="date"> Unfortunately, this approach is resulting in the following error message: Error: err ...

Manipulating the DOM with AngularJS directives is not functioning as expected

I am facing an issue with integrating a code that needs to scroll slowly on the page using AngularJS 1.4. When I try to include this code in a directive using the link function(scope, element, attrs), it doesn't work properly. Interestingly, the code ...

Developing an Angular Chart with AJAX

Utilizing the power of angular-chart.js, I have successfully generated a chart with the provided code snippet. However, my goal is to dynamically generate a chart using AJAX calls. Unfortunately, when I attempt to load the chart through AJAX, it fails to r ...

AngularJS, Apache Tomcat, and hashtag URLs

I feel like I'm stuck in a nightmare trying to figure out how to work with Tomcat Apache. My Goal One issue I've encountered in AngularJs is the # problem like www.example.com/#/aboutus After doing some research, I discovered that $locatio ...

There was an error because the variable "items" has not been defined

Having some trouble with an AngularJS service where I am attempting to add an item to an array every 5 seconds. However, I keep encountering the error 'items is not defined' after the first dynamic addition... I've been tinkering with this ...

What is the process for extracting JSON values by specifying keys within a nested JSON structure?

I am attempting to extract specific JSON values for particular keys from a JSON structure. I have made the following attempt: var jsonstring; jsonstring = JSON.stringify(myjsonObjectArray); alert(jsonstring);//displaying the JSON structure below jsonstri ...

Always ensure that only one div is visible at a time

I am currently working on a project where I cannot use ng-cloak due to the way the css files are loaded. I have been exploring alternative solutions and have tried a few different approaches. My main goal is to ensure that two icons are never shown at the ...