Articles in this section

How to customize the units for values of PivotGrid at server-side in JavaScript?

This KB document explains how to customize the units for values in JavaScript PivotGrid OLAP at server-side.

Solution:

You can customize the units of PivotGrid values as kilo, million, billion, etc.

Step 1:

Render the PivotGrid using OLAP server mode, and add the unitFormatting method in the WebAPI controller.

public string unitFormatting(dynamic pivotRecords) {
 
List<Dictionary<string, object>> newPivotRecords = new List<Dictionary<string, object>>();
foreach (Dictionary<string, object> item in pivotRecords)
 {
  string currentValue = item.ElementAt(2).Value.ToString();
  string key = item.ElementAt(2).Key;
  if (currentValue.Contains("$"))
  {
  double val = Double.Parse(item.ElementAt(2).Value.ToString(), NumberStyles.Currency);
  string[] suffixes = { "", "k", "M", "G" };
  int digit;
  if (val == 0)
     digit = 0; // log10 of 0 is not valid
   else
     digit = (int)(Math.Log10(val) / 3); // get number of digits and divide by 3
   var dividor = Math.Pow(10, digit * 3);
   var text = "$" + (val / dividor).ToString("F1") + suffixes[digit];
   item[key] = text;
 }
newPivotRecords.Add(item);
}
return serializer.Serialize(newPivotRecords);
}

 

Step 2:

Call the above method wherever you want to format the PivotGrid values by passing pivot records as parameter. Now, it has been modified for the InitializeGrid and drill-down functionalities. Add the following code snippet in your controller file.

Code Snippet: [C#]

[System.Web.Http.ActionName("InitializeGrid")]
[System.Web.Http.HttpPost]
public Dictionary<string, object> InitializeOlapGrid(Dictionary<string, object> jsonResult)
 {
 //…
 DataManager.OverrideDefaultFormatStrings = true;
 var result = htmlHelper.GetJsonData(jsonResult["action"].ToString(), DataManager, jsonResult.ContainsKey("gridLayout") ? jsonResult["gridLayout"].ToString() : null, Convert.ToBoolean(jsonResult["enablePivotFieldList"].ToString()));
 result["PivotRecords"] = unitFormatting(serializer.Deserialize<dynamic>(result["PivotRecords"].ToString()));
// Pass pivot records to the unityFormatting method
return result;
}
 
 
[System.Web.Http.ActionName("DrillGrid")]
[System.Web.Http.HttpPost]
public Dictionary<string, object> DrillGrid(Dictionary<string, object> jsonResult)
{
OlapDataManager DataManager = new OlapDataManager(connectionString);
            DataManager.SetCurrentReport(Utils.DeserializeOlapReport(jsonResult["currentReport"].ToString()));
var result = htmlHelper.GetJsonData(jsonResult["action"].ToString(), connectionString, DataManager, jsonResult["cellPosition"].ToString(), jsonResult["headerInfo"].ToString(), jsonResult["layout"].ToString());
result["PivotRecords"] = unitFormatting(serializer.Deserialize<dynamic>(result["PivotRecords"].ToString()));
// Pass the pivot records to the unityFormatting method
return result;
}

Conclusion

I hope you enjoyed learning about how to customize the units for values of PivotGrid at server-side.

You can refer to our JavaScript PivotGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript PivotGrid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied