Added chart, organized file tree, fixed add behaviour.

This commit is contained in:
Alexis Lahouze
2013-01-12 13:56:52 +01:00
parent 86cee7faa9
commit 37010cca06
73 changed files with 40043 additions and 13 deletions

View File

@ -42,13 +42,69 @@ var ListViewModel = function() {
self.selectedItem = ko.observable();
self.savedItem = ko.observable();
self.entriesChart = ko.computed(function() {
var entries = self.entries().slice().reverse();
var chartValues = [];
var chartValuesTmp = {};
$.each(entries, function(index, entry) {
//var date = entry.value_date() ? entry.value_date().toString() : null;
var date = entry.value_date();
var value = entry.value ? Number(entry.value()) : null;
if(date && value) {
var values = {};
var sold = Number(entry.sold());
var open = Number((sold - value).toFixed(2));
values['open'] = open;
values['high'] = sold > open ? sold : open;
values['low'] = sold < open ? sold : open;
values['close'] = sold;
if(chartValuesTmp[date]) {
var oldValues = chartValuesTmp[date];
if(oldValues['high'] > values['high']) {
values['high'] = oldValues['high'];
}
if(oldValues['low'] < values['low']) {
values['low'] = oldValues['low'];
}
values['open'] = oldValues['open'];
}
chartValuesTmp[date] = values;
}
});
$.each(chartValuesTmp, function(key, value) {
var ohlc = [key, value['open'], value['high'], value['low'], value['close']];
chartValues.push(ohlc);
});
chartValues.sort(function(a, b){
var aDate = Date.parse(a[0]);
var bDate = Date.parse(b[0]);
return aDate < bDate ? -1 : 1;
});
return chartValues;
}, self);
self.loadEntries = function(account, month) {
$.post("api/entry.php", {action: "get_entries", account: account.id, year: month.year, month: month.month}, function(data) {
self.entries.removeAll();
self.selectedItem(null);
var entries = [];
$.each(data, function(index, element) {
self.entries.push(new entry({
entries.push(new entry({
id: element.id,
value_date: new Date(element.value_date),
operation_date: element.operation_date ? new Date(element.operation_date) : null,
@ -59,6 +115,8 @@ var ListViewModel = function() {
pointedSold: element.operation_date ? element.pointedsold : ''
}));
});
self.entries(entries);
});
};
@ -66,6 +124,14 @@ var ListViewModel = function() {
$.post("api/entry.php", {action: "get_accounts"}).success(function (result) {
self.accounts(result);
if(self.account()) {
$.each(self.accounts(), function(index, account) {
if(self.account().id == account.id) {
self.account(account);
}
});
}
if(!self.account()){
self.account(result[0]);
}
@ -78,6 +144,14 @@ var ListViewModel = function() {
$.post("api/entry.php", {action: "get_months", account: account.id}).success(function (result) {
self.months(result);
if(self.month()) {
$.each(self.months(), function(index, month) {
if(self.month().year == month.year && self.month().month == month.month) {
self.month(month);
}
});
}
if(!self.month()) {
self.month(result[result.length - 1]);
}
@ -131,10 +205,15 @@ var ListViewModel = function() {
};
self.add = function() {
newEntry = new entry({
if(self.savedItem() != null) {
self.cancel();
}
var newEntry = new entry({
account: self.account().id
});
this.entries.unshift(newEntry);
self.entries.unshift(newEntry);
self.selectedItem(newEntry);
$("#value_date").datepicker({format: "yyyy-mm-dd"}).on('changeDate', function(ev){
@ -206,7 +285,7 @@ function dateToString(date) {
var month = date.getMonth() + 1;
var day = date.getDate();
dateStr = year + "-" + (month < 10 ? '0' : '') + month + "-" + (day < 10 ? '0' : '') + day;
var dateStr = year + "-" + (month < 10 ? '0' : '') + month + "-" + (day < 10 ? '0' : '') + day;
return dateStr;;
} else {
@ -220,7 +299,7 @@ ko.bindingHandlers.date = {
var unwrap = ko.utils.unwrapObservable;
var dataSource = valueAccessor();
value = dataSource ? unwrap(dataSource) : null;
var value = dataSource ? unwrap(dataSource) : null;
if(value) {
$(element).text(dateToString(value));
@ -233,7 +312,7 @@ ko.bindingHandlers.date = {
var unwrap = ko.utils.unwrapObservable;
var dataSource = valueAccessor();
value = dataSource ? unwrap(dataSource) : null;
var value = dataSource ? unwrap(dataSource) : null;
if(value) {
$(element).text(dateToString(value));
@ -248,7 +327,7 @@ ko.bindingHandlers.dateValue = {
var unwrap = ko.utils.unwrapObservable;
var dataSource = valueAccessor();
value = dataSource ? unwrap(dataSource) : null;
var value = dataSource ? unwrap(dataSource) : null;
if(value) {
$(element).val(dateToString(value));
@ -261,7 +340,7 @@ ko.bindingHandlers.dateValue = {
var unwrap = ko.utils.unwrapObservable;
var dataSource = valueAccessor();
value = dataSource ? unwrap(dataSource) : null;
var value = dataSource ? unwrap(dataSource) : null;
if(value) {
$(element).val(dateToString(value));
@ -271,6 +350,76 @@ ko.bindingHandlers.dateValue = {
}
};
ko.bindingHandlers.chart = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
// empty - left as placeholder if needed later
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
var unwrap = ko.utils.unwrapObservable;
var dataSource = valueAccessor();
//var entries = dataSource ? unwrap(dataSource) : null;
var entries = dataSource ? unwrap(dataSource) : null;
if(entries && entries.length > 0) {
var firstDate, lastDate;
var chartValues = [[], []];
var day = 24 * 60 * 60 * 1000;
lastDate = new Date(Date.parse(entries[entries.length -1][0]).valueOf() + 1 * day);
firstDate = new Date(Date.parse(entries[0][0]).valueOf() - 1 * day);
// For the '0' line.
chartValues[0].push(
[new Date(firstDate.valueOf()).toString(), 0],
[new Date(lastDate.valueOf()).toString(), 0]);
// Push last entry
chartValues[1] = entries;
// clear previous chart
$(element).html("");
// plot chart
$.jqplot(element.id, chartValues, {
axes:{
xaxis:{
//autoscale: true,
//min: firstDate.toString(),
//max: lastDate.toString(),
renderer:$.jqplot.DateAxisRenderer,
tickOptions: {formatString: "%F"}
},
yaxis: {
autoscale: true,
//tickOptions: {formatString: "%f"}
}
},
highlighter: {
show:true,
//tooltipAxes: 'y',
yvalues: 4,
formatString:'<table class="jqplot-highlighter"><tr><td>date:</td><td>%s</td></tr><tr><td>open:</td><td>%s</td></tr><tr><td>hi:</td><td>%s</td></tr><tr><td>low:</td><td>%s</td></tr><tr><td>close:</td><td>%s</td></tr></table>'
},
series: [{
linePattern: "dashed",
lineWidth: 1,
color: "red",
showMarker: false
},{
renderer:$.jqplot.OHLCRenderer,
color: "blue",
lineWidth: 3,
rendererOptions:{
//candleStick: true
}}],
});//*/
}
}
};
ko.applyBindings(new ListViewModel());