Change the renderer of a JRChart in a precompiled Jasperreport - java

I am creating a Jasper report template in IReport which I'm compiling and filing using a Java application.
On a scatter chart I want to plot a regression line as a spline. Currently, the only way I found to do this was using the JFreeCharts XYSplineRenderer. However, this requires me to create the chart in my Java code and then pass it to the report as a parameter while filling it. This also requires me to customize the appearance of my chart in the code.
I would like to be able to design my chart inside the jrxml with IReport, compile the report in Java, fetch the chart object and set a custom renderer to it before filling it.
Something like this:
JasperReport report = JasperCompileManager.compileReport(reportIs);
JRChart jrChart = (JRChart) report.getSummary().getElementByKey("chart");
JFreeChart chart = jrChart.getJFreeChart(); // this method does not exist
((XYPlot)chart.getPlot()).setRenderer(new XYSplineRenderer());
The problem is that I can't find a way to retrieve an actual JFreeChart object from a compiled report, nor can I find any other method to pass a custom plot renderer to a chart.
Does anyone have any ideas?
Thanks in advance.

You can use a chart customizer to accomplish this. It allows you to access the chart as a JFreeChart (as well as a JRChart) which should be just what you need. Add a chart customizer to your chart by setting the customizerClass attribute. The customize method is called by Jasper and allows you to modify the chart during the report filling process.
Create a customizer that looks something like this:
public class MyChartCustomizer implements JRChartCustomizer {
#Override
public void customize(JFreeChart jFreeChart, JRChart jRChart) {
((XYPlot)jFreeChart.getPlot()).setRenderer(new XYSplineRenderer());
}
}
Then compile it and make sure that it's on Jasper's classpath when you come to fill the report.

Related

Sencha GXT4 : How to display text inside a pie chart?

I'm developing an application with the GWT framework and Sencha GXT on the front-end.
I managed to display some data in a pie chart but I would also like to have a label inside the donut pie chart like . I looked at the demo provided by sencha as well as the "Chart" class but I don't see any cases where there is text inside a pie chart. Is it feasible ?
I'm using "Sencha GXT 4.0.0 - Sencha for GWT"
I finally found a solution, I just need to use the setX method in my textConfig so my code looks something like this :
TextSprite textConfig = new TextSprite();
textConfig.setFont("Arial");
textConfig.setTextBaseline(TextBaseline.MIDDLE);
textConfig.setFontSize(18);
textConfig.setTextAnchor(TextAnchor.MIDDLE);
textConfig.setZIndex(15);
textConfig.setX(-150);
150 worked for me but maybe it won't for you.

Implementing custom barchart in android

I'm trying to implement a custom barchart
which should look exactly like this. I tried mpChart but could only get this far
barDataSet = new BarDataSet(barEntries, "");
barData = new BarData(barDataSet);
barData.setBarWidth(.5f);
barChart.setData(barData);
barDataSet.setColors(getResources().getColor(R.color.colorPrimary));
barDataSet.setValueTextColor(Color.BLACK);
barDataSet.setDrawValues(false);
barChart.getXAxis().setDrawAxisLine(false);
barChart.setDrawGridBackground(false);
barChart.getXAxis().setDrawGridLines(false);
barChart.getAxisLeft().setDrawGridLines(false);
barChart.getAxisRight().setDrawGridLines(false);
barChart.getAxisLeft().setEnabled(false);
barChart.getAxisRight().setEnabled(false);
barChart.getLegend().setEnabled(false);
barChart.setPinchZoom(false);
barChart.setDescription(null);
barChart.setTouchEnabled(false);
barChart.setDoubleTapToZoomEnabled(false);
barChart.setExtraOffsets(20, 0, 0, 30);
XAxis xAxis = barChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextSize(14f);
xAxis.setTextColor(Color.BLACK);
xAxis.setYOffset(5);
xAxis.setDrawLabels(true);
xAxis.setGranularityEnabled(true);
xAxis.setGranularity(1f);
xAxis.setXOffset(30);
xAxis.setValueFormatter(new IndexAxisValueFormatter(xAxisLabel));
xAxis.setCenterAxisLabels(true);
barChart.invalidate();
I tried almost all possible examples and questions here, couldn't get the label to centre of the bars. Is it possible to put the labels exactly below the bars in non grouped bars? all the solutions I saw is only for grouped bars or combined charts.
I was plotting the x axis values from 1. when changed it to 0 and removed
xAxis.setCenterAxisLabels(true);
and i was able to centre the axis labels
You need to create a custom renderer for it.
Subclass BarChartRenderer and then call:
barChart.setRenderer(new MyBarChartRenderer(barChart, barChart.getAnimator(), barChart.getViewPortHandler()))
It will even allow you to alter the shape of it as described here.
But I think what you're looking for comes really close to this.
Download the MPAndroidChart Source from here. Then, you can customize the code according to your need and add it your project by
Clicking on File>New>Import Module.

