JFreeChart XYbar graph without margin between bars [duplicate] - java

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.

Related

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.

JfreeChart background image not showing up

I'm creating a chart in a servlet, and it works great.
chart = ChartFactory.createPieChart("Smart Chart", ds, true, true,
true);
PiePlot plot = (PiePlot) chart.getPlot();
ImageIcon icon = new ImageIcon(bgImageStr);
plot.setBackgroundPaint(Color.CYAN);
plot.setBackgroundAlpha(0.15f);
chart.setBackgroundPaint(Color.WHITE);
chart.getTitle().setBackgroundPaint(Color.PINK);
chart.setBackgroundImage(icon.getImage());
Problem is, the background image is not showing up. I've tried the plot and chart bg, and all kinds of other stuff. It must be simple, anyone see what is wrong? I'm just using the write to PNG to dump it to the browser. It shows up fine, with all the color changes, just no image.
OK, OP here, I fixed it.
I was calling the servlet from itself, and it did not like that. I used a resource (as suggested) and it worked fine without the infinite recursion (imagine that).
apply
try{
Thread.sleep(100);
}
catch(InterruptedException IntExp)
{
//your implementation
}
before chart.setBackgroundImage(icon.getImage());

JFreeChart - Shared Crosshair between Subplots

I would like to know how to have a shared Crosshair. I have a CombinedXYPlot with 5 XYPlots and when I click on the graph, I would like to have one single Crosshair that will appear on each SubPlot. For the moment, when I click on a SubPlot, the Crosshair appears only on this SubPlot :
List<XYPlot> lxyp = t.getSubplots();
for (XYPlot xyp : lxyp) {
xyp.setDomainCrosshairVisible(true);
xyp.setDomainCrosshairLockedOnData(false);
xyp.setRangeCrosshairVisible(false);
}
Then, how to change the color and the thickness of this Crosshair ?
Thanks for your response !
I am unfamiliar with CombinedXYPlot, but the method setDomainCrosshairStroke() may available. This affords all the features of BasicStroke.
subplot1.setDomainCrosshairVisible(true);
subplot1.setDomainCrosshairPaint(Color.red);
subplot1.setDomainCrosshairStroke(new BasicStroke(1f));
...
subplot2.setDomainCrosshairVisible(true);
subplot2.setDomainCrosshairPaint(Color.blue);
subplot2.setDomainCrosshairStroke(new BasicStroke(1f));

How to disable scaling in JFreeChart?

We're using JFreeChart to build an engine to display graphs. This is a web service that runs on Tomcat + Java 1.5.0, and renders charts to PNGs and JPEGs (using ChartUtilities.writeChartAs{PNG,JPEG}() ).
We've run into a problem where JFreeChart seems to scale everything inside the Plot area, but only by a few pixels. The result is that the graph looks inconsistent, e.g.:
Minor ticks are sometimes stretched horizontally, so that they seem to be two pixels wide instead of one.
We use a small image in the top-right of the plot area as a watermark. This is stretched by one pixel horizontally and vertically somewhere near (but not exactly) its middle.
Background grid lines seem to appear on sub-pixel boundaries. I have not found a way to create an accurately dotted grid line.
We have tried both 1.0.9 and 1.0.13, with exactly the same results (except for the minor ticks, which were not available in the older version). Also, rendering the image to a Frame instead of JPEG/PNG produced an identical result.
Help is greatly appreciated, in advance :)
EDIT: An SSCCE:
#Test
public void testScaling1() throws InterruptedException {
// Load Image:
Component dummy = new Component() {};
MediaTracker tracker = new MediaTracker(dummy);
Image img = Toolkit.getDefaultToolkit().getImage("C:\\My\Image.gif");
tracker.addImage(img, 0);
tracker.waitForAll();
// Build Data set and base chart.
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries ts = new TimeSeries("Sample");
ts.add(new Second(0, 0, 0, 1, 1, 1900), 1.0);
ts.add(new Second(1, 0, 0, 1, 1, 1900), 3.0);
ts.add(new Second(2, 0, 0, 1, 1, 1900), 4.0);
ts.add(new Second(3, 0, 0, 1, 1, 1900), 2.0);
dataset.addSeries(ts);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"blabla",
null,
null,
dataset,
true,
true,
false
);
// Add BG image in top-right corner.
XYPlot xy = chart.getXYPlot();
xy.setBackgroundAlpha(0.0F);
xy.setBackgroundImage(img);
xy.setBackgroundImageAlignment(Align.NORTH_WEST);
xy.setBackgroundImageAlpha(1.0F);
paintChart(chart);
}
Use an image with small-font text, or a grid. This will show the scaling effect on the background image.
Edit 2:
We've resorted to subclassing or proxying Renderers and drawing the label in text in their drawItem() (or similar) methods. This works well.
However, minor ticks are now a problem - they also seem to be getting scaled. E.g.: See the 9th and 15th ticks.
look at the bottom http://img14.imageshack.us/img14/3625/76676732.jpg
I am unable to reproduce the effect you describe using either saveChartAsJPEG() or writeChartAsPNG() with version 1.0.13, Java 1.5, Mac OS X, in code like this:
try {
ChartUtilities.writeChartAsPNG(new FileOutputStream(
new File("test.png")), chart, 600, 400);
} catch (IOException ex) {
ex.printStackTrace();
}
Does the screen exhibit the same artifacts? What happens when you change the WIDTH and HEIGHT parameters or omit the watermark? Are you using special fonts with unusual metrics? Have you tried a different platform?
You can run TimeSeriesChartDemo1 as follows:
java -cp jfreechart-1.0.13.jar:jcommon-1.0.16.jar org.jfree.chart.demo.TimeSeriesChartDemo1
Mac OS 10.5.8, Java 1.5.0_24, JFreeChart 1.0.13, TimeSeriesDemo1, using saveChartAsPNG(), ImageIO.read() and setBackgroundImage(). setBackgroundImageAlignment(Align.NORTH_WEST) is a little funky, though.

Categories

Resources