API calls to add, get or remove calculated measures

Calculated measures

by Flexmonster Pivot Table

HTML

<script src="https://cdn.flexmonster.com/flexmonster.js"></script>

<div>
  <h2>API calls to add, get or remove calculated measures</h2>

  <p>
    Click the <b>Add calculated measure</b> button to add the calculated measure
    <code>Average Quantity</code> which is calculated as
    <code>sum("Quantity")/count("Quantity")</code>.
  </p>

  <button onclick="addCalculatedMeasure()">Add Calculated Measure</button>

  <p>
    Click the <b>Get measures</b> button to get selected measures in the report.
    Click the <b>Get all measures</b> button to get all available measures.<br />
    The output will appear under the component.
  </p>

  <button onclick="getMeasures()">Get Measures</button>
  <button onclick="getAllMeasures()">Get All Measures</button>

  <p>
    Use the buttons below to remove either the
    <code>Avarage Quantity</code> measure or all calculated measures.
  </p>

  <button onclick="removeCalculatedMeasure()">Remove Calculated Measure</button>
  <button onclick="removeAllCalculatedMeasures()">
    Remove All Calculated Measures
  </button>
</div>

<div id="pivot-container">The component will appear here</div>

<div class="output" id="output">Output will appear here</div>

CSS

@import url(https://cdn.flexmonster.com/assets/jsfiddle-styles.css);

JavaScript

let pivot = new Flexmonster({
  container: "pivot-container",
  componentFolder: "https://cdn.flexmonster.com/",
  height: 260,
  report: {
    dataSource: {
      filename: "data/data.csv"
    }
  }
});

function addCalculatedMeasure() {
  let measure = {
    formula: "sum('Quantity')/count('Quantity')",
    uniqueName: "Average Quantity",
    caption: "Average Quantity",
    grandTotalCaption: "Total Average Quantity",
    active: true
  };
  pivot.addCalculatedMeasure(measure);
}

function getMeasures() {
  let measures = pivot.getMeasures();
  showOutput(JSON.stringify(measures, null, 2));
}

function getAllMeasures() {
  let measures = pivot.getAllMeasures();
  showOutput(JSON.stringify(measures, null, 2));
}

function removeCalculatedMeasure() {
  let measureName = "Average Quantity";
  pivot.removeCalculatedMeasure(measureName);
}

function removeAllCalculatedMeasures() {
  pivot.removeAllCalculatedMeasures();
}

function showOutput(content) {
  document.getElementById("output").innerText = content;
}