HTML Presentation Object: Bubble chart

From Mea Wiki
Revision as of 19:57, 6 December 2017 by Ollvihe (talk | contribs)
Jump to navigation Jump to search

Following query contains example data for the bubble chart. Add it to a dataset with name "exampleData". Note that this example needs QPR Reporting Expression datasource installed.

data0=CreateDataset(Array('Risk', 'Value', 'Cost', 'Category'))
data1=AddDatasetRow([data0], 4, 1, 6, 'Category 1')
data2=AddDatasetRow([data1], 8, 5, 4, 'Category 2')
data3=AddDatasetRow([data2], 1, 9, 4, 'Category 3')
data4=AddDatasetRow([data3], 3, 2, 8, 'Category 1')
data5=AddDatasetRow([data4], 3, 8, 9, 'Category 3')
data6=AddDatasetRow([data5], 7, 4, 5, 'Category 2')
data7=AddDatasetRow([data6], 6, 7, 6, 'Category 1')
data8=AddDatasetRow([data7], 9, 1, 7, 'Category 4')
data9=AddDatasetRow([data8], 2, 3, 4, 'Category 1')
data10=AddDatasetRow([data9], 8, 6, 7, 'Category 4')

Paste this example code into a HTML presentation object.

<div id="container<#uniqueId>"></div>

<script>
var poData<#uniqueId>;
var width<#uniqueId>;
var height<#uniqueId>;

function <#datasetChangeFunction>(datasetIdentifier, datasetChangeCallbackFunction) {
  if (datasetIdentifier == "exampleData") {
    datasetChangeCallbackFunction(myDatasetAvailable<#uniqueId>);
  }
}

function myDatasetAvailable<#uniqueId>(datasetData) {
  if (datasetData == null || datasetData.sheets == null || datasetData.sheets.length == 0 || datasetData.sheets[0] == null || datasetData.sheets[0].values == null || datasetData.sheets[0].values.length == 0) return;
  poData<#uniqueId> = [];
  for (var row = 1; row < datasetData.sheets[0].values[0].length; row++) {
    var rowObject = {};
    for (var column = 0; column < datasetData.sheets[0].values.length; column++) {
      rowObject[datasetData.sheets[0].values[column][0].attribute] = datasetData.sheets[0].values[column][row].value;
    }
    poData<#uniqueId>.push(rowObject);
  }
  drawPO<#uniqueId>();
}

function <#resizeFunction>(newWidth, newHeight) {
  width<#uniqueId> = newWidth;
  height<#uniqueId> = newHeight;
  $("#container<#uniqueId>").width(newWidth);
  $("#container<#uniqueId>").height(newHeight);
  drawPO<#uniqueId>();
}

function drawPO<#uniqueId>() {
  if (poData<#uniqueId> == null || width<#uniqueId> == null) return;
  $("#container<#uniqueId>").empty();
  Highcharts.chart("container<#uniqueId>", {
    chart: {
      type: 'bubble',
      plotBorderWidth: 1,
      zoomType: 'xy'
    },
    title: {
      text: 'Example bubble chart with random data'
    },
    credits: {
      enabled: false
    },
    xAxis: {
      gridLineWidth: 1
    },
    yAxis: {
      startOnTick: false,
      endOnTick: false,
      title: ""
    },
    series: formatDataset(poData<#uniqueId>, {"x": "Value", "y": "Risk", "x": "Value", "z": "Cost"}, "Category", "name", "data")
  });
}

function formatDataset(data, mappings, groupColumn, groupAttribute, innerArrayAttribute) {
  var output = [];
  var namesToMap = Object.getOwnPropertyNames(mappings);
  for (var i = 0; i < data.length; i++) {
    var row = {};
    var mappedColumns = [];
    for (var j = 0; j < namesToMap.length; j++) {
      row[namesToMap[j]] = data[i][mappings[namesToMap[j]]];
      mappedColumns.push(mappings[namesToMap[j]]);
    }
    var dataColumns = Object.getOwnPropertyNames(data[i]);
    for (j = 0; j < dataColumns.length; j++) {
      if (mappedColumns.indexOf(dataColumns[j]) == -1) {
        row[dataColumns[j]] = data[i][dataColumns[j]];
      }
    }
    output.push(row);
  }

  if (groupColumn == null) {
    return output;
  } else {
    var groups = {};
    for (var i = 0; i < output.length; i++) {
      var groupName = output[i][groupColumn];
      if (groups[groupName] == null) groups[groupName] = [];
      groups[groupName].push(output[i]);
      delete output[i][groupColumn];
    }
    var outputArray = [];
    var groupNames = Object.getOwnPropertyNames(groups);
    for (var i = 0; i < groupNames.length; i++) {
      var groupItem = {};
      groupItem[groupAttribute] = groupNames[i];
      groupItem[innerArrayAttribute] = groups[groupNames[i]];
      outputArray.push(groupItem);
    }
    return outputArray;
  }
}
</script>