Is it possible to draw a 3D chart using JfreeChart like in the following link.If possible can anyone give some hints and some snippets of code on what parameters of Plot can be used to do this.
link text
It's possible though it won't look exactly the same. The easiest way is to create a dataset (descendant of org.jfree.data.general.PieDataset) and use one of org.jfree.chart.ChartFactory methods:
PieDataset data = new DefaultPieDataset();
data.setValue("Section1", 30);
data.setValue("Section2", 60);
data.setValue("Section3", 120);
JFreeChart pieChart = ChartFactory.createPieChart3D(
"My Pie Chart", // title
data, // data set
true, // draw a legend
true, // show tooltips over sections
false); // do not generate image map with URLs
You can then further customize your chart through pieChart methods. For example, here's how to explode one pie section:
PiePlot plot = (PiePlot) pieChart.getPlot();
plot.setExplodePercent("Section2", 0.25);
Related
I am creating JFreeChart as follows (see below). On the x axis I need to show around 1000 ticks. So, that's why I am putting JFreeChart into the JScrollPane. When I try to set the WIDTH of a chart panel to 2000, the plot is not sized proportionally. Is there any way to define the WIDTH of a plot (XYPlot).
XYDataset dataset = createData(data);
JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(2000, this.height));
JScrollPane scrl = new JScrollPane(chartPanel);
scrl.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrl.setViewportView(chartPanel);
scrl.setPreferredSize(new Dimension(this.width, this.height));
this.add(scrl);
So far I found an answer that helped solving the problem. However, maybe there is a better solution:
// The first step is to avoid scaling up a small chart.
// The value of "MaximumDrawWidth" should be large, for instance 4000
chartPanel.setMaximumDrawWidth(4000);
// The second step is to set the width of a chart panel.
chartPanel.setPreferredSize(new Dimension(2000,this.height));
I am using JFreeChart library to create Chart on website (library integrated with my application according to this tutorial). Everything looks great except one thing: for some reason, for some data line chart is not completly visible (please see screen).
I don't know why is it happening. I'm posting code responsible for configuration:
public JFreeChart createChart()
{
NumberAxis numberaxis = new NumberAxis("X");
numberaxis.setAutoRangeIncludesZero(false);
NumberAxis numberaxis1 = new NumberAxis("Y");
numberaxis1.setAutoRangeIncludesZero(false);
XYSplineRenderer xysplinerenderer = new XYSplineRenderer();
XYPlot xyplot = new XYPlot(createSampleData(), numberaxis, numberaxis1, xysplinerenderer);
xyplot.setBackgroundPaint(new Color(238, 242, 250));//
xyplot.setDomainGridlinePaint(new Color(238, 242, 250));
xyplot.setRangeGridlinePaint(new Color(238, 242, 250));
xyplot.getRenderer().setSeriesPaint(0, Color.BLUE);
xyplot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyplot.getRenderer();
renderer.setSeriesShapesVisible(0, true);//FIXME Dots
xyplot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
JFreeChart jfreechart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
jfreechart.setBackgroundPaint(Color.white);
return jfreechart;
}
The link provided by Richard describes the problem well; but, ultimately, it looks like you need to manually set the upper bound of your range axis to account for the upper curve of the spline produced by XYSplineRenderer. For your example above, this might be:
xyplot.getRangeAxis().setUpperBound(22.5);
For practical purposes, you would probably want to calculate the maximum Y value, and either add a percentage to it, or more complicated, calculate a ceiling based on its surrounding points. I would start by adding 10% and see how that goes:
// Iterate data values; use Math.max() to determine maxYValue; then:
xyplot.getRangeAxis().setUpperBound( maxYValue + maxYValue * 0.1);
It's at patch, but it should provide the result you want, depending on the nature of your data and the curves produced connecting data points.
I have created XY line chart using JFreeChart, having two datasets, I want both the lines to be in different colors. I tried using following code-
XYPlot plot = chart.getXYPlot();
XYItemRenderer xyir = plot.getRenderer();
xyir.setSeriesPaint(0, Color.GREEN);
plot.setDataset(0, xyDataset1);
xyir.setSeriesPaint(1, Color.blue);
plot.setDataset(1, xyDataset2);
Also I have tried using following code, where I am using different renderer (don't know whether this is correct way to do it)-
XYPlot plot1 = chart.getXYPlot();
XYPlot plot2 = chart.getXYPlot();
XYItemRenderer xyir1 = plot1.getRenderer();
xyir1.setSeriesPaint(0, Color.GREEN);
plot1.setDataset(0, xyDataset1);
XYItemRenderer xyir2 = plot2.getRenderer();
xyir2.setSeriesPaint(1, Color.blue);
plot2.setDataset(1, xyDataset2);
In both the cases its printing both the lines in blue color.
What's wrong?? Any suggestions??
Found the solution, it works for me, using two different Renderer, earlier i was not doing it properly--
XYPlot plot = chart.getXYPlot();
plot.setDataset(0, xyDataset1);
plot.setDataset(1, xyDataset2);
XYLineAndShapeRenderer renderer0 = new XYLineAndShapeRenderer();
XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer();
plot.setRenderer(0, renderer0);
plot.setRenderer(1, renderer1);
plot.getRendererForDataset(plot.getDataset(0)).setSeriesPaint(0, Color.red);
plot.getRendererForDataset(plot.getDataset(1)).setSeriesPaint(0, Color.blue);
The approach shown works in this example, and a single renderer should be sufficient. An sscce may help isolate the problem.
To control individual items, you can override getItemPaint(), shown here.
Try to set the Series paint to null in the renderer setSeriesPaint(null);
If you take a look at the source it first checks to see if the paint is !null, then uses the base color.
If null it uses the colors associated with the time serie from a lookup table.
Can someone tell me how to change samples of series color in legend in jfreechart. What I have now is small line of series color eg: I would like to have square sample of those colors. Here is an example
Can someone help me?
Ok I found the solution. At least I think. Of course there is no simple way to do this. There is now, you know, setShape(square) method, that will do the trick, at least i haven't found one.
Basicly XY chart and time chart have "line style" legend by default in contrary to bar chart for example (if has square legend by default). So I had to remove current legend and create new one with square samples of color and this new legend add to my time chart.
LegendItemCollection legend = new LegendItemCollection();
for (int i = 0; i < seriecCount; ++i) {
chart.getXYPlot().getRenderer().setSeriesPaint(i, colorPalette.get(i));
LegendItem li = new LegendItem(data.getSeriesName(i), "-", null, null, Plot.DEFAULT_LEGEND_ITEM_BOX, colorPalette.get(i));
legend.add(li);
}
chart.getXYPlot().setFixedLegendItems(legend);
Thanks for attention. I hope it will help someone.
Generating your own legend, as you do above, is a perfectly acceptable way of doing things in JFreeChart. If you didn't want to do it, you can also define your own renderer with the lookupLegendShape() method overridden.
thePlot.setRenderer(new XYLineAndShapeRenderer()
{
public Shape lookupLegendShape(int series)
{
return new Rectangle(15, 15);
}
});
If you use a XYBarRenderer Class XYBarRenderer
(Subclasses: ClusteredXYBarRenderer, StackedXYBarRenderer)
You can use XYBarRenderer.setLegendBar(java.awt.Shape bar);
See: Javadoc
to get nice squares.
Example:
JFreeChart chart = ChartFactory.createXYBarChart(/*...*/);
XYPlot plot = (XYPlot) chart.getPlot();
ClusteredXYBarRenderer renderer = new ClusteredXYBarRenderer();
renderer.setLegendBar(new Rectangle(17, 17));
plot.setRenderer(renderer);
How could I add to a plot an OHLCSeriesCollection and a TimeSeriesCollection , in order to represent their values in the same chart ?
Both OHLCSeriesCollection and TimeSeriesCollection are based on XYDataset so you should be able to add them both to an XYPlot with something like the following:
JFreeChart chart = // create your XY chart here.
XYPlot plot = chart.getXYPlot();
OHLCSeriesCollection ohlsSeriesDataset = // create you ohlc dataset here.
TimeSeriesCollection timeSeriesDataset = // create you time dataset here.
AbstractXYItemRenderer olhsSeriesRenderer = // create your ohlc renderer here.
AbstractXYItemRenderer timeSeriesRenderer = // create your time renderer here.
plot.setDataset(0, ohlsSeriesDataset);
plot.setDataset(1, timeSeriesDataset);
plot.setRenderer(0, olhsSeriesRenderer);
plot.setRenderer(1, timeSeriesRenderer);
The type of renderer to use for olhsSeriesRenderer and timeSeriesRenderer really depends on the type of chart you want to generate so I cannot give you specifics here.
I have not tried this myself with XY datasets, but I have been able to do combine CategoryDatasets using this.