added sales analytics
diff --git a/erpnext/accounts/page/financial_analytics/financial_analytics.js b/erpnext/accounts/page/financial_analytics/financial_analytics.js
index 1f0a01d..6fbecd6 100644
--- a/erpnext/accounts/page/financial_analytics/financial_analytics.js
+++ b/erpnext/accounts/page/financial_analytics/financial_analytics.js
@@ -164,13 +164,5 @@
 		});
 	
 		return data;
-	},
-	get_plot_options: function() {
-		return {
-			grid: { hoverable: true, clickable: true },
-			xaxis: { mode: "time", 
-				min: dateutil.str_to_obj(this.from_date).getTime(),
-				max: dateutil.str_to_obj(this.to_date).getTime() }
-		}
-	},
+	}
 })
\ No newline at end of file
diff --git a/erpnext/patches/patch_list.py b/erpnext/patches/patch_list.py
index dbd4b8c..d0ec56d 100644
--- a/erpnext/patches/patch_list.py
+++ b/erpnext/patches/patch_list.py
@@ -584,4 +584,8 @@
 		'patch_module': 'patches.september_2012',
 		'patch_file': 'add_stock_ledger_entry_index',
 	},
+	{
+		'patch_module': 'patches.september_2012',
+		'patch_file': 'plot_patch',
+	},
 ]
