Jump to content
TechAttax

[Help/Request] Using Chart.js to replace default charts.

Recommended Posts

If I don´t remember bad, I read a post from Nabeel that stated that the charts were hard-coded and there was no way to change them.

Share this post


Link to post
Share on other sites

I've never done it using Charts.js, but I've used Morris Charts before.

- You could probably tweak it for charts.js

What I did was put the following in

core/modules/Pilots/Pilots.php

public function morrisstatsbymonthdata()
 {

		 $data = PIREPData::getIntervalDataByMonth(array('p.pilotid'=>Auth::$userinfo->pilotid), 3);
		 header("Content-type: application/json");
		 echo json_encode($data);

 }

public function morrisstatsaircraftdata($pilotid)
 {

		 $data = StatsData::PilotAircraftFlownCountsMorris($pilotid);
		 header("Content-type: application/json");
		 echo json_encode($data);

 }

core/common/StatsData.class.php

public static function PilotAircraftFlownCountsMorris($pilotid)
{
$key = 'ac_flown_counts_1_'.$pilotid;

$counts = CodonCache::delete($key);

if($counts === true)
{
//Select aircraft types
$sql = 'SELECT a.name AS label, COUNT(p.aircraft) AS value, SUM(p.flighttime) AS hours
 FROM '.TABLE_PREFIX.'pireps p, '.TABLE_PREFIX.'aircraft a
 WHERE p.aircraft = a.id AND p.pilotid='.intval($pilotid).'
 GROUP BY a.name';

$counts = DB::get_results($sql);
CodonCache::write($key, $counts, 'medium');
}

return $counts;
}

If your using phpvms 5.5.x

core/common/StatsData.class.php

public static function PilotAircraftFlownCountsMorris($pilotid)
{
$key = 'ac_flown_counts_1_'.$pilotid;

$counts = CodonCache::delete($key);

if($counts === false)
{
//Select aircraft types
$sql = 'SELECT a.name AS label, COUNT(p.aircraft) AS value, SUM(p.flighttime) AS hours
   FROM '.TABLE_PREFIX.'pireps p, '.TABLE_PREFIX.'aircraft a
   WHERE p.aircraft = a.id AND p.pilotid='.intval($pilotid).'
   GROUP BY a.name';

$counts = DB::get_results($sql);
CodonCache::write($key, $counts, 'medium');
}

return $counts;
}

And here's my core/templates/profile_stats.tpl/php (should really be in lib/skins/XXX/profile_stats.tpl/php)

<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<h3>Your Stats</h3>
<?php
/*
Added in 2.0!
*/
$chart_width = '800';
$chart_height = '250';
/* Don Not need to change anything below this here */
?>
<div align="center" style="width: 100%;">
<div id="monthdata" style="height: 250px;"></div>
</div>
<br />
<div align="center" style="width: 100%;">
<div id="aircraftdata" style="height: 250px;"></div>
</div>
<script type="text/javascript">
var json = (function () {
		 var json = null;
		 $.ajax({
				 'async': false,
				 'global': false,
				 'url': "<?php echo actionurl('/pilots/morrisstatsbymonthdata');?>",
				 'dataType': "json",
				 'success': function (data) {
						 json = data;
				 }
		 });
		 return json;
 })
 ();

new Morris.Line({
// ID of the element in which to draw the chart.
element: 'monthdata',
// Chart data records -- each entry in this array corresponds to a point on
// the chart.
data: json,
// The name of the data record attribute that contains x-values.
xkey: 'ym',
// A list of names of data record attributes that contain y-values.
ykeys: ['total'],
// Labels for the ykeys -- will be displayed when you hover over the
// chart.
labels: ['Flights']
});
</script>
<script type="text/javascript">
var json = (function () {
		 var json = null;
		 $.ajax({
				 'async': false,
				 'global': false,
				 'url': "<?php echo actionurl('/pilots/morrisstatsaircraftdata/'.Auth::$userinfo->pilotid.'');?>",
				 'dataType': "json",
				 'success': function (data) {
						 json = data;
				 }
		 });
		 return json;
 })
 ();

Morris.Donut({
element: 'aircraftdata',
data: json
});
</script>

The end result http://imgur.com/wyb2lUz

Thanks to Vangelis for providing most of the base code.

Share this post


Link to post
Share on other sites

@web541

After trying that I get this error

Parse error: syntax error, unexpected 'public' (T_PUBLIC) in /home/techattax/public_html/airline/core/modules/Pilots/Pilots.php on line 123

Share this post


Link to post
Share on other sites

Correct me if I'm wrong, but I don't think phpVMS stores all that information in the database. So I don't think that map is possible.

Share this post


Link to post
Share on other sites

As I do not own that module, I can't help you, however if it records the positions in your database then it is probably possible in some way, shape or form.

Share this post


Link to post
Share on other sites

Hey all,

For some reason, I couldn't get the PIREP chart to show.  The aircraft one works perfectly, but not the PIREP line graph. Going to /action.php/pilots/morrisstatsbymonthdata does not show populated JSON data either.

I am on phpvms 5.5.2 on php5.6

Edit: Ignore.  It was because I was running it on a local copy of my site and the database is older than a month, hence no data for the last month! Duh...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×