arg_stats.js 6.33 KB
/*
Copyright (c) 2013, Bartłomiej Nitoń
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

$(function() {
	$('.arg-stats-form #arg-stats-gen-list').live('click', gen_list);
	$('form.arg-stats-form').live('submit', prepare_graph);
});

function gen_list()
{
    ShowProgressAnimation();
    this_form = $('.arg-stats-form');
    form_data = this_form.serializeArray();
    
    form_data = $.map(form_data, function(elem)
    {
      if (elem.name != 'arguments' && elem.name != 'arg_types' &&
         elem.name != 'bucket_size')
        return elem;
      else {
        if (elem.name == 'bucket_size')
          bucket_size = elem.value;
      }
    });

	form_data.push({name: 'bucket_size', value: bucket_size});

    $.ajaxJSON({
      method: 'post',
      url: ajax_gen_list,
      data: {
        form_data: form_data
      },

    callback: function(result) {
      HideProgressAnimation();
    },
    error_callback: function(xhr, status, error) {
      HideProgressAnimation();
      error_alert(status + ': ' + error);
    },
    bad_data_callback: function(result) {
      if (result == 'bucket size not selected') {
        HideProgressAnimation();
        error_alert('Wyznacz wielkość kubełka.');
        return false;
      }
      else if(result == 'bucket size not digit' || 
    		  result == 'wrong bucket size range') {
          HideProgressAnimation();
          error_alert('Wielkość kubełka musi być liczbą większą od 0.');
          return false;
      }
      else
      {
        HideProgressAnimation();
        return true;
      }
    },
  });
  return false;	
}

function draw_graph(arguments, selector, xmax, ylabel, dict_label, show_legend)
{
	var series = [];
    var seriesconf = [];
  	for (var i=0; i<arguments.length; i++) {
  		points = [];
  		for (var j=0; j<arguments[i][dict_label].length; j++)
      	{
  			points.push([j+1, arguments[i][dict_label][j]]);
      	}
  		series.push(points);
      	seriesconf.push({markerOptions: { size: 3 },
				label: arguments[i]['text_rep']});
    }
                
    var plot = $.jqplot(selector, series,
    {
      	title:{
      		text: 'Graf wpływu częstości występowania czasowników na posiadane realizacje',
      		fontFamily: 'Georgia, Serif',
            fontSize: '12pt'
      	},
      	series: seriesconf,
      	axes:{
        	xaxis:{
          		label: 'Częstość występowania czasownika (malejąco)',
          		labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
          		pad: 0,
          		labelOptions: {
                	fontFamily: 'Georgia, Serif',
            		fontSize: '12pt'
          		},
          		min: 1,
          		max: xmax,
          		numberTicks: xmax 
        	},
        	yaxis:{
          		label: ylabel,
          		labelRenderer: $.jqplot.CanvasAxisLabelRenderer,
          		pad: 0,
          		labelOptions: {
            		fontFamily: 'Georgia, Serif',
            		fontSize: '12pt'
          		},
        	}
      	},
      	legend: { show     :show_legend, 
      		      location : 'ne',
      		      placement: 'outside',
        },
    });
}

function prepare_graph() {
    ShowProgressAnimation();
    this_form = $(this);
    form_data = this_form.serializeArray();
    var args = new Array();
  
    form_data = $.map(form_data, function(elem)
    {
        if (elem.name != 'arguments' && elem.name != 'arg_types' &&
    	    elem.name != 'bucket_size')
    	    return elem;
    	else {
    	    if (elem.name == 'bucket_size')
    	        bucket_size = elem.value;
    	    else if(elem.name == 'arg_types')
    	    	arg_types = elem.value;
    	    else if(elem.name == 'arguments')
    	    	args.push(elem.value);
    	}
    });

    form_data.push({name: 'bucket_size', value: bucket_size});
    form_data.push({name: 'arg_types', value: arg_types});
    form_data.push({name: 'arguments', value: args});

    $.ajaxJSON({
      method: 'post',
      url: ajax_prepare_graph_data,
      data: {
        form_data: form_data
      },

    callback: function(result) {
      $('#graph-count').empty();
      $('#graph-own').empty();
      draw_graph(result['graph_data'], 'graph-count', result['xmax'], 'Liczba realizacji danego typu', 'freq', true);
      draw_graph(result['graph_data'], 'graph-own', result['xmax'], 'Liczba czasowników posiadająca realizację', 'own', false);
      HideProgressAnimation();
    },
    error_callback: function(xhr, status, error) {
      HideProgressAnimation();
      error_alert(status + ': ' + error);
    },
    bad_data_callback: function(result) {
      if (result == 'select arguments') {
        HideProgressAnimation();
        error_alert('Wybierz realizację i/lub typ realizacji.');
        return false;
      }
      else if (result == 'bucket size not selected') {
          HideProgressAnimation();
          error_alert('Wyznacz wielkość kubełka.');
          return false;
      }
      else if(result == 'bucket size not digit' || 
      		  result == 'wrong bucket size range') {
          HideProgressAnimation();
          error_alert('Wielkość kubełka musi być liczbą od 1 do 100.');
          return false;
      }
      else {
        HideProgressAnimation();
        return true;
      }
    },
  });
  return false;
}