Sunday Aug 16, 2009

Many a times, it used to be a difficult task to incorporate a div inside a HTML document that can be re-sized and dragged dynamically.I was quite intrigued on the draggable and re-sizable feature that can be used on HTML divs.This blog depicts on incorporating a drag-able and re-sizable feature on a HTML document using  ExtJS framework. Special thanks to Reeta Kumari for providing advise on technology integration. This code snippet was built on an ExtJS framework, where by multiple divs inside a HTML document can have the property of being re sized. This code too supports the re-sizable feature for the divs. In this case, we have two divs with div-ids "textdata" and "blockdata". As you are aware, the Ext.onReady function will be called immediately upon page load.  This function in turn will look for all the divs. The attribute "res" is provided as an attribute to differentiate between the divs which require the draggable and re-sizable feature and the normal one. The re sizable divs in turn will have the id as "resizetextdata" and "resizeblockdata" (resize + textdata). Click here to watch it in action.



<HTML>
<HEAD>
<TITLE> Re-sizable and Draggable divs </TITLE>
<!-- Adding the stylesheet and the JavaScript files from ExtJs library -->
<link rel="stylesheet" type="text/css" href="ext-all.css" />
<script type="text/javascript" src="ext-base.js"></script>
<script type="text/javascript" src="ext-all.js"></script>
<script>
Ext.onReady(function(){
var divarray=document.getElementsByTagName("div");
var barray;
for(i=0;i<divarray.length;i++)
{
var k="resize";
if(divarray[i].getAttribute('res') =="1")
{
var g=k+divarray[i].id;
var resizeid=g;
var h = k+divarray[i].id;
h = new Ext.Resizable(divarray[i].id, {
id:resizeid,
transparent:true,
disableTrackOver:true,
minWidth:0,
minHeight:0,
handles: 'all',
draggable:true,
multiDirectional:true,
resizeChild: true,
dynamic:true

});
h.on('resize',function(obj,width,height,event)
{
// Add your functionality for Resizing
//alert('write your own code here');
});
h.dd.startDrag = function(){
// Add your functionality while dragginf
// alert('write your own code here');
}
}
}
});
</script>
</HEAD>
<BODY>
<div id="textdata" res="1" style="width:300px; border:2.5px solid; font-size:12px; font-weight:bold; background-color:#99CCFF;">This is the first Div<br/> Click on the blue box and drag<br/> Click on the border and drag it for resizing!! </div>

<div id="blockdata" res="1" style=" top:40px; width:300px; border:2.5px solid; font-size:12px; font-weight:bold; background-color:#99ff99;">This is the Second Div<br/> Click on the green box and drag<br/> Click on the border and drag it for resizing!! </div>

</BODY>
</HTML>


Monday Aug 03, 2009

At Clemson, we have commenced an industry sponsored project to build Web 2.0 based business dashboards. I was in the process of designing a RIA based architecture for the same. I was considering RIA' s like jQuery, ExtJS and YUI. Some of the features of ExtJS 3.0 were impressive and the ExtJS charts caught my attention. Though it has been implemented using flash, I thought of performing a proof-of-concept to understand the capabilities of the ExtJS charts with XML data.These were the steps which I have followed.


Step 1 : Building Charts.jsp


  • Go to the ExtJS web page and Download the ExtJS 3.0.0 (Including full source code, build, HTML documentation and samples)

  • Extract the downloaded file to your local drive. For now lets say it's is in C:/ext-3.0.0.

  • Start a new web application project in NetBeans IDE.

  • Copy the /resources folder /adapter from C:/ext-3.0.0/ to your project directory in NetBeans

  • Copy the ext-all.js from C:/ext-3.0.0/ext-all.js to your project directory in Netbeans (in /webpages).

  • Now build a JSP as depicted below and name it as Charts.jsp

  • Include a JavaScript file charts.js  (we will discuss about this file as we go along)

  • <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Charts</title>
            <link rel="stylesheet" type="text/css" href="resources/css/ext-all.css" />
            <link rel="stylesheet" type="text/css" href="resources/stylesheet.css"/>
            <script type ="text/javascript" src="adapter/ext/ext-base.js"> </script>
            <script type="text/javascript" src="ext-all.js"> </script>
            <script type="text/javascript" src="charts.js"> </script>
        </head>
        <body>
            <h1>Charts Example</h1>
            <div  style="position:relative" align="center" id="container">
            </div>
        </body>
    </html>

    Step 2: test1.xml


    (This XML file shows of the total number of members in Clemson University OSUM Club divided on a monthly basis)


    Step 3: charts.js


    (This will be the file which will load the Chart object with the parameters as mentioned in the excel )


    Ext.chart.Chart.CHART_URL = 'resources/charts.swf'; 
    Ext.onReady(function(){
        var store = new Ext.data.XmlStore({
             url: 'test1.xml',
             record: 'test',
             fields:[{name:'name'},{name:'members', type:'int'}]
        });
            store.load(); 
        new Ext.Panel({
            title: 'Clemson University OSUM Club Members',
            renderTo: 'container',
            width:500,
            height:300,
            layout:'fit',
            items: {
                xtype: 'linechart',
                store: store,
                xField: 'name',
                yField: 'members',
                listeners: {
                    itemclick: function(o){
                        var rec = store.getAt(o.index);
                        Ext.example.msg('Item Selected', 'You chose {0}.', rec.get('name'));
                    }
                }
            }
        });
    });

     In this case, we have mentioned the url as test1.xml, since we want to load the data from the xml and we have used an XmlStore for this data. You will be ina position to call a servlet by mentioning the servlet name instead of the "test1.xml" in the url: parameter.The take away from this proof-of-concept is that we should parse the string data from the xml to an 'int' while generating graphs using ExtJS where ever required. In our case we need to parse the number of members to the data type "int". Hence we parsed the members node as "int" in the datastore as depicted below.


    fields:[{name:'name'},{name:'members', type:'int'}]

     Now you may build the application and deploy the war file in a web server. The final chart will look like the following. Special thanks to Reeta Kumari for the advise provided.