How to format to two decimal places on CategoryPlot chart - jasper-reports

I'm trying to format my report to only show 2 decimal places with percentage through my JRXML report, but even with a class customizer, it's not working and when it displays on PDF file.
My code (customizer class):
import net.sf.jasperreports.engine.JRChart;
import net.sf.jasperreports.engine.JRChartCustomizer;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
public void customize(JFreeChart chart, JRChart jasperChart){
CategoryPlot barPlot = (CategoryPlot) chart.getPlot();
BarRenderer renderer = (BarRenderer) barPlot.getRenderer();
NumberFormat formatter = new DecimalFormat("#0.00%");
formatter.setMinimumFractionDigits(2);
StandardCategoryItemLabelGenerator scilg2 = new StandardCategoryItemLabelGenerator("{0} {2}", formatter);
renderer.setItemLabelGenerator(scilg2);
}
I already searched on every forum, but the answers didn't worked, most of them were applied to Pie chart, but that's not my case.
PS: I'm using JFreeChart 1.0.0 version and iReport 1.2.8.

Use the ArgumentIndex value {3}, seen here, and supply a separate percentFormatter. Using v.1.5.3, I added the following to BarChartDemo1 to get the image shown:
plot.setOrientation(PlotOrientation.HORIZONTAL);
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(2);
renderer.setDefaultItemLabelGenerator(
new StandardCategoryItemLabelGenerator(
"{0} {1} {2} {3}", NumberFormat.getInstance(), percent));
renderer.setDefaultItemLabelsVisible(true);

I'm using JFreeChart 1.0.0 version and iReport 1.2.8.
So I have finally been able to format the numbers by using this code below—posting so if someone ends up with the same problem as I was, they can correct it.
NumberFormat formatter = new DecimalFormat("#0.00'%'");
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
StandardCategoryItemLabelGenerator base = new StandardCategoryItemLabelGenerator("{2}",formatter);
renderer.setBaseItemLabelGenerator(base);
barPlot.setRenderer(renderer);
You have to use the ArgumentIndex value {2}, and after that you need to use the method setRenderer() to send a change event to all registered listeners.
Thanks #trashgod (https://stackoverflow.com/users/230513/trashgod) for your help, I got to work around your code and display the results on previous versions of the JFreeChart.
Links:
CategoryPlot - setRenderer
AbstractCategoryItemLabelGenerator

Related

XLSX file via OpenXml SDK Both Valid and Invalid

I have a program which exports a System.Data.DataTable to an XLSX / OpenXml Spreadsheet. Finally have it mostly working. However when opening the Spreadsheet in Excel, Excel complains about the file being invalid, and needing repair, giving this message...
We found a problem with some content in . Do you want us to
try to recover as much as we can? If you trust the source of the
workbook, clik Yes.
If I click Yes, it comes back with this message...
Clicking the log file and opening that, just shows this...
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error268360_01.xml</logFileName>
<summary>Errors were detected in file 'C:\Users\aabdi\AppData\Local\Temp\data.20190814.152538.xlsx'</summary>
<repairedRecords>
<repairedRecord>Repaired Records: Cell information from /xl/worksheets/sheet1.xml part</repairedRecord>
</repairedRecords>
</recoveryLog>
Obviously, we don't want to deploy this into a production environment like this. So I've been trying to figure out how to fix this. I threw together a quick little sample to validate the XML and show the errors, based on this link from MSDN. But when I run the program and load the exact same XLSX document that Excel complains about, the Validator comes back saying that the file is perfectly Valid. So I'm not sure where else to go from there.
Any better tools for trying to validate my XLSX XML? Following is the complete code I'm using to generate the XLSX file. (Yes, it's in VB.NET, it's a legacy app.)
If I comment out the line in the For Each dr As DataRow loop, then the XLSX file opens fine in Excel, (just without any data). So it's something with the individual cells, but I'm not really DOING much with them. Setting a value and data type, and that's it.
I also tried replacing the For Each loop in ConstructDataRow with the following, but it still outputs the same "bad" XML...
rv.Append(
(From dc In dr.Table.Columns
Select ConstructCell(
NVL(dr(dc.Ordinal), String.Empty),
MapSystemTypeToCellType(dc.DataType)
)
).ToArray()
)
Also tried replacing the call to Append with AppendChild for each cell too, but that didn't help either.
The zipped up XLSX file (erroring, with dummy data) is available here:
https://drive.google.com/open?id=1KVVWEqH7VHMxwbRA-Pn807SXHZ32oJWR
Full DataTable to Excel XLSX Code
#Region " ToExcel "
<Extension>
Public Function ToExcel(ByVal target As DataTable) As Attachment
Dim filename = Path.GetTempFileName()
Using doc As SpreadsheetDocument = SpreadsheetDocument.Create(filename, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook)
Dim data = New SheetData()
Dim wbp = doc.AddWorkbookPart()
wbp.Workbook = New Workbook()
Dim wsp = wbp.AddNewPart(Of WorksheetPart)()
wsp.Worksheet = New Worksheet(data)
Dim sheets = wbp.Workbook.AppendChild(New Sheets())
Dim sheet = New Sheet() With {.Id = wbp.GetIdOfPart(wsp), .SheetId = 1, .Name = "Data"}
sheets.Append(sheet)
data.AppendChild(ConstructHeaderRow(target))
For Each dr As DataRow In target.Rows
data.AppendChild(ConstructDataRow(dr)) '// THIS LINE YIELDS THE BAD PARTS
Next
wbp.Workbook.Save()
End Using
Dim attachmentname As String = Path.Combine(Path.GetDirectoryName(filename), $"data.{Now.ToString("yyyyMMdd.HHmmss")}.xlsx")
File.Move(filename, attachmentname)
Return New Attachment(attachmentname, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
End Function
Private Function ConstructHeaderRow(dt As DataTable) As Row
Dim rv = New Row()
For Each dc As DataColumn In dt.Columns
rv.Append(ConstructCell(dc.ColumnName, CellValues.String))
Next
Return rv
End Function
Private Function ConstructDataRow(dr As DataRow) As Row
Dim rv = New Row()
For Each dc As DataColumn In dr.Table.Columns
rv.Append(ConstructCell(NVL(dr(dc.Ordinal), String.Empty), MapSystemTypeToCellType(dc.DataType)))
Next
Return rv
End Function
Private Function ConstructCell(value As String, datatype As CellValues) As Cell
Return New Cell() With {
.CellValue = New CellValue(value),
.DataType = datatype
}
End Function
Private Function MapSystemTypeToCellType(t As System.Type) As CellValues
Dim rv As CellValues
Select Case True
Case t Is GetType(String)
rv = CellValues.String
Case t Is GetType(Date)
rv = CellValues.Date
Case t Is GetType(Boolean)
rv = CellValues.Boolean
Case IsNumericType(t)
rv = CellValues.Number
Case Else
rv = CellValues.String
End Select
Return rv
End Function
#End Region
For anyone else coming in and finding this, I finally tracked this down to the Cell.DataType
Setting a value of CellValues.Date will cause Excel to want to "fix" the document.
(apparently for dates, the DataType should be NULL, and Date was only used in Office 2010).
Also, if you specify a DataType of CellValues.Boolean, then the CellValue needs to be either 0 or 1. "true" / "false" will also cause Excel to want to "fix" your spreadsheet.
Also, Microsoft has a better validator tool already built for download here:
https://www.microsoft.com/en-us/download/details.aspx?id=30425

Alternative to arrayToDataTable for date column

I'm new to stackexchange so my apologies if this question is too extensive or already answered somewhere I couldn't find. You can find the spreadsheet here, the script here and the dashboard (dev version) here.
I have been banging my head on handling dates in the google app script visualization for days.
My ultimate goal is to make a dashboard that includes an annotated timeline as well as other charts based on a data set in a spreadsheet. I have started this process using Mogsdad tutorial on creating a 3-tier google visualization dashboard, where the data is pulled from external spreadsheet and then pulled into the DataTable using arrayToDataTable. Everything worked great out of the box. However, my data contains dates, so I added a date column to the original data, but alas arrayToDataTable doesn't accept date type per this post. So when a Date column is added i get the following result:
ScriptError: The script completed but the returned value is not a
supported return type.
I have tried multiple approaches to ensure even date formatting: options includes putting the values in the date column through new Date(dateColumn[i]), dateColumn[i].toJSON() (renders the dash board, but dates aren't able to be processed), forced date formats in the spreadsheet (yyyy-MM-dd), using the DataView outlined in the post above (dashboards don't get past 'Loading'), and such.
So my question is what is the alternatives to arrayToDataTable that will accept date columns in this 3-tier approach? Or alternatively, what are the errors in the below methods?
For all the cases when I have attempted to add columns I have changed the code from var data = google.visualization.arrayToDataTable(response,false) to var data = google.visualization.DataTable()
I have tried the following:
Manually adding columns and manually adding data (not working)
//Add Columns
data.addColumn('string','Name');
data.addColumn('string','Gender');
data.addColumn('number','Age');
data.addColumn('number','Donuts eaten');
data.addColumn('date','Last Donut Eaten');
//Add Rows
data.addRows([
['Miranda','Female', 22,6,6],
['Jessica','Female',22,6,12],
['Aaron','Male',3,1,13]
]);
Automatically adding the rows without dates (The rows are added, but it only works if there are no date columns)
//Add Rows
for (var i=1; i<response.length; i++) {
data.addRow(response[i]);
}
Manually adding columns and automatically adding rows (not working, combination of 1 and 2)
Automatically adding the columns with loops (not working, neither if dates or not)
for (var i=0; i<response[0].length; i++) {
if (response[1][i] instanceof Date) { //Checks if first value is Date
data.addColumn('date',response[0][i]);
};
else if (response[1][i] instanceof Number) //Checks if first value is Number
data.addColum('number',response[0][i]);
else data.addColumn('string',response[0][i]; //Otherwise assume string
};
Thank you so much for your help!
you can use the Query (google.visualization.Query) class to pull the data from the spreadsheet,
this will convert the date column properly...
google.charts.load('current', {
packages:['table']
}).then(function () {
var queryURL = 'https://docs.google.com/spreadsheets/d/1aaxYNLCuPz3o3TA1jdryenUP01Qbkdaut4AR5eIhe9s/edit#gid=0';
var query = new google.visualization.Query(queryURL).send(function (response) {
var data = response.getDataTable();
// show column types
for (var i = 0; i < data.getNumberOfColumns(); i++) {
console.log(data.getColumnLabel(i), '=', data.getColumnType(i));
}
// draw table chart
var table = new google.visualization.Table(document.getElementById('chart-table'));
table.draw(data);
});
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart-table"></div>
note: the example uses jsapi to load the library,
this library should no longer be used.
according to the release notes...
The version of Google Charts that remains available via the jsapi loader is no longer being updated consistently. Please use the new gstatic loader.js from now on.
this will only change the load statement, see above snippet...

Jasper report excel export output

I have total 5 reports in 5 different files and i want to make only single excel file which contains these five reports and five different sheets how it will be possible with jasper report?
Please help
Thanks in advance
Instead of creating 5 reports just use one report and set the property net.sf.jasperreports.export.xls.one.page.per.sheet to true.
Then just rename them with net.sf.jasperreports.export.xls.sheet.name
and use the Page Break element
Search on the internet for more information if needed. There are actually a lot of examples for this.
First you have to create JasperPrint objects for each sheet as you normally would.
JasperPrint firstSheet = ...;
JasperPrint secondSheet = ...;
JasperPrint thirdSheet = ...;
JasperPrint fourthSheet = ...;
JasperPrint fifthSheet = ...;
Now JasperPrint already have the datasource at this point. Use the Arraylist to add the JasperPrint object and the exporter will use each object to create a sheet
JRXlsxExporter exporter = new JRXlsxExporter();
ArrayList<JasperPrint> list = new ArrayList<JasperPrint>();
list.add(firstSheet);
list.add(secondSheet);
list.add(thirdSheet);
list.add(fourthSheet);
list.add(fifthSheet);
exporter.setExporterInput(SimpleExporterInput.getInstance(list));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputFile));
exporter.exportReport();
And you need to use net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter as import

jasper reports table creation

I would like to create a table in my Jasper Report.
This could be done (and it is done :-) ) in iReport, but the content of the table (for example names of the columns) are changing, so I would like to create the report solely from JAVA code (by not using jrxml files).
How is it possible?
So far, this is what I have done (the main parts only):
//table component
final StandardTable table = new StandardTable();
final StandardColumn col1 = new StandardColumn();
final DesignCell col1Header = new DesignCell();
final JRDesignStaticText textElement = new JRDesignStaticText();
col1Header.addElement(textElement);
col1.setDetailCell(col1Header);
table.addColumn(col1);
/datasource
final JRDesignDatasetParameter param = new JRDesignDatasetParameter();
param.setName("REPORT_DATA_SOURCE");
final JRDesignExpression exp = new JRDesignExpression("$P{REPORT_DATA_SOURCE}");
param.setExpression(exp);
//datasetrun
final JRDesignDatasetRun drs = new JRDesignDatasetRun();
table.setDatasetRun(drs);
drs.addParameter( param );
How to continue?
Thank you,
krisy
In case anyone stumbles upon the same problem (and for future myself :-) ) here is how to continue the code above, to add a table element to the summary of the report:
//create reportElement (inside componentelement)
JRDesignComponentElement reportElem = new JRDesignComponentElement();
//reportElem.setKey("table");
reportElem.setComponentKey(new ComponentKey("http://jasperreports.sourceforge.net/jasperreports/components", "jr", "table"));
reportElem.setComponent(table);
reportElem.setHeight(100);
reportElem.setWidth(100);
//add to summary
jRSummary.addElement(reportElem);
(the hard part was to find out how to create the ComponentKey object)
Note: in order to use the table, the subdataset element needs to be created as well.
For htmlComponent the ComponentKey should be:
new ComponentKey("http://jasperreports.sourceforge.net/htmlcomponent", "jr",
"html");

How to limit export formats in crystal reports

For Crystal reports for visual studio .net 2005, you can export the report
to various file format such as pdf, excel, word, rpt etc. If I just want to
limit the user see only excel and word format and set the default file
format to excel, is there a way to do it? Sometimes too many choose is
not good, is it?
Using CRVS2010 , you can remove unwanted export Option.
A new feature of CRVS2010 is the ability to modify the available export formats from the viewer export button. The following C# sample code demonstrates how to set the CrystalReportViewer to export only to PDF and Excel file formats:
int exportFormatFlags = (int)(CrystalDecisions.Shared.ViewerExportFormats.PdfFormat | CrystalDecisions.Shared.ViewerExportFormats.ExcelFormat);
CrystalReportViewer1.AllowedExportFormats = exportFormatFlags;
For More Details Please refer below link..
http://scn.sap.com/community/crystal-reports-for-visual-studio/blog/2011/01/26/export-file-formats-for-sap-crystal-reports-for-vs-2010
Try this:
Dim formats As Integer
formats = (CrystalDecisions.Shared.ViewerExportFormats.PdfFormat Or CrystalDecisions.Shared.ViewerExportFormats.XLSXFormat)
CrystalReportViewer1.AllowedExportFormats = formats
You don't mention whether you are using C# / VB.NET or Web/WinForms.
C#
I don't think this is possible. You would have to implement your own Export Button.
Something along the lines of this MSDN article
C#
// Declare variables and get the export options.
ExportOptions exportOpts = new ExportOptions();
ExcelFormatOptions excelFormatOpts = new ExcelFormatOptions ();
DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
exportOpts = Report.ExportOptions;
// Set the excel format options.
excelFormatOpts.ExcelUseConstantColumnWidth = true;
exportOpts.ExportFormatType = ExportFormatType.Excel;
exportOpts.FormatOptions = excelFormatOpts;
// Set the disk file options and export.
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
diskOpts.DiskFileName = fileName;
exportOpts.DestinationOptions = diskOpts;
Report.Export ();
VB.NET
' Declare variables and get the export options.
Dim exportOpts As New ExportOptions()
Dim diskOpts As New DiskFileDestinationOptions()
Dim excelFormatOpts As New ExcelFormatOptions()
exportOpts = Report.ExportOptions
' Set the excel format options.
excelFormatOpts.ExcelTabHasColumnHeadings = true
exportOpts.ExportFormatType = ExportFormatType.Excel
exportOpts.FormatOptions = excelFormatOpts
' Set the export format.
exportOpts.ExportFormatType = ExportFormatType.Excel
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile
' Set the disk file options.
diskOpts.DiskFileName = fileName
exportOpts.DestinationOptions = diskOpts
Report.Export()
VB.NET
You used to be able to remove certain export DLLs from the client installation. i.e remove all apart from the Excel DLLs and then it would only display the export options as Excel
To disable Crystal Report Rpt format try this :
Dim formats As Integer
formats = (CrystalDecisions.Shared.ViewerExportFormats.AllFormats Xor CrystalDecisions.Shared.ViewerExportFormats.RptFormat)
CrystalReportViewer1.AllowedExportFormats = formats
Or Short Version :
CrystalReportViewer1.AllowedExportFormats = (CrystalDecisions.Shared.ViewerExportFormats.AllFormats Xor CrystalDecisions.Shared.ViewerExportFormats.RptFormat)