How to integrate Google charts as an AngularJs directive? - angularjs

There are some examples of integrating Google charts as an AngularJs directive.
Like this one: http://plnkr.co/edit/YzwjuU?p=preview
Update: I want to avoid waiting on google charts to be ready before bootstrapping the whole application (as shown in the example above):
google.setOnLoadCallback(function() {
angular.bootstrap(document.body, ['myApp']);
});
Is there a way to do this in a single module and not on the whole app?
Update 2: I have created my plunker and it works without waiting for the callback but I'm not sure if it will work in all cases.
http://plnkr.co/edit/7UUfcq4dD7nd4MylB4ni

Here is a good example of a AngularJs Google Chart Tools directive in action.
Example plunker.
Instructions
These same instructions are in the plunker itself.
Download ng-google-chart.js from github and add a script tag to your html.
Create a div like:
<div google-chart chart="chart" style="{{chart.cssStyle}}">
</div>
Add 'googlechart' to your module like this:
angular.module('myApp',[ 'googlechart', ...
Populate the $scope.chart like this:
{
"type": "ColumnChart",
"cssStyle": "height:200px; width:300px;",
"data": {
"cols": [
{
"id": "month",
"label": "Month",
"type": "string",
"p": {}
},
{
"id": "laptop-id",
"label": "Laptop",
"type": "number",
"p": {}
},
{
"id": "desktop-id",
"label": "Desktop",
"type": "number",
"p": {}
},
{
"id": "server-id",
"label": "Server",
"type": "number",
"p": {}
},
{
"id": "cost-id",
"label": "Shipping",
"type": "number"
}
],
"rows": [
{
"c": [
{
"v": "January"
},
{
"v": 19,
"f": "42 items"
},
{
"v": 12,
"f": "Ony 12 items"
},
{
"v": 7,
"f": "7 servers"
},
{
"v": 4
}
]
},
{
"c": [
{
"v": "February"
},
{
"v": 13
},
{
"v": 1,
"f": "1 unit (Out of stock this month)"
},
{
"v": 12
},
{
"v": 2
}
]
},
{
"c": [
{
"v": "March"
},
{
"v": 24
},
{
"v": 0
},
{
"v": 11
},
{
"v": 6
}
]
}
]
},
"options": {
"title": "Sales per month",
"isStacked": "true",
"fill": 20,
"displayExactValues": true,
"vAxis": {
"title": "Sales unit",
"gridlines": {
"count": 6
}
},
"hAxis": {
"title": "Date"
}
},
"formatters": {},
"displayed": true
}

As you already figured out, you can initialize angular in the html or body tag, without waiting for google charts.
To ensure you don't try to render a chart before the google chart JavaScript code is ready,
I would have the directive $watch a new controller $scope property/flag that you set inside the callback function for google.setOnLoadCallback. Inside the $watch callback, check to ensure the flag is set, then do your initialization.

There is Github project that wraps Google Charts into AngularJS directive
https://github.com/angular-google-chart/angular-google-chart
http://angular-google-chart.github.io/angular-google-chart/

I was having periodic trouble in Angular with the google chart API not being loaded on time to receive the data from a $http fetch. The data came first sometimes (not always), and then the usage of it in the callback would fail with "google.visualization.DataTable is not a function"
Inside the callback returning data:
var dataTable = new google.visualization.DataTable(data);
To solve it, I found in ng-google-chart.js that there was a promise called "googleChartApiPromise", so I injected it, and wrapped the refresh call in it:
googleChartApiPromise.then(function() {
refreshCharts();
});
This seems to solve the problem.

Related

How to work with multistep form in react js with Nested FormArray using React Hook Form

I'm new in react. Need help to create form with multiple nested field array like I have provide json, I have used react hook form for validation.
Facing issue to create dynamic setting,model array data form elements,
I need to create below Json on form submit
{
"field_data": [
{
"id": 1,
"fields": [
{
"name": "Field 1",
"config": {
"type": "Number",
"setting": {
"model": [
{
"id": 7,
"value": []
},
{
"id": 6,
"value": []
}
]
},
"extra_info": {
"suffix": ""
}
}
},
{
"name": "Field 2",
"config": {
"type": "Any",
"setting": {
"model": []
},
"extra_info": {
"suffix": ""
}
}
},
{
"name": "Field 3",
"config": {
"type": "Any",
"setting": {
"model": []
},
"extra_info": {
"suffix": ""
}
}
}
]
}
],
"name": "sacascasc"
}
In above json input will be "model" and "extra_info"
How to work with multiple nexted formarray
Thank you in advance.

AngularJS scope of variable [duplicate]

This question already has answers here:
not able to access $scope object outside a http response callback function in angularjs
(3 answers)
Closed 5 years ago.
I'm trying to get a json file within a a controller with $http.get. All well until I try to access the property which holds the response. If I try console.log(property) inside the $http.get() it outputs the returned object, outside $http.get() accessing the very same property and it outputs undefined!
I even try to attache it to the $scope and it's the same result?!
angular.module('todayfmApp')
.controller('MainCtrl', ['$http', '$scope', function ($http, $scope) {
var self = this;
$http.get('data/form-data.json').then(function(response) {
self.formdata = response.data;
console.log(self.formdata);
});
console.log(self.formdata);
}]);
{
"settings": {
"version": "",
"Step": "",
"filterBreak": "",
"pid": ""
},
"category": [
{ "name": "Select All", "selected": true },
{ "name": "Carlow", "value": "Carlow" },
{ "name": "Cavan", "value": "Cavan" },
{ "name": "Clare", "value": "Clare" },
{ "name": "Cork", "value": "Cork" },
{ "name": "Derry", "value": "Derry" },
{ "name": "Donegal", "value": "Donegal" },
{ "name": "Down", "value": "Down" },
{ "name": "Dublin", "value": "Dublin" },
{ "name": "Galway", "value": "Galway" },
{ "name": "Kerry", "value": "Kerry" },
{ "name": "Kildare", "value": "Kildare" },
{ "name": "Kilkenny", "value": "Kilkenny" },
{ "name": "Laois", "value": "Laois" },
{ "name": "Leitrim", "value": "Leitrim" },
{ "name": "Limerick", "value": "Limerick" },
{ "name": "Louth", "value": "Louth" },
{ "name": "Mayo", "value": "Mayo" },
{ "name": "Meath", "value": "Meath" },
{ "name": "Monaghan", "value": "Monaghan" },
{ "name": "Offaly", "value": "Offaly" },
{ "name": "Roscommon", "value": "Roscommon" },
{ "name": "Sligo", "value": "Sligo" },
{ "name": "Tipperary", "value": "Tipperary" },
{ "name": "Waterford", "value": "Waterford" },
{ "name": "Westmeath", "value": "Westmeath" },
{ "name": "Wexford", "value": "Wexford" },
{ "name": "Wicklow", "value": "Wicklow" }
],
"num_nights": [
{ "name": "1 Night", "value": 1, "selected": true},
{ "name": "2 Nights", "value": 2 },
{ "name": "3 Nights", "value": 3 },
{ "name": "4 Nights", "value": 4 },
{ "name": "5 Nights", "value": 5 },
{ "name": "6 Nights", "value": 6 },
{ "name": "7 Nights", "value": 7 }
],
"num_rooms": [
{ "name": "1 Room", "value": 1, "selected": true },
{ "name": "2 Rooms", "value": 2 },
{ "name": "3 Rooms", "value": 3 },
{ "name": "4 Rooms", "value": 4 }
],
"num_adults": [
{ "name": "1 Adult", "value": 1 },
{ "name": "2 Adult", "value": 2, "selected": true }
],
"num_child": [
{ "name": "0 Kids", "value": 0, "selected": true },
{ "name": "1 Kids", "value": 1 },
{ "name": "2 Kids", "value": 2 }
]
}
In your case the problem is that $http.get response data is a late binding method. The response of this method will be available only after the service call is finished. This is an asynchronous method so that this will not be accessible inside scope as you expect. For that you need to bind your response inside a $scope variable and use it later inside a function call.
You will need to call the self.getResponseData method at first, say at the time of controller initialization. There after call a method self.logResponseData to log the response data.
You may need to change your calls something like this.
angular.module('todayfmApp')
.controller('MainCtrl', ['$http', '$scope', function ($http, $scope) {
var self = this;
// Get the response data with service call
self.getResponseData = function(){
$http.get('data/form-data.json').then(function(response) {
self.formdata = response.data;
console.log(self.formdata);
});
}
//Method to read the response data later
self.logResponseData = function() {
console.log(self.formdata);
}
//Call the method at the time controller loaded to initialize the `self` variable
self.getResponseData();
}]);
self.formdata = response.data.jsondatavariablename;
// Use the JSON data name
$http.get() is asynchronous which roughly means that when you call this it sends request and doesn't stop code on here to wait until it responded, next statements execution is continued though response is still hasn't come and you have no data from it. That's why you need callback function in .then() method, it is being called when you get your response, where you can use your response data. For example, you can assign it to some $scope property and it will update this value everywhere you use this property.
$http uses so called Promises to achieve this. I tried to make it simple but if you want know more detailed how it works, guess that's what will help you: Promise

Fetch and display data from web service in ionic/angularJS

I am building an app using ionic/AngularJS. To test the web service calls I am using the best buy service. The response is as follows:
{
"from": 1,
"to": 10,
"total": 4665,
"currentPage": 1,
"totalPages": 467,
"queryTime": "0.009",
"totalTime": "0.045",
"partial": false,
"canonicalUrl": "/v1/categories?format=json&apiKey=****",
"categories": [
{
"id": "abcat0010000",
"name": "Gift Ideas",
"active": true,
"path": [
{
"id": "cat00000",
"name": "Best Buy"
},
{
"id": "abcat0010000",
"name": "Gift Ideas"
}
],
"subCategories": [
{
"id": "pcmcat140000050035",
"name": "Capturing Photos & Videos"
},
{
"id": "pcmcat140000050036",
"name": "Listening to Digital Music"
},
{
"id": "pcmcat140000050037",
"name": "Computing Made Easy"
},
{
"id": "pcmcat140000050039",
"name": "Simple GPS Navigation"
},
{
"id": "pcmcat140000050040",
"name": "Playing Video Games"
},
{
"id": "pcmcat140000050041",
"name": "Watching HDTV"
},
.........
How can I retrieve and display this information as a list on my device? I am aware of the $http service request, but how do I incorporate this into an ionic list?
Assuming that you already have written the logic to fetch data I have written this JSBin for implementing ion-list.
Let's Assume you are going to display the name field in your list.
app.js
var baseURL="*here your json request*";
var categoriesName=[];
$http.get('baseURL')
.success(function(response){
$scope.objectResponse = response.categories;
console.log($scope.objectResponse);
$scope.objectResponse.forEach(function(item) {
categoriesName.push(item.name);
console.log(categoriesName);
})
.error(function(response){
console.log(response);
});
html
<ul class="list">
<li class="item" ng-repeat="listname in categoriesName">
{{listname}}
</li>
</ul>
This is How your can retrieve and display this information as a list on device.

$elemMatch query in looback-angularjs sdk with loopback-mongodb-connector?

I have a collection called Dish in mongodb the sample data is -
[{
"_id": ObjectId("56c839b969431a9913cafa65"),
"name": "Dish 01",
"description": "Dish 01 description",
"ingredients": [{
"id": "56c4a40bf97c039d44e89185",
"name": "onion",
}, {
"id": "56c4a40bf97c039d44e89145",
"name": "salt",
}, {
"id": "56c4a40bf97c039d44e89176",
"name": "spices",
}],
"category": "entree",
"image": "https://s3-ap-southeast-1.amazonaws.com/sichu-bucket/2016-02-20T13:10:31.334Zapi.js"
}, {
"_id": ObjectId("56c839b969431a9913cafa70"),
"name": "Dish 02",
"description": "Dish 02 description",
"ingredients": [{
"id": "56c4a40bf97c039d44e89185",
"name": "onion",
}, {
"id": "56c4a40bf97c039d44e89145",
"name": "oil",
}],
"category": "main",
"image": "https://s3-ap-southeast-1.amazonaws.com/sichu-bucket/2016-02-20T13:10:31.334Zapi.js"
}]
now I want dishes which has onion in it and I want to get result by using loopback-angularjs-sdk
I have added this to my Dish.json file
"settings": {
"mongodb": {
"allowExtendedOperators": true
}
}
I don't know how it should be done. Please guide me into this.
In your ngApp controller pass the Dish object, and then you can use the Dish.find filter to create a filter api, which will internally search mongo for all the dishes with indegredients as onion.
Dish.find({"filter": {"where": {"indegredients.id": "56c4a40bf97c039d44e89185"}}}, function (dishes) {
});
For more on filter api, or how to call them through angular sdk you can use.
https://docs.strongloop.com/display/public/LB/Where+filter
https://docs.strongloop.com/display/public/LB/AngularJS+JavaScript+SDK
Plus you can run a local server for the loopback angular sdk documentation also.
For generating the angular sdk documentation, what you can do is
lb-ng-doc client/js/services/lb-services.js
Browse the documentation at http://localhost:3030/

How can I convert object data to an array for use with bs-typeahead in AngularJS?

My data looks like:
[
{
"_id": "531dbf8b9b9fc50000a8d611",
"active": true,
"client": {
"_id": "531dbf8b9b9fc50000a8d60e",
"name": "TR"
},
"company_id": "531dbf8b9b9fc50000a8d60c",
"createdOn": "2014-03-10T13:35:07.313Z",
"description": "Gentle Action Application Pads",
"dimensions": {
"weight": 22.4
},
"lot": [
],
"meta": {
"category": "Face",
"msrp": 7.75
},
"sku": "11002",
"unit_of_measure": "each",
"updatedOn": "2014-03-10T13:35:07.314Z"
},
{
"_id": "531dbf8b9b9fc50000a8d612",
"active": true,
"client": {
"_id": "531dbf8b9b9fc50000a8d60e",
"name": "TR"
},
"company_id": "531dbf8b9b9fc50000a8d60c",
"createdOn": "2014-03-10T13:35:07.317Z",
"description": "Skin Renewal System - Enriched (CA)",
"dimensions": {
"weight": 22.4
},
"lot": [
],
"meta": {
"category": "Face",
"msrp": 321.6
},
"sku": "11700CA",
"unit_of_measure": "each",
"updatedOn": "2014-03-10T13:35:07.318Z"
}
]
In my view, I have:
<input type="text" ng-model="receivingSku" placeholder="Locations loaded via $http" typeahead="sku for sku in getSku($viewValue) | filter:$viewValue" typeahead-on-select="selectedSku()" class="form-control">
I need the sku field for each item to be in the typeahead. How can I accomplish this?
Presently, I get an error:
Error: matches is undefined
You can loop over your original array and create an array of just SKU's:
var skus = origArray.map(function(e) {
return e.sku;
});
The new array can be used for typeahead.
PS - I'm not very familiar with typeahead. If typeahead is capable of peeking into objects, you don't need to do this.

Resources