diff --git a/erpnext/patches/september_2012/plot_patch.py b/erpnext/patches/september_2012/plot_patch.py
new file mode 100644
index 0000000..fda0a27
--- /dev/null
+++ b/erpnext/patches/september_2012/plot_patch.py
@@ -0,0 +1,32 @@
+import webnotes
+def execute():
+	set_master_name_in_accounts()
+	set_customer_in_sales_invoices()
+	reset_lft_rgt()
+
+def set_master_name_in_accounts():
+	accounts = webnotes.conn.sql("""select name, account_name, master_type from tabAccount
+		where ifnull(master_name, '')=''""", as_dict=1)
+	for acc in accounts:
+		if acc["master_type"] in ["Customer", "Supplier"]:
+			master = webnotes.conn.sql("""select name from `tab%s`
+				where name=%s """ % (acc["master_type"], "%s"), acc["account_name"])
+			if master:
+				webnotes.conn.sql("""update `tabAccount`
+					set master_name=%s where name=%s""", (master[0][0], acc["name"]))
+	
+def set_customer_in_sales_invoices():
+	webnotes.conn.sql("""update `tabSales Invoice` si
+		set si.customer=(select a.master_name from `tabAccount` a where a.name=si.debit_to)
+		where ifnull(si.customer, '')=''""")
+		
+def reset_lft_rgt():
+	from webnotes.utils.nestedset import rebuild_tree
+	
+	rebuild_tree("Item Group", "parent_item_group")
+	rebuild_tree("Customer Group", "parent_customer_group")
+	rebuild_tree("Territory", "parent_territory")
+	rebuild_tree("Account", "parent_account")
+	rebuild_tree("Cost Center", "parent_cost_center")
+	rebuild_tree("Sales Person", "parent_sales_person")
+	
\ No newline at end of file
diff --git a/erpnext/selling/page/sales_analytics/__init__.py b/erpnext/selling/page/sales_analytics/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/erpnext/selling/page/sales_analytics/__init__.py
diff --git a/erpnext/selling/page/sales_analytics/sales_analytics.js b/erpnext/selling/page/sales_analytics/sales_analytics.js
new file mode 100644
index 0000000..8a0d9b2
--- /dev/null
+++ b/erpnext/selling/page/sales_analytics/sales_analytics.js
@@ -0,0 +1,262 @@
+wn.pages['sales-analytics'].onload = function(wrapper) { 
+	wn.ui.make_app_page({
+		parent: wrapper,
+		title: 'Sales Analytics',
+		single_column: true
+	});
+	new erpnext.SalesAnalytics(wrapper);
+}
+
+erpnext.SalesAnalytics = wn.views.GridReportWithPlot.extend({
+	init: function(wrapper) {
+		this._super({
+			title: "Sales Analytics",
+			page: wrapper,
+			parent: $(wrapper).find('.layout-main'),
+			appframe: wrapper.appframe,
+			doctypes: ["Item", "Item Group", "Customer", "Customer Group", "Company",
+				"Sales Invoice", "Sales Invoice Item", "Territory"],
+			tree_grid: { show: true }
+		});
+		
+		this.tree_grids = {
+			"Customer Group": {
+				label: "Customer Group / Customer",
+				show: true, 
+				item_key: "customer",
+				parent_field: "parent_customer_group", 
+				formatter: function(item) {
+					// return repl('<a href="#Report2/stock-invoices/customer=%(enc_value)s">%(value)s</a>', {
+					// 		value: item.name,
+					// 		enc_value: encodeURIComponent(item.name)
+					// 	});
+					return item.name;
+				}
+			},
+			"Customer": {
+				label: "Customer",
+				show: false, 
+				item_key: "customer",
+				formatter: function(item) {
+					return item.name;
+				}
+			},	
+			"Item Group": {
+				label: "Item",
+				show: true, 
+				parent_field: "parent_item_group", 
+				item_key: "item_code",
+				formatter: function(item) {
+					return item.name;
+				}
+			},	
+			"Item": {
+				label: "Item",
+				show: false, 
+				item_key: "item_code",
+				formatter: function(item) {
+					return item.name;
+				}
+			},	
+			"Territory": {
+				label: "Territory / Customer",
+				show: true, 
+				item_key: "customer",
+				parent_field: "parent_territory", 
+				formatter: function(item) {
+					return item.name;
+				}
+			}			
+		}
+	},
+	setup_columns: function() {
+		this.tree_grid = this.tree_grids[this.tree_type];
+
+		var std_columns = [
+			{id: "check", name: "Plot", field: "check", width: 30,
+				formatter: this.check_formatter},
+			{id: "name", name: this.tree_grid.label, field: "name", width: 300,
+				formatter: this.tree_formatter, doctype: "Customer"},
+			{id: "total", name: "Total", field: "total", plot: false,
+				formatter: this.currency_formatter}
+		];
+
+		this.make_date_range_columns();		
+		this.columns = std_columns.concat(this.columns);
+	},
+	filters: [
+		{fieldtype:"Select", label: "Tree Type", options:["Customer Group", "Customer", 
+			"Item Group", "Item", "Territory"],
+			filter: function(val, item, opts, me) {
+				return me.apply_zero_filter(val, item, opts, me);
+			}},
+		{fieldtype:"Select", label: "Value or Qty", options:["Value", "Quantity"]},
+		{fieldtype:"Select", label: "Company", link:"Company", 
+			default_value: "Select Company..."},
+		{fieldtype:"Date", label: "From Date"},
+		{fieldtype:"Label", label: "To"},
+		{fieldtype:"Date", label: "To Date"},
+		{fieldtype:"Select", label: "Range", 
+			options:["Daily", "Weekly", "Monthly", "Quarterly", "Yearly"]},
+		{fieldtype:"Button", label: "Refresh", icon:"icon-refresh icon-white", cssClass:"btn-info"},
+		{fieldtype:"Button", label: "Reset Filters"}
+	],
+	setup_filters: function() {
+		var me = this;
+		this._super();
+
+		this.filter_inputs.value_or_qty.change(function() {
+			me.filter_inputs.refresh.click();
+		});
+
+		this.filter_inputs.tree_type.change(function() {
+			me.filter_inputs.refresh.click();
+		});
+
+		this.show_zero_check()		
+		this.setup_plot_check();
+	},
+	init_filter_values: function() {
+		this._super();
+		this.filter_inputs.range.val('Weekly');
+	},
+	prepare_data: function() {
+		var me = this;
+		if (!this.tl) {
+			this.make_tl("Sales Invoice", "Sales Invoice Item");
+
+			// add 'Not Set' Customer & Item
+			// (Customer / Item are not mandatory!!)
+			wn.report_dump.data["Customer"].push({
+				name: "Not Set", 
+				parent_customer_group: "All Customer Groups",
+				parent_territory: "All Territories",
+				id: "Not Set",
+			});
+
+			wn.report_dump.data["Item"].push({
+				name: "Not Set", 
+				parent_item_group: "All Item Groups",
+				id: "Not Set",
+			});
+		}
+		
+		if(!this.data || me.item_type != me.tree_type) {
+			if(me.tree_type=='Customer') {
+				var items = wn.report_dump.data["Customer"];
+			} if(me.tree_type=='Customer Group') {
+				var items = this.prepare_tree("Customer", "Customer Group");
+			} else if(me.tree_type=="Item Group") {
+				var items = this.prepare_tree("Item", "Item Group");
+			} else if(me.tree_type=="Item") {
+				var items = wn.report_dump.data["Item"];
+			} else if(me.tree_type=="Territory") {
+				var items = this.prepare_tree("Customer", "Territory");
+			}
+
+			me.item_type = me.tree_type
+			me.parent_map = {};
+			me.item_by_name = {};
+			me.data = [];
+
+			$.each(items, function(i, v) {
+				var d = copy_dict(v);
+
+				me.data.push(d);
+				me.item_by_name[d.name] = d;
+				if(d[me.tree_grid.parent_field]) {
+					me.parent_map[d.name] = d[me.tree_grid.parent_field];
+				}
+				me.reset_item_values(d);
+			});			
+			
+			this.set_indent();
+			
+		} else {
+			// otherwise, only reset values
+			$.each(this.data, function(i, d) {
+				me.reset_item_values(d);
+			});
+		}
+		
+		this.prepare_balances();
+		if(me.tree_grid.show) {
+			this.set_totals(false);			
+			this.update_groups();
+		} else {
+			this.set_totals(true);
+		}
+
+	},
+	make_tl: function(parent_doctype, doctype) {
+		var me = this;
+		var tmap = {};
+		$.each(wn.report_dump.data[doctype], function(i, v) {
+			if(!tmap[v.parent]) tmap[v.parent] = [];
+			tmap[v.parent].push(v);
+		});
+		this.tl = [];
+		$.each(wn.report_dump.data[parent_doctype], function(i, parent) {
+			$.each(tmap[parent.name], function(i, d) {
+				me.tl.push($.extend(copy_dict(parent), d));
+			});
+		});
+	},
+	prepare_balances: function() {
+		var me = this;
+		var from_date = dateutil.str_to_obj(this.from_date);
+		var to_date = dateutil.str_to_obj(this.to_date);
+		var is_val = this.value_or_qty == 'Value';
+		
+		$.each(this.tl, function(i, tl) {
+			if (me.is_default('company') ? true : me.apply_filter(tl, "company")) { 
+				var posting_date = dateutil.str_to_obj(tl.posting_date);
+				if (posting_date >= from_date && posting_date <= to_date) {
+					var item = me.item_by_name[tl[me.tree_grid.item_key]] || me.item_by_name['Not Set'];
+					item[me.column_map[tl.posting_date].field] += (is_val ? tl.amount : tl.qty);
+				}
+			}
+		});
+	},
+	update_groups: function() {
+		var me = this;
+
+		$.each(this.data, function(i, item) {
+			var parent = me.parent_map[item.name];
+			while(parent) {
+				parent_group = me.item_by_name[parent];
+				
+				$.each(me.columns, function(c, col) {
+					if (col.formatter == me.currency_formatter) {
+						parent_group[col.field] = 
+							flt(parent_group[col.field])
+							+ flt(item[col.field]);
+					}
+				});
+				parent = me.parent_map[parent];
+			}
+		});
+	},
+	set_totals: function(sort) {
+		var me = this;
+		var checked = false;
+		$.each(this.data, function(i, d) { 
+			d.total = 0.0;
+			$.each(me.columns, function(i, col) {
+				if(col.formatter==me.currency_formatter && !col.hidden && col.field!="total") 
+					d.total += d[col.field];
+				if(d.checked) checked = true;
+			})
+		});
+
+		if(sort)this.data = this.data.sort(function(a, b) { return a.total < b.total; });
+
+		if(!this.checked) {
+			this.data[0].checked = true;
+		}
+	},
+	get_plot_points: function(item, col, idx) {
+		return [[dateutil.str_to_obj(col.id).getTime(), item[col.field]], 
+			[dateutil.user_to_obj(col.name).getTime(), item[col.field]]];
+	}
+});
\ No newline at end of file
diff --git a/erpnext/selling/page/sales_analytics/sales_analytics.txt b/erpnext/selling/page/sales_analytics/sales_analytics.txt
new file mode 100644
index 0000000..3782df6
--- /dev/null
+++ b/erpnext/selling/page/sales_analytics/sales_analytics.txt
@@ -0,0 +1,28 @@
+# Page, sales-analytics
+[
+
+	# These values are common in all dictionaries
+	{
+		u'creation': '2012-09-21 12:06:14',
+		u'docstatus': 0,
+		u'modified': '2012-09-21 12:06:14',
+		u'modified_by': u'Administrator',
+		u'owner': u'Administrator'
+	},
+
+	# These values are common for all Page
+	{
+		u'doctype': u'Page',
+		'module': u'Selling',
+		u'name': u'__common__',
+		'page_name': u'sales-analytics',
+		'standard': u'Yes',
+		'title': u'Sales Analytics'
+	},
+
+	# Page, sales-analytics
+	{
+		u'doctype': u'Page',
+		u'name': u'sales-analytics'
+	}
+]
\ No newline at end of file
diff --git a/erpnext/startup/report_data_map.py b/erpnext/startup/report_data_map.py
index 5066853..0de1e73 100644
--- a/erpnext/startup/report_data_map.py
+++ b/erpnext/startup/report_data_map.py
@@ -20,6 +20,16 @@
 # "remember to add indexes!"
 
 data_map = {
+	"Company": {
+		"columns": ["name"],
+		"conditions": ["docstatus < 2"]
+	},
+	"Fiscal Year": {
+		"columns": ["name", "year_start_date", 
+			"adddate(adddate(year_start_date, interval 1 year), interval -1 day) as year_end_date"]
+	},
+
+	# Accounts
 	"Account": {
 		"columns": ["name", "parent_account", "lft", "rgt", "debit_or_credit", "is_pl_account",
 			"company"],
@@ -40,13 +50,23 @@
 			"company": ["Company", "name"]
 		}
 	},
