doing a line graph with jfreechart in excel file - java

I am doing a lineal graph in java with JFreeChart, i was checking in internet and this is that i have at the moment:
DefaultKeyedValues data = new DefaultKeyedValues();
data.addValue("01/04/2012",7);
data.addValue("01/05/2012",5);
data.addValue("01/06/2012",6);
data.addValue("01/07/2012",2);
CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Population", data);
JFreeChart chart = ChartFactory.createBarChart("Population","Date","Population",dataset,PlotOrientation.VERTICAL,true,true,false);
LineAndShapeRenderer renderer = new LineAndShapeRenderer();
renderer.setBaseLinesVisible(true);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setRenderer(0, renderer);
NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
numberAxis.setRange(new Range(0,10));
CategoryAxis axis = plot.getDomainAxis();
axis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
The problem comes when I try to insert more categories in the linear graph, I don't know how to do it. I need to do 3 categories more.
actual graph
I need use DefaultKeyedValues or some series collection that let me use String param with double/int param. I want to make a generalized method that passes different formats of dates, and the method always draw a graph. In the photo of the example I am passing a date range 'dd-MM-yyyy' but later I will pass a format 'MM-yyyy'.
Edit: Okey i find this example in internet that solves my problem.
enter link description here

Related

Change Plot label position in a Combined Plot JFreeChart

Is there a way to change the position of the Category Plot Labels (title) (censored with black bars in the image) to be on top of the chart and / or aligned vertically to each other?
Here's the classes I use to construct it.
var renderer = new LineAndShapeRenderer();
...
var yAxis = new NumberAxis();
...
var xAxis = new CategoryAxis(title);
...
CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
...
var combinedPlot = new CombinedRangeCategoryPlot();
combinedPlot.add(plot, weight);
...
I'm using version 1.0.19 of JFreeChart since newer versions produce some visual artifacts.
There has been a misunderstanding: I meant the labels, "Plot 0" and "Plot1", in your example; and I'm asking for the ability to move them on top of the chart…
Invoke setDomainAxisLocation() on each subplot domain axis to move the axis and its label. Alternatively, obtain the required axis references from the List<CategoryPlot> shown below.
plot0.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
plot1.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
I only need those "Plot 0" and "Plot1" labels on top, not the tick labels…
You can hide the tick labels as desired using setTickLabelsVisible(false), and you can have multiple axes, as shown here.
To adjust the tick label orientation, invoke setCategoryLabelPositions() on each sublot comprising the combined plot. Do this either
When the plot is created.
CategoryAxis axis0 = new CategoryAxis("Plot 0");
axis0.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
CategoryPlot plot0 = new CategoryPlot(…, axis0, …);
…
CategoryAxis axis1 = new CategoryAxis("Plot 1");
axis1.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
CategoryPlot plot1 = new CategoryPlot(…, axis1, …);
After creating the combined plot.
CombinedRangeCategoryPlot combinedplot = new CombinedRangeCategoryPlot(…);
…
List<CategoryPlot> list = combinedplot.getSubplots();
list.get(0).getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
list.get(1).getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_90);
Vertical labels are illustrated below.

Getting Assigned Paint for TimeSeries

I am obviously not understanding the documentation for the getSeriesPaint method. I have the TimeSeries object and I want to get the color being used to render it. However, it seems like I am in a catch-22. I need to know the series index (getIndex), but to find that I need to know the series time period. However, to find the series time period, I need to know the index. I'm looking to do something like this:
Color color=(Color) r1.getSeriesPaint(arg0);
where r1 is the XYLineAndShapeRenderer. What do I use for arg0 given the TimeSeries object?
Because XYLineAndShapeRenderer is an XYItemRenderer, it invokes the AbstractRenderer method getItemPaint(), which "Returns the paint used to color data items as they are drawn." Note that "The default implementation passes control to the lookupSeriesPaint() method." Starting from this example, the following fragment obtains the dataset and renderer from the chart. It then enumerates the series paints—shades of red and blue seen in the image:
JFreeChart chart = chartPanel.getChart();
XYPlot plot = (XYPlot) chart.getPlot();
TimeSeriesCollection tsc = (TimeSeriesCollection) plot.getDataset();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
for (int i = 0; i < tsc.getSeriesCount(); i++) {
System.out.println(renderer.lookupSeriesPaint(i));
}
Console:
java.awt.Color[r=255,g=85,b=85]
java.awt.Color[r=85,g=85,b=255]
Alternatively, consider a custom DrawingSupplier, mentioned here.

