javascriptjsonwebdatarocks

How to get column total on webdatarocks using getData?


I tried recommendations given here.. but I still couldn't get specific column total details. e.g. v0. May I please request you to share details on how to extract specific column & row details in webdatarocks pivot without a cell selection.

I also reviewed recommendations given at Here. I need additional help..


Solution

  • Below is a code snippet. Pay attention to the getTotals function - it shows how to get row and column totals while iterating getData response.

    let pivot = new WebDataRocks({
        container: "#wdr-component",
        toolbar: true,
        report: {
        "dataSource": {
            "dataSourceType": "csv",
            "filename": "https://cdn.webdatarocks.com/data/data.csv"
        },
        "slice": {
            "rows": [
                {
                    "uniqueName": "Category"
                }
            ],
            "columns": [
                {
                    "uniqueName": "Business Type"
                },
                {
                    "uniqueName": "Measures"
                }
            ],
            "measures": [
                {
                    "uniqueName": "Price"
                }
            ]
        }
    }
    });
    
    webdatarocks.getData({}, getTotals);
    
    function getTotals(rawData) {
      let columnTotals = [];
      let rowTotals = [];
      for (let i = 0; i < rawData.data.length; i++) {
        let record = rawData.data[i];
        if (record["r0"] == undefined && record["c0"] == undefined) continue;
        if (record["r0"] == undefined) { 
          let _record = {
            member: record["c0"],
            value: !isNaN(record["v0"]) ? record["v0"] : 0
          };     columnTotals.push(_record);
        }
        if (record["c0"] == undefined) { 
          let _record = {
            member: record["r0"],
            value: !isNaN(record["v0"]) ? record["v0"] : 0
          };     rowTotals.push(_record);
        }
      } 
      console.log(columnTotals);
      console.log(rowTotals); 
    }
    <link href="https://cdn.webdatarocks.com/latest/webdatarocks.min.css" rel="stylesheet"/>
    <script src="https://cdn.webdatarocks.com/latest/webdatarocks.toolbar.min.js"></script>
    <script src="https://cdn.webdatarocks.com/latest/webdatarocks.js"></script>
    <div id="wdr-component"></div>