-	"Company": {
-		"columns": ["name"],
-		"conditions": ["docstatus < 2"]
+
+	# Stock
+	"Item": {
+		"columns": ["name", "if(item_name=name, '', item_name) as item_name", 
+			"item_group as parent_item_group", "stock_uom", "brand"],
+		"order_by": "name",
+		"links": {
+			"parent_item_group": ["Item Group", "name"],
+		}
 	},
-	"Fiscal Year": {
-		"columns": ["name", "year_start_date", 
-			"adddate(adddate(year_start_date, interval 1 year), interval -1 day) as year_end_date"]
+	"Item Group": {
+		"columns": ["name", "parent_item_group"],
+		"order_by": "lft"
+	},
+	"Warehouse": {
+		"columns": ["name"],
+		"order_by": "name"
 	},
 	"Stock Ledger Entry": {
 		"columns": ["posting_date", "posting_time", "item_code", "warehouse", "actual_qty as qty",
@@ -59,17 +79,41 @@
 		},
 		"force_index": "posting_sort_index"		
 	},
-	"Item": {
-		"columns": ["name", "if(item_name=name, '', item_name) as item_name", 
-			"item_group as parent_item_group", "stock_uom", "brand"],
-		"order_by": "name"
+
+	# Sales
+	"Customer": {
+		"columns": ["name", "if(customer_name=name, '', customer_name) as customer_name", 
+			"customer_group as parent_customer_group", "territory as parent_territory"],
+		"order_by": "name",
+		"links": {
+			"parent_customer_group": ["Customer Group", "name"],
+			"parent_territory": ["Territory", "name"],
+		}
 	},
-	"Item Group": {
-		"columns": ["name", "lft", "rgt", "parent_item_group"],
+	"Customer Group": {
+		"columns": ["name", "parent_customer_group"],
 		"order_by": "lft"
 	},
-	"Warehouse": {
-		"columns": ["name"],
-		"order_by": "name"
+	"Territory": {
+		"columns": ["name", "parent_territory"],
+		"order_by": "lft"
+	},
+	"Sales Invoice": {
+		"columns": ["name", "customer", "posting_date", "company"],
+		"conditions": ["docstatus=1"],
+		"order_by": "posting_date",
+		"links": {
+			"customer": ["Customer", "name"],
+			"company":["Company", "name"]
+		}
+	},
+	"Sales Invoice Item": {
+		"columns": ["parent", "item_code", "qty", "amount"],
+		"conditions": ["docstatus=1", "ifnull(parent, '')!=''"],
+		"order_by": "parent",
+		"links": {
+			"parent": ["Sales Invoice", "name"],
+			"item_code": ["Item", "name"]
+		}
 	}
 }
diff --git a/erpnext/stock/page/stock_analytics/stock_analytics.js b/erpnext/stock/page/stock_analytics/stock_analytics.js
index 2e65f5c..42b60b2 100644
--- a/erpnext/stock/page/stock_analytics/stock_analytics.js
+++ b/erpnext/stock/page/stock_analytics/stock_analytics.js
@@ -25,7 +25,7 @@
 							enc_value: encodeURIComponent(item.name)
 						});
 				}
-			}			
+			},		
 		})
 	},
 	setup_columns: function() {
@@ -35,10 +35,6 @@
 			{id: "name", name: "Item", field: "name", width: 300,
 				formatter: this.tree_formatter, doctype: "Item"},
 			{id: "opening", name: "Opening", field: "opening", hidden: true,
-				formatter: this.currency_formatter},
-			{id: "balance_qty", name: "Balance Qty", field: "balance_qty", hidden: true,
-				formatter: this.currency_formatter},
-			{id: "balance_value", name: "Balance Value", field: "balance_value", hidden: true,
 				formatter: this.currency_formatter}
 		];
 
