Skip to content

Commit

Permalink
Better charts size
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel authored and daniel committed Jul 9, 2017
1 parent dce4f84 commit 084e373
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 27 deletions.
7 changes: 4 additions & 3 deletions objects/video.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,11 @@ static function getAllVideos($status = "viewable", $showOnlyLoggedUserVideos = f
require_once 'userGroups.php';
while ($row = $res->fetch_assoc()) {
if($getStatistcs){
$previewsMonth = date("Y-m-d", strtotime("-30 days"));
$previewsWeek = date("Y-m-d", strtotime("-7 days"));
$today = date('Y-m-d');
$previewsMonth = date("Y-m-d 00:00:00", strtotime("-30 days"));
$previewsWeek = date("Y-m-d 00:00:00", strtotime("-7 days"));
$today = date('Y-m-d 23:59:59');
$row['statistc_all'] = VideoStatistic::getStatisticTotalViews($row['id']);
$row['statistc_today'] = VideoStatistic::getStatisticTotalViews($row['id'],false, date('Y-m-d 00:00:00'), $today);
$row['statistc_week'] = VideoStatistic::getStatisticTotalViews($row['id'],false, $previewsWeek, $today);
$row['statistc_month'] = VideoStatistic::getStatisticTotalViews($row['id'], false,$previewsMonth, $today);
$row['statistc_unique_user'] = VideoStatistic::getStatisticTotalViews($row['id'], true);
Expand Down
17 changes: 14 additions & 3 deletions objects/video_statistic.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ static function getStatisticTotalViews($videos_id, $uniqueUsers = false, $startD
$sql .= " AND videos_id = {$videos_id} ";
}
if (!empty($startDate)) {
$sql .= " AND `when` >= '{$startDate} 00:00:00' ";
$sql .= " AND `when` >= '{$startDate}' ";
}

if (!empty($endDate)) {
$sql .= " AND `when` <= '{$endDate} 23:59:59' ";
$sql .= " AND `when` <= '{$endDate}' ";
}
$res = $global['mysqli']->query($sql);

Expand All @@ -79,9 +79,20 @@ static function getTotalLastDays($video_id, $numberOfDays, $returnArray = array(
return $returnArray;
}
$date = date("Y-m-d", strtotime("-{$numberOfDays} days"));
$returnArray[] = static::getStatisticTotalViews($video_id, false, $date, $date);
$returnArray[] = static::getStatisticTotalViews($video_id, false, $date." 00:00:00", $date." 23:59:59");
$numberOfDays--;
return static::getTotalLastDays($video_id, $numberOfDays, $returnArray);
}

static function getTotalToday($video_id, $hour=0, $returnArray = array()) {
if ($hour >= 24) {
return $returnArray;
}
$date = date("Y-m-d h:i:s {$hour}", time());
//echo $date;exit;
$returnArray[] = static::getStatisticTotalViews($video_id, false, $date.":00:00", $date.":59:59");
$hour++;
return static::getTotalToday($video_id, $hour, $returnArray);
}

}
111 changes: 90 additions & 21 deletions view/charts.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

$videos = Video::getAllVideos("viewableNotAd", true, true, array(), true);

$labelToday = array();
for ($i = 0; $i < 24; $i++) {
$labelToday[] = "{$i} h";
}
$label7Days = array();
for ($i = 7; $i >= 0; $i--) {
$label7Days[] = date("Y-m-d", strtotime("-{$i} days"));
Expand All @@ -26,15 +30,17 @@
for ($i = 90; $i >= 0; $i--) {
$label90Days[] = date("Y-m-d", strtotime("-{$i} days"));
}
$statistc_lastToday = VideoStatistic::getTotalToday("");
$statistc_last7Days = VideoStatistic::getTotalLastDays("", 7);
$statistc_last30Days = VideoStatistic::getTotalLastDays("", 30);
$statistc_last90Days = VideoStatistic::getTotalLastDays("", 90);

$bg = $bc = $labels = $labelsFull = $datas = $datas7 = $datas30 = array();
$bg = $bc = $labels = $labelsFull = $datas = $datas7 = $datas30 = $datasToday = array();
foreach ($videos as $value) {
$labelsFull[] = $value["title"];
$labels[] = substr($value["title"], 0, 20);
$datas[] = $value["statistc_all"];
$datasToday[] = $value["statistc_today"];
$datas7[] = $value["statistc_week"];
$datas30[] = $value["statistc_month"];
$datasUnique[] = $value["statistc_unique_user"];
Expand All @@ -54,8 +60,7 @@
include $global['systemRootPath'] . 'view/include/head.php';
?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.bundle.min.js" integrity="sha256-+q+dGCSrVbejd3MDuzJHKsk2eXd4sF5XYEMfPZsOnYE=" crossorigin="anonymous"></script>
<style>
</style>

</head>
<body>
<?php
Expand All @@ -65,12 +70,14 @@
<div class="container-fluid">
<nav class="navbar navbar-default nav-chart">
<div class="container-fluid">
<button class="btn btn-default navbar-btn active" id="btnAll" ><?php echo __("Total Views"); ?></button>
<div class="btn-group">
<button class="btn btn-primary navbar-btn active" id="btnAll" ><?php echo __("Total Views"); ?></button>
<button class="btn btn-warning navbar-btn" id="btnToday"><?php echo __("Today Views"); ?></button>
<button class="btn btn-default navbar-btn" id="btn7"><?php echo __("Last 7 Days"); ?></button>
<button class="btn btn-default navbar-btn" id="btn30" ><?php echo __("Last 30 Days"); ?></button>
<!--
<button class="btn btn-default navbar-btn" id="btnUnique" ><?php echo __("Unique Users"); ?></button>
-->
--></div>
</div>
</nav>

Expand Down Expand Up @@ -100,7 +107,7 @@
<div class="panel panel-default">
<div class="panel-heading when"># <?php echo __("Total Views"); ?></div>
<div class="panel-body">
<canvas id="myChart" height="100" ></canvas>
<canvas id="myChart" height="60" ></canvas>
</div>
</div>
</div>
Expand All @@ -110,25 +117,36 @@
<div class="panel panel-default">
<div class="panel-heading when"># <?php echo __("Total Views"); ?></div>
<div class="panel-body">
<canvas id="myChartPie" height="225" ></canvas>
<canvas id="myChartPie" height="200" ></canvas>
</div>
</div>
</div>
<div class="col-md-8">
<div class="panel panel-default">
<div class="panel-heading when"># <?php echo __("Timeline"); ?></div>
<div class="panel-body" id="timeline">
<canvas id="myChartLine" height="100" ></canvas>
<canvas id="myChartLine" height="90" ></canvas>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading when"># <?php echo __("Total Views Today"); ?></div>
<div class="panel-body">
<canvas id="myChartLineToday" height="60" ></canvas>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
var ctx = document.getElementById("myChart");
var ctxPie = document.getElementById("myChartPie");
var ctxLine = document.getElementById("myChartLine");
var ctxLineToday = document.getElementById("myChartLineToday");
var chartData = {
labels: <?php echo json_encode($labelsFull); ?>,
datasets: [{
Expand All @@ -139,6 +157,27 @@
borderWidth: 1
}]
};

var lineChartData = {
labels: <?php echo json_encode($label90Days); ?>,
datasets: [{
backgroundColor: 'rgba(255, 0, 0, 0.3)',
borderColor: 'rgba(255, 0, 0, 0.5)',
label: '# <?php echo __("Total Views (90 Days)"); ?>',
data: <?php echo json_encode($statistc_last90Days); ?>
}]
};

var lineChartDataToday = {
labels: <?php echo json_encode($labelToday); ?>,
datasets: [{
backgroundColor: 'rgba(0, 0, 255, 0.3)',
borderColor: 'rgba(0, 0, 255, 0.5)',
label: '# <?php echo __("Total Views (Today)"); ?>',
data: <?php echo json_encode($statistc_lastToday); ?>
}]
};

var myChart = new Chart(ctx, {
type: 'bar',
data: chartData,
Expand Down Expand Up @@ -175,16 +214,6 @@
}
});

var lineChartData = {
labels: <?php echo json_encode($label90Days); ?>,
datasets: [{
backgroundColor: 'rgba(255, 0, 0, 0.3)',
borderColor: 'rgba(255, 0, 0, 0.5)',
label: '# <?php echo __("Total Views (90 Days)"); ?>',
data: <?php echo json_encode($statistc_last90Days); ?>
}]
};

var myChartLine = new Chart(ctxLine, {
type: 'line',
data: lineChartData,
Expand All @@ -211,16 +240,56 @@
}
}
});

var myChartLineToday = new Chart(ctxLineToday, {
type: 'line',
data: lineChartDataToday,
fill: false,
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function (value, index, values) {
if (Math.floor(value) === value) {
return value;
}
}
}
}]
},
legend: {
display: false
},
responsive: true,
title: {
display: true
}
}
});

