Thursday, November 13, 2008

13 useful graphing solutions for web developers

Many a web application can be enhanced with a decent graph to show data. Unfortunately out of the programming languages / frameworks that we have available to us, none seem to be very forthcoming with decent graph components, and it is left up to third party solutions to provide the necessary. From my hunts around the web the following showcases some of the better options. I’ve tried to keep these limited to the open source / free options.

LibChart

http://naku.dohcrew.com/libchart/pages/introduction/

Libchart is a simple PHP charting component, and it is totally free free free! The project is an ongoing one - with the last release in April 2008. If you are a PHP coder, you’ll love its simplicity.

Currently supports:

  • Bar charts (horizontal or vertical).
  • Line charts.
  • Pie charts.
  • Single or multiple data sets.
  • Compatibility with PHP 5.

PlotKit

http://media.liquidx.net/js/plotkit-doc/PlotKit.QuickStart.html

Primarily for Barcharts and Piecharts, plotkit uses some fancy Javascript, SVG and Canvas to do some wonderous things. Both these formats are gaining traction in the javascript community, and with implementations in major browser engines, we are deffo going to see more useful javascript drawing libraries cropping up over time.

FusionCharts Free

http://www.fusioncharts.com/free/

Fusioncharts offer some of the best animated flash charting components on the web, and luckily for us they also offer free components. Not only are they used commercial with clients such as Oracle, Microsoft and Dell amongst others, so you’d be in good company. FusionCharts Free can be distributed for free with your free or commercial softwares, irrespective of whether they’re open source or closed source. Hurray!

Chartpart

http://chartpart.com/

Google’s chart drawing API is pretty useful. More so when a little website generates and previews a chart automatically for you. Mighty useful.

Simile Widgets

http://code.google.com/p/simile-widgets/

The Simile project at MIT stands for Semantic Interoperability of Metadata and Information in unLike Environments, with alot of their work focused on visualising digital assets. Luckily for us, they have open sourced some useful widgets on Google code. The timeline is of particular interest, as it allows you to plot events as well as data, which can give some pretty cool results.

Simple Accessible Charts

http://grassegger.at/xperimente/charts-daten-semantik-css/

Inspired by some of the work done by ThinkVitamin - Out of all of the graphing solutions presented here, this is by far the simplest. HTML and CSS charts. Great if you are short on time, and need an easy bar chart.

AmCharts

http://www.amcharts.com/

Created by the talented Antanas Marcelionis Amcharts offer both a commercial charting solution and also offer a free linked version of their flash charting components. The project backups great components with even better documentation, with data (as you would expect) being pulled in via an XML file. They aso offer easy to use server side ASP.NET controls - which make the implementation even simpler for those of us who use the framework.

Features:

  • Stock
  • Column and Bar chart
  • Line and Area chart
  • Pie & Donut
  • Scatter and Bubble chart
  • Radar and Polar

JFreeChart

http://www.jfree.org/jfreechart/

If you develop on the java platform JFreechart is a 100% Java library that is used extensively in existing projects. Established eight years ago, the project has gone from strength to strength, and is now used by 40,000+ Java developers.

The library includes support for multiple chart types including gauges - common in many dashboard applications.

Charting Component Dojo Framework

http://archive.dojotoolkit.org/nightly/dojotoolkit/dojox/charting/

We covered Dojo as one of the Javascript frameworks worth taking a serious look at. Dojo has a similar implementation of graphing via Canvas that we seen used in Plotkit. It is more extensive in its offering however, with cleaner lines generated overall.

Open Flash Chart

http://teethgrinder.co.uk/open-flash-chart-2/

Open Flash chart instead of XML as its datasource uses JSON, which in my opinion, opens it up for further expansion via javascript interaction. Now running on Flash Actionscript 3.0 model - open flash chart is one of the most impressive open source charting ciomponents on the Web today.

Zedgraph

http://zedgraph.org

Written in C# on the .NET framework, ZedGraph is a class library, user control, and web control for .net, for drawing 2D Line, Bar, and Pie Charts. You can easily implement the library in VB however with a binary download offered from SourceForge. Codeproject also offer a good example C# program for utilising the control.

Open source gauge component

http://www.digitaldarknet.net/flash/index.php?selector=dgauge

Gauge components are a new addition to many useful dashboards. These are open source flash swf files, and data passed in via javascript. Source files are not included, but these may be useful for small scale data display. There’s also a JS canvas sample available via the above link.