@@ -46,7 +42,7 @@
 		this.columns = std_columns.concat(this.columns);
 	},
 	filters: [
-		{fieldtype:"Select", label: "Value or Qty", options:["Value", "Quantity"],
+		{fieldtype:"Select", label: "Value or Qty", options:["Value (Weighted Average)", "Value (FIFO)", "Quantity"],
 			filter: function(val, item, opts, me) {
 				return me.apply_zero_filter(val, item, opts, me);
 			}},
@@ -90,9 +86,9 @@
 	},
 	prepare_data: function() {
 		var me = this;
-		
+				
 		if(!this.data) {
-			var items = this.get_item_tree();
+			var items = this.prepare_tree("Item", "Item Group");
 
 			me.parent_map = {};
 			me.item_by_name = {};
@@ -109,6 +105,7 @@
 				me.reset_item_values(d);
 			});
 			this.set_indent();
+			this.data[0].checked = true;
 		} else {
 			// otherwise, only reset values
 			$.each(this.data, function(i, d) {
@@ -120,30 +117,20 @@
 		this.update_groups();
 		
 	},
-	get_item_tree: function() {
-		// prepare map with items in respective item group
-		var item_group_map = {};
-		$.each(wn.report_dump.data["Item"], function(i, item) {
-			var parent = item.parent_item_group
-			if(!item_group_map[parent]) item_group_map[parent] = [];
-			item_group_map[parent].push(item);
-		});
-		
-		// arrange items besides their parent item groups
-		var items = [];
-		$.each(wn.report_dump.data["Item Group"], function(i, group){
-			group.is_group = true;
-			items.push(group);
-			items = items.concat(item_group_map[group.name] || []);
-		});
-		return items;
-	},
 	prepare_balances: function() {
 		var me = this;
 		var from_date = dateutil.str_to_obj(this.from_date);
 		var to_date = dateutil.str_to_obj(this.to_date);
 		var data = wn.report_dump.data["Stock Ledger Entry"];
-		var is_value = me.value_or_qty == "Value";
+
+		var warehouse_item = {};
+		var get_warehouse_item = function(warehouse, item) {
+			if(!warehouse_item[warehouse]) warehouse_item[warehouse] = {};
+			if(!warehouse_item[warehouse][item]) warehouse_item[warehouse][item] = {
+				balance_qty: 0.0, balance_value: 0.0, fifo_stack: []
+			};
+			return warehouse_item[warehouse][item];
+		}
 				
 		for(var i=0, j=data.length; i<j; i++) {
 			var sl = data[i];
@@ -152,17 +139,13 @@
 			
 			if(me.is_default("warehouse") ? true : me.warehouse == sl.warehouse) {			
 				var item = me.item_by_name[sl.item_code];
-
-				// value
-				var rate = sl.qty > 0 ? sl.incoming_rate :
-					(item.balance_qty.toFixed(2) == 0.00 ? 0 : flt(item.balance_value) / flt(item.balance_qty));
-				var value_diff = (rate * sl.qty);
-								
-				// update balance
-				item.balance_qty += sl.qty;
-				item.balance_value += value_diff;
-
-				var diff = is_value ? value_diff : sl.qty;
+				
+				if(me.value_or_qty!="Quantity") {
+					var wh = get_warehouse_item(sl.warehouse, sl.item_code);
+					var diff = me.get_value_diff(wh, sl);
+				} else {
+					var diff = sl.qty;
+				}
 
 				if(posting_datetime < from_date) {
 					item.opening += diff;
@@ -174,6 +157,92 @@
 			}
 		}
 	},
+	get_value_diff: function(wh, sl) {
+		var is_fifo = this.value_or_qty== "Value (FIFO)";
+				
+		// value
+		if(sl.qty > 0) {
+			// incoming - rate is given
+			var rate = sl.incoming_rate;	
+			var add_qty = sl.qty;
+			if(wh.balance_qty < 0) {
+				// negative valuation
+				// only add value of quantity if
+				// the balance goes above 0
+				add_qty = wh.balance_qty + sl.qty;
+				if(add_qty < 0) {
+					add_qty = 0;
+				}
+			}
+			var value_diff = (rate * add_qty);
+			
+			if(add_qty)
+				wh.fifo_stack.push([add_qty, sl.incoming_rate]);					
+		} else {
+			// outgoing
+						
+			if(is_fifo)	{
+				var value_diff = this.get_fifo_value_diff(wh, sl);				
+			} else {
+				// average rate for weighted average
+				var rate = (wh.balance_qty.toFixed(2) == 0.00 ? 0 : 
+					flt(wh.balance_value) / flt(wh.balance_qty));
+				
+				// no change in value if negative qty
+				if((wh.balance_qty + sl.qty).toFixed(2) >= 0.00)
+					var value_diff = (rate * sl.qty);			
+				else 
+					var value_diff = -wh.balance_value;
+			}
+		}
+
+		// update balance (only needed in case of valuation)
+		wh.balance_qty += sl.qty;
+		wh.balance_value += value_diff;
+
+		if(sl.item_code=="0.5Motor") {
+			console.log([sl.voucher_no, sl.qty, sl.warehouse, value_diff]);
+			console.log(wh.fifo_stack);			
+		}
+		
+		return value_diff;
+	},
+	get_fifo_value_diff: function(wh, sl) {
+		// get exact rate from fifo stack
+		var fifo_stack = wh.fifo_stack.reverse();
+		var fifo_value_diff = 0.0;
+		var qty = -sl.qty;
+		
+		for(var i=0, j=fifo_stack.length; i<j; i++) {
+			var batch = fifo_stack.pop();
+			if(batch[0] >= qty) {
+				batch[0] = batch[0] - qty;
+				fifo_value_diff += (qty * batch[1]);
+				
+				qty = 0.0;
+				if(batch[0]) {
+					// batch still has qty put it back
+					fifo_stack.push(batch);
+				}
+				
+				// all qty found
+				break;
+			} else {
+				// consume this batch fully
+				fifo_value_diff += (batch[0] * batch[1]);
+				qty = qty - batch[0];
+			}
+		}
+		if(qty) {
+			// msgprint("Negative values not allowed for FIFO valuation!\
+			// Item " + sl.item_code.bold() + " on " + dateutil.str_to_user(sl.posting_datetime).bold() +
+			// " becomes negative. Values computed will not be accurate.");
+		}
+		
+		// reset the updated stack
+		wh.fifo_stack = fifo_stack.reverse();
+		return -fifo_value_diff;
+	},
 	update_groups: function() {
 		var me = this;
 
@@ -203,36 +272,7 @@
 			}
 		});
 	},
