Follow

Creating a report from the JavaScript code

This is a JavaScript code that creates a report dynamically and passes it to a viewer.

JavaScript
var viewer = new Stimulsoft.Viewer.StiViewer(null, "StiViewer", false);

function onShowClick(){

	var jsonData = JSON.parse(jsonString.value);
	
	var dataSet = new Stimulsoft.System.Data.DataSet();
	dataSet.readJson(jsonData);
	var data = dataSet.tables.getByIndex(0);

	var report = new Stimulsoft.Report.StiReport();

	//Add data to datastore
	report.regData("data", "data", dataSet);

	//Fill dictionary
	var dataSource = new Stimulsoft.Report.Dictionary.StiDataTableSource(data.tableName, data.tableName, data.tableName);
	dataSource.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("Column1", "Column1", "Column1"));
	dataSource.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("Column2", "Column2", "Column2"));
	report.dictionary.dataSources.add(dataSource);

	var page = report.pages.getByIndex(0);

	//Create HeaderBand
	var headerBand = new Stimulsoft.Report.Components.StiHeaderBand();
	headerBand.height = 0.5;
	headerBand.name = "HeaderBand";
	page.components.add(headerBand);

	//Create Databand
	var dataBand = new Stimulsoft.Report.Components.StiDataBand();
	dataBand.dataSourceName = data.tableName;
	dataBand.height = 0.5;
	dataBand.name = "DataBand";
	page.components.add(dataBand);

	//Create texts
	var pos = 0;

	var columnWidth = Stimulsoft.Base.StiAlignValue.alignToMinGrid(page.width / data.columns.count, 0.1, true);

	var nameIndex = 1;

	for (var index in data.columns.list) {
		var dataColumn = data.columns.list[index];
		//Create text on header
		var headerText = new Stimulsoft.Report.Components.StiText();
		headerText.clientRectangle = new Stimulsoft.Base.Drawing.RectangleD(pos, 0, columnWidth, 0.5);
		headerText.text = dataColumn.caption;
		headerText.horAlignment = Stimulsoft.Base.Drawing.StiTextHorAlignment.Center;
		headerText.name = "HeaderText" + nameIndex.toString();
		headerText.brush = new Stimulsoft.Base.Drawing.StiSolidBrush(Stimulsoft.System.Drawing.Color.lightGreen);
		headerText.border.side = Stimulsoft.Base.Drawing.StiBorderSides.All;
		headerBand.components.add(headerText);

		//Create text on Data Band
		var dataText = new Stimulsoft.Report.Components.StiText();
		dataText.clientRectangle = new Stimulsoft.Base.Drawing.RectangleD(pos, 0, columnWidth, 0.5)
		dataText.text = String.format("{{0}.{1}}", data.tableName, dataColumn.columnName);
		dataText.name = "DataText" + nameIndex.toString();
		dataText.border.side = Stimulsoft.Base.Drawing.StiBorderSides.All;

		//Add highlight
		var condition = new Stimulsoft.Report.Components.StiCondition();
		condition.backColor = Stimulsoft.System.Drawing.Color.cornflowerBlue;
		condition.textColor = Stimulsoft.System.Drawing.Color.black;
		condition.expression = "(Line & 1) == 1";
		condition.item = Stimulsoft.Report.Components.StiFilterItem.Expression;
		dataText.conditions.add(condition);

		dataBand.components.add(dataText);

		pos = pos + columnWidth;

		nameIndex++;
	}

	//Create FooterBand
	var footerBand = new Stimulsoft.Report.Components.StiFooterBand();
	footerBand.height = 0.5;
	footerBand.name = "FooterBand";
	page.components.add(footerBand);

	//Create text on footer
	var footerText = new Stimulsoft.Report.Components.StiText();
	footerText.clientRectangle = new Stimulsoft.Base.Drawing.RectangleD(0, 0, page.width, 0.5);
	footerText.text = "Count - {Count()}";
	footerText.horAlignment = Stimulsoft.Base.Drawing.StiTextHorAlignment.Right;
	footerText.name = "FooterText";
	footerText.brush = new Stimulsoft.Base.Drawing.StiSolidBrush(Stimulsoft.System.Drawing.Color.lightGreen);
	footerBand.components.add(footerText);


	//Render report
	report.render();

	viewer.report = report;
};

0 Comments

Please sign in to leave a comment.