Change space between lines in tick labels - java

I need to implement a new "graphic generator". To ilustrate this requirement, I'll show an example of output using the old generator and the output of the one I'm trying to develop (or where I got so far).
My main issue right now is the space between the lines on the tick labels. The image must be 600x150, so I need to compress the text part of the graphic as much as I can (It doesn't need to be visually beautiful), but I could not find any suitable code so far.
Actually I need to eliminate as much "white spacing" as I can. If anyone have a hint how to implement the following, I would really appreciate:
Remove white gap between chart tittle and chart itself;
Remove white gap between tick labels and the bottow end of the image;
Remove the light gray line that represent the ticks on the bottom part of the chart. Right now the ticks labels and this line are overlapping.
This is the code I'm using to customize the chart:
CategoryPlot categoryplot = lineChart.getCategoryPlot();
lineChart.setTitle(
new org.jfree.chart.title.TextTitle(lineChart.getTitle().getText(),
new Font("SansSerif", Font.PLAIN, 10)
)
);
lineChart.setBackgroundPaint(Color.white);
categoryplot.setBackgroundPaint(Color.WHITE);
CategoryAxis domainAxis = new CategoryAxis();
domainAxis.setMaximumCategoryLabelLines(4);
categoryplot.setDomainAxis(domainAxis);
Font font = new Font("Courier", Font.PLAIN, 8);
categoryplot.getDomainAxis().setTickLabelFont(font);
categoryplot.getDomainAxis().setCategoryLabelPositionOffset(-6);
categoryplot.getDomainAxis().setTickLabelInsets(
new RectangleInsets(1.0, 1.0, 1.0, 1.0));
NumberAxis yAxis = (NumberAxis) categoryplot.getRangeAxis();
yAxis.setAutoRangeIncludesZero(false);
categoryplot.getRenderer().setSeriesStroke(
1,
new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {6.0f, 6.0f}, 0.0f
)
);
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setRangeGridlinesVisible(true);
categoryplot.setDomainGridlinePaint(Color.GRAY);
categoryplot.setRangeGridlinePaint(Color.GRAY);
categoryplot.getRenderer().setSeriesStroke(
2,
new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {6.0f, 6.0f}, 0.0f
)
);
categoryplot.getRenderer().setSeriesPaint(0, Color.BLUE);
categoryplot.getRenderer().setSeriesPaint(1, Color.RED);
categoryplot.getRenderer().setSeriesPaint(2, Color.RED);
LineAndShapeRenderer renderer = (LineAndShapeRenderer) categoryplot.getRenderer();
renderer.setBaseShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setBaseFillPaint(Color.white);
renderer.setSeriesShape(0, new Ellipse2D.Double(-5, -5, 10, 10));

Related

How do I remove the points in XYLineAndShapeRenderer?

I'm making an application in Java using JFreeChart which shows an XY line chart. The problem is that it shows every point of the dataset on the lines, and I don't want to display these points. Any idea on how to remove the points or make them not visible?
This is a sample screenshot:
Here is the code:
JFrame frame = new JFrame();
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesPaint(0, Color.RED);
renderer.setSeriesStroke(0, new BasicStroke(2.0f,
BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
new float[] { 2.0f, 4.0f }, 0.0f));
XYDataset ds = createDataset(i, p, capacity);
JFreeChart xylineChart = ChartFactory.createXYLineChart("", "",
"", ds, PlotOrientation.VERTICAL, true, true, false);
XYPlot plot = xylineChart.getXYPlot();
plot.setDomainGridlinePaint(Color.BLACK);
plot.setRangeGridlinePaint(Color.BLACK);
plot.setBackgroundPaint(Color.WHITE);
plot.setRenderer(renderer);
ChartPanel cp = new ChartPanel(xylineChart);
frame.getContentPane().add(cp);
By default, the no-argument constructor of XYLineAndShapeRenderer "Creates a new renderer with both lines and shapes visible." To remove the points, you can
Use the alternative constructor to specify the desired combination—lines without shapes, as shown here and in ChartFactory.createXYLineChart:
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false);
Invoke setSeriesShapesVisible(), as #abhinavxeon suggests here and the author suggests here:
renderer.setSeriesShapesVisible(0, false)

Difficulty Changing background colour of JFreeChart

