Calculated Field add to pivot grid (olap)

Posted by: sravya_tamma on 28 April 2020, 6:28 pm EST

    • Post Options:
    • Link

    Posted 28 April 2020, 6:28 pm EST

    Hello,

    From the below screenshot: Conversion is a calculated field which is using getValue function to do the dynamic calculation.

    What I’m looking for is :

    Provide User capability to :

    Step1: Add Column button : To add empty column to the grid.

    Step2: Calculate button: Where he can perform some calculation and display on grid.

    I was able to add an empty column to the grid, but this column is not available when I try to get the field from the grid using ```

    this.ng.columnFields.getField(‘New Column’);

    
    

    addNewColumn() {

    this.pivotGrid.columns.push(new wjcGrid.Column({ header:

    ‘new_column’, binding: ‘new_column’}));

    }

    calculate () {
        let field = this.ng.columnFields.getField('New Column');
        field.getValue = function (item) {
            return 100 * 5 ; // demo calculation
        };
        this.ng.refresh();
    }
    
    
    [img]https://gccontent.blob.core.windows.net/forum-uploads/file-f4b0c6f5-670b-46a1-9fdd-1f26c020ff63.png[/img]
  • Posted 29 April 2020, 2:46 am EST

    Hi Sravya,

    While using OLAP and PivotGrid, if you wish to add a new column in the grid, then you do not push a Column instance to the PivotGrid, instead you need to create an instance of PivotField class and push it into the PivotFieldCollection instance of the PivotEngine. And to display this field on the columns of PivotField, you set the isActive property of this field to true.

    Please refer to the sample link below for reference:

    https://stackblitz.com/edit/query-pivot-panel-chart-gg7a63

    Also, make sure that the header of each field should be unique.

    Regards,

    Ashwin

  • Posted 29 April 2020, 7:51 pm EST

    Thank you. It worked!

  • Posted 30 April 2020, 8:36 pm EST

    Hi Ashwin,

    One more question:

    Lets say I want to add New Field 2. From the below code I’m not sure how can i get the computed value of New Field1. ?

     // Add Field 1 
    addField(engine) {
        var fld = new wjcOlap.PivotField(engine, 'amount', 'New Field');
        engine.fields.push(fld);
        fld.isActive = true;
      }
    
      calcField(engine) {
        var fld = engine.fields.getField('New Field');
        fld.getValue = (item) => item.amount * 50;
        engine.refresh();
      } 
    
    // Add Field 2
    addField2(engine) {
        var fld = new wjcOlap.PivotField(engine, 'what is the binding here ?? ', 'New Field2');
        engine.fields.push(fld);
        fld.isActive = true;
      }
    
      calcField2(engine) {
        var fld = engine.fields.getField('New Field2');
        fld.getValue = (item) => {
          // *** Is there a way to get the new field1 computed value and divide it by 100......
    
    
        };
        engine.refresh();
      }
    
  • Posted 1 May 2020, 1:53 am EST

    Hi Sravya,

    Simply use the getValue property of the ‘New Field’ and provide the current item as the parameter:

    var fld = engine.fields.getField('New Field2');
    var fld1 = engine.fields.getField('New Field');
    fld.getValue = (item) => {
    	var fldValue = fld1.getValue(item);
    	return fldValue / 100;
    };
    

    Also, if you are using the getValue property, you may not need the binding. In both the cases, you can set the binding to null. But, make sure that you have set the dataType of the field correctly because the isActive property will move the field to rowFields or valueFields according to the dataType.

    fld.dataType = wjCore.DataType.Number;
    engine.fields.push(fld);
    fld.isActive = true;
    

    ~regards

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels