Saturday 27 October 2012

How to Run Ruby on Mac?

1.First install Ruby on Mac

Actually Ruby-1.8.7 is preinstalled on Mac OS X, so you no need to install.

Just run $ruby -v in your mac Terminal to check your ruby version:



2.Edit your ruby program file

You can use Textwrangler to edit, save your program file with .rb postfix
For example you create a file rubytest.rb under directory Document.
Inside rubytest.rb only include one line

puts "My first ruby program."

Then on your Terminal, you can run

$ruby Document/rubytest.rb



That's it, very simple.


Thursday 25 October 2012

Using Highcharts with CodeIgniter| The easiest way to draw chart

These days I'm doing CodeIgniter project, need chart library. I tried pChart, chartdirector,Highcharts etc. 

I have to say Highcharts is the easiest to use, other tools like pChart, chartdirector need to write a extra library in CodeIgniter to use them,it's not convenient, but Highcharts no need.

Highcharts is a charting library written in pure JavaScript, all you need is down load Highcharts and jquery then put them into your CodeIgniter directory.
How to use? Look at here: http://www.highcharts.com/documentation/how-to-use

<script src="/js/jquery.js" type="text/javascript"></script>
<script src="/js/highcharts.js" type="text/javascript"></script>
 
<script type="text/javascript">
 var chart1; // globally available
jQuery(document).ready(function() {

        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container'
            },
            title: {
                text: 'Combination chart'
            },
            xAxis: {
                categories: ['Apples', 'Oranges', 'Pears', 'Bananas', 'Plums']
            },
            tooltip: {
                formatter: function() {
                    var s;
                    if (this.point.name) { // the pie chart
                        s = ''+
                            this.point.name +': '+ this.y +' fruits';
                    } else {
                        s = ''+
                            this.x  +': '+ this.y;
                    }
                    return s;
                }
            },
            labels: {
                items: [{
                    html: 'Total fruit consumption',
                    style: {
                        left: '40px',
                        top: '8px',
                        color: 'black'
                    }
                }]
            },
            series: [{
                type: 'column',
                name: 'Jane',
                data: [3, 2, 1, 3, 4]
            }, {
                type: 'column',
                name: 'John',
                data: [2, 3, 5, 7, 6]
            }, {
                type: 'column',
                name: 'Joe',
                data: [4, 3, 3, 9, 0]
            }, {
                type: 'spline',
                name: 'Average',
                data: [3, 2.67, 3, 6.33, 3.33],
                marker: {
                 lineWidth: 2,
                 lineColor: Highcharts.getOptions().colors[3],
                 fillColor: 'white'
                }
            }, {
                type: 'pie',
                name: 'Total consumption',
                data: [{
                    name: 'Jane',
                    y: 13,
                    color: '#4572A7' // Jane's color
                }, {
                    name: 'John',
                    y: 23,
                    color: '#AA4643' // John's color
                }, {
                    name: 'Joe',
                    y: 19,
                    color: '#89A54E' // Joe's color
                }],
                center: [100, 80],
                size: 100,
                showInLegend: false,
                dataLabels: {
                    enabled: false
                }
            }]
        });
    });

</script> 
 
<div id="chartcontainer"></div>
 
 
The above script will draw a combination of column line pie chart like bellow.

 
 
Highcharts got a lot of demo which is very convenient for you to draw a chart, check
 out here http://www.highcharts.com/demo/