Error with a class customiser for charts - jasper-reports

I want to customise my charts so I want to use a class customiser,
I have used the following code to change the categorie label to vertical , but I don't know why it generate an error!
the code:
public void customize(JFreeChart jfc, JRChart jrc) {
CategoryPlot myPlot = jfc.getCategoryPlot();
HorizontalCategoryAxis axis = (HorizontalCategoryAxis)myPlot.getDomainAxis();
axis.setVerticalCategoryLabels(true);
}
the error is:
cannot find symbol : Class HorizontalCategoryAxis
Also I have tried:
CategoryItemRenderer renderer = (CategoryItemRenderer) plot.getRenderer();
CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator("{2}", new DecimalFormat("0.00"));
renderer.setLabelGenerator(generator);
renderer.setItemLabelsVisible(true);
XYItemRenderer renderer2 = (XYItemRenderer) plot.getRenderer();
XYItemLabelGenerator generator2 = new StandardXYItemLabelGenerator("{2}", new DecimalFormat("0.00"));
renderer.setLabelGenerator(generator);
and the errors:
cannot find symbol appear for the line :
renderer.setLabelGenerator(generator);
and
no suitable constructor for
XYItemLabelGenerator generator2
my reference is:
http://files.blogjava.net/hao446tian/jfreechart-1.0.1-US_developer_guide.pdf
UPDATE
Still I can't see the categorieExpression( on the Y axis) :(((

First of all your failed code seems to be version mismatch between your examples and the library you use. The setLabelGenerator method has been removed and replaced with setBaseItemLabelGenerator:
CategoryPlot plot = yourPlot;
CategoryItemRenderer renderer = (CategoryItemRenderer) plot.getRenderer();
CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator("{2}", new DecimalFormat("0.00"));
renderer.setBaseItemLabelGenerator(generator);
renderer.setBaseItemLabelsVisible(true);
XYItemRenderer renderer2 = (XYItemRenderer) plot.getRenderer();
XYItemLabelGenerator generator2 = new StandardXYItemLabelGenerator("{2}",
new DecimalFormat("0.00"),
new DecimalFormat("0.00"));
renderer.setBaseItemLabelGenerator(generator);
You can see in my example that the StandardXYItemLabelGenerator takes two formats, one for the x values and one for y.
As for the use of HorizontalCategoryAxis it suffered the same fate as setLabelGenerator. Assuming you want a vertical plot with a CategoryAxis that has labels that are vertical, not horizontal, you can use setLabelAngle to achieve the desired effect:
public void customize(JFreeChart jfc, JRChart jrc) {
CategoryPlot myPlot = jfc.getCategoryPlot();
CategoryAxis axis = myPlot.getDomainAxis();
axis.setLabelAngle(-Math.PI / 2);
}

Related

Box Cloud Annotation Appearance in iText

