Formula
Formula is used to define calculation rule, you can define the formula inside the data-formula
attribute and write the formula like the you write it in excel. There are a lot excel compatible formula can be used from simple SUM
, HLOOKUP
, VLOOKUP
to complex financial function like NPV
, IRR
, etc.
Beside the excel compatible formula, there is also custom formula built in jQuery Calx, they are SERVER()
and GRAPH()
SERVER()
This is special function to perform calculation in server side, you must define ajaxUrl
parameter when configuring calx. The first parameter of this function is the formula name, and the rest is formula parameters
When it invoked, jQuery Calx will send the request to the configured ajaxUrl and wait for the response before continue to the next formula execution.
SERVER('SUM', A1:A3, B4)
Formula above will send request looks like below
[
function => 'SUM',
params => array(
/* range from A1 to A3 */
1 => array(
A1 => 'value of A1',
A2 => 'value of A2',
A3 => 'value of A3'
),
/* the value of B4 */
2 => 'value of B4'
)
]
#ERROR_SEND_REQUEST!
will be returned when error occured.
GRAPH()
This is special function used to draw graphic or chart to represent the data in graphical way. You need to place the GRAPH formula in the div element with specified height and width.
The first parameter is cell range contains data that need to be represented as graphic.
The second parameter is array containing some ‘key=value’ to define how the chart should be rendered.
- type:
type could be one of the following bar, line, pie, or doughnut, default is line. - label:
label is used in bar or line type chart to draw label in the x-axis of the chart. If none is given, the label will be incremental number starting from 0. - legend:
legend is used to give explanation on the chart. If none is given, the legend will be blank - bar_orientation:
if the chart type is bar, you can define bar_orientaion, it could be vertical or horizontal. If none is given, the default will be vertical - show_x_axis:
show_x_axis is used to control the appearance of x axis label, it could be true or false. If none is given, default is true. - show_y_axis:
show_y_axis is used to control the appearance of y axis label, it could be true or false. If none is given, default is true. - reverse:
revers is used to control the order of the data, it could be true or false. If none is given, default is false. - orientation:
orientation is used to define the table orientation, it could be vertical or horizontal, default it horizontal
Horizontal table:
jQuery Calx will parse single row as single series.+-------+-------+-------+-------+-------+
| val 1 | val 2 | val 3 | val 4 | val 5 |
+-------+-------+-------+-------+-------+
| val 1 | val 2 | val 3 | val 4 | val 5 |
+-------+-------+-------+-------+-------+
Vertical table:
jQuery Calx will parse single column as single series.
+-------+-------+
| val 1 | val 1 |
+-------+-------+
| val 2 | val 2 |
+-------+-------+
| val 3 | val 3 |
+-------+-------+
| val 4 | val 4 |
+-------+-------+
| val 5 | val 5 |
+-------+-------+
Is there a way to read the value from a data-attribute? For example:
thank you in advance!
Hello.
I am hooked on the following, I am very pleased if you can help.
I would like to print the result of true / false on the screen in the if loop, but I could not.
How to close true false?
Just i want the results numbers in column A5.
Thank you for interest.
Hi Cenk,
it seems you write similar IF condition twice, the format should be
IF(A4>300, "do something", "do something else")
once more.. 🙂
if i modify … replace(‘$’,”) with …replace(/\$/g,”) it works very nice. I ‘d tested with complex formulas in a sheet with about 120 calculations.
by the way: how to donate? paypal said, your account is not available
Best regards from bernd in germay
Dear ikhsan,
one another change to provide the absolute cell-relation is to do the same replacement of ‘$’ in the parser at line 1854 and it seems to work fine!
parse: function parse(input) {
var self = this,
stack = [0],
vstack = [null],
lstack = [],
table = this.table,
yytext = "",
yylineno = 0,
yyleng = 0,
recovering = 0,
TERROR = 2,
EOF = 1;
var args = lstack.slice.call(arguments, 1);
// --- update to provide absolute cell-relations ---
this.lexer.setInput(input.replace('$',''));
...
Dear ikhsan,
i searched a way to provide absolute cell-relations in calcX and solved this by inserting one line in the
cell.fx.setFormula – method. maybe this is interesting for your?
Than add folowing to the plug-in:
if(typeof(formula) !== ‘string’){
return false;
}
// — update to provide absolute cell-relations —
this.formula = formula.replace(‘$’,”);
if(false !== this.el){
this.el.attr(‘data-formula’, formula);
}…
Several questions regarding the GRAPH option
1. Is it possible to choose the colors used by the various graph options ?
2. The type=bar provides stacked bars, is there an option to get adjacent bars (ie next to each other?)
3. a more general question, you provide the .min.ls version of calx, is there any way to get access to the non minimised version ?
Thanks in advance
Didier
Hi Didier,
GRAPH function actually come as a helper to render simple graph with ease, but it does not support wide range of graph option.
regarding to your question;
1. Unfortunately it’s currently no option for setting up graph color, it is randomly generated.
2. The underlying flot plugin doesn’t support grouped bar, but you can use any other graph plugin together with calx and hook up into
onAfterCalculate
oronBeforeRender
event.3. Yes, it is there in the zip file if you download it from github, all source code is even there.
Best regards,
Ikhsan
Hello Ikhsan
Thank you for your fast response, regarding the source code, you are obviously right, but for some weird reason I could not SEE the source file until you pointed it out!!!!
Hi friend
i have a formula like this data-formula=”[1 – (IND33 / IND5)] * 100″. when IND33 and IND5 are not yet filled, i got result 100. is it possible to display empty or 0 instead?
Hi Keomorakortman,
I think in this case, when IND33 and IND5 is both empty, the calculation will be processed as follow
my suggestion is to do manual check in your formula, something like
How can I list all formulas and respective syntax from calx? I need that to create an user help.
Sergio,
I am sorry that the list of formula and its usage is not documented yet, but you can also refer to excel formula documentation since most of it already implemented.
to be precise, all available formula is located here https://github.com/xsanisty/jquery-calx/tree/master/php-build/include/formula
Is there a way for me to list all the formulas and respective syntax availables? I need that to be able to put in an application help.
Love calx, but now having a problem getting If to work. I have two cells. If a value is entered in one cell, I want one calculation, if entered in the other cell, a different calculation.
I have tried IF(A7>0,A6/A7), IF (A8>0,A8*A1) but get #ERROR
Is there something wrong with this syntax?
Hi Joe,
Yes, it seem your formula isn’t correctly typed, if your formula works on excel, then it should also works with calx
try to change your formula :
regards
Ikhsan
Ikhsan,
Thank you so much for your quick reply. Donation coming your way.
You are welcome Joe, and really appreciate your donation
Thanks!
This formula works great:
0,(A6/1000)*A7,IF(A8>0,A8*A1))” data-format=”$0.0.00″ >
But it puts a “true” in the field if I don’t have numbers for the calculation, (and I don’t on some lines). This gives me an error when I write to the database. Is there anyway to get it blank or a zero in there if I don’t have data to calculate?
Hi Joe,
You can try to add third param of the second IF
That does the trick. Thanks much.
Dear ikhsan,
What is the correct syntax for using the SUMPRODUCT formula?
I tried:
SUMPRODUCT(A1:A10,B1:B10)
but that doesn’t seem to work.This is because of:
arguments[0].length
in the SUMPRODUCT function always equals zero and will never enter the loop.So it always returns result = 0;
Best regards,
Eakdafreak
Hi Eak,
I think there is a bug in the sumproduct function, please try using the 2.2.2 version, I just push the hotfix
regards
Ikhsan
Thank you! Works like a charm now!
Berikut ini yg tak bikin tapi untuk online grafik gak muncul, padahal jquery calx dan numeral sudah di load
Itu mas, kalo mau pake grafik, masukin jquery-flot juga
Iyo mas, grafik yo rak gelem muncul, piye yo, nek tap bikin offline iso ning pas online gak nongol. Solusinye piye? Suwun.
Hi Ikhsan,
Thank you for a great piece of code!
Can you please give an example of charting with d3 (I saw you have it as an option in config –
And another question: is the following the optimal way to pass parameters to custom function on cell change?
Hi Stan,
currently the other chart options is not implemented yet, but you can freely render your chart on
onAfterCalculate
,onBeforeRender
, oronAfterRender
eventregarding to the activeCell, its refer to the currently calculated cell, which can be used in custom formula, if you access the activeCell on
onAfterRender
event, you will get the latest cell that calculated by calx, if you want to get specific cell, you can usegetCell(addr)
Hi ikhsan,
I’m having the same problem mentioned by Diogo…seems that graph works locally, but when I try to put files on web server, the graph doesn’t work.
Firebug console says:
sheet.getCellRange is not a function
What am I doing wrong?
Best regards
Andrea
Hi Andrea,
if it works locally, but not work on your server, maybe you are using the sample version, which is intended to only run at localhost
try to replace with the release version of jQuery Calx
Regards
Ikhsan
Very thanks for reply, but now firebug says:
r.plot is not a function
Sorry but I’m newbie!
Then, you need to load the jquery flot before calx, as it is used for rendering the graph, it is in js folder if you download calx from github, or you can download it from the jquery flot site
Ok, now work perfectly! Thanks again 🙂
Hi ikhsan, I have to say, your job with calx2 it’s the best I found!
But I’m having some trouble using graph function.
You can see my code here: http://codeviewer.org/view/code:495b
Formulas on cells A13, A14 and A15 are working and I loaded every js files.
I just would like a pie graph with macronutrients (protein, carbohydrates and fat).
This values need to be in a table and percentage?
What am I doing wrong?
Thanks for your time and best regards.
Hi Diogo,
what is the actual problem? is the pie chart not rendered, or the label is undefined? or what?
I am trying to reproduce it here, and everything seems to be works properly.
Did you already include the flot and flot-pie plugin?
regards
Ikhsan
Thanks for your (quick) reply ikhsan!
Well my problem is that doesn’t appears anything where the graph should be.
If I use div I get this: http://i.imgur.com/XUnVxEB.jpg
If I use input I get this: http://i.imgur.com/A42uwlT.jpg
And I can’t figure out what I’m doing wrong.
In both images you can see the code related to the cells used in graph() and plugins includes, which are working fine for data-format.
In your sample pages you include bootstrap.js plugin. Is that necessary? You don’t mention it in any pages here.
Seems strange,
bootstrap.js is not necessary, it is js file for twitter bootstrap template.
could you open the developer console tab or firebug console tab and see what’s happen there?
I tried to reproduce it locally, but the graph is rendered properly.
Best regards
Ikhsan
Also, graph formula will always return false if the formula is placed on input or textarea field
How to use below formula in calx2 because AND operator is working fine perfect in calx1 ..but in calx 2 its showing ERROR message .. could you please let me whether below condition removed from if condition
data-formula=”IF($D1 > $D2 AND $D3 > $D4, $D3, $D4″ />
Hi Manish, in calx2, and is no more AND operator, it is now a function
You can use it by using
IF(AND(A1=0, B1=0), E1, F1)