I am trying to integrate this component jchartfx with Durandel. I have a standalone prototype using knockout without Durandel and it works perfectly, so I am convinced the issue is Durandel related.
Here is my viewmodel code:
define(function (require) {
var http = require('durandal/http');
var serverUrl = '/api/burndown';
var chart1;
function burnDownModel() {
var self = this;
self.initilize = true;
self.displayName = 'Burndown Chart';
self.description = 'Burndown Chart';
self.chartData = ko.observableArray([]);
self.viewAttached = function() {
};
// testing with fake data
self.activate = function () {
return http.get(serverUrl).then(function (response) {
for (var i = 0; i < 10; i++) {
var data = {
"Date": '2013-02-18T00:00:00',
"DevCurrentEstimates": 6.5,
"TestCurrentEstimates": 7.5,
"DevOriginalEstimates": 8.5,
"TestOriginalEstimates": 9.5
};
self.chartData.push(data);
}
ko.bindingHandlers.jchartFx = {
init: function (element, valueAccessor) {
chart1 = new cfx.Chart();
chart1.getData().setSeries(4);
chart1.getAxisX().getLabelsFormat().setFormat(cfx.AxisFormat.Date);
chart1.getAxisX().getLabelsFormat().setCustomFormat("MMM-dd");
debugger;
var value = ko.utils.unwrapObservable(valueAccessor());
chart1.setDataSource(value);
chart1.create(element);
}
};
});
};
};
return burnDownModel;
});
and my html binding
<div id="ChartDiv1" class="chartdiv" data-bind="jchartFx:chartData"style="width:550px;height:400px;display:inline-block"></div>
As I said I have this working without Durandel.
Here is the script for that solution:
<script type="text/javascript">
$(function () {
var viewModel = {
chartDatas: ko.observableArray([])
};
LoadChartData();
function LoadChartData() {
for (var i = 0; i < 10; i++) {
var chartData = {
"Date": '2013-02-18T00:00:00',
"DevCurrentEstimates": 6.5,
"TestCurrentEstimates": 7.5,
"DevOriginalEstimates": 8.5,
"TestOriginalEstimates": 9.5
};
viewModel.chartDatas.push(chartData);
}
}
ko.applyBindings(viewModel);
});
</script>
The only difference I can see is that this script runs when the page loads and also I have to apply the ko bindings manually as opposed to Durandel doing it for me. I don't get any errors, it's just that no chart data is displayed.
First thing on your code, change the chartData to an Observable. With jchartfx it is expecting a single array of objects (json), not an array of objects. I'm passing in a single array to the chartData observable.
Add an "update" to your Knockout bindingHandler and do the chart create method there. This will get fired when you fetch your data from either viewAttached method or afterBind in your viewModel. Since viewAttached is after Durandal binds to the DOM, your charts will show up as expected.
The viewModel file
and the view file