Increase the width of the bars while using jfreechart - java

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.

Related

How to set uneven (space) tick Units in JFreeChart XYLine

I've trying to find a solution to this problem.
I have an XYLineChart and I would like to have uneven spaces between tick units.
This is my code:
//Method (receive the dataset and return the chart)
private JFreeChart createChart(XYDataset dataset){
//--------create XYlineChart
JFreeChart oChart = ChartFactory.createXYLineChart("XYLineChart", "X", "Y", dataset);
XYPlot plot = (XYPlot) oChart.getPlot();
plot.setOrientation(PlotOrientation.VERTICAL);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
plot.setBackgroundPaint(Color.WHITE);
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.black);
plot.setRangeGridlinePaint(Color.black);
// render shapes and lines
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, true);
plot.setRenderer(renderer);
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
xAxis.setNumberFormatOverride(THOUSANDS); //Override and Use a my NumberFormat
xAxis.setRange(0, 8000);
//I want to use this with my own set of tickUnits, the values(x) that are on the image I'm trying to achieve
//xAxis.setStandardTickUnits();
xAxis.setTickUnit(new NumberTickUnit(500));
yAxis.setRange(0.0, 130.0);
yAxis.setTickUnit(new NumberTickUnit(10));
yAxis.setInverted(true);
return oChart;
}
I dont know how to make my own set of TickUnits or how to space the domainAxis (xAxis) unevenly.
I'll be glad to read any examples of something like that, so i can try to use it or apply it.
I'll really appreciate your help, is the only thing missing in my project.

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)

How to show shapes at plot points with JFreeChart TimeSeries chart?

I'm using JFreeChart v1.0.19 to create the following time series chart:
...using the following code:
final String chartTitle = "";
final XYDataset dataset = createDataset1();
final JFreeChart chart = ChartFactory.createTimeSeriesChart(
chartTitle, "Year End", "Turnover", dataset, true, false, false);
final XYPlot plot = chart.getXYPlot();
final NumberAxis axis2 = new NumberAxis("Cash");
axis2.setAutoRangeIncludesZero(false);
plot.setRangeAxis(1, axis2);
plot.setDataset(1, createDataset2());
plot.mapDatasetToRangeAxis(1, 1);
final StandardXYItemRenderer renderer1 = new StandardXYItemRenderer();
renderer1.setSeriesPaint(0, Color.blue);
renderer1.setSeriesShape(0, ShapeUtilities.createDiamond(5));
renderer1.setSeriesShapesFilled(0, true);
plot.setRenderer(0, renderer1);
final StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
renderer2.setSeriesPaint(0, Color.black);
renderer2.setSeriesShape(0, ShapeUtilities.createDiamond(5));
renderer2.setSeriesShapesFilled(0, true);
plot.setRenderer(1, renderer2);
chart.getPlot().setBackgroundPaint(Color.WHITE);
chart.getXYPlot().setDomainGridlinePaint(Color.LIGHT_GRAY);
chart.getXYPlot().setRangeGridlinePaint(Color.LIGHT_GRAY);
...but I can't get the data points to show shapes. Is this possible?
It was my renderer that was the problem. Changing from StandardXYItemRenderer to XYLineAndShapeRenderer e.g.
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true,true);
...did the trick. The two boolean args in the constructor are for switching on/off lines and shapes, respectively.

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;

JFreeChart PolarPlot remove radius labels?

Any ideas? There is no setRadiusLabelsVisible(...) or setLabelGenerator(null) method which exists for at least some of the other charts. :)
It looks like a PolarPlot has a radial Axis, so
PolarPlot plot = (PolarPlot) chart.getPlot();
ValueAxis axis = plot.getAxis();
axis.setTickLabelsVisible(false);

Categories