Skip to content

Commit

Permalink
Add 'roles' parameter to Chart header to define column roles.
Browse files Browse the repository at this point in the history
Add 'styles' and 'refresh' support to dashboards.
Improved column datatype detection for the Chart header.
  • Loading branch information
Jeremy Dorn committed Dec 21, 2014
1 parent d433d85 commit be664db
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 36 deletions.
18 changes: 11 additions & 7 deletions classes/headers/ChartHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ class ChartHeader extends HeaderBase {
'type'=>'array',
'default'=>array()
),
'roles'=>array(
'type'=>'object',
'default'=>array()
),
'markers'=>array(
'type'=>'boolean',
'default'=>false
),
'omit-columns'=>array(
'type'=>'array',
'default'=>array()
),
'omit-columns'=>array(
'type'=>'array',
'default'=>array()
),
'options'=>array(
'type'=>'object',
'default'=>array()
Expand Down Expand Up @@ -314,9 +318,9 @@ protected static function determineDataType($value) {
if(is_null($value)) return null;
elseif($value === '') return null;
elseif(preg_match('/^([$%(\-+\s])*([0-9,]+(\.[0-9]+)?|\.[0-9]+)([$%(\-+\s])*$/',$value)) return 'number';
elseif($temp = strtotime($value)) {
if(preg_match('/^[0-2][0-9]:/',$value)) return 'timeofday';
elseif(date('H:i:s',$temp) === '00:00:00') return 'date';
elseif(preg_match('/^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$/',$value)) return 'timeofday';
elseif(preg_match('/^[0-9]+(\/|-)[0-9]+/',$value) && strtotime($value)) {
if(date('H:i:s',strtotime($value))==='00:00:00') return 'date';
else return 'datetime';
}
else return 'string';
Expand Down
8 changes: 6 additions & 2 deletions templates/default/html/chart_report.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
{% for chart in Charts %}
var data_{{chart.num}} = new google.visualization.DataTable();
{% for key,value in chart.datatypes %}
data_{{chart.num}}.addColumn("{{value}}","{{chart.Rows[0].values[key].key}}");
{% for key,value in chart.Rows[0].values %}
{% if chart.roles[key] %}
data_{{chart.num}}.addColumn({{chart.roles[key]|json_encode|raw}});
{% else %}
data_{{chart.num}}.addColumn("{{chart.datatypes[key]}}","{{chart.Rows[0].values[key].key}}");
{% endif %}
{% endfor %}
var rows = [
Expand Down
117 changes: 90 additions & 27 deletions templates/default/html/dashboard.twig
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
{% extends "html/page.twig" %}

{% block title %}Dashboard List{% endblock %}
{% block title %}{{dashboard.title | default('Dashboard')}}{% endblock %}

{% block header %}
<h2>{{dashboard.title}}</h2>
{% if dashboard.description %}<p>{{dashboard.description|raw}}</p>{% endif %}
{% endblock %}

{% block content %}
<div id='reports_holder' style='margin-top: 20px;'>
<div id='reports_holder'>

</div>
{% endblock %}

{% block stylesheets %}
{{ parent() }}
<style>
#reports_holder {
margin-top: 20px;
}
.rendered {
font-size: .7em;
padding-left: 8px;
}
h2.report-title {
margin: 0;
padding: 0;
}
</style>
{% if dashboard.style %}
<style type="text/css">
{{ dashboard.style|raw }}
</style>
{% endif %}
{% endblock %}

{% block javascripts %}
{{ parent() }}
Expand All @@ -34,14 +55,19 @@
var dashboard = {{ dashboard|json_encode|raw }};
var last_row = $("<div class='row'>").appendTo($("#reports_holder"));
var first = true;
if(dashboard.refresh) {
window.setTimeout(function() {
window.location.reload(true);
},dashboard.refresh*1000);
}
$.each(dashboard.reports, function(i,report) {
if(!first && report.newRow) {
if(i && report.newRow) {
last_row = $("<div class='row'>").appendTo($("#reports_holder"));
}
var container = $("<div>").appendTo(last_row);
var container = $("<div class='report-holder'>").appendTo(last_row);
if(report.class) container.addClass(report.class);
Expand All @@ -52,37 +78,37 @@ $.each(dashboard.reports, function(i,report) {
if(report.title) {
container.append("<div style='float: right; line-height: 30px;'><a href='{{base}}/report/html/?"+$.param(report_data)+"'>View full report</a></div>");
container.append($("<h2>").text(report.title).css({
marginTop: 0,
paddingTop: 0
}));
}
else {
container.append("<p><a href='{{base}}/report/html/?"+$.param(report_data)+"'>View full report</a></p>");
container.append($("<h2 class='report-title'>").text(report.title));
}
container.append("<div class='info'><a class='full-link' href='{{base}}/report/html/?"+$.param(report_data)+"'>View full report</a><span class='rendered'></span></div>");
if(report.description) {
container.append($("<p>").text(report.description));
container.append($("<p class='description'>").text(report.description));
}
var holder;
var render = function() {
if(!report.format || report.format === 'html' || report.format === 'table') {
var report_url = "{{base}}/report/html";
// Loading message
holder.html("<p>Loading...</p>");
$.get(report_url,report_data,function(response) {
holder.empty().html(response);
});
}
else {
holder.attr('src',"{{base}}/report/"+report.format+"?"+$.param(report_data));
}
};
if(!report.format || report.format === 'html' || report.format === 'table') {
holder = $("<div>").appendTo(container);
var report_url = "{{base}}/report/html";
// Loading message
holder.append("<p>Loading...</p>");
holder = $("<div>").appendTo(container);
report_data.content_only = true;
report_data.no_charts = true;
$.get(report_url,report_data,function(response) {
holder.empty().html(response);
});
if(report.style) holder.attr('style',report.style);
}
else {
Expand All @@ -91,11 +117,48 @@ $.each(dashboard.reports, function(i,report) {
width: '100%'
}).iframeAutoHeight();
holder.attr('src',"{{base}}/report/"+report.format+"?"+$.param(report_data));
if(report.style) holder.attr('style',holder.attr('style')+';'+report.style);
}
var render_date = new Date();
var rendered_span = $('.rendered',container);
if(report.refresh) {
window.setInterval(function() {
render();
render_date = new Date();
rendered_span.attr('title','refreshed at '+render_date.toTimeString().substr(0,8)).text('last refreshed 0 seconds ago');
},report.refresh*1000);
var refreshRenderDate = function() {
var date = new Date();
var diff = Math.floor((date.getTime() - render_date.getTime())/1000);
console.log(diff);
if(diff < 60) {
rendered_span.text('last refreshed '+diff+' seconds ago');
window.setTimeout(refreshRenderDate,5000);
}
else if(diff < 300) {
rendered_span.text('last refreshed '+Math.floor(diff/60)+' minute'+(diff>=120?'s':'')+((diff%60)?' '+diff%60+' second'+((diff%60 !== 1)?'s':'') : '')+' ago');
window.setTimeout(refreshRenderDate,20000);
}
else if(diff < 3600) {
rendered_span.text('last refreshed '+Math.floor(diff/60)+' minutes ago');
window.setTimeout(refreshRenderDate,60000);
}
else if(diff < 84600){
rendered_span.text('last refreshed '+Math.floor(diff/3600)+' hour'+(diff>=7200?'s':'')+' ago');
window.setTimeout(refreshRenderDate,600000);
}
else {
rendered_span.text('last refreshed more than 1 day ago');
}
};
window.setTimeout(refreshRenderDate,5000);
}
render();
});
Expand Down

0 comments on commit be664db

Please sign in to comment.