ExtJS line Charts and improper string data from PHP

While working on the main dashboard for the current extjs project I am working I had to create a line chart which showed the performance over the last 1 week. This was across 3 different variables.

The code to show that with tips and all as an item in an ExtJS Panel is

new Ext.chart.LineChart({
                    store: new Ext.data.JsonStore({
                        url:PHP_AJAX_URL + '/hometab.php',
                        baseParams:{
                            task:'graphdata'
                        },
                        idProperty: 'gdate',
                        root:'data',
                        fields: ['gdate', 'f1', 'f2','f3'],
                        autoLoad: true
                    }),
                    xField:'gdate',
                    series:[
                    {
                        type:'line',
                        displayName: 'FONE',
                        yField: 'f1',
                        style: {
                            color: 0x889fbb
                        }
                    }, {
                        type: 'line',
                        displayName: 'FTWO',
                        yField: 'f2',
                        style: {
                            color: 0x889fcc
                        }
                    }, {
                        type:'line',
                        displayName: 'FTHREE',
                        yField: 'f3',
                        style: {
                            color: 0x6666bb
                        }
                    }
                    ],
                    tipRenderer: function (chart, record, index, series){
                        if (series.yField=='f1')
                            return Ext.util.Format.number(record.data.f1, '0,0') +
                            ' f1 on ' + record.data.gdate;
                        if (series.yField=='f2')
                            return Ext.util.Format.number(record.data.f2, '0,0')+
                            ' f2 on ' + record.data.gdate;
                        if (series.yField=='f3')
                            return Ext.util.Format.number(record.data.f3, '0,0') + 
                            ' f3 on ' + record.data.gdate;

                    },
                    extraStyle: {
                        padding: 10,
                        animationEnabled: true,
                        legend:{
                            display:'bottom'
                        },
                        xAxis: {
                            color: 0x3366cc,
                            majorGridLines: {
                                size: 0,
                                color: 0xdddddd
                            }
                        },
                        yAxis: {
                            color: 0x3366cc,
                            majorTicks: {
                                color: 0x3366cc,
                                length: 0
                            },
                            minorTicks: {
                                color: 0x3366cc,
                                length: 0
                            },
                            majorGridLines: {
                                size: 0,
                                color: 0xdddddd
                            }
                        }
                    }
                })

Like all other parts of the backend the code was being pulled via a quick mysql_query from the database and provided to the ExtJS frontend via JSON.

        $graph_query="select 
				date(f_date) as gdate,
                sum(f1) as f1,
                sum(f2) as f2,
                sum(f3) as f3
                from stats
				where id=user
                and date(f_date)>date_sub(current_date, interval 7 day)
                group by date(f_date)";
        $graph_recordset=mysql_query($graph_query);
		while($graph_row=mysql_fetch_assoc($graph_recordset)){
			$response['data']=$graph_row;
		}
		$response['success']=true;
		print json_encode($response);

However, the LineChart wasn’t rendering properly and it was showing wierd values for some and not rendering at all for the others. The axes were fine and firebug showed proper values for the JSON string.

After a bit of looking around, it seems that ExtJS does not convert from string numeric values to numeric values. Remember that mysql_fetch_assoc provides an associative array with string values. All that was needed was to manually cast to integer/float values that the LineChart needs.

The bit of modified code is below. Note the casts inside the while loop

        $graph_query="select 
				date(f_date) as gdate,
                sum(f1) as f1,
                sum(f2) as f2,
                sum(f3) as f3
                from stats
				where id=user
                and date(f_date)>date_sub(current_date, interval 7 day)
                group by date(f_date)";
        $graph_recordset=mysql_query($graph_query);
		while($graph_row=mysql_fetch_assoc($graph_recordset)){
			$graph_row['f1']=intval($graph_row['f1']); //or floatval as needed;
			$graph_row['f2']=intval($graph_row['f2]); //or floatval as needed;
			$graph_row['f3']=intval($graph_row['f3]); //or floatval as needed;
			$response['data']=$graph_row;
		}
		$response['success']=true;
		print json_encode($response);

To Cake or not to cake

To Cake or not
I am starting work on a project for a client that involves complicated workflows when the customer orders a product and also a simple user interface. Even thought I have worked with a few php frameworks, this is the first time I am starting solo on a project from scratch. While looking around for the best options that fit the bill, I shortlisted 3 options
1. Core PHP
2. Drupal
3. CakePHP or Zend Framework
Option 1 might be the simplest to start with but would be not be a wise decision since it would be a maintenance and extensibility nightmare. I started working on a prototype for Drupal, but a few weeks down the line, the limits of a CMS for such a project became obvious. The best option now seems to be to use a framework, which should have been obvious from the start in hindsight.
I considered going with Zend but Cake seems to be much lighter and also places more emphasis on convention which supposedly makes me a ‘better’ programmer. Additionally, the fact that Zend seems to be more feature rich that cake actually pushed me more towards cake since the project is quite simple technically. And Zend seems to be too powerful for that.
So I am off to the bakery.
Cheers and Peace!
Jeba Singh Emmanuel
————————————-
Those who would joyously march in rank and file have already earned my contempt, for they were given a large brain by accident when a spinal chord would have sufficed.
–Albert Einstein

I am starting work on a project for a client that involves complicated workflows when the customer orders a product and also a simple user interface. Even thought I have worked with a few php frameworks, this is the first time I am starting solo on a project from scratch. While looking around for the best options that fit the bill, I shortlisted 3 options

1. Core PHP

2. Drupal

3. CakePHP or Zend Framework

Option 1 might be the simplest to start with but would be not be a wise decision since it would be a maintenance and extensibility nightmare. I started working on a prototype for Drupal, but a few weeks down the line, the limits of a CMS for such a project became obvious. The best option now seems to be to use a framework, which should have been obvious from the start in hindsight.

I considered going with Zend but Cake seems to be much lighter and also places more emphasis on convention which supposedly makes me a ‘better’ programmer. Additionally, the fact that Zend seems to be more feature rich that cake actually pushed me more towards cake since the project is quite simple technically. And Zend seems to be too powerful for that.

So I am off to the bakery.