Im trying to change the background color to my bar chart and so far nothing seems to be working
Here is my code below:
JFreeChart expbarchart = ChartFactory.createBarChart("Monthly Expenditures", "Expenditure Type", "Amount (£)", barexp, PlotOrientation.VERTICAL, false, true, false);
ChartPanel expframe = new ChartPanel(expbarchart);
expframe.setLocation(695, 49);
expframe.setSize(641,500);
expframe.setBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(173, 216, 230), null));
graphpanel.add(expframe);
I have tried doing .setbackground() and it does not seem to work
Thanks
BarChartDemo1 shows you how to set the chart background paint:
chart.setBackgroundPaint(new Color(173, 216, 230));
It also shows you how to set a ChartTheme that you can change:
StandardChartTheme theme = new StandardChartTheme("JFree/Shadow", true);
Color color = new Color(173, 216, 230);
theme.setPlotBackgroundPaint(color);
theme.setChartBackgroundPaint(color.brighter());
ChartFactory.setChartTheme(theme);
try this and customize as per your need.
// create the chart...
JFreeChart chart = ChartFactory.createLineChart(
"# of Sales by Month", // chart title
"Month", // domain axis label
"# of Sales", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
if(subTitle != null && !subTitle.isEmpty())
chart.addSubtitle(new TextTitle(subTitle));
chart.setBackgroundPaint(Color.BLUE);
// Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green);
// chart.setBackgroundPaint(p);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setRangePannable(true);
plot.setRangeGridlinesVisible(true);
plot.setBackgroundAlpha(1);
plot.setBackgroundPaint(Color.BLUE);
// Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green);
// plot.setBackgroundPaint(p);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
ChartUtilities.applyCurrentTheme(chart);
You have to use JFreeChart.getPlot().setBackgroundPaint(Color.XXXXXX); like this:
public static void main(String[] args) {
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("LoggedIn" +": "+ 5, 10);
pieDataset.setValue("LoggedOut" +": "+ 8, 17);
JFreeChart jfc = ChartFactory.createPieChart("title", pieDataset, false, false, false );
jfc.getPlot().setBackgroundPaint(Color.BLUE);
ChartPanel chart = new ChartPanel(jfc);
JFrame frame = new JFrame();
frame.add(chart);
frame.pack();
frame.setVisible(true);
}
Of course, you can catch the desired color by several different methods regarding your needs, for example:
Color color1 = "anyComponent".getBackgroundColor();
and then apply
jfc.getPlot().setBackgroundPaint(color1);
I hope it helps!

JFreechart fill color outside polygon

I create a polygon annotation on my chart and would like to know how to fill the chart with color only outside of the drawn polygon. I'm using Jfreechart 1.0.17.
I do it this way at the moment:
Color plotBackground = (Color) plot.getBackgroundPaint();
plot.setBackgroundPaint(new Color(0xff0000));
XYLineAndShapeRenderer renderer
= (XYLineAndShapeRenderer) plot.getRenderer();
XYPolygonAnnotation a = new XYPolygonAnnotation(new double[] {2.0,
5.0, 2.5, 8.0, 3.0, 5.0, 2.5, 2.0}, null, null,
new Color(plotBackground.getRed(), plotBackground.getGreen(),
plotBackground.getBlue(), 255));
But it is not really what I want, we can't see grid lines this way.
Here is possible solution :
Rectangle2D r2d = new Rectangle2D.Double(plot.getQuadrantOrigin().getX(),
plot.getQuadrantOrigin().getY(),
3.2, 9);
Area a1 = new Area(r2d);
Path2D.Float p = new Path2D.Float();
p.moveTo(2.0, 5.0);
p.lineTo(2.5, 8.0);
p.lineTo(3.0, 5.0);
p.lineTo(2.5, 2.0);
p.closePath();
Area a2 = new Area(p);
a1.subtract(a2);
XYShapeAnnotation a = new XYShapeAnnotation(a1, new BasicStroke(),
new Color(0xff0000),
new Color(0xff0000));
renderer.addAnnotation(a, Layer.BACKGROUND);
Looking into jfreecharts source code the annotations are always drawn after gridlines are painted. So there seems to be no possiblity to draw them before the gridlines.
I would give the XOR - mode drawing a try.
XYPolygonAnnotation a = new XYPolygonAnnotation(new double[]{2.0,
5.0, 2.5, 8.0, 3.0, 5.0, 2.5, 2.0}, null, null,
new Color(plotBackground.getRed(), plotBackground.getGreen(),
plotBackground.getBlue(), 255)) {
#Override
public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis, int rendererIndex, PlotRenderingInfo info) {
Graphics2D g22 = (Graphics2D) g2.create();
g22.setXORMode(new Color(0xff0000));
super.draw(g22, plot, dataArea, domainAxis, rangeAxis, rendererIndex, info);
}
};
renderer.addAnnotation(a, Layer.BACKGROUND);
With xor combined drawing the annotations are somekind merged with the background and the gridlines. So the following result appears:

JFreeChart Pie graph labels squishing

As you can see from the picture, the labels for the arrows pointing to the sections of the pie graph are overlapping on the left side. Is there a way to shrink the font of the labels so more can fit? Or make them go out so they dont overlap?strong text
you can rotate chart by some angle by using following code
JFreeChart chart = ChartFactory.createPieChart3D("World Polulation by Countries ", dataset, true, true, true);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setPieIndex(0);
// plot.setLabelFont(new Font("SansSerif", Font.NORMAL, 12));
plot.setNoDataMessage("No data available");
plot.setCircular(false);
plot.setLabelLinksVisible(true);
// plot.zoom(200);
// plot.setStartAngle(180);
// plot.setLabelBackgroundPaint(paint);
plot.setLabelLinkMargin(0.01);
return chart;

Increase the width of the bars while using jfreechart

I am using StackedBarChart in the jfreechart API to display no.records in a table which comes from the database. There are 82 tables which im putting on the domain axis and the no. of records in the Range axis. But the problem that I am facing is that because of so many tables in the domain axis, the width of the bar reduces.
Following is my code :
private JFreeChart createChart(CategoryDataset dataset)
{
JFreeChart chart = ChartFactory.createStackedBarChart(
"Stacked Bar Chart", "Task Description", "No_Of_Records", dataset,
PlotOrientation.VERTICAL, true, true, false);
CategoryPlot categoryplot = (CategoryPlot)chart.getPlot();
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
categoryaxis.setMaximumCategoryLabelWidthRatio(100);
categoryaxis.setLowerMargin(0.02D);
categoryaxis.setUpperMargin(0.02D);
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setRange(0.0D, 1000D);
BarRenderer barrenderer = (BarRenderer)categoryplot.getRenderer();
barrenderer.setDrawBarOutline(false);
GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, new Color(0, 0, 64));
barrenderer.setSeriesPaint(0, gradientpaint);
return chart;
}
I have tried using a
setItemMargin();
setMaxWidth();
setMaximumBarWidth();
on the BarRenderer, but all in vain..
Any help is appreciated
Regards,
You can experiment with setCategoryMargin(). Also, see how it looks when you right-click and zoom in.

Categories