-	get_plot_data: function() {
-		var data = [];
-		var me = this;
-		$.each(this.data, function(i, item) {
-			if (item.checked) {
-				data.push({
-					label: item.name,
-					data: $.map(me.columns, function(col, idx) {
-						if(col.formatter==me.currency_formatter && !col.hidden) {
-							return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]]
-						}
-					}),
-					points: {show: true},
-					lines: {show: true, fill: true},
-				});
-				
-				// prepend opening 
-				data[data.length-1].data = [[dateutil.str_to_obj(me.from_date).getTime(), 
-					item.opening]].concat(data[data.length-1].data);
-			}
-		});
-	
-		return data;
-	},
-	get_plot_options: function() {
-		return {
-			grid: { hoverable: true, clickable: true },
-			xaxis: { mode: "time", 
-				min: dateutil.str_to_obj(this.from_date).getTime(),
-				max: dateutil.str_to_obj(this.to_date).getTime() }
-		}
+	get_plot_points: function(item, col, idx) {
+		return [[dateutil.user_to_obj(col.name).getTime(), item[col.field]]]
 	}
-});
+});
\ No newline at end of file
diff --git a/public/js/all-app.js b/public/js/all-app.js
index fdd710b..d6c99a0 100644
--- a/public/js/all-app.js
+++ b/public/js/all-app.js
@@ -1076,8 +1076,8 @@
  *	lib/js/wn/views/grid_report.js
  */
 wn.provide("wn.report_dump");$.extend(wn.report_dump,{data:{},with_data:function(doctypes,callback,progress_bar){var missing=[];$.each(doctypes,function(i,v){if(!wn.report_dump.data[v])missing.push(v);})
-if(missing.length){wn.call({method:"webnotes.widgets.report_dump.get_data",args:{doctypes:missing},callback:function(r){$.each(r.message,function(doctype,doctype_data){var data=[];$.each(doctype_data.data,function(i,d){var row={};$.each(doctype_data.columns,function(idx,col){row[col]=d[idx];});row.id=row.name||doctype+"-"+i
-data.push(row);});wn.report_dump.data[doctype]=data;});$.each(r.message,function(doctype,doctype_data){if(doctype_data.links){$.each(wn.report_dump.data[doctype],function(row_idx,row){$.each(doctype_data.links,function(link_key,link){row[link_key]=wn.report_dump.data[link[0]][row[link_key]][link[1]];})})}});callback();},progress_bar:progress_bar})}else{callback();}}});wn.provide("wn.views");wn.views.GridReport=Class.extend({init:function(opts){this.filter_inputs={};this.preset_checks=[];this.tree_grid={show:false};$.extend(this,opts);this.wrapper=$('<div>').appendTo(this.parent);if(this.filters){this.make_filters();}
+if(missing.length){wn.call({method:"webnotes.widgets.report_dump.get_data",args:{doctypes:doctypes,missing:missing},callback:function(r){$.each(r.message,function(doctype,doctype_data){var data=[];$.each(doctype_data.data,function(i,d){var row={};$.each(doctype_data.columns,function(idx,col){row[col]=d[idx];});row.id=row.name||doctype+"-"+i
+data.push(row);});wn.report_dump.data[doctype]=data;});$.each(r.message,function(doctype,doctype_data){if(doctype_data.links){$.each(wn.report_dump.data[doctype],function(row_idx,row){$.each(doctype_data.links,function(link_key,link){if(wn.report_dump.data[link[0]][row[link_key]]){row[link_key]=wn.report_dump.data[link[0]][row[link_key]][link[1]];}else{row[link_key]=null;}})})}});callback();},progress_bar:progress_bar})}else{callback();}}});wn.provide("wn.views");wn.views.GridReport=Class.extend({init:function(opts){this.filter_inputs={};this.preset_checks=[];this.tree_grid={show:false};$.extend(this,opts);this.wrapper=$('<div>').appendTo(this.parent);if(this.filters){this.make_filters();}
 this.make_waiting();this.import_slickgrid();var me=this;this.get_data();},bind_show:function(){var me=this;$(this.page).bind('show',function(){wn.cur_grid_report=me;me.apply_filters_from_route();me.refresh();});},get_data:function(){var me=this;wn.report_dump.with_data(this.doctypes,function(){me.setup_filters();me.init_filter_values();me.refresh();},this.wrapper.find(".progress .bar"));},setup_filters:function(){var me=this;$.each(me.filter_inputs,function(i,v){var opts=v.get(0).opts;if(opts.fieldtype=="Select"&&inList(me.doctypes,opts.link)){$(v).add_options($.map(wn.report_dump.data[opts.link],function(d){return d.name;}));}});this.filter_inputs.refresh&&this.filter_inputs.refresh.click(function(){me.set_route();});this.filter_inputs.reset_filters&&this.filter_inputs.reset_filters.click(function(){me.init_filter_values();me.set_route();});},init_filter_values:function(){var me=this;$.each(this.filter_inputs,function(key,filter){var opts=filter.get(0).opts;if(sys_defaults[key]){filter.val(sys_defaults[key]);}else if(opts.fieldtype=='Select'){filter.get(0).selectedIndex=0;}else if(opts.fieldtype=='Data'){filter.val("");}})
 if(this.filter_inputs.from_date)
 this.filter_inputs.from_date.val(dateutil.str_to_user(sys_defaults.year_start_date));if(this.filter_inputs.to_date)
@@ -1107,7 +1107,7 @@
 (15*dataContext["indent"])+"px'></span>";var idx=me.dataView.getIdxById(dataContext.id);var link=me.tree_grid.formatter(dataContext);if(columnDef.doctype){link+=me.get_link_open_icon(columnDef.doctype,value);}
 if(data[idx+1]&&data[idx+1].indent>data[idx].indent){if(dataContext._collapsed){return spacer+" <span class='toggle expand'></span>&nbsp;"+link;}else{return spacer+" <span class='toggle collapse'></span>&nbsp;"+link;}}else{return spacer+" <span class='toggle'></span>&nbsp;"+link;}},tree_dataview_filter:function(item){var me=wn.cur_grid_report;if(!me.apply_filters(item))return false;var parent=item[me.tree_grid.parent_field];while(parent){if(me.item_by_name[parent]._collapsed){return false;}
 parent=me.parent_map[parent];}
-return true;},set_indent:function(){var me=this;$.each(this.data,function(i,d){var indent=0;var parent=me.parent_map[d.name];if(parent){while(parent){indent++;parent=me.parent_map[parent];}}
+return true;},prepare_tree:function(item_dt,group_dt){var group_data=wn.report_dump.data[group_dt];var item_data=wn.report_dump.data[item_dt];var me=this;var item_group_map={};var group_ids=$.map(group_data,function(v){return v.id;});$.each(item_data,function(i,item){var parent=item[me.tree_grid.parent_field];if(!item_group_map[parent])item_group_map[parent]=[];if(group_ids.indexOf(item.name)==-1){item_group_map[parent].push(item);}else{msgprint("Ignoring Item "+item.name.bold()+", because a group exists with the same name!");}});var items=[];$.each(group_data,function(i,group){group.is_group=true;items.push(group);items=items.concat(item_group_map[group.name]||[]);});return items;},set_indent:function(){var me=this;$.each(this.data,function(i,d){var indent=0;var parent=me.parent_map[d.name];if(parent){while(parent){indent++;parent=me.parent_map[parent];}}
 d.indent=indent;});},apply_filters:function(item){var filters=this.filter_inputs;if(item._show)return true;for(i in filters){if(!this.apply_filter(item,i))return false;}
 return true;},apply_filter:function(item,fieldname){var filter=this.filter_inputs[fieldname].get(0);if(filter.opts.filter){if(!filter.opts.filter(this[filter.opts.fieldname],item,filter.opts,this)){return false;}}
 return true;},apply_zero_filter:function(val,item,opts,me){if(!me.show_zero){for(var i=0,j=me.columns.length;i<j;i++){var col=me.columns[i];if(col.formatter==me.currency_formatter&&!col.hidden){if(flt(item[col.field])>0.001||flt(item[col.field])<-0.001){return true;}}}
@@ -1131,7 +1131,7 @@
 else{$("#"+me.tooltip_id).remove();me.previousPoint=null;}});},get_tooltip_text:function(label,x,y){var date=dateutil.obj_to_user(new Date(x));var value=fmt_money(y);return value+" on "+date;},get_view_data:function(){var res=[];var col_map=$.map(this.columns,function(v){return v.field;});for(var i=0,len=this.dataView.getLength();i<len;i++){var d=this.dataView.getItem(i);var row=[];$.each(col_map,function(i,col){var val=d[col];if(val===null||val===undefined){val=""}
 row.push(val);})
 res.push(row);}
-return res;},})
+return res;},get_plot_data:function(){var data=[];var me=this;$.each(this.data,function(i,item){if(item.checked){data.push({label:item.name,data:$.map(me.columns,function(col,idx){if(col.formatter==me.currency_formatter&&!col.hidden&&col.plot!==false){return me.get_plot_points(item,col,idx)}}),points:{show:true},lines:{show:true,fill:true},});data[data.length-1].data=[[dateutil.str_to_obj(me.from_date).getTime(),item.opening]].concat(data[data.length-1].data);}});return data;},get_plot_options:function(){return{grid:{hoverable:true,clickable:true},xaxis:{mode:"time",min:dateutil.str_to_obj(this.from_date).getTime(),max:dateutil.str_to_obj(this.to_date).getTime()}}}})
 /*
  *	lib/js/legacy/widgets/dialog.js
  */