Ranges in bar graph JFreeChart

I have created a graph with JFreeChart and I've been trying to change the ranges on the Y-axis.
I need it to go from 1 to 10, but with intervals of 1 unit. (1,2,3,...,9,10)
I haven't been able to find a way for that, can anyone help me?
Try this it will work
CategoryPlot p = chart.getCategoryPlot();
final NumberAxis rangeAxis = (NumberAxis) p.getRangeAxis();
rangeAxis.setTickUnit(new NumberTickUnit(10));
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

JFreeChart Margin

I am using JasperReports to create a line chart for my webapps.
I have successfully passed the dataset to the compiled report (created in iReport) and can see the data correctly.
However, I want to do some customization on the margin.
The value shown on the line chart is trimming for the highest value as there is no margin.
The X-Axis label is coming after few empty space from Y-Axis 0 value. I want to remove that margin and start the X-Axis from very close to the meeting point of X & Y.
Please see the picture:
I am using customized class which is defined in my webspps. I am able to change the font size and rotation of the label but don't know how to adjust margin.
public class LineChartCustomizer implements JRChartCustomizer {
#Override
public void customize(JFreeChart jFreeChart, JRChart jrChart) {
CategoryPlot plot = jFreeChart.getCategoryPlot();
DecimalFormat dfKey = new DecimalFormat("###,###");
StandardCategoryItemLabelGenerator labelGenerator = new StandardCategoryItemLabelGenerator("{2}", dfKey);
LineAndShapeRenderer renderer = new LineAndShapeRenderer();
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseItemLabelGenerator(labelGenerator);
renderer.setBaseItemLabelFont(new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 4));
renderer.setSeriesShape(0, ShapeUtilities.createDiamond(1F));
plot.setRenderer(renderer);
}
}
I think* you're looking for ValueAxis#setUpperMargin(double) and CategoryAxis#setLowerMargin(double). You can get the CategoryAxis and ValueAxis from plot.getDomainAxis() and plot.getRangeAxis(). Note that the margins are a percentage of the axis length and not pixel values.
* I'm not familiar with JasperReports, but it seems a little strange that you have a CategoryPlot in hand as opposed to an XYPlot. I would have expected the chart in your picture to have used an xy time series. I have only ever tested this with an XYPlot, so I'm not entirely sure how it will behave with a CategoryPlot.

How to construct and use TimeSeriesCollections

I want to display some dates in the X axis of a chart, and here it is said that i have to use a TimeSeriesCollections object
It seems that i have to add a TimeSeries to the TimeSeriesCollections, and that the TimeSeries has to be constructed using a RegularTimePeriod...
I am a bit confused...
Can you please explain me what i have to do?
If possible can you provide some example code?
Thanks
TimeSeriesCollections are made up of TimeSeries objects
Use this method to add series to the dataset: addSeries(TimeSeries series)
When creating TimeSeries objects. Fill them with the time and values. Here is a rough example:
TimeSeries ts= new TimeSeries("Name of Series");
ts.addOrUpdate(new Year(2008), 42);
ts.addOrUpdate(new Year(2009), 51);
ts.addOrUpdate(new Year(2010), 97);
ts.addOrUpdate(new Year(2011), 45);
For getting the Axis to display the dates nicely, you will have to do something like this:
XYPlot plot = chart.getXYPlot();
DateAxis axis = new DateAxis();
plot.setDomainAxis(axis);
axis.setDateFormatOverride(new SimpleDateFormat("yyyy"));

Categories

Resources