XML/SWF gauge

http://www.maani.us/gauge/

XML/SWF gauge is a simple, yet powerful tool to create attractive web gauges and dials from dynamic data.

Create an XML source to describe a gauge, then pass it to this tool’s flash file to generate the gauge. Type the XML source manually, or use any scripting language (PHP, Perl, ASP, CFML, etc.) to generate it automatically.

Wednesday, November 12, 2008

Khi nào và tại sao phải dùng Index trong Mysql

Một ngày kia bạn nhận ra rằng website của bạn chạy chậm đi, có thể là do đường truyền nhưng còn 1 nguyên nhân khác, đó là máy chủ server tính toán quá nhiều dẫn đến kết quả đưa ra chậm.

Khi số lượng record tăng lên thì thời gian tìm kiếm của bạn sẽ tăng đáng kể.
“Too many connections” không phải là hiếm gặp trong các website trên Internet.

Hãy xem xét ví dụ sau:

CREATE TABLE employee (
employee_number char(10) NOT NULL,
firstname varchar(40),
surname varchar(40),
address text,
tel_no varchar(25),
salary int(11),
overtime_rate int(10) NOT NULL
);

Và để tìm thông tin Lương của Nguyễn Nam (mã số 101802) , bạn sẽ query như sau:
SELECT salary FROM employee WHERE employee_number = ‘101832′;

MySQL biết rằng phải tìm ở table Employee nhưng nó sẽ không biết bắt đầu từ đâu. Thậm chí nó cũng không biết trước rằng có bao nhiêu kết quả . Do đó nó sẽ duyệt qua tất cả danh sách (vd Hơn 300000 người) để tìm thông tin về Nguyễn Nam.

Một trong những cách để giải quyết vấn đề là dùng Index cho table

Làm sao để quyết định Index cho những field nào hay làm index cho tất cả các field

===> Trả lời

- Index những field mà bạn thường xuyên dùng để làm tiêu chí search( ý nói ở đây là trong phần điều kiện where)
- Nên hạn chế index những field không cần thiết và thường xuyên bị update... vì DBMS cần thời gian để cập nhật cho file index.

Cách theo dõi "công việc tăng tốc" mà ta đang làm

Cú pháp: EXPLAIN Query;

Bằng lệnh này bạn sẽ nhận ra được với câu Query của bạn , điều gì đang xảy ra và kiểu kết hợp (Join) nào đang diễn ra bên trong.

mysql>> EXPLAIN SELECT employee_number,firstname,surname FROM employee WHERE employee_number= ‘10875′;
+———-+——-+—————+———+———+——-+——+——-+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+———-+——-+—————+———+———+——-+——+——-+
| employee | const | PRIMARY | PRIMARY | 10 | const | 1 | |
+———-+——-+—————+———+———+——-+——+——-+
Tốt hơn nhiều rồi , kiểu TYPE = Const có nghĩa rằng MYSQL hiểu ra chỉ có 1 hàng đúng với ý ta, và thể hiện qua cột Rows = 1 , kiểu key= PRIMARY được sử dụng và chiều dài key_len là 10.Chỉ tìm 1 hàng tất nhiên rằng tốt hơn nhiều so với tìm N hàng.

Vậy câu hỏi đặt ra là , nếu tôi muốn thêm Index cho những cột mà có thể có nhiều hơn 1 kết quả khi query thì sao? Vẫn add index như bình thường

Nhưng nên lưu ý vấn đề sau:
mysql>> ALTER TABLE employee ADD INDEX(surname,firstname);
mysql>> EXPLAIN SELECT overtime_rate FROM employee WHERE surname=’Nguyễn’ and firstname=”Nam”;
+——–+——+——————-+———+———+——-+——+———–+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+——–+——+——————-+———+———+——-+——+———–+
|employee| ref | surname,surname_2 | surname | 41 | const | 1 |where used |
+——–+——+——————-+———+———+——-+——+———–+

Tuy nhiên , nếu chỉ cần Firstname

mysql>> EXPLAIN SELECT overtime_rate FROM employee WHERE firstname=’Name’;
+———-+——+—————+——+———+——+——+————+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+———-+——+—————+——+———+——+——+————+
| employee | ALL | NULL | NULL | NULL | NULL | 9475| where used |
+———-+——+—————+——+———+——+——+————+
thì MySQL sẽ tìm hết vì không hề có Index cho Firstname mà chỉ có Index cho (Surname,Firstname)

Nếu chỉ muốn Index cho 1 phần??
Bạn không cần phải làm Index cho cả Field mà chỉ cần 1 phần. Giống như chi tiết Mục Lục của sách mà quá dài cũng làm bạn khá vất vả, do đó họ chỉ trích dẫn 1 tựa đề. Quay lại với table của chúng ta , Surname và Firstname chỉ maximum là 40 chars , nếu chúng ta index nó , chúng ta tạo ra mỗi record đến 80 chars . Có thể tiết kiệm bằng cách sau

mysql>> ALTER TABLE employee ADD INDEX(surname(20),firstname(20));


ĐIỀU KÌ DIỆU VỚI OPTIMIZE VÀ ANALYZE

“Ma thuật” của MySQL là biết cách chọn khoá (key) nào để query(nếu có). Quá trình này gọi là “query optimizer”, nó sẽ “liếc” qua những Index đang có để quyết định sẽ dùng Index nào để tìm. Hãy tưởng tượng bạn đang tìm 1 dĩa CD của “Maria Carrey” có tên là “I Love You” , có nghĩa là có 2 Indexes ở đây , 1 cho tên tác giả và 1 cho tên CD. Bạn nhận thấy rằng danh mục có 20000 tên tác giả và 400000 tên Album. Một cách đơn giản ,bạn sẽ tìm theo tên tác giả. Khi có được , bạn lại thấy rằng “Maria Carrey” có 50 CDs và CD “I Love You” bắt đầu bằng chữ I. Đơn giản và dễ dàng tìm ra cái mình muốn phải không ? MySQL cũng vậy nhưng …bạn phải chỉ cho nó bằng cách:

ANALYZE TABLE tablename;

Những lệnh DELETE và UPDATE để lại rất nhiều những khoảng trống (gaps) vô nghĩa cho table(Đặc biệt là khi bạn dùng kiểu varchar hay text/blob). Điều đó có nghĩa rằng MySQL cũng phải đọc và phân tích những thứ vô nghĩa đó khi query. Điều này được khắc phục khi bạn chạy

OPTIMIZE tablename;

Wednesday, October 22, 2008

Today, My problem is wrong character with Postgres

I must maintance a website that i dont know

Using primary key is character, but user input data by SHIFT-JIS. So when executeSQL ---> Bum

I suggest add new column to indentify that data.

MySQL versus PostgreSQL: Adding an Auto-Increment Column to a Table

Posted in Databases and Programming on Wednesday, the 5th of March, 2008.

The bulk of my database experience (almost eight years now) has been with the popular open-source MySQL database management system. MySQL has progressed significantly over the years, and has grown into a remarkable product. It finally has all the must-have features such as views, stored procs and referential integrity, coupled with the blistering performance for which MySQL has always been known. In short, it rocks.

But I digress. I've recently been having to get to grips with PostgreSQL (an old version of course - 7.1 or so - just to make life really interesting). It's largely intuitive, but there are quirks around most corners. This is my favourite so far: I recently needed to add an auto-incrementing integer "id" column to a table.
MySQL

This sort of thing will be second nature to MySQL developers:
ALTER TABLE mytable ADD myid INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE;

One SQL command - not bad.
PostgreSQL

It turned out not to be so easy with our Postgres installation. For a start, there are no auto_increment columns. So, as with several other major RDBMSs, the solution is to create a "sequence", which is kind of like a pseudo-table which acts as a counter:
CREATE SEQUENCE mytable_myid_seq;

Next, we have to add our new column to the table, and specify that for each new row it should take its value from the sequence, using the NEXTVAL() function. For reasons best known to the Postgres guys, you can't do this in one step:
ALTER TABLE mytable ADD myid INT UNIQUE;

And then:
ALTER TABLE mytable ALTER COLUMN myid SET DEFAULT NEXTVAL('mytable_myid_seq');

We're getting there. We now have an auto increment column. The problem is that Postgres won't backfill this with values automatically: all pre-existing rows are currently null for this column. Let's change that:
UPDATE mytable SET myid = NEXTVAL('mytable_myid_seq');

Job done. Well, some time later, the job will be done. That final step is one of the slowest things you can possibly ask Postgres to do. For a mid-sized table (around 5,000,000 rows, with a handful of small numeric and text columns), that took about 2.5 hours on powerful hardware - so you'll want to leave this for a quiet time. Fortunately Postgres treats the UPDATE as an atomic transaction: nothing is committed until the command completes, so it will be difficult for you to leave the data in an inconsistent state.

Monday, October 20, 2008

TIP Convert latin1 to UTF-8 in MySQL

Today my problem is convert database from latin1 to utf-8 (with data is Shift-jis)

- Create new database, one table, input a lot fot record to test

Step by step:

Since MySQL 4.1, UTF-8 is the default charset. If you have an old database, containing data encoded in latin1, and you want upgrade to a newer MySQL server, than you have to do the following:

MySQL dump

First of all, we need to dump the old data into a file.


Code: Create a MySQL dump
 $ mysqldump -h host.com --user=frog -p --default-character-set=latin1 -c \
--insert-ignore --skip-set-charset dbname > dump.sql


Please mention, that you have to replace the user, the host and the dbname, otherwise it will result in an error :)


Convert dump

Next thing to do is, converting the characters in the MySQL dump from latin1 to UTF-8


Code: Convert dump
 $ iconv -f ISO-8859-1 -t UTF-8 dump.sql > dump_utf8.sql
$ perl -pi -w -e 's/CHARSET=latin1/CHARSET=utf8/g;' dump_utf8.sql


If you have another source charset, you need to replace the -f option with your local character set.


Drop and create

Now it's time to drop the old database and create a new one with UTF-8 support.


Code: Drop and Create
 $ mysql --user=frog -p --execute="DROP DATABASE dbname;
CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;"

(MySql seems to recommend utf8_unicode_ci over utf8_general_ci for 5.1+, see http://dev.mysql.com/doc/refman/5.1/de/charset-unicode-sets.html)

Import dump to databse

Last but not least, we need to import the converted data back to the new database.


Code: Import dump
 $ mysql --user=frog --max_allowed_packet=16M -p --default-character-set=utf8 dbname <>  


The max_allowed_packet-option is sometimes important. If your import ends up with a "ERROR 1153 at line 42: Got a packet bigger than 'max_allowed_packet'", you need to increase the packet size. Please mention, that you also need to update /etc/mysql/my.cnf and set max_allowed_packet=16M under the [mysqld] directive

Done!!!Everything is okie

But I using upflow code, convert fail

Why???

Spent some of minute

There are some wrong character in table "member"

Clean table

Ping pong ping pong

Monday, September 29, 2008

How to integrate Appcelerator with CI

/*
* Author: tri_ngo83@yahoo.com
* Version: beta
* Requirement: PHP5.x, CI 1.6.3
*/

- Using one controller to make a delivery service class.
- Modify servicebroker to library serviceroker
- Modify file appcelerator.xml: servicebroker match to controller
<servicebroker poll="false" marshaller="application/json">@{rootPath}../index.php/service <servicebroker>
- Create new library: My_URI -- comment some lines in function _fetch_uri_string

File attachement:
system folder
index.php


contact mailto: tri_ngo83@yahoo.com

Sunday, September 14, 2008

Hoc Tieng Anh: basic

Mot so cau dam thoai thong dung:
Mot yeu cau thong thuong:

Is there a module out there or a way to allow the customer to upload a File?

For example I'm working on a T-Shirt printing business and I want the user to be able to upload a graphic for printing on the shirt.

do is there anything available with this type of capability?

Thursday, January 24, 2008

Wine cann't connect audio on Linux Fedora

- My Computer have a lot of problem about audio when restart computer. I think that this is error pulseaudio or haldaemon, but restart again is everything is Okie
- Because of Wine isn't running good.
Todo fixed this bug: you're restart Wine:
wineboot -r

Good luck to you

Friday, January 18, 2008

How to run httpd service automatically during startup

- How to run httpd service, or any other service automatically during startup
- I know its got something to do with the chkconfig command
- chkconfig --list
+ chkconfig [--level ]

- Example: If you want to off sendmail process when your system start up GUI, following line
chkconfig --level 5 sendmail off

How to enable superkey(Window key) in linux

Modify file X11/xorg.conf
Finding line
Section "InputDevice"
Identifier "Keyboard0"
Driver "kbd"
Option "XkbModel" "pc105"
Option "XkbLayout" "us"
Option "XkbOptions" "altwin:super_win"
EndSection

Good to you!