$(document).ready(function () {

$('#btnAll').click(function () {
$('.nav-chart .btn').removeClass('active');
$(this).addClass('active');
chartData.datasets[0].data = <?php echo json_encode($datas); ?>;
chartData.datasets[0].label = '# <?php echo __("Total Views"); ?>';
lineChartData.labels = <?php echo json_encode($label90Days); ?>;
lineChartData.datasets[0].data = <?php echo json_encode($statistc_last90Days); ?>;
lineChartData.datasets[0].label = '# <?php echo __("Total Views (90 Days)"); ?>';
lineChartData.datasets[0].label = '# <?php echo __("Total Views (90 Days)"); ?>';
myChart.update();
myChartPie.update();
myChartLine.update();
});
$('#btnToday').click(function () {
$('.nav-chart .btn').removeClass('active');
$(this).addClass('active');
chartData.datasets[0].data = <?php echo json_encode($datasToday); ?>;
chartData.datasets[0].label = '# <?php echo __("Today"); ?>';
lineChartData.labels = <?php echo json_encode($labelToday); ?>;
lineChartData.datasets[0].data = <?php echo json_encode($statistc_lastToday); ?>;
lineChartData.datasets[0].label = '# <?php echo __("Today"); ?>';
myChart.update();
myChartPie.update();
myChartLine.update();
Expand All @@ -232,7 +301,7 @@
chartData.datasets[0].label = '# <?php echo __("Last 7 Days"); ?>';
lineChartData.labels = <?php echo json_encode($label7Days); ?>;
lineChartData.datasets[0].data = <?php echo json_encode($statistc_last7Days); ?>;
lineChartData.datasets[0].label = '# <?php echo __("Last 7 Days"); ?>';
lineChartData.datasets[0].label = '# <?php echo __("Last 7 Days"); ?>';
myChart.update();
myChartPie.update();
myChartLine.update();
Expand All @@ -244,7 +313,7 @@
chartData.datasets[0].label = '# <?php echo __("Last 30 Days"); ?>';
lineChartData.labels = <?php echo json_encode($label30Days); ?>;
lineChartData.datasets[0].data = <?php echo json_encode($statistc_last30Days); ?>;
lineChartData.datasets[0].label = '# <?php echo __("Last 30 Days"); ?>';
lineChartData.datasets[0].label = '# <?php echo __("Last 30 Days"); ?>';
myChart.update();
myChartPie.update();
myChartLine.update();
Expand Down

0 comments on commit 084e373

Please sign in to comment.