Specify a hierarchy level shown on the grid or charts using levelName

Slice configuration

by Flexmonster Pivot Table

HTML

<script src="https://cdn.flexmonster.com/flexmonster.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinycolor/1.4.1/tinycolor.min.js"></script>

<div>
  <p>
    In this example, we have a multilevel hierarchy with three levels:
    <code>Country</code>, <code>State</code>, and <code>City</code>. Using the
    <code>levelName</code> property, we can specify which hierarchy level is
    shown on the grid or charts.
  </p>
  <p>
    Click on the buttons below to change the <code>levelName</code> value from
    <code>null</code> to <code>State</code> and back.
  </p>
</div>

<button onclick="setLevel('State')">Specify level</button>
<button onclick="setLevel()">Reset level</button>

<div id="pivot-container"></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: 400,
  report: {
    dataSource: {
      data: getData(),
      mapping: {
        "Country": {
          hierarchy: "Geography",
        },
        "State": {
          hierarchy: "Geography",
          parent: "Country"
        },
        "City": {
          hierarchy: "Geography",
          parent: "State"
        }
      }
    },
    slice: {
      rows: [{
        uniqueName: "[Geography]",
        levelName: null
      }],
      measures: [{
        uniqueName: "Price",
        aggregation: "sum"
      }],
      drills: {
            rows: [
                {
                    tuple: [
                        "geography.[canada]"
                    ]
                },
                {
                    tuple: [
                        "geography.[usa]"
                    ]
                }
            ]
        }
    }
  }
});

function getData() {
  return [{
      "Color": "red",
      "Country": "Canada",
      "State": "Ontario",
      "City": "Toronto",
      "Price": 174
    },
    {
      "Color": "blue",
      "Country": "USA",
      "State": "California",
      "City": "Los Angeles",
      "Price": 166
    },
    {
      "Color": "green",
      "Country": "USA",
      "State": "New York",
      "City": "New York",
      "Price": 186
    }
  ];
}

function setLevel(levelName = null) {
	let slice = pivot.getReport().slice;
  slice.rows[0].levelName = levelName;
  pivot.runQuery(slice);
}