Aspose slides data labels are overlapping in 'Area Chart'

I am using aspose-slides-17.3-jdk16.jar for java. I have created the Area Chart using IChartDataWorkbook. I have used multiple series to plot area chart and following image is the chart output, where the data labels are overlapped.
Is there any way to organize or fit the data labels properly?
#Chandra Shekar,
I have observed your requirements and like to mention that you can try using different options label.getDataLabelFormat().setLabelPosition() and label.getDataLabelFormat().setShowLabelAsDataCallout(true) methods on your end to set the individual label positions for chart data points. You can please try using following sample code on your end and may alter this as per your requirements on your end.
public static void TestAreaChart()
{
Presentation pres = new Presentation();
IChart chart = pres.getSlides().get_Item(0).getShapes().addChart(ChartType.Area, 50, 50, 500, 400);
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat().setShowValue(true);
chart.getChartData().getSeries().get_Item(0).getLabels().getDefaultDataLabelFormat()
.setShowLabelAsDataCallout(true);
chart.getChartData().getSeries().get_Item(0).getLabels().get_Item(2).getDataLabelFormat()
.setShowLabelAsDataCallout(true);
chart.getChartData().getSeries().get_Item(0).getLabels().get_Item(2).getDataLabelFormat()
.setPosition(LegendDataLabelPosition.OutsideEnd);
pres.save("C:\\Aspose Data\\AreaChart.pptx", SaveFormat.Pptx);
}
I am working as Support developer/ Evangelist at Aspose.

Vaadin Charts: Pie chart won't update dynamically

I created a Vaadin pie chart with following code.
Chart chart = new Chart(ChartType.PIE);
DataSeries dataSeries = new DataSeries("Logins");
chart.getConfiguration().setSeries(dataSeries);
I need to update my chart dynamically. I tried following (this code gets executed whenever new data is available).
adding new item:
dataSeries.add(new DataSeriesItem("New item", value), true, false);
updating existing item:
DataSeriesItem dataSeriesItem = dataSeries.get(0);
dataSeriesItem.setY(newValue);
dataSeries.update(dataSeriesItem);
But none of the above worked.
The only solution I could find is clearing the chart (chart.clear()), re, populating the data series and re drawing the chart (chart.drawChart()).
This method is not optimal since it re-draws the chart, and also the selection in the chart gets lost.
Does pie chart support dynamic updating? Can anyone suggest a way to fix this?
Try to call chart.getConfiguration().addSeries(dataSeries) before updating items. If that doesn't work, I think you should re-draw your chart.
I had the same problem and fixed it with chart.drawChart() after updating the data series in configurations (chart.getConfiguration.setSeries(newSeries)).
In my case (vaadin version 7.6.3), chart.clear() was not required.
There are other more complicated ways like vaadin-push plugin as well which I didn't find necessary for my case.
I know this question is old, but I will share how I solved this problem to Chart Pie in DChart 1.7.0.
DataSeries dataSeries = new DataSeries();
dataSeries.newSeries();
dataSeries.add( "Food", 500);
dataSeries.add( "Clothes", 1000);
chart.setDataSeries(dataSeries);
chart.show(); //this will update the chart in the browser;
Late to the question, but I encountered similar problem in 2020, I solved it using below code:
dataSeries.clear();
dataSeries.add(new DataSeriesItem("Expense", 120));
dataSeries.add(new DataSeriesItem("Saving", 20));
dataSeries.add(new DataSeriesItem("Balance", 100));
chart.getConfiguration().setSeries(dataSeries);
chart.drawChart();

Nebula Nattable - Change columns dynamically

I am using a Nattable inside a eclipse ViewPart. My RCP application is such that, each time I open the view, the table can have different columns based on the datamodel(represented by a different POJO) I wish to display.In short, can I do something like the below? :
`
IWorkBenchPage page = .....;
MyView view = page.openView(MyViewID,"myview",VIEW_ACTIVATE);
IDataProvider dp = ....;
// the following statement will set the dataprovider, the necessary layers
view.dataProvider(dp);
view.getTable().refresh();
....
`
I Believe this is possible, if you change the data provider (including the column accessor).
I have a similar situation in my product, and I found that the easiest way is to dispose the old table and recreate a new table with the new data.

Categories

Resources