Sunday, August 10, 2014

Adding String values to nvd3 line chart x axis values

If you're familier with nvd3 line charts you may experienced that adding a string values as x axis values may be little bit messy.
Here is the solution.
first you need to specify the xAxis tickformat function.


chart.xAxis
.axisLabel('Date')
.tickFormat(function(d) {
 var label = scope.totalLoanAmountData[0].values[d].label;
 return label;
});
here I'm using "label" attribute to display strings in the chart x axis.
And then add your data values with label attribute as follows.

scope.totalLoanAmountData=[{
                        "key": "name of line one",
                        "values":[
                {x:1,y:2, label:"label1"},
                {x:1,y:2, label:"label2"},
                {x:1,y:2, label:"label3"},
                {x:1,y:2, label:"label4"},
                {x:1,y:5, label:"label5"},
                {x:1,y:2, label:"label6"},
                {x:1,y:7, label:"label7"},
                {x:1,y:2, label:"label8"},
                {x:1,y:8, label:"label9"}]

                           },

                      {"key": "name of line two",
                        "values": [

                {x:1,y:8, label:"label1"},
                {x:1,y:2, label:"label2"},
                {x:1,y:2, label:"label3"},
                {x:1,y:6, label:"label4"},
                {x:1,y:5, label:"label5"},
                {x:1,y:2, label:"label6"},
                {x:1,y:8, label:"label7"},
                {x:1,y:2, label:"label8"},
                {x:1,y:2, label:"label9"}]

                      }];
You can use date, name, number or whatever string you want.

Sunday, June 15, 2014

Google Summer of Code 2014

Organization - Mifos Initiative 

Mifos is a diverse community of microfinance institutions, technology professionals, business people, volunteers, and contributors. The Mifos Initiative is a 501(c)3 non-profit incorporated in Washington state whose mission is to speed the elimination of poverty by enabling financial service providers to more effectively and efficiently deliver responsible financial services to the world’s 2.5 billion poor and unbanked

Project - Client Impact Portal 

For a Microfinance institution it is useful to have a way of presenting overall client details and other institution details in a high level manner. This information may be useful to external parties that involve with the institutions such as funders and investors. Idea of this project is to provide such a capability to Mifos using a Client Impact Portal. This project consists of both the backend support and frontend for the user who intend to retrieve high level detail of the institutions. Project wiki page

Summer of code Interns in Mifos

This year Mifos have seven interns from four different countries who will be working under the leadership of  mentors from six different countries. They'll be working on a wide spectrum of projects that range from the back-end platform to the front-end community app, on down to mobile apps used directly in the field. By the end of the summer, Mifos community will benefit from a native Android app dedicated to making field staff more efficient, a mobile app for enabling Pay as you Go Solar Energy transactions, a vastly improved user experience for  community app, enhancements to  data migration tool, a batching API that will unlock performance improvements across the entire platform, as well as a powerful ad-hoc reporting tool and evolutions to the client impact portal to a production-ready release.
A Light-hearted Look at  2014 GSOC Interns
2014 Google Summer of Code – Bigger and Better than Ever

Thursday, March 6, 2014

File Uploading with Spring

In web applications that uses Spring framework some times we may need to add functionality to upload file to server. This can be done easily as follows.



First create a form to select a  file,

<!--  Form -->

<i>Uploading File With Ajax</i><br/>

<form id="form" method="post" action="/fileUpload/upload/" enctype="multipart/form-data">

  <!-- File input -->   

  <input name="file" id="file" type="file" /><br/>

</form>

<button value="Submit" onclick="uploadJqueryForm()" >Upload</button><i>Using JQuery Form Plugin</i><br/>

<div id="result"></div>

</body>

</html>
when the button clicks, it calls uploadJqueryForm() function. In that function we can submit the file to the respective controller method. Here is the uploadJqueryForm() function


function uploadJqueryForm(){
    $('#result').html('');
   $("#form").ajaxForm({
    success:function(data) { 
          $('#result').html(data);
     },
     dataType:"text"
   }).submit();
}
This fuction will submit the file and calls the path specified in the "action" tag in the above form. Then we need to write a controller method to handle that request from the function uploadJqureyForm()

@RequestMapping(value = "fileUpload/upload", method = RequestMethod.POST)
    public @ResponseBody String upload(MultipartHttpServletRequest request, HttpServletResponse response,Model model) {                 
 
      Iterator itr =  request.getFileNames();
  
      MultipartFile mpf = request.getFile(itr.next());
      System.out.println(mpf.getOriginalFilename() +" uploaded!");
      String fullFileName="";
      String filePath="";
  
      try {
                
         ufile.length = mpf.getBytes().length;
         ufile.bytes= mpf.getBytes();
         ufile.type = mpf.getContentType();
         ufile.name = mpf.getOriginalFilename();
         
         
             InputStream in= mpf.getInputStream();    
             String fileName = mpf.getOriginalFilename();            
             
             String fileExtension = fileName.substring(fileName.indexOf(".")+1,fileName.length());
                
          
           filePath="D:\\DEV\\work\\files\\Temp\\";
           fullFileName = "D:/DEV/work/files/Temp/"+fileName;
          File files = new File(filePath);
          if (!files.exists()) {
           if (files.mkdirs()) {
            System.out.println("Multiple directories are created!");
           } else {
            System.out.println("Failed to create multiple directories!");
           }
          }
          writeFile(in,fullFileName);
          
      
  
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
  
      
      return "home"; //or whatever the model you need
  
   }
In that methode we get the file as a Multipart request.

Inside this method we call writeFile() method. Here is that method.
this method will write the file into the server folder which we created in the above controller method.

private void writeFile(InputStream in,String fileName){
   
    File file = new File(fileName);
    FileOutputStream fo;
    logger.info("Saving File "+fileName);
   try {
    fo = new FileOutputStream(file);
      byte[] buf = new byte[512]; // optimize the size of buffer to your need
        int num;
        while ((num = in.read(buf)) != -1) {
         fo.write(buf, 0, num);
        }
      fo.flush(); 
        fo.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
    }

References : Reference 1Reference 2