This is my sample code to draw a box cloud annotation. I used code in PDFBox's implementation to draw a box cloud but i have a little problem when used in iText. I modified the border class and some parts to be usable in iText.
you can find the border class here.
My problem is, the top and right border clouds are not drawn. it seems their location is drawn beyond the rect difference. I figure the issue is with drawing the curves in cloudyPolygonImpl(). maybe itext has different ways to draw in PdfAppearance? I am not sure.
This is the what i have so far.
public class Test {
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("src.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("result.pdf"));
PdfDictionary be = new PdfDictionary();
be.put(PdfName.S, PdfName.C);
be.put(PdfName.I, new PdfNumber(1));
Rectangle location = new Rectangle(123.6f, 584.4f, 252.6f, 653.4f);
PdfAnnotation stamp = PdfAnnotation.createSquareCircle(stamper.getWriter(), location, "", true);
stamp.setBorderStyle(new PdfBorderDictionary(1, PdfBorderDictionary.STYLE_SOLID));
stamp.put(new PdfName("BE"), be);
stamp.setColor(BaseColor.RED);
PdfContentByte cb = stamper.getOverContent(1);
PdfAppearance app = cb.createAppearance(location.getWidth(), location.getHeight());
stamp.setAppearance(PdfName.N, app);
PdfArray stickyRect = stamp.getAsArray(PdfName.RECT);
Rectangle annotRect = new Rectangle(stickyRect.getAsNumber(0).floatValue(),
stickyRect.getAsNumber(1).floatValue(),
stickyRect.getAsNumber(2).floatValue(),
stickyRect.getAsNumber(3).floatValue());
PdfArray arrDiff = annotation.getAsArray(PdfName.RD);
Rectangle annotRectDiff = null;
if (arrDiff != null) {
annotRectDiff = new Rectangle(arrDiff.getAsNumber(0).floatValue(), arrDiff.getAsNumber(1).floatValue(),
arrDiff.getAsNumber(2).floatValue(), arrDiff.getAsNumber(3).floatValue()
}
// Create cloud appearance
CBorder cborder = new CBorder(app, 1, 1, annotRect);
cborder.createCloudyRectangle(annotRectDiff);
stamp.put(PdfName.RECT, new PdfRectangle(cborder.getRectangle()));
stamp.put(PdfName.RD, new PdfArray(new float[] {
cborder.getRectDifference().getLeft(),
cborder.getRectDifference().getBottom(),
cborder.getRectDifference().getRight(),
cborder.getRectDifference().getTop() }));
app.rectangle(cborder.getBBox());
app.transform(cborder.getMatrix());
app.setColorStroke(BaseColor.RED);
app.setLineWidth(1);
app.stroke();
stamper.addAnnotation(stamp, 1);
stamper.close();
reader.close();
}
}
The correct output should be that all borders be drawn with cloud but currently only the left and bottom are drawn.
(This answer is based on the code in revision 3 of your question as the changes in revision 4 introduced multiple errors.)
Your code here creates an invalid annotation appearance stream:
CBorder cborder = new CBorder(app, 1, 1, annotRect);
cborder.createCloudyRectangle(null);
stamp.put(PdfName.RECT, new PdfRectangle(cborder.getRectangle()));
stamp.put(PdfName.RD, new PdfArray(new float[] {
cborder.getRectDifference().getLeft(),
cborder.getRectDifference().getBottom(),
cborder.getRectDifference().getRight(),
cborder.getRectDifference().getTop() }));
app.rectangle(cborder.getBBox());
app.transform(cborder.getMatrix());
app.setColorStroke(BaseColor.RED);
app.setLineWidth(1);
app.stroke();
Its upper part creates a path:
2 j
121.58 588.63 m
122.06 588.95 122.6 589.18 123.16 589.3 c
120.73 588.78 119.18 586.4 119.7 583.96 c
120.19 581.67 122.35 580.14 124.68 580.44 c
...
122.06 596.42 122.6 596.64 123.16 596.76 c
121.09 596.32 119.6 594.49 119.6 592.36 c
119.6 590.87 120.34 589.47 121.58 588.63 c
h
Then app.rectangle(cborder.getBBox()) does not create anything (beware, this rectangle overload does not what you expect it to do!).
Then app.transform(cborder.getMatrix()) adds a change to the current transformation matrix, app.setColorStroke(BaseColor.RED) adds a change of the stroking color, and app.setLineWidth(1) adds a change of the line width:
1 0 0 1 -118.68 -579.48 cm
1 0 0 RG
1 w
And finally app.stroke() adds the command to stroke the path:
S
But between the definition of a path and the corresponding path drawing command, only clipping path instructions are allowed! Cf. Figure 9 – Graphics Objects – in the PDF specification ISO 32000-1.
You can fix this code like this, pulling up color and line width changes, and directly using the cloud bounding box:
// Create cloud appearance
app.setColorStroke(BaseColor.RED);
app.setLineWidth(1);
CBorder cborder = new CBorder(app, 1, 1, annotRect);
cborder.createCloudyRectangle(null);
stamp.put(PdfName.RECT, new PdfRectangle(cborder.getRectangle()));
stamp.put(PdfName.RD, new PdfArray(new float[] {
cborder.getRectDifference().getLeft(),
cborder.getRectDifference().getBottom(),
cborder.getRectDifference().getRight(),
cborder.getRectDifference().getTop() }));
app.stroke();
app.setBoundingBox(cborder.getBBox());
(CloudBoxAnnotation test testDrawLikeChitgoksImproved)
This in particular changes the result (as seen in Adobe Acrobat) from
to

Winform chart label wrong position

See Image. The £12,845.63 is 1st columns label. I've tried running below code on a blank, fresh chart, with all default settings (white background too) it does the same thing.
I'm populating chart like this:
private void InitializeData()
{
chart1.Series.Clear();
double i = 0;
double spacing = 0.1;
foreach (DataRow rows in DailyBarChartT.Rows)
{
Series series = chart1.Series.Add(rows[0].ToString());
series.Points.AddXY(i, rows[1]);
series.IsValueShownAsLabel = true;
series.LabelFormat = "C";
series.LabelForeColor = Color.White;
i = i + spacing;
}
chart1.Update();
}
I'm guessing the number doesn't fit above the blue bar? how could I fix this?
I've tried setting label margins to 0 and a few other things.
EDIT:
setting my "spacing" variable to 0, sets the label to correct position.
How can I have it in a correct position with spacing?
You create a new series for every value, which is not how you are supposed to do it. The spacing works properly if you make one single series for all values.
Quick sample code (works on default chart):
string[] values =
{
"12845.63", "1174.89",
"344.04", "180.83",
"82.50", "55.00"
};
chart1.ChartAreas[0].BackColor = Color.Black;
chart1.Series.Clear();
Series series = new Series();
series.IsValueShownAsLabel = true;
series.LabelFormat = "C";
series.LabelForeColor = Color.White;
foreach (var value in values)
{
var pointIndex = series.Points.AddY(value);
series.Points[pointIndex].AxisLabel = "Custom label for each value here";
}
chart1.Series.Add(series);

Unable to draw in processing language when I integrated it with Eclipse

My setup method looks like below, I want to read one location file(City names with x and y co-ordinates) and then I am creating one hash-map of all cities so that I can draw(Will make points) them all on canvas
public void setup(){
background(0);
PFont title = createFont("Georgia", 16);
textFont(title);
text("This is a visualization of A* algorithm", 240, 20);
stroke(255);
line(0,25,800,25);
selectInput("Select a file for Locations:", "locFileSelected");
}
locFileSelected method(locFilePath is a global variable used):
public void locFileSelected(File locFile) {
locFilePath = locFile.toString();
this.readLocFileAndDraw();
}
Now control is transferred to readLocFileAndDraw (Each line in file has space separated 3 words, 1st is city name followed by x and y co-ordinates:
private void readLocFileAndDraw() {
try (Stream<String> lines = Files.lines(Paths.get(locFilePath))) {
for (String line : (Iterable<String>) lines::iterator){
// Last line in file is END, skip it
if(!line.equalsIgnoreCase("END")) {
List<Double> list = new ArrayList<Double>();
String[] arr= line.split(" ");
// adding coordinates into the list
list.add(Double.valueOf(arr[1]));
list.add(Double.valueOf(arr[2]));
// adding the list into the map with key as city name
locationsMap.put(arr[0], list);
}
}
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
// Draw cities on map
// Draw graph of all cities
int w=1, h=1;
Set<Entry<String, List<Double>>> locationKeyEntries = locationsMap.entrySet();
for(Entry<String, List<Double>> currEntry: locationKeyEntries) {
String currCity = currEntry.getKey();
List<Double> currLocationList = currEntry.getValue();
int x = currLocationList.get(0).intValue();
int y = currLocationList.get(1).intValue();
stroke(255);
ellipse(x, y, w, h);
if(x>755)
x = x-(8*currCity.length());
if(y>755)
y=y-(8*currCity.length());
text(currCity, x,y);
}
return;
}
I tried to debug it, control is going to ellipse method but nothing is getting drew. Any idea? As far as I understand, I am missing passing reference of PApplet but I don't know how to do it...
Like you've mentioned, you really need to debug your program. Verifying that you're calling the ellipse() function is a great first step, but now you should be asking yourself more questions:
What is the value of x, y, w, and h being passed into the ellipse() function?
What is the value of currEntry in the for loop? What is the value of line when you're reading it in?
What are the fill, stroke, and background colors when you're drawing?
Note that I'm not asking you to tell me the answer to these questions. I'm pointing out these questions because they're what you should be asking yourself when you debug your program.
If you still can't figure it out, I really recommend breaking your problem down into smaller pieces and approaching each of those steps one at a time. For example, can you just show a single circle at a hard-coded point? Then work your way up from there. Can you read a single point in from a file and draw that to the screen? Then read two points. Work your way forward in small incremental steps, and post a MCVE if you get stuck. Good luck.

How to set multiple Renderers on a single XYDataSet in jaspersoft reports customizer for jFreeChart

We have a jfreechart in jaspersoft reports community edition where we want to apply two renderers to the same DataSet. The approach we are currently using is not working as expected.
Our current approach is as follows where we attempt to copy the DataSet from index 0 into index 1 and then set a renderer for each index.
xyplot.setDataset( 1, xyplot.getDataset(0) );
xyplot.setRenderer( 1, XYLineAndShapeRenderer_DashedLines );
xyplot.setRenderer( 0, xYDifferenceRenderer_GrayBand );
We don't get any errors, but the line is not dashed and we do get the gray band but it is not drawn correctly.
However when we comment out one or the other, they work fine on their own.
It kinda feels like the second one overwrites the first one.
Is this the right approach to setting multiple renderers on a single DataSet and if so what are we doing wrong?
Or should a different approach be taken and if so what is it?
For the renderers to work correctly you need 2 different dataset (2:nd needs to be another object in your case a clone not a pointer) and 2 different renderer's (seems that you already have this).
XYDataset xyDataset1 = .... //I'm a dataset
XYDataset xyDataset2 = .... //I'm a another dataset if same values I need to be a clone
//you can't do xyDataset2 = xyDataset1 since like this I become xyDataset1
XYPlot plot = chart.getXYPlot();
plot.setDataset(0, xyDataset1);
plot.setDataset(1, xyDataset2);
XYLineAndShapeRenderer renderer0 = new XYLineAndShapeRenderer();
//do you personalizzation code
XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer();
//do you personalizzation code
plot.setRenderer(0, renderer0);
plot.setRenderer(1, renderer1);
Ok, so here is what finally solved it. I was attempting to use two renderers, one for the grey band and one for the dashed lines, but I only needed to use one.
So the final code ended up being:
package gprCustomizer;
import org.jfree.chart.JFreeChart;
import net.sf.jasperreports.engine.JRChart;
import net.sf.jasperreports.engine.JRChartCustomizer;
import org.jfree.chart.renderer.xy.XYDifferenceRenderer;
import java.awt.BasicStroke;
import org.jfree.chart.plot.XYPlot;
import java.awt.Color;
public class GPRCustomizations implements JRChartCustomizer {
public void customize(JFreeChart chart, JRChart jrChart) {
// Get Plot
XYPlot plot = (XYPlot)chart.getPlot();
// Apply Gray Band Style
XYDifferenceRenderer xYDifRnd_GrayBand = new XYDifferenceRenderer();
xYDifRnd_GrayBand.setNegativePaint(Color.lightGray);
xYDifRnd_GrayBand.setPositivePaint(Color.lightGray);
xYDifRnd_GrayBand.setShapesVisible(false);
xYDifRnd_GrayBand.setRoundXCoordinates(true);
// Apply Dashed Style to Series 0,1
xYDifRnd_GrayBand.setSeriesStroke(0,
new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {6.0f, 6.0f}, 0.0f
)
);
xYDifRnd_GrayBand.setSeriesStroke(1,
new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {6.0f, 6.0f}, 0.0f
)
);
plot.setRenderer(xYDifRnd_GrayBand);
// Remove Borders from Legend
if(chart.getLegend() != null)
{
chart.getLegend().setBorder(0.0, 0.0, 0.0, 0.0);
}
}
}
Which produced the expected outcome of the grey band and the dashed lines either side:

How to create a GEF figure with separate label?

I've been trying to create a Draw2D Figure that consists of two parts - a central resizeable shape, such as a circle or rectangle, and an editable label for the bottom part. An example of this type of figure is the icon/label you see on a computer's Desktop.
The first attempt was to create a parent container figure with two child sub-figures - a shape figure placed centrally and a label placed at the bottom. It also implemented HandleBounds so that selection and resizing occurs only on the upper shape sub-figure. This turned out not to be a working solution because as the label gets wider with more text so does the main parent figure and consequently the central shape figure. In other words the overall parent figure is as wide as the child label figure.
What I'm seeking is a Figure that maintains the size of the shape figure but allows the width of the label figure to grow independently. Exactly the same behaviour as a desktop icon.
Ok I get your question right now. It's impossible to do what you want:
The parent figure can't be smaller than one of its children or this child will not be visible !!!
You have to create a container figure as you mentioned with an XYLayout and "manually" place and "size" the 2 (the shape and the label) children figure inside this layout using the IFigure.add(IFigure child, Object constraint) method with a Constraint of type Rectangle (Draw2d)
Edit with code sample
Here is an example of what your figure class could look like:
package draw2dtest.views;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureListener;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;
public class LabeledFigure extends Figure {
private final Figure shapeFigure;
private final Label labelFigure;
private Rectangle customShapeConstraint;
public LabeledFigure(String label) {
setLayoutManager(new XYLayout());
setBackgroundColor(ColorConstants.lightGray);
setOpaque(true);
shapeFigure = new Ellipse();
this.add(shapeFigure);
shapeFigure.setBackgroundColor(ColorConstants.yellow);
shapeFigure.addMouseListener(new MouseListener.Stub() {
#Override
public void mousePressed(MouseEvent me) {
customShapeConstraint = new Rectangle(
(Rectangle) LabeledFigure.this.getLayoutManager()
.getConstraint(shapeFigure));
customShapeConstraint.width -= 6;
customShapeConstraint.x += 3;
LabeledFigure.this.getLayoutManager().setConstraint(
shapeFigure, customShapeConstraint);
LabeledFigure.this.revalidate();
}
});
labelFigure = new Label(label);
labelFigure.setOpaque(true);
labelFigure.setBackgroundColor(ColorConstants.green);
labelFigure.addMouseListener(new MouseListener.Stub() {
#Override
public void mousePressed(MouseEvent me) {
Rectangle shapeFigureConstraint = new Rectangle(0, 0,
bounds.width, bounds.height - 15);
LabeledFigure.this.getLayoutManager().setConstraint(
shapeFigure, shapeFigureConstraint);
LabeledFigure.this.revalidate();
}
});
this.add(labelFigure);
this.addFigureListener(new FigureListener() {
#Override
public void figureMoved(IFigure source) {
Rectangle bounds = LabeledFigure.this.getBounds();
Rectangle shapeFigureConstraint = new Rectangle(0, 0,
bounds.width, bounds.height - 15);
LabeledFigure.this.getLayoutManager().setConstraint(
shapeFigure, shapeFigureConstraint);
Rectangle labelFigureConstraint = new Rectangle(0,
bounds.height - 15, bounds.width, 15);
if (customShapeConstraint != null) {
labelFigureConstraint = customShapeConstraint;
}
LabeledFigure.this.getLayoutManager().setConstraint(
labelFigure, labelFigureConstraint);
}
});
}
}
This is not a clean class but it should be a good entry to show you how to achieve your goal. This is an example based on pure Draw2d without any Gef code, thus the resizing of the shape is done by clicking in the yellow Ellipse (the size is decreased) and on the green label (the initial size is restored)
To test this class I created a simple Eclipse view as following:
#Override
public void createPartControl(Composite parent) {
FigureCanvas fc = new FigureCanvas(parent, SWT.DOUBLE_BUFFERED);
fc.setBackground(ColorConstants.red);
Panel panel = new Panel();
panel.setLayoutManager(new XYLayout());
LabeledFigure labeledFigure = new LabeledFigure("This is the label");
fc.setContents(panel);
panel.add(labeledFigure, new Rectangle(10,10, 200,100));
}
Hope this can help,
Manu