jQWidgets Forums

jQuery UI Widgets Forums Grid Weighted average of two columns

This topic contains 2 replies, has 2 voices, and was last updated by  ivailo 9 years, 11 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • Weighted average of two columns #72958

    drv232
    Participant

    I need to calculate the weighted average over two columns in the Grid

    COL1 COL2
    1 2
    3 4

    2.3

    So I need to show 2.3 at the end of Col2.

    Calculation:((1*2)+(3*4))/(2+4)

    How can I go about this.

    Can you provide me with an example if possible. Thank You.

    Weighted average of two columns #72963

    drv232
    Participant

    I created the following custom aggregate function that help’s me get the right weighted mean.

    
    { text: 'MarketVal', datafield: 'MarketVal',  cellsformat: 'F2',cellsalign: 'right', align: 'right',  width: 200 ,aggregates: [{'<b>Weighted Mean</b>':
                                function (aggregatedValue, currentValue, column, record) {
                                   // counter=counter+1;
                                    var total = currentValue * record['Mark'];
                                    markSum=currentValue+markSum;
                                    var yesMarketVal=markSum;
                                    var yesP=aggregatedValue + total;
                                   return ((yesP)/(yesMarketVal)+aggregatedValue);
                                   
                                }
                          }]  }
    
    

    However, once I do grouping or carry out a filter, it changes its value but gives a wrong answer altogether.

    A bizzare thing happens as well. When I change the MarketVal column width, it changes the weighted mean value by itself.

    Any help to tackle this issue?

    Weighted average of two columns #73073

    ivailo
    Participant

    Hi drv232,

    Try this code.
    Here the calculations are made outside in function.The function is called :
    1. When the Grid is rendered.
    3. When the sort event is occured.

    Best Regards,
    Ivailo Ivanov

    jQWidgets Team
    http://www.jqwidgets.com

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.