Putting data from a database into highcharts using $.getJSON - highcharts

I'm having some problems with $.getJSON, I really need some help because I don't know how to solve the problem and I've been stuck here for days. This is the js file that should generate highcharts when selecting a select option in HTML:
$(document).ready(function() {
$("#paese").change(function(){
var seriesOptions = [];
$.getJSON("prova.php", function(data) {
seriesOptions = data;
});
var chart1 = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'column',
spacingLeft: 20,
borderWidth: 1
},
....
series: [{
name: 'Italia',
data: seriesOptions
}],
Is there anything wrong in the first part? When I select an option, it seems like highcharts don't get the php file, but I'm pretty sure it's correct, here it is(PHP file):
<?PHP
header("Content-Type: application/json; charset=UTF-8");
$conn = mysql_connect("localhost", "root");
$paese = null;
if(isset($_GET['paese'])) $paese = $_GET['paese'];
$ok = mysql_select_db("liberta", $conn);
$response = array();
$sql="SELECT `valori`.Punteggio FROM `valori` INNER JOIN `nazioni` ON `valori`.Nazione
= `nazioni`.ID WHERE `nazioni`.Nome = '$paese'";
$res=mysql_query($sql);
while($record=mysql_fetch_row($res)){
$response[] = intval("$record[0]");
}
mysql_close($conn);
print(json_encode($response));
I'm trying to get the data from a database I created with PHPmyadmin and put them directly into highcharts, but it doesn't work. I'd be very pleased if you could help me, also because this is is an exam I have to sit. Thank you very much.

I advice to familair with article about preprocessing data http://docs.highcharts.com/#preprocessing.
You need to have strucuture like:
{
data:[1,2,3]
}
Try to replace
while($record=mysql_fetch_row($res)){
$response[] = intval("$record[0]");
}
with
while($record=mysql_fetch_row($res)){
$response['data'][] = intval($record[0]);
}

Your problem is with the javascript part. When you call $.getJSON, the callback you provide will be called asynchronously when the server responds. Howether in your code you are calling the Highcharts() constructor immediately in a synchronous way. At the point its called the seriesOption variable still equals to []. Try calling the constructor from the callback like this:
$.getJSON("prova.php", function(data) {
var chart1 = new Highcharts.Chart(....
series: [{
name: 'Italia',
data: data}])});

Related

Trying to Script a Search Result into a Netsuite Entity Field

Having two issues with this. One is that I keep getting an error when trying to upload my script. The other is that one version that I did get to upload, didn't load any value into the field (ie. field blank after script ran)
The error I keep getting on upload is "Fail to evaluate script: All SuiteScript API Modules are unavailable while executing your define callback." And although I've made drastic changes to the script, it still won't allow me to upload.
/**
*#NApiVersion 2.x
*#NScriptType ScheduledScript
*/
define(['N/search', "N/record"],
function(search, record) {
function loadAndRunSearch(scriptContext) {
var mySearch = search.load({
id: 'customsearch1088'
});
mySearch.run().each(function (result) {
var countt = result.getValue({
name: 'number'
});
var entity = result.getValue({
name: 'internalid'
});
var objRecord = record.load({
type: record.Type.CUSTOMER,
id: entity,
isDynamic: true,
});
var vfield = objRecord.getField({
fieldId: 'custentity_orders_12m'
});
objRecord.setValue({fieldId: 'custentity_orders_12m', value: countt});
objRecord.save();
});
}
return {
execute: loadAndRunSearch
};
});
That's the script stripped down to the bare bones (FYI still doesn't upload), and the script that uploaded was basically a more complicated version of the same script, except it didn't set the field value. Can anyone see where I've gone wrong?
You haven't returned the entry function.
/**
*#NApiVersion 2.x
*#NScriptType ScheduledScript
*/
define(['N/search', 'N/record'],
function(search, record) {
function loadAndRunSearch(scriptContext) {
var mySearch = search.load({
id: 'customsearch1088'
});
mySearch.run().each(function (result) {
var countt = result.getValue({
name: 'number'
});
var entity = result.getValue({
name: 'internalid'
});
record.submitField({
type: record.Type.CUSTOMER,
id: entity,
values: {
'custentity_orders_12m' :countt
}
});
});
}
return {
execute : loadAndRunSearch
}
});

Splitting Yeoman prompts into multiple separate groups

how would one split a yeoman prompt into parts?
I have a rather extended prompt that i'd like to split into parts with a title for each part.
CSS
- prompt1
HTML
-prompt 2
Something like this:
prompt1: function(){
var done = this.async();
condole.log('title 1');
var prompts = [{
name: 'prompt1',
message: 'Prompt 1:',
}]
},
prompt2: function(){
var done = this.async();
condole.log('title 2');
var prompts = [{
name: 'prompt2',
message: 'Prompt 2:',
}]
},
Thanks!
Update as #Deimyts notes in the comments, the original code stopped working. This is due to API changes in Inquirer.JS documented here.
The base API interface is now inquirer.prompt(questions).then(). There's no more callback function.
Any async question functions is taking a promise as return value instead of requiring this.async().
In a nutshell, instead of using the old var done = this.async() API and resolving the prompt inside the callback with done() just return a promise from the prompting functions (see docs).
prompt1: function() {
this.log("HTML")
return this.prompt([
// configure prompts as before
]).then(function (answers) {
// callback body as before, but no more calling done()
}.bind(this));
},
For more details see #Deimyts answer below.
Yeoman uses a run loop with certain predefined priorities that you can use to put your actions into. As mentioned in the ☞ docs you can group several methods at one priority. Here is a snippet to illustrate a generator with prompts split into two groups HTML and CSS:
'use strict';
var generators = require('yeoman-generator');
module.exports = generators.Base.extend({
constructor: function () {
generators.Base.apply(this, arguments);
},
prompting: {
prompt1: function() {
this.log("HTML")
var done = this.async();
this.prompt([{
type : 'input',
name : 'foo',
message : 'Foo',
}, {
type : 'input',
name : 'bar',
message : 'Bar'
}], function (answers) {
this.foo = answers.foo;
this.bar = answers.bar;
done();
}.bind(this));
},
prompt2: function() {
this.log("CSS")
var done = this.async();
this.prompt([{
type : 'input',
name : 'bam',
message : 'Bam',
}], function (answers) {
this.bam = answers.bam;
done();
}.bind(this));
}
},
configuring: function () {
console.log(this.foo, this.bar, this.bam);
}
});
Using this feature of Yeoman you could modularize your code even further, e.g. by putting your different prompts in separate code files and require / import them into your generator file. But basically the above snippet should do the trick.
Let me know if that helps.
The previous answer wasn't working for me until I made several modifications to the example code.
I can't be 100% certain, but I believe that the difference might be due to differing versions of the yeoman-generator module. So, I'm recording this here in case anyone else runs into the same issue.
For reference, I'm using yeoman-generator v0.23.4, yo v1.8.4, node v6.2.2, & npm v3.9.5.
Modifications:
Remove all instances of var done = this.async(); and done().
The async() function was causing the generator to exit after prompt1, and never run prompt2 or the configuring function.
Add return before calling this.prompt();
Removing async() causes the generator to rush through the prompts without waiting for an answer. Adding return fixes this.
Replace the callback function inside this.prompt() with .then().
Before making this change, the generator would run through the prompts correctly, but would not save the answers, and configuring would simply log undefined undefined undefined.
Original: this.prompt(prompts, callback(answers).bind(this))
Revised: this.prompt(prompts).then(callback(answers).bind(this));
Full Example:
'use strict';
var generators = require('yeoman-generator');
module.exports = generators.Base.extend({
constructor: function () {
generators.Base.apply(this, arguments);
},
prompting: {
prompt1: function() {
this.log("HTML")
return this.prompt([{
type : 'input',
name : 'foo',
message : 'Foo',
}, {
type : 'input',
name : 'bar',
message : 'Bar'
}]).then(function (answers) {
this.foo = answers.foo;
this.bar = answers.bar;
}.bind(this));
},
prompt2: function() {
this.log("CSS")
return this.prompt([{
type : 'input',
name : 'bam',
message : 'Bam',
}])
.then(function(answers) {
this.bam = answers.bam;
}.bind(this));
}
},
configuring: function () {
console.log(this.foo, this.bar, this.bam);
console.log('Config: ', this.config);
},
});

Manage multiple highchart charts in a single webpage

I am having multiple highchart charts of various types(Bar,Pie, Scatter type) in a single web page. Currently I am creating config object for each graph like,
{
chart : {},
blah blah,
}
And feeding them to a custom function which will just call HighCharts.chart(). But this results in duplication of code. I want to manage all this chart rendering logic centrally.
Any Idea on how to do this?
You can use jQuery.extend() and Highcharts.setOptions.
So first you'll make the first object which will be extended by all your charts, this object will contain your Highchart default functions.
You can do it using namespacing.
The following way is good when you have very different charts.
Default graphic:
var defaultChart = {
chartContent: null,
highchart: null,
defaults: {
chart: {
alignTicks: false,
borderColor: '#656565',
borderWidth: 1,
zoomType: 'x',
height: 400,
width: 800
},
series: []
},
// here you'll merge the defauls with the object options
init: function(options) {
this.highchart= jQuery.extend({}, this.defaults, options);
this.highchart.chart.renderTo = this.chartContent;
},
create: function() {
new Highcharts.Chart(this.highchart);
}
};
Now, if you want to make a column chart, you'll extend defaultChart
var columnChart = {
chartContent: '#yourChartContent',
options: {
// your chart options
}
};
columnChart = jQuery.extend(true, {}, defaultChart, columnChart);
// now columnChart has all defaultChart functions
// now you'll init the object with your chart options
columnChart.init(columnChart.options);
// when you want to create the chart you just call
columnChart.create();
If you have similar charts use Highcharts.setOptions which will apply the options for all created charts after this.
// `options` will be used by all charts
Highcharts.setOptions(options);
// only data options
var chart1 = Highcharts.Chart({
chart: {
renderTo: 'container1'
},
series: []
});
var chart2 = Highcharts.Chart({
chart: {
renderTo: 'container2'
},
series: []
});
Reference
http://api.highcharts.com/highcharts#Highcharts.setOptions%28%29
COMPLETE DEMO
I know this has already been answered, but I feel that it can be taken yet further. I'm still newish to JavaScript and jQuery, so if anyone finds anything wrong, or thinks that this approach breaks guidelines or rules-of-thumb of some kind, I'd be grateful for feedback.
Building on the principles described by Ricardo Lohmann, I've created a jQuery plugin, which (in my opinion) allows Highcharts to work more seamlessly with jQuery (i.e. the way that jQuery works with other HTML objects).
I've never liked the fact that you have to supply an object ID to Highcharts before it draws the chart. So with the plug-in, I can assign the chart to the standard jQuery selector object, without having to give the containing <div> an id value.
(function($){
var chartType = {
myArea : {
chart: { type: 'area' },
title: { text: 'Example Line Chart' },
xAxis: { /* xAxis settings... */ },
yAxis: { /* yAxis settings... */ },
/* etc. */
series: []
},
myColumn : {
chart: { type: 'column' },
title: { text: 'Example Column Chart' },
xAxis: { /* xAxis settings... */ },
yAxis: { /* yAxis settings... */ },
/* etc. */
series: []
}
};
var methods = {
init:
function (chartName, options) {
return this.each(function(i) {
optsThis = options[i];
chartType[chartName].chart.renderTo = this;
optsHighchart = $.extend (true, {}, chartType[chartName], optsThis);
new Highcharts.Chart (optsHighchart);
});
}
};
$.fn.cbhChart = function (action,objSettings) {
if ( chartType[action] ) {
return methods.init.apply( this, arguments );
} else if ( methods[action] ) {
return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
} else if ( typeof action === 'object' || !action ) {
$.error( 'Invalid arguments to plugin: jQuery.cbhChart' );
} else {
$.error( 'Action "' + action + '" does not exist on jQuery.cbhChart' );
}
};
})(jQuery);
With this plug-in, I can now assign a chart as follows:
$('.columnChart').cbhChart('myColumn', optionsArray);
This is a simplistic example of course; for a real example, you'd have to create more complex chart-properties. But it's the principles that concern us here, and I find that this approach addresses the original question. It re-uses code, while still allowing for individual chart alterations to be applied progressively on top of each other.
In principle, it also allows you to group together multiple Ajax calls into one, pushing each graph's options and data into a single JavaScript array.
The obligatory jFiddle example is here: http://jsfiddle.net/3GYHg/1/
Criticism welcome!!
To add to #Ricardo's great answer, I have also done something very similar. In fact, I won't be wrong if i said I went a step further than this. Hence would like to share the approach.
I have created a wrapper over the highchart library. This gives multiple benefits, following being the main advantages that encouraged going in this path
Decoupling: Decouples your code from highcharts
Easy Upgrades: This wrapper will be the only code that will require modification in case of any breaking changes in highchart api after upgrades, or even if one decides to move to a differnt charting library altogether (even from highchart to highstock can be exhaustive if your application uses charts extensively)
Easy of use: The wrapper api is kept very simple, only things that may vary are exposed as options (That too whose values won't be as a deep js object like HC already has, mostly 1 level deep), each having a default value. So most of the time our chart creation is very short, with the constructor taking 1 options object with merely 4-5 properties whose defaults don't suit the chart under creation
Consistent UX: Consistent look & feel across the application. eg: tool tip format & position, colors, font family, colors, toolbar (exporting) buttons, etc
Avoid duplication: Of course as a valid answer of the asked question it has to avoid duplication, and it does to a huge extent
Here is what the options look like with their default values
defaults : {
chartType : "line",
startTime : 0,
interval : 1000,
chartData : [],
title : "Product Name",
navigator : true,
legends : true,
presetTimeRanges : [],
primaryToolbarButtons : true,
secondaryToolbarButtons : true,
zoomX : true,
zoomY : false,
height : null,
width : null,
panning : false,
reflow : false,
yDecimals : 2,
container : "container",
allowFullScreen : true,
credits : false,
showAll : false,
fontSize : "normal", // other option available is "small"
showBtnsInNewTab : false,
xAxisTitle : null,
yAxisTitle : null,
onLoad : null,
pointMarkers : false,
categories : []
}
As you can see, most of the times, its just chartData that changes. Even if you need to set some property, its mainly just true/false types, nothing like the horror that highchart constructor expects (not critizing them, the amount of options they provide is just amazing from customization Point of View, but for every developer in the team to understand & master it can take some time)
So creation of chart is as simple as
var chart=new myLib.Chart({
chartData : [[1000000,1],[2000000,2],[3000000,1],[4000000,5]]
});

Problem with heatmaps in Google Fusion Tables

These are my two functions for searching my table and making overlays. (the map is created in a different function). For some reason, the dotmap one works fine, but the heatmap one doesn't work with the where clause. Any insight?
function heatmap() {
var layer = new google.maps.FusionTablesLayer({
query: {
select: 'LOCATION',
from: '1614684',
where: "CRIME = 'HOMICIDE'"
},
heatmap: {
enabled: true
}
});
layer.setMap(map);
}
function dotmap() {
var layer = new google.maps.FusionTablesLayer({
query: {
select: 'LOCATION',
from: '1614684',
where: "CRIME = 'HOMICIDE'"
}
});
layer.setMap(map);
}
I think the problem is the same as mentioned here:
https://groups.google.com/forum/#!topic/fusion-tables-users-group/MkZ8KJT6oic
In short, Heatmaps in Google Fusion Tables is a little flaky. You might want to use something like gheat which somehow worked, but not as nice and simple as I wanted.

Retrieve Grid Id on CellEdit JqGrid

i was trying to do a Cell Editing based on this documentation
http://www.trirand.com/jqgridwiki/doku.php?id=wiki:cell_editing
I have two questions:
How can i get the Index of my row posted to the server:
The information i'm getting posted is the following:
a) value of the cell
b) RowId
The thing is that the rowId doesn't help me. I need the actual Id of the information I'm displaying so i can do the server update with that Id.
colNames: ['Id', 'Codigo', 'Nombre'],
colModel: [
{ name: 'Id', index: 'Id', width: 50, align: 'left', hidden: true },
{ name: 'Codigo', index: 'Codigo', width: 55, align: 'left', editable: true, editrules:
{ number: true} },
{ name: 'Nombre', index: 'Nombre', width: 200, align: 'left' }],
I need the value of the column 'Id' to do my update.
2.I don't understand in the documentation how to manage an error from the server, so I can display the error message.
Thank you very much!
Notes:
a) I've already asked in the forum of trirand, but no one reply it to me.
b) If anyone has done this, it would help if help me pasting the code.
c) I'm working on MVC 2 Asp.net
mostly one you inline editing or form editing and not cell editing. I recommend you to switch to one of the two modern form editing or you
The information RowId is already the value of the column 'Id'. getInd(rowid,false) method returns the index of the row in the grid table specified by id=rowid.
To be able to display error returns from server you needs know the format of data returned from server in the error case. If error returned from server have, for example, JSON format {"Detail":"error text"} (errors from WFC service) you can define loadError parameter of jqGrid like:
loadError: function(xhr, st, err) { alert(errorTextFormat(xhr)); }
where errorTextFormat function which decode the error message and can looks like
var errorTextFormat = function (data) {
var str = data.responseText.substr(0, 10);
if (str === '{"Detail":') {
var errorDetail = jQuery.parseJSON(data.responseText);
var s = "Error: '";
s += data.statusText;
s += "'. Details: ";
s += errorDetail.Detail;
return s;
} else {
var res = "Status: '";
res += data.statusText;
res += "'. Error code: ";
res += data.status;
return res;
}
};
The same function you can use to decode errors of row editing (at least inline editing or form editing). ASP.NET MVC returns mostly messages in HTML format so your error decoding function should be another. I don't use cell editing as the most other people do, so can not help you in the case or customizing of the error messages in cell editing.
Q1:
you can use Key:true , editable: true, in colModel
{ key:true, name: 'Id', index: 'Id', width: 50, align: 'left', editable: true, hidden:true}
Then in add/edit method ( add beforeShowForm method in add/edit method), you have to explicitly hide this field the field of id inside of beforeShowForm method
$('#tr_Id').hide();
i.e
beforeShowForm: function (e) {
$('#tr_Id').hide();
}
Q2:
add 'afterSubmit' method in add/edit/delete method , i'm using Web api Server ,
i.e
afterSubmit: function (response) {
if (response.statusText == 'Created') {
// alert("Create Successfully")
ShowMessage("Add Successfully", 'Success');
//reload the grid
$(this).jqGrid("setGridParam", { datatype: 'json' });
return [true];
}
else {
ShowMessage("Operation Failed", 'Error');
return [false];
}
},
I hope this will work for you. Still u Need Any kind of help please comment below

Resources