2017-01-03 10:42:22 -07:00
|
|
|
// D3 visualisation
|
2016-08-11 05:50:52 -06:00
|
|
|
|
2017-01-03 10:42:22 -07:00
|
|
|
d3.lineChart = function(telemetry_key, unit) {
|
2016-08-11 05:50:52 -06:00
|
|
|
var config = {
|
2016-11-20 10:20:23 -07:00
|
|
|
margin: {top: 20, right: 20, bottom: 115, left: 100},
|
2016-08-11 05:50:52 -06:00
|
|
|
width: 700,
|
|
|
|
height: 500
|
|
|
|
};
|
2017-01-03 10:42:22 -07:00
|
|
|
|
2016-08-11 05:50:52 -06:00
|
|
|
var svg;
|
|
|
|
|
2016-11-20 10:13:30 -07:00
|
|
|
// Define the div for the tooltip
|
|
|
|
var div = d3.select("body").append("div")
|
|
|
|
.attr("class", "chart-tooltip")
|
|
|
|
.style("opacity", 0);
|
2016-08-11 05:50:52 -06:00
|
|
|
|
2017-01-03 10:42:22 -07:00
|
|
|
function render(selection) {
|
|
|
|
selection.each(function(_data) {
|
2016-08-11 05:50:52 -06:00
|
|
|
var chartW = config.width - config.margin.left - config.margin.right,
|
|
|
|
chartH = config.height - config.margin.top - config.margin.bottom;
|
|
|
|
|
2016-11-24 07:03:36 -07:00
|
|
|
var xInterval = chartW / (_data.length - 1);
|
|
|
|
|
2016-08-11 05:50:52 -06:00
|
|
|
var x1 = d3.scale.ordinal()
|
|
|
|
.domain(_data.map(function(d, i){
|
2016-11-20 10:02:37 -07:00
|
|
|
return parseDate(d.telemetry.observation_datetime);
|
2016-08-11 05:50:52 -06:00
|
|
|
}))
|
2016-11-24 07:03:36 -07:00
|
|
|
.rangePoints([0, chartW]);
|
2016-08-11 05:50:52 -06:00
|
|
|
|
|
|
|
var y1 = d3.scale.linear()
|
2016-11-24 08:51:48 -07:00
|
|
|
.domain(d3.extent(_data, function(d, i){ return +d.telemetry.damod_data[telemetry_key]; }))
|
2016-08-11 05:50:52 -06:00
|
|
|
.range([chartH, 0])
|
|
|
|
.nice(4);
|
|
|
|
|
|
|
|
var xAxis = d3.svg.axis()
|
|
|
|
.scale(x1)
|
|
|
|
.orient('bottom');
|
|
|
|
|
|
|
|
var yAxis = d3.svg.axis()
|
|
|
|
.scale(y1)
|
|
|
|
.orient('left');
|
|
|
|
|
|
|
|
if(!svg) {
|
|
|
|
svg = d3.select(this)
|
|
|
|
.append('svg')
|
|
|
|
.classed('chart', true);
|
|
|
|
var container = svg.append('g').classed('container-group', true);
|
|
|
|
container.append('g').classed('chart-group', true);
|
|
|
|
container.append('g').classed('x-axis-group axis', true);
|
|
|
|
container.append('g').classed('y-axis-group axis', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
svg.transition().attr({width: config.width, height: config.height});
|
2016-11-20 10:15:32 -07:00
|
|
|
|
2016-08-11 05:50:52 -06:00
|
|
|
svg.select('.container-group')
|
|
|
|
.attr({transform: 'translate(' + config.margin.left + ',' + config.margin.top + ')'});
|
|
|
|
|
|
|
|
svg.select('.x-axis-group.axis')
|
|
|
|
.attr({transform: 'translate(0,' + (chartH) + ')'})
|
|
|
|
.transition()
|
|
|
|
.call(xAxis);
|
|
|
|
|
|
|
|
svg.select('.y-axis-group.axis')
|
|
|
|
.transition()
|
|
|
|
.call(yAxis);
|
|
|
|
|
2016-11-20 10:20:23 -07:00
|
|
|
svg.selectAll(".x-axis-group.axis text") // select all the text elements for the xaxis
|
|
|
|
.attr("transform", function(d) {
|
|
|
|
return "translate(-50,50)rotate(-45)";
|
|
|
|
});
|
2016-11-20 10:02:37 -07:00
|
|
|
|
|
|
|
// Axis labels
|
2016-08-11 05:50:52 -06:00
|
|
|
svg.append("text")
|
2016-11-20 10:02:37 -07:00
|
|
|
.attr("transform", "translate(" + (chartW + config.margin.right + 18) + " ," + (chartH + 10) + ")")
|
2016-08-11 05:50:52 -06:00
|
|
|
.style("text-anchor", "middle")
|
|
|
|
.text("Observation Datetime");
|
|
|
|
|
|
|
|
svg.append("text")
|
|
|
|
.attr("transform", "rotate(-90)")
|
2016-11-20 10:20:23 -07:00
|
|
|
.attr("y", 40)
|
2016-08-11 05:50:52 -06:00
|
|
|
.attr("x", 0 - (chartH / 2))
|
|
|
|
.attr("dy", "1em")
|
|
|
|
.style("text-anchor", "middle")
|
2016-11-20 10:11:19 -07:00
|
|
|
.text("Value (" + unit + ")");
|
2016-08-11 05:50:52 -06:00
|
|
|
|
|
|
|
// Define the line
|
|
|
|
var valueline = d3.svg.line()
|
|
|
|
.x(function(d,i) { return (xInterval*i + config.margin.left); })
|
|
|
|
.y(function(d) { return y1(d.telemetry.damod_data[telemetry_key]) + config.margin.top; });
|
|
|
|
|
|
|
|
// Add the valueline path
|
|
|
|
svg.append("path")
|
|
|
|
.attr("class", "line")
|
|
|
|
.attr("d", valueline(_data));
|
|
|
|
|
|
|
|
// Add the scatterplot
|
|
|
|
svg.selectAll("dot")
|
|
|
|
.data(_data)
|
2016-11-20 10:20:23 -07:00
|
|
|
.enter().append("circle")
|
|
|
|
.attr("r", 4)
|
2016-08-11 05:50:52 -06:00
|
|
|
.attr("cx", function(d, i) { return xInterval*i + config.margin.left; })
|
2016-11-20 10:13:30 -07:00
|
|
|
.attr("cy", function(d) { return y1(d.telemetry.damod_data[telemetry_key]) + config.margin.top; })
|
|
|
|
.attr("class", "circle")
|
|
|
|
.on("mouseover", function(d) {
|
|
|
|
div.transition()
|
|
|
|
.duration(200)
|
|
|
|
.style("opacity", 1);
|
|
|
|
div.html(d.telemetry.damod_data[telemetry_key] + ' (' + unit + ')')
|
|
|
|
.style("left", (d3.event.pageX) + "px")
|
|
|
|
.style("top", (d3.event.pageY - 26) + "px");
|
|
|
|
})
|
|
|
|
.on("mouseout", function(d) {
|
|
|
|
div.transition()
|
|
|
|
.duration(500)
|
|
|
|
.style("opacity", 0);
|
|
|
|
});
|
2016-08-11 05:50:52 -06:00
|
|
|
});
|
|
|
|
}
|
2017-01-03 10:42:22 -07:00
|
|
|
return render;
|
2016-08-11 05:50:52 -06:00
|
|
|
};
|
|
|
|
|
2017-01-03 10:42:22 -07:00
|
|
|
// Retreive Satellite Id
|
2016-08-11 05:50:52 -06:00
|
|
|
|
2017-01-03 10:42:22 -07:00
|
|
|
var satelliteId = $('#telemetry-block').data('satid');
|
|
|
|
|
|
|
|
// Backbone Models
|
|
|
|
|
|
|
|
var TelemetryData = Backbone.Model.extend({});
|
|
|
|
|
|
|
|
// Backbone Collections
|
|
|
|
|
|
|
|
var TelemetryCollection = Backbone.Collection.extend({
|
|
|
|
url:"/api/telemetry/?satellite=" + satelliteId
|
|
|
|
});
|
|
|
|
|
|
|
|
var TelemetryDescriptors = TelemetryCollection.extend({
|
|
|
|
parse: function(response){
|
|
|
|
return response[0].appendix;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var TelemetryValues = TelemetryCollection.extend({
|
|
|
|
comparator: function(collection){
|
|
|
|
return( collection.get('telemetry').observation_datetime );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Backbone Views
|
|
|
|
|
|
|
|
var TelemetryDescriptorsView = Backbone.View.extend({
|
|
|
|
el: "#telemetry-descriptors",
|
|
|
|
template: _.template($('#telemetryDescriptorsTemplate').html()),
|
|
|
|
initialize: function(){
|
|
|
|
this.listenTo(this.collection, 'add reset change remove', this.renderItem);
|
|
|
|
this.collection.fetch();
|
|
|
|
},
|
|
|
|
renderItem: function (model) {
|
|
|
|
this.$el.append(this.template(model.toJSON()));
|
|
|
|
$('#telemetry-descriptors li:first-child').addClass('active');
|
|
|
|
}
|
|
|
|
});
|
2016-08-11 05:50:52 -06:00
|
|
|
|
2017-01-03 10:42:22 -07:00
|
|
|
var TelemetryChartView = Backbone.View.extend({
|
2016-08-11 05:50:52 -06:00
|
|
|
el: ".chart",
|
|
|
|
chart: null,
|
|
|
|
chartSelection: null,
|
|
|
|
initialize: function() {
|
2017-01-03 10:42:22 -07:00
|
|
|
this.collection.fetch();
|
2016-08-11 05:50:52 -06:00
|
|
|
_.bindAll(this, 'render', 'update');
|
2017-01-03 10:42:22 -07:00
|
|
|
this.collection.on('update reset', this.render);
|
|
|
|
chart = d3.lineChart();
|
2016-08-11 05:50:52 -06:00
|
|
|
},
|
|
|
|
events: {
|
|
|
|
"click .telemetry-key": "update",
|
|
|
|
},
|
|
|
|
render: function() {
|
2017-01-03 10:42:22 -07:00
|
|
|
var data = this.collection.toJSON();
|
2016-08-11 05:50:52 -06:00
|
|
|
this.chartSelection = d3.select(this.el)
|
2017-01-03 10:42:22 -07:00
|
|
|
.datum(data)
|
|
|
|
.call(d3.lineChart(data[0].appendix[0].key, data[0].appendix[0].unit));
|
2016-08-11 05:50:52 -06:00
|
|
|
},
|
|
|
|
update: function(e){
|
2016-11-20 10:13:30 -07:00
|
|
|
d3.select('svg').remove();
|
2017-01-03 10:42:22 -07:00
|
|
|
this.chartSelection.call(d3.lineChart($(e.currentTarget).data("key"), $(e.currentTarget).data("unit")));
|
2016-11-20 10:14:31 -07:00
|
|
|
var active = $(e.currentTarget);
|
|
|
|
active.addClass('active');
|
|
|
|
$('li').not(active).removeClass('active');
|
2016-08-11 05:50:52 -06:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Fetch data and render views
|
|
|
|
|
|
|
|
var telemetryDescriptorsView = new TelemetryDescriptorsView({ collection: new TelemetryDescriptors() });
|
2017-01-03 10:42:22 -07:00
|
|
|
var telemetryChartView = new TelemetryChartView({collection: new TelemetryValues});
|
|
|
|
|
|
|
|
|
|
|
|
// Parse datetime values
|
|
|
|
|
|
|
|
function parseDate (date) {
|
|
|
|
var res = date.substring(4,6) + '/' + date.substring(6,8) + '/' + date.substring(0,4) + ' ' + date.substring(9 ,11) + ':' + date.substring(11 ,13) + ':' + date.substring(13 ,15);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|