Thursday, April 28, 2011

Use charts in PHP


I was struggling to use the charts in my PHP script so finally when I found out an easy way I thought of sharing it very everyone.

I am using the GD library available in php. Only thing that needs to be done is to configure the php.ini file.
Go to the php.ini file and enable extension=php_gd2.dll

Now to create the charts I was following the method as specified at http://www.qualitycodes.com/tutorial.php?articleid=20&title=How-to-create-bar-graph-in-PHP-with-dynamic-scaling

But the problem was that I was not able to write any text before or after the chart.

so

text
chart
text

the above kind of output is what I wanted and all I could get was a

Chart


So after doing some experiment I finally found a way

1. Create a file called imageGeneration.php
Create a funcction createImage($values,$imageName)
where $values is an associative array while $imageName is the name of the chart file.
The function will take the values to be plotted in the form of an associtive array
like
$values=array(
"Jan" => 110,
"Feb" => 130,
"Mar" => 215,
"Apr" => 81,
"May" => 310,
"Jun" => 110,
"Jul" => 190,
"Aug" => 175,
"Sep" => 390,
"Oct" => 286,
"Nov" => 150,
"Dec" => 196
);


So that the x axis is the name of the months and the y-axis is the values


function createImage($values,$imageName)
{

$img_width=450;
$img_height=300;
$margins=20;


# ---- Find the size of graph by substracting the size of borders
$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2;
$img=imagecreate($img_width,$img_height);


$bar_width=20;
$total_bars=count($values);
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);


# ------- Define Colors ----------------
$bar_color=imagecolorallocate($img,0,64,128);
$background_color=imagecolorallocate($img,240,240,255);
$border_color=imagecolorallocate($img,200,200,200);
$line_color=imagecolorallocate($img,220,220,220);

# ------ Create the border around the graph ------

imagefilledrectangle($img,1,1,$img_width-2,$img_height-2,$border_color);
imagefilledrectangle($img,$margins,$margins,$img_width-1-$margins,$img_height-1-$margins,$background_color);


# ------- Max value is required to adjust the scale -------
$max_value=max($values);
$ratio= $graph_height/$max_value;


# -------- Create scale and draw horizontal lines --------
$horizontal_lines=20;
$horizontal_gap=$graph_height/$horizontal_lines;

for($i=1;$i<=$horizontal_lines;$i++){
$y=$img_height - $margins - $horizontal_gap * $i ;
imageline($img,$margins,$y,$img_width-$margins,$y,$line_color);
$v=intval($horizontal_gap * $i /$ratio);
imagestring($img,0,5,$y-5,$v,$bar_color);

}


# ----------- Draw the bars here ------
for($i=0;$i< $total_bars; $i++){
# ------ Extract key and value pair from the current pointer position
list($key,$value)=each($values);
$x1= $margins + $gap + $i * ($gap+$bar_width) ;
$x2= $x1 + $bar_width;
$y1=$margins +$graph_height- intval($value * $ratio) ;
$y2=$img_height-$margins;
imagestring($img,0,$x1+3,$y1-10,$value,$bar_color);
imagestring($img,0,$x1+3,$img_height-15,$key,$bar_color);
imagefilledrectangle($img,$x1,$y1,$x2,$y2,$bar_color);
}
$temp_chart_file_name = $imageName;//"s/chart2.png";

imagepng($img, $temp_chart_file_name,0);
return $imageName;
}

one important difference from the http://www.qualitycodes.com/tutorial.php?articleid=20&title=How-to-create-bar-graph-in-PHP-with-dynamic-scaling
is that I am not using the header("Content-type:image/png"); since my intention is not to print the chart from this function. The intention is to get the chart and save it in the file.



2. Now create another file called test.php that will use this function


include 'imageGeneration';

echo “ this is a sample test application for the chart”;

// supply the parameters to the chart
$values=array(
"Jan" => 510,
"Feb" => 130,
"Mar" => 215,
"Apr" => 81,
"May" => 310,
"Jun" => 110,
"Jul" => 190,
"Aug" => 175,
"Sep" => 390,
"Oct" => 286,
"Nov" => 150,
"Dec" => 196
);

$imageName="Chart.png";
createImage($values,$imageName);
?>

<img src=" <?php echo $imageName; ?>" alt="some_text"/>


echo “ the chart is placed”;
?>



You will get the output as
this is a sample test application for the chart
Image of the chart
the chart is placed

Convert from System.Drawing.Image to System.Windows.Controls.Image

I was struck with the problem of converting a System.Drawing.Image type Image to System.Windows.Controls.Image. It was very difficult to find relevant resources on the Internet. So I thought of sharing the method for the benefits of everyone.

It works perfectly for me!!.

private System.Windows.Controls.Image ConvertDrawingImageToWPFImage(System.Drawing.Image gdiImg)
{


System.Windows.Controls.Image img = new System.Windows.Controls.Image();

//convert System.Drawing.Image to WPF image
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(gdiImg);
IntPtr hBitmap = bmp.GetHbitmap();
System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

img.Source = WpfBitmap;
img.Width = 500;
img.Height = 600;
img.Stretch = System.Windows.Media.Stretch.Fill;
return img;
}

Sunday, April 17, 2011

How to connect MSSQL server 2005/ SQLExpress from php

Sometimes we want to connect to MSSql server from PHP running on Apache.
The following steps are required to perform a hassle free connection
I am assuming that the PHP has been installed using the winlamp so that it has copied the PHP in the Apache folder.


1.) Download ntwdblib.dll (version : 2000.80.194.0) from Webzila.com

2.) Copy this dll file to apache\bin. Make sure you have the windows path set to this directory.

3.) Restart the apache service by going to Control Panel->Administrative Tools->Services.

4.) Go to “SQL Server Configuration Manager”.

5.) Under “SQL Server Network Configuration” clickon “Protocols for SQLExpress”.
or “Protocols for MSSQLSERVER” (in case you are using the complete SQL server)

6.) On the right hand side,right clicked on “Named Pipes” and clicked on Enable

7.) Also, right clicked on TCP/IP and clicked on enable

steps 6 and 7 are very important. If you forget any of this the connection wont happen

8.) Restart the SQL Server Express/ SQL Server service.

9.) Uncomment the line extension=mssql in the php.ini

If you check phpinfo you should see values for mssql section

Also you need to make sure that the authentication mode is mixed. The connection wont work for windows authentication.
You can do that by

In Express Edition
Right click on the SQLSERVEREXPRESS database and then properties->Security (Choose Sql server and Windows Autherntication mode)

In SQL Server 2005
Right click on the MSSQLSERVER database and then properties->Security (Choose Sql server and Windows Autherntication mode)

Create a user by clicking on security-> Logins and create new login. Make sure you give strong password. In the UseerMapping tab give control to all the database and all databse rights

Now try connection the database seeting should be
$db1 = mssql_connect("hostname\SQLEXPRESS", username, password) in case of the SQL server express edition
while
$db1 = mssql_connect("hostname", username, password) in case of the MSSQL Server

This will make the connection to the database.

Friday, April 15, 2011

Healthcare social platform (Heath 2.0)

Healthcare social platform (Heath 2.0) leverages the social software to promote collaboration among patients caregivers medical professionals and other health stakeholders

Healthcare social networks provide an active platform for sharing the ideas, discussing symptoms and debating the treatment options…all of them can improve the patient care.

There are several issues which raises questions over the entire Health2.0 concept:

  • What is the guarantee that the information shared is correct..basically the factor of trust?
  • Why will anybody share his/her experiences?
  • What about the loss of privacy?

There has been some research on the kind of information people would like to find out on the internet pertaining to healthcare. Some of the key reasons for the online activity are

  • To conduct research on a disease..done mostly by medical practitioners
  • To find out possible diagnosis for the condition …done mostly by patients
  • Find out the insurance plans..done mostly by patients
  • Research the reputation/quality of a doctor or the hospital..done mostly by the patients
  • Look for a doctor to get the treatment for a particular disease.. done by patients. Now a days it is one the use case for creating the local search applications
  • Access or manage the personal health records…There are websites that allow the patients to maintain their medical records
  • Connect to other patient to understand the problems of a particular treatment..done by the patients..patient social networks

Healthcare social networks can be either physicians or patient oriented

Physician social networks provide an online technical infrastructure for the doctors to share the clinical cases, images, videos, medical knowledge

Patient social networks emphasize direct patient support, promote disease awareness, find out what it feels when you are suffering from a particular disease.

Physician Social Networks

Sermo(www.sermo.com) is one of the biggest online social network of the physicians

These platforms (Physician Social Netwoks) bring thousands of the physicians together to exchange the latest in the medical advances. Some of the advantages are:

  • Physicians exchange views about the drugs, devices and the treatment options.
  • They can each other specific clinical questions (just like the software forums) that are not so obvious from the medical literature
  • The doctors in the rural places with limited access to the libraries and specialists can join the communities to get help in diagnosing and prescribing the correct medicines
  • Physicians can discuss ongoing research and help speed up the process of bringing the advances to the patient care.
  • The physician networks also provide useful VOC (voice of customer) to the pharmaceutical companies. By monitoring the conversations on the network they can understand if there is a need of a particular type of drug , or what are the kind of diseases in a specific region (potential markets), or why one particular drug is getting prescribed by various doctors etc.
  • Similarly the device manufacturing companies can get the feedback for their devices, or if there is a need for a new kind of device for diagnosis

There are also some obvious concerns in such networks:

· How do you gauge the accuracy of an advice or expertise on a particular topic?

There are few measures taken in this respect. Some networks require the physician to verify their credentials such as the state license, their degrees, before becoming the member or the membership is only by invitations(so trust is there). Also the advice given may be ranked by the other members (like in stack overflow http://stackoverflow.com/ forum)

Patient Network

The patient networks support the patients worldwide by promoting the disease awareness and positive and proactive behaviors to stay healthy while coping with a disease. Some of the key befits are:

  • They can ask each other about the after effects of a particular medication. They can ask the other patient who is suffering from the same disease about the medications and what is to be expected in terms of the effect of the disease
  • The network helps to lower the anxiety level of the patients and the caregivers particularly for newly diagnoses patients who are unsure about the future. Such patients require variety of information quickly and without interruption.
  • Patients also feel that they are not alone in the world but there are so many in the same situation which act as a soother for them.
  • Between the doctors’s appointments the patients can contact the other patients to gain enough knowledged about the treatment, symptons and to frame the right questions to ask the physicians
  • They can also get reference of a physician or hospital to get the treatment for a particular disease.
  • Also before going to a doctor the patients can find out the reputation of a doctor and the hospital
  • Even the healthy people can find out the information regarding how to keep them protected from a particular disease. During the outbreak of the Swine flu these social networks helped a lot to spread the information about the disease i.e. how to protect yourself , what precautions one must take.
  • In few networks even physicians are members and they volunteer to give answers to patients queries
  • The patient networks also act as a VOC for the drug companies because they can find out the side effects of their drugs or if a particular drug is causing miracles

There are some concerns too.

  • The biggest being the trust. To what extent the people can trust the content of the patient networks. Most medical advice or the comments come from other patients. The sites do not perform any validation of the content. Patients have no choice but to believe in Collective Intelligence (collective understanding of many will be better than a single person). I think with the maturity of the collaboration model of the web (travel forums, software forums) the collective intelligence is more or less established.
  • Privacy is another concern. Since patients are revealing their medical conditions it can be an issue with the insurance companies. They may increase the premium depending on the patients conditions

But still the benefits of the network can outweigh the concerns

What distinguishes the physician and the patient networks from the other online communities is the motivation for the interaction: The professional interest for the doctors and coping with disease or diability for the patients.

How as a software developer we can contribute to Health 2.0?

The crux of the Health 2.0 is a social application that is intended to be used for the discussion on Health related issues. These days many social networking provide APIs to create the social applications. Facebook is a powerful platform which supports the social networking a big time. It is currently the most popular and the largest social networking site. There is a huge potential for a health2.0 kind of Facebook application. The application will allow Facebook users to form a specialized group for enabling the Patient Social networks or the physician social networks. Each member can view others Facebook profile to know more about him. This way the trust might be build which is a major issue in the existing Healthcare Networks. Since the users can just add the app to their facebook profiles (without going to any third party sites) the chances for active participation is also more. There are many ideas that can be drafted around the concept of social app…its just the matter of brainstorming