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);
Related
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;
}
I would like to have shapes (small squares) that mark data points in the line chart that I am creating with ChartFactory.createLineChart().
It should look something like this (this image was not created with JFreeChart):
I have followed the description here, however, they don't appear for me. This is what the output from my JFreeChart software looks like:
My code is:
JFreeChart lineChart = ChartFactory.createLineChart(...);
CategoryPlot plot = (CategoryPlot) lineChart.getPlot();
plot.getRenderer().setBaseShape(
new Rectangle2D.Double(-20.0, -20.0, 40.0, 40.0));
I also tried using setSeriesShape instead of setBaseShape for all the series I'm plotting, it didn't make any difference either.
What am I doing wrong?
Given a reference to the renderer,
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
Invoke setBaseShapesVisible() to enable the shapes provided by your chosen DrawingSupplier.
renderer.setBaseShapesVisible(true);
To change the appearance, pass a custom Shape to setSeriesShape() for the desired series.
renderer.setSeriesShape(0, new Ellipse2D.Double(-3d, -3d, 6d, 6d));
From JFreechart 1.5.0
LineAndShapeRenderer renderer = (LineAndShapeRenderer) lineChart.getCategoryPlot().getRenderer();
renderer.setDefaultShapesVisible(true);
We are using the jfreechart with Jasper reports and we are struggling to put the benchmark line on the bar chart.
How can this be achieved using jasper reports?
To customize your bar chart in jasper report create a customizer class (ChartCustomizer) extending the JRChartCustomizer.
public void customize(JFreeChart chart, ChartComponent chartComponent)
{
//get the ploy
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//Now add your markers
ValueMarker vm = new ValueMarker(200); //200 is the position you like it to be
vm.setPaint(Color.RED);
vm.setStroke(new BasicStroke(1));
vm.setLabel("BeanchMark value"); //The label
vm.setLabelAnchor(RectangleAnchor.TOP);
vm.setLabelTextAnchor(TextAnchor.BOTTOM_RIGHT);
plot.addRangeMarker(vm);
}
add the class to classpath and in jrxml set the customizerClass attribute
<barChart>
<chart customizerClass="my.package.ChartCustomizer">
....
</chart>
...
</barChart>
We have resolved it by using the following code in customise class
ValueMarker marker = new ValueMarker(30);
marker.setLabel("Average 30%");
marker.setPaint(Color.black);
plot.addRangeMarker(marker);
However we need to change the label position, currently it is showing at the start of the line.
For one single horizontal line you can use provided chart customizer:
Go to Chart -> Properties -> Chart (tab) -> Chart customizers
There you can add a Range Interval Marker and configure it with start and end values with the desired value (35 in your example).
This way an horizontal line will be drawn in the 35 vertical value as you wanted.
This has reference to JFreeChart rendering of candlestick charts. Below is the code fragment that generates a candle stick chart with JFreeChart.
This code has been tested and has been working for a long time. However, the version of JFreeChart was changed from 1.0.17 to 1.0.19 and the candlestick chart generated with 1.0.19 is showing smudging of the candle objects/lines. When I changed the library back to 1.0.17, the candlestick objects/lines once again becomes clear.
The images with both the libraries are provided below.
I have tried to find the cause of this and have been unsuccessful as yet. Now, the question is, since the code is tested and possibly does not have any error (at least what I can figure or am I missing something?), is the issue with the library? Have anyone faced this problem and has an work around
I shall be rather grateful, if someone has found the reason/solution to this and shared the same.
Please use MS Paint to view the images.
try{
chart=ChartFactory.createCandlestickChart("Candlestick Chart", "Date", "EOD Closing Price", (OHLCDataset)dataset, true);
plot=(XYPlot)chart.getPlot();
CandlestickRenderer renderer=new Chart_CandlestickRenderer();//(CandlestickRenderer)plot.getRenderer();
renderer.setSeriesPaint(0, Color.BLACK);
renderer.setUpPaint(Color.WHITE);
renderer.setDownPaint(Color.BLACK);
//HighLowItemLabelGenerator candleTooltipGenerator=new HighLowItemLabelGenerator(new SimpleDateFormat("dd-MMM-yyyy"), new DecimalFormat());
XYToolTipGenerator candleTooltipGenerator=Chart_TooltipProvider.getOHLCTooltipGenerator();
renderer.setBaseToolTipGenerator(candleTooltipGenerator);
plot.setRenderer(0,renderer);
//Organize the data to draw Fibbonacci retracements with highs and lows
DefaultOHLCDataset ohlcDataset=(DefaultOHLCDataset)dataset;
int dataCount=ohlcDataset.getItemCount(0);
data=new double[dataCount*2];//for each data item we shall get 2 values, high and low
for(int i=0;i<dataCount;i++){
//for each i 2 data values need to be put into the array and adjust the index accordingly
data[i*2]=ohlcDataset.getHighValue(0, i);
data[i*2+1]=ohlcDataset.getLowValue(0, i);
}//for closing
//If there is only the candlestick to be drawn, return, as the job has been done, draw the Fibonnaci and return
if(indicators.length==1){
this.drawFibonnaciRetracement(data, plot);
retVal=true;
return retVal;
}//if closing
}catch(Exception e){e.printStackTrace();return retVal;}
Try setting setAntiAlias of the JFreeChart to false.
JFreeChart chart = ChartFactory.createCandlestickChart(...);
chart.setAntiAlias(false);
my bar chart is always drawn with a gradient color by default. I just want a simple color without any styled effects.
Can anyone help ?
Code:
final JFreeChart chart = ChartFactory.createBarChart(
"", // chart title
xLabel, // domain axis label
yLabel, // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
false, // tooltips?
false // URLs?
);
final CategoryPlot plot = chart.getCategoryPlot();
// SOMETHING HAS TO BE DONE HERE
showChart(chart); // Simply shows the chart in a new window
Thanks
The problem lies in the BarPainter you are using. The JFreeChart version 1.0.13 default is to use GradientBarPainter which adds a metallic-ish look to the bar. If you want the "old" look the solution is to use the StandardBarPainter.
final CategoryPlot plot = chart.getCategoryPlot();
((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());
That should do it.
Alternatively, if you want use JFreeChart's BarRenderer, you could force it to use the StandardBarPainter by calling the static method setDefaultBarPainter() before initializing your renderer.
final CategoryPlot plot = chart.getCategoryPlot();
BarRenderer.setDefaultBarPainter(new StandardBarPainter());
((BarRenderer) plot.getRenderer()).setBarPainter(new BarPainter());
If you want more control of the chart you can always build it from the ground up instead of using ChartFactory, but that does require a lot extra code.
Before you create the chart from ChartFactory you can set the chart theme:
ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
The default is the JFreeTheme which adds the gradient. The following themes are available:
ChartFactory.setChartTheme(StandardChartTheme.createJFreeTheme());
ChartFactory.setChartTheme(StandardChartTheme.createDarknessTheme());
The source code for an older version of org.jfree.chart.demo.BarChartDemo1 shows how to set the series colors. Just specify plain colors instead of gradients.
renderer.setSeriesPaint(0, Color.red);
renderer.setSeriesPaint(1, Color.green);
renderer.setSeriesPaint(2, Color.blue);
Correction: The key to #Jes's helpful answer may be found in the initialization of defaultBarPainter in BarRenderer.