Thr following examples shows how to create charts from data sets.

1 Charts from data

Let's see how we can generate Excel charts using the following example.

Copy
<script>
    var data = [
    	["1","China",                   1378020000,1578020000],
    	["2","India",                   1266884000,1366884000],
    	["3","United States of America", 323128000, 353128000],
    	["4","Indonesia",                257453000, 374530000],
    	["5","Brazil",                   206081000, 246081000]
    ];
    
    // ==================================================================
    // Create an Excel workbook and sheet
    // ==================================================================
    var wb = new Ax.ms.Excel("countries");
    var sheet = wb.createSheet("sheet1");
    
    // ==================================================================
    // Create the row of columns including a comment on each
    // ==================================================================
    
    var columns = ["Rank", "Country", "Population\r\n2016", "Population\r\n2030"];
    var comment = ["The rank", "The country", "Total population at 2016", "Estimated population at 2030"];
    var row = sheet.createRow(0);
    for (var col = 0; col < columns.length; col++) {
    	var cell = row.createCell(col, columns[col]);
    	cell.setCellComment(comment[col]);
    }
    
    var wrapStyle = wb.createCellStyle();
    wrapStyle.setWrapText(true);
    row.getCell(2).setCellStyle(wrapStyle);
    
    // ==================================================================
    // Create the style for numbers
    // ==================================================================
    
    var numberStyle = wb.createCellStyle();
    console.log("Data formats:" + Ax.ms.Excel.DataFormat);
    numberStyle.setDataFormat("#,##0");
    numberStyle.setFont(wb.createFont().setBold(true).setColor(Ax.ms.Excel.Color.BLUE));
    
    // ==================================================================
    // Fill with data
    // ==================================================================
    
    for (var idx = 0; idx < data.length; idx++) {
    	var row = sheet.createRow(idx+1);
    	for (var col = 0; col < data[idx].length; col++) {
    		var cell = row.createCell(col, data[idx][col]);
    		if (col == 2 || col == 3) {
    			cell.setCellStyle(numberStyle);
    		}
    	}
    }
    
    // ==================================================================
    // Ensure all fits
    // ==================================================================
    wb.setAutoSizeColumns();
    
    // ==================================================================
    // Generate charts of all types
    // PIE not included as it requires extra config
    // ==================================================================
    
    console.log("Chart types: " + Ax.ms.Excel.CharType);
    
    var offset = 0;
    [
    Ax.ms.Excel.CharType.BAR, 
    Ax.ms.Excel.CharType.LINE, 
    Ax.ms.Excel.CharType.RADAR, 
    Ax.ms.Excel.CharType.SCATTER, 
    ].forEach( function(chartType) { 
    	// ChartType,title, col1, row1, col2, row2
    	var col = (offset & 1) == 0 ? 4 : 12;
    	var row = (offset & 1) == 0 ? (offset) * 5 : (offset - 1) * 5;
    	console.log("OFFSET=" + offset + " COL=" + col + " ROW=" + row + " " + chartType);
    	var chart = sheet.createChart(chartType.toString(), col, row, col + 8, row + 10);
    	
    	chart.setLegendDataSource (1, 5, 1, 1);
    	
    	// firstRow, lastRow, firstCol, lastCol, title
    	chart.addNumericDataSource(1, 5, 2, 2, "2016");
    	chart.addNumericDataSource(1, 5, 3, 3, "2030");
    	
    	chart.createLegend().setPosition(Ax.ms.Excel.LegendPosition.BOTTOM);
    	//chart.getCategoryAxis().setPosition(MSExcel.AxisPosition.RIGHT);
    	chart.getValueAxis().setNumberFormat("0.0");
    	
    	var series = chart.plot(chartType);
    	if (chartType == Ax.ms.Excel.CharType.LINE) {
    		console.log("Set marker for LINE on first series (year=2016)");
    		series[0].setMarkerStyle(Ax.ms.Excel.MarkerStyle.STAR);
    		series[0].setMarkerSize(26);
    	}
    	
    	offset++;
    })
    
    
    // ==================================================================
    // Return Excel workbook as a Blob 
    // ==================================================================
    return wb;
</script>

Copy the code in the SQL editor and place it inside <script> tags. Then, press run to proceed. The output will show the returned value of the script.

In the results window, click on disk blob image. The blob will be written to a temporary file with given Worksheet name and Excel will be open.

1.1 Chart from database data

Let's see how to get data from database (or any data source in the form or ResultSet) and how to place it at given sheet position.

Copy
<script>

var data = Ax.db.of("sysmaster").executeQuery(`
    SELECT TRUNC( sum(A.chksize* (C.pagesize/ A.pagesize) ) ) as totalsize ,
           TRUNC( sum(A.nfree * (C.pagesize/ A.pagesize) )  ) as nfree,
           TRUNC( sum(D.data * (C.pagesize/ A.pagesize)  ) ) as data,
           TRUNC( sum(D.idx  * (C.pagesize/ A.pagesize)  ) ) as idx,
           TRUNC( CASE WHEN SUM(D.data) + SUM(D.idx) + SUM(D.other) + SUM(A.nfree * (C.pagesize/ A.pagesize) ) < sum(A.chksize* (C.pagesize/ A.pagesize) )
                THEN sum(A.chksize* (C.pagesize/ A.pagesize) ) - SUM(A.nfree * (C.pagesize/ A.pagesize) ) - SUM(D.data) - SUM(D.idx)
                ELSE SUM(D.other)
             END ) other
      FROM syschktab A, sysdbstab B, syschktab C,
           (SELECT  TRUNC(partnum/1048575,0) dbsnum,
            sum(case when nkeys = 1 and npdata = 0 THEN 0
                 else npdata
             end) data,
            sum(case when nkeys = 1 and npdata = 0 THEN npused
                 when nkeys > 0 then npused - npdata
                 else 0
             end) idx,
            sum(case when nkeys = 0 then npused-npdata
                 else 0
             end) other
            FROM sysptnhdr
            GROUP BY 1) AS D(dbsnum, data, idx, other)
    `
    );
    
    var wb = new Ax.ms.Excel("Database storage");
    var sheet = wb.createSheet("sheet1");
    
    var useCellRef = false;
    if (useCellRef) {
        // 	cellref, ResultSet
        sheet.addHead("A1", data.toResultSet());
        sheet.addRows("A2", data.toResultSet());
    } else {
    	// col, row, ResultSet
        sheet.addHead(0, 0, data.toResultSet());
        sheet.addRows(0, 1, data.toResultSet());
    }
    
    var CharType = Ax.ms.Excel.CharType;
    var start_col = 0;
    var start_row = 4;
    var chart = sheet.createChart("DB Occupation", start_col, start_row, start_col + 8, start_row + 20);
    
    
    if (useCellRef) {
        // Using CellRangeAddress
        chart.setLegendDataSource ("A1:E1");
        chart.setLegendDataSource ("A2:E2", "Space");
    } else {
        // firstRow, lastRow, firstCol, lastCol, title (BASE 0!)
        chart.setLegendDataSource (0, 0, 0, 4);
        chart.addNumericDataSource(1, 1, 0, 4, "Space");
    }
 
    chart.createLegend().setPosition(Ax.ms.Excel.LegendPosition.BOTTOM);
    chart.getValueAxis().setNumberFormat("0.0");
    chart.plot(CharType.BAR);
    
    return wb;

</script>