Specify a hierarchy level shown on the grid or charts using levelName
Slice configuration
by juvian
HTML
<link rel="stylesheet" href="https://s3.amazonaws.com/flexmonster/2.3/demo.css">
<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
.fm-charts-color-1 {
fill: rgb(0, 164, 90) !important;
}
.fm-charts-color-2 {
fill: rgb(223, 56, 0) !important;
}
.fm-charts-color-3 {
fill: rgb(255, 184, 0) !important;
}
.fm-charts-color-4 {
fill: rgb(109, 59, 216) !important;
}
.fm-charts-color-5 {
fill: rgb(0, 117, 255) !important;
}
.fm-charts-color-6 {
fill: rgb(206, 151, 230) !important;
}
.fm-charts-color-7 {
fill: none !important;
}
#fm-pivot-view .fm-bar,
#fm-pivot-view .fm-charts-view .fm-chart-legend ul li .fm-icon-display,
#fm-pivot-view .fm-line,
#fm-pivot-view .fm-arc path,
#fm-pivot-view .fm-bar-stack,
#fm-pivot-view .fm-scatter-point {
opacity: 70% !important;
}
/********************* Styles for the description *********************/
h2 {
text-align: center;
}
p {
font-size: 15px;
line-height: 1.5;
padding-right: 10px;
padding-left: 10px;
}
a {
color: #00a45a;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
code {
font-size: 105%;
color: #DF3800;
background-color: #f5f5f5;
padding: 1px 4px;
font-family: monospace !important;
border-radius: 4px;
}
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]"
]
}
]
}
},
options: {
grid: {
type: 'flat'
}
}
}
});
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 = flexmonster.getReport().slice;
slice.rows[0].levelName = levelName;
flexmonster.runQuery(slice);
}