How to combine multiple charts created using JFreeChart API? - java

My requirement is need to display 2 barchart and 3 trendchart in single page. For that, i have used JFreeChart API and sending the image in bytearray format through response. Its working fine for single image. How do i merge all 5 charts and send that through response?. Please help me.
Sample Code:
chartTypeCode="1";
chartTypeDesc="Order Line Status Bar Chart";
chart = createChart(defaultFromDate,defaultToDate,chartTypeCode);
chartImage[0] = chart.createBufferedImage(600, 400);
chartTypeCode="2";
chartTypeDesc="Order Sent To OMS Trend Chart";
chart = createTrendChart(defaultFromDate,defaultToDate,chartTypeCode);
chartImage[1] = chart.createBufferedImage(600, 400);
chartTypeCode="3";
chartTypeDesc="Shipment Trend Chart";
chart = createTrendChart(defaultFromDate,defaultToDate,chartTypeCode);
chartImage[2] = chart.createBufferedImage(600, 400);
chartTypeCode="4";
chartTypeDesc="Return Trend Chart";
chart = createTrendChart(defaultFromDate,defaultToDate,chartTypeCode);
chartImage[3] = chart.createBufferedImage(600, 400);
chartTypeCode="5";
chartTypeDesc="PSP Email Bar Chart";
chart = createChart(defaultFromDate,defaultToDate,chartTypeCode);
chartImage[4] = chart.createBufferedImage(600, 400);

I don't know that you can send many charts as they are individual images.
You should consider instead saving each chart to your server using servlet utilities and then using JFreeChart's servlet to query each image. This will allow you to use standard image tags which means you can use text html with maps (for tooltips) and css.
See my answer to a similar question for more info.

Related

Vaadin Pie chart takes a strange form when none of the legend labels is selected

I am using Vaadin framework with Spring boot and I need to display a PIE Chart with its contents. The thing is everything is working fine and when I click the legends labels the animation works fine.
Pre-click:
When I click the labels:
Note that when I clicked the labels the pie changed, which is what I want.
But when I click all the labels:
The pie chart turns into this blue I don't even know what this is.
I was wondering if anyone knows how would I access the labels in the legends to check if they are selected via Java and if none of them is selected I wouldn't let the user deselect the last one.
Thank you for your help
Find the chart code here:
private Component getCompaniesChart() {
Chart chart = new Chart(ChartType.PIE);
Configuration conf = chart.getConfiguration();
DataSeries dataSeries = new DataSeries();
service.findAllDepartments()
.forEach(Department -> dataSeries.add(new DataSeriesItem(Department.getName(), Department.getEmployeeCount())));
Options3d options3d = new Options3d();
options3d.setEnabled(true);
options3d.setAlpha(70);
options3d.setBeta(0);
conf.getChart().setOptions3d(options3d);
PlotOptionsPie plotOptions = new PlotOptionsPie();
plotOptions.setDepth(100);
plotOptions.setAllowPointSelect(true);
plotOptions.setShowInLegend(true);
plotOptions.setShadow(true);
plotOptions.setSize("200%");
conf.getLegend().setLabelFormat("{name} ({y})");
conf.getLegend().setItemMarginTop(100);
conf.setPlotOptions(plotOptions);
conf.setSeries(dataSeries);
conf.getChart().setStyledMode(true);
return chart;
}

Generate a png of a chart in Java [duplicate]

How does one programmatically render image files from JFreeChart . I have code for ChartPanel: is it possible to render directly to an image - without creating AWT/GUI components?
val chart = .. // Imagine code to generate chart data here..
val panel = new ChartPanel(chart)
panel.renderToImage() // replace this with logic to save image file
Use one of the ChartUtilities methods, such as writeChartAsJPEG() or writeChartAsPNG(), that can write to a java.io.OutputStream; or saveChartAsJPEG() or saveChartAsPNG(), that can write to a java.io.File. Some examples are seen here and here. For example,
ChartUtilities.saveChartAsPNG(new File("chart.png"), chart, width, height);
You'll also want to look into headless mode, mentioned here.

Aspose slides 'Area chart' series value not shown in the tooltip

I am using aspose-slides-17.3-jdk16.jar for java. I have created the Area Chart using IChartDataWorkbook and I'm able to get the series values in the tool tip for all the charts except Area chart.
Following is the code I have done so far.
public class AreaChartSlide {
public static void main(String[] args) {
Presentation pres = new Presentation();
ISlide slide = pres.getSlides().get_Item(0);
// Creating the default chart
IChart chart = slide.getShapes().addChart(ChartType.Area, 0, 0, 500, 400);
// Getting the default chart data worksheet index
int defaultWorksheetIndex = 0;
// Getting the chart data worksheet
IChartDataWorkbook fact = chart.getChartData().getChartDataWorkbook();
// Delete demo series
chart.getChartData().getSeries().clear();
// Add new series
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.getType());
chart.getChartData().getSeries().add(fact.getCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.getType());
// Adding new categories
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 1, 0, "Cat 1"));
chart.getChartData().getCategories().add(fact.getCell(defaultWorksheetIndex, 2, 0, "Cat 2"));
// Take first chart series
IChartSeries series = chart.getChartData().getSeries().get_Item(0);
// Now populating series data
series.getDataPoints().addDataPointForAreaSeries(fact.getCell(defaultWorksheetIndex, 1, 1, 20));
series.getDataPoints().addDataPointForAreaSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 50));
// Setting fill color for series
series.getFormat().getFill().setFillType(FillType.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(Color.RED);
// Take second chart series
series = chart.getChartData().getSeries().get_Item(1);
// Now populating series data
series.getDataPoints().addDataPointForAreaSeries(fact.getCell(defaultWorksheetIndex, 2, 1, 30));
series.getDataPoints().addDataPointForAreaSeries(fact.getCell(defaultWorksheetIndex, 2, 2, 10));
series.getFormat().getFill().setFillType(FillType.Solid);
series.getFormat().getFill().getSolidFillColor().setColor(Color.GREEN);
pres.save("E:/PPT/Aspose.pptx", SaveFormat.Pptx);
System.out.println("done");
}
Below is the chart output i got
Any help would be greatly appreciated.
I have worked with shared sample code using Aspose.Slides for Java 17.5 on my end and have generated a presentation with Area chart having two series and two categories. I have opened the generated presentation in PowerPoint and able to observe the tool-tip text for both chart series. Please observe Series 1 and Series 2 tool-tip text display for your kind reference. I hope the shared information will be helpful. Please share, if I may help you further in this regard.
I am working as Support developer/ Evangelist at Aspose.
We are sorry for your inconvenience.
I have observed your comments and comparison image of tool-tip text display for Bar chart and Area chart. I like to add here that this is not some issue in Aspose.Slides charting engine but its a PowerPoint implementation. If you add an Area chart using PowerPoint, that too will display the point and series name in tool-tip text. Please observe the tool-tip text for slides 2 and 3 in this presentation. You can observe in this snapshot as well that tool-tip text for PowerPoint generated chart is also displaying series and point as it is being shown for similar chart type generated using Aspose.Slides. I hope the shared information will be helpful.
I am working as Support developer/ Evangelist at Aspose.
Many Thanks.

Server-side/ non-interactive rendering of Charts to image files from JFreeChart

How does one programmatically render image files from JFreeChart . I have code for ChartPanel: is it possible to render directly to an image - without creating AWT/GUI components?
val chart = .. // Imagine code to generate chart data here..
val panel = new ChartPanel(chart)
panel.renderToImage() // replace this with logic to save image file
Use one of the ChartUtilities methods, such as writeChartAsJPEG() or writeChartAsPNG(), that can write to a java.io.OutputStream; or saveChartAsJPEG() or saveChartAsPNG(), that can write to a java.io.File. Some examples are seen here and here. For example,
ChartUtilities.saveChartAsPNG(new File("chart.png"), chart, width, height);
You'll also want to look into headless mode, mentioned here.

Generating PDF report : jFreeChart and DynamicReports

I need to generate a report as shown below:
I have designed a GUI using swing in NetBeans to enter the details:
The Plot I have generated using jFreeChart:
JFreeChart chart = ChartFactory.createXYLineChart(
"Hysteresis Plot", // chart title
"Pounds(lb)", // domain axis label
"Movement(inch)", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
true, // tooltips
false // urls
);
OutPut:
I was searching the internet and read that I can use iText or JasperReports or DynamicReports (Based on Jasper Report)
http://www.dynamicreports.org/getting_started.html#step9
I found using Dynamic Reports easier. My question is- can i use DynamicReports for my purpose (I suppose- yes looking at the sample reports) and if yes then how do I export my jFreeChart to the report.
Please help as I do not have much time left to complete this project.
Thanks
You can create the chart directly in DynamicReports instead of JFreeChart. Use the DynamicReports XYLineChartReport component to do this. See the example code at http://www.dynamicreports.org/examples/xylinechartreport.html.
If you want to use the JFreeChart output, export the chart to an image and then include that image in the report using cmp.image():
// Create the chart.
JFreeChart chart = ChartFactory.createXYLineChart(
"Hysteresis Plot", // chart title
"Pounds(lb)", // domain axis label
"Movement(inch)", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
true, // tooltips
false // urls
);
// Export the chart to an image.
BufferedImage image = chart.createBufferedImage( 300, 300);
report()
.title(cmp.text("XYZ HOSPITAL"))
.columns(fieldNameColumn, fieldValueColumn)
.summary(
cmp.verticalList()
.add(cmp.text("HYSTERISIS PLOT"))
.add(cmp.text("A brief description of what this plot signifies"))
.add(cmp.image(image)) // Add the exported chart image to the report.
.add(cmp.text("REMARKS"))
)
.setDataSource(createDataSource())
.toPDF(outputStream);

Categories