JFreeChart using Large Numbers as Values - jfreechart

I am currently working on a project using JFreeChart. I got everything working except when I start using large numbers, it goes all funny.
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1000, "Cars", "2015");
dataset.addValue(5000, "Bikes", "2015");
return dataset;
Output from above works perfect, but when I do this:
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(999999999, "Cars", "2015");
dataset.addValue(999999999, "Bikes", "2015");
return dataset;
The 999999999 is converted to something like 1E7, 2E7, etc, etc.
How do I format it to show correctly?
This is my chart code.
JFreeChart chart = ChartFactory.createBarChart3D("My Title", "", "", catdata, PlotOrientation.VERTICAL, true, true, false);
BufferedImage bufferedImage = chart.createBufferedImage(780, 170, BufferedImage.TYPE_INT_RGB, null);
ImageIO.write(bufferedImage, "jpeg", out);
Any help will be much appreciated.
Thanks

You can use setNumberFormatOverride() and pass in a suitable NumberFormat. Hover over any bar to see the exact value in a tool tip.
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
NumberFormat formatter = NumberFormat.getIntegerInstance();
rangeAxis.setNumberFormatOverride(formatter);

Related

Jfree chart version of jide soft line chart with gradient fill

I need to get the code that would help me create a chart similar to the one shown in the image http://www.jidesoft.com/images/line-chart-gradient-fill.png also it should be a time series chart
First of all I want to recommend to buy the JFreeChart developers guide. It contains a large set of good examples and it supports the maintainer of this project.
Regarding your question: I don't know what your main target is.
A good starting point is the class TimeSeriesChartDemo1 provided with the JFreeChart sources.
Modify the createChart method of this demo by setting a different renderer:
private static JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Legal & General Unit Trust Prices", // title
"Date", // x-axis label
"Price Per Unit", // y-axis label
dataset, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
chart.setBackgroundPaint(Color.white);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYItemRenderer r = new XYAreaRenderer2();
r.setSeriesPaint(0, new Color(255, 0, 0, 50));
r.setSeriesPaint(0, new Color(0, 255, 0, 50));
plot.setRenderer(r);
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
return chart;
}

JFreeChart: move column labels upper

As I showed in the picture, I want to move the column labels (values 434, 2562, ....) a little upper. Is there any way to configure that?
MY initial problem was that due to the differences between columns, the labels on the last 2 columns are not visible anymore (they are actually 15 and 24 or stg like that). I dont know how to change that.
Thank you
You can use BarRenderer3D.setSeriesPositiveItemLabelPosition.
For example :
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(51.0, "Series 1", "key1");
dataset.addValue(44.3, "Series 1", "key2");
// create the chart...
JFreeChart chart = ChartFactory.createBarChart3D("Demo","Category","Value",dataset,PlotOrientation.VERTICAL,false,true,false);
CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setVisible(false);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setUpperMargin(0.15);
BarRenderer3D renderer = (BarRenderer3D) plot.getRenderer();
CategoryItemLabelGenerator generator = new StandardCategoryItemLabelGenerator();
renderer.setSeriesItemLabelGenerator(0, generator);
renderer.setSeriesItemLabelsVisible(0, true);
renderer.setSeriesPositiveItemLabelPosition(0, new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_CENTER));
renderer.setItemLabelAnchorOffset(10);
The result is :
CategoryPlot plot = (CategoryPlot) chart.getPlot();
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.TOP_CENTER,TextAnchor.HALF_ASCENT_CENTER,0D));

JFreeChart : How to make a series invisible?

I am trying to make a chart of ohlc bars invisible so that I can leave the window with only the moving average.
Here is the code with the two series (ohlc bars and moving average) :
private static JFreeChart createChart(OHLCDataset dataset)
{
JFreeChart chart = ChartFactory.createHighLowChart(
"HighLowChartDemo2",
"Time",
"Value",
dataset,
true);
XYPlot plot = (XYPlot)chart.getPlot();
DateAxis axis = (DateAxis)plot.getDomainAxis();
axis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
NumberAxis yAxis = (NumberAxis)plot.getRangeAxis();
yAxis.setNumberFormatOverride(new DecimalFormat("$0.00"));
//overlay the moving average dataset...
XYDataset dataset2 = MovingAverage.createMovingAverage(dataset, "-MAVG", 3 * 24 * 60 * 60 * 1000L, 0L);
plot.setDataset(1, dataset2);
plot.setRenderer(1, new StandardXYItemRenderer());
XYItemRenderer theRenderer = plot.getRenderer(0);
theRenderer.setSeriesVisible(0, false);
return chart;
}
For some reason setSeriesVisible function is not working.
Any ideas?
Thank you.
HighLowRenderer ignores getSeriesVisible() and getBaseSeriesVisible(), although it does check getDrawOpenTicks() and getDrawCloseTicks(). You can replace the OHLCDataset:
plot.setDataset(0, null);
Alternatively, don't add the OHLCDataset in the first place; just use it to create the MovingAverage.

Jfree Using custom names to categories

I'm very new to Jfreechart, Please help me to get the solution for this, I'm using jfree to draw a graph for a categories which has specific code names like 563,258.855,etc... So i want to assign these values as labels to the domain axis, but by default it is assigning value1,value2,value3,etc... to categories.
But I wan to make it something like below
and here is my code,
final CategoryDataset dataset1 = DatasetUtilities.createCategoryDataset("Month to Date Occurences","value", data);
JFreeChart dualchart = ChartFactory.createBarChart(
"Top Ten Diagnostic Occurences", // chart title
"Category", // domain axis label
"Score", // range axis label
dataset1, // data
PlotOrientation.HORIZONTAL, // orientation
true, // include legend
true,
false
);
CategoryPlot plot = dualchart.getCategoryPlot();
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
plot.setBackgroundPaint(Color.WHITE);
plot.setRangeGridlinePaint(Color.black);
BarRenderer renderer= (BarRenderer) plot.getRenderer();
renderer.setItemMargin(0.0);
CategoryAxis domainAxis = plot.getDomainAxis ();
domainAxis.setCategoryMargin(0.30); //distance between series
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setRange(0.0, 100.0);
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
return dualchart;
Your suggestions are most welcome,
Thanks in advance.
You haven't included the code for dataset1 so it's hard to tell what the problem is but if you use something like this:
private static CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(15, "1", "451");
dataset.addValue(12, "1", "851");
dataset.addValue(10, "2", "362");
dataset.addValue(5, "2", "142");
return dataset;
}
You will get a chart like this:

JFreeChart How create an areachart with an TimeLine axis

I want to make the example of jfreechart: XYAreaChartDemo2.java, an xyareachart but the X axis must be of timeSeries. i have tried this:
TimeSeriesCollection dataset1 = new TimeSeriesCollection(timeSeries);//my timeseries
XYSeriesCollection dataset2 = new XYSeriesCollection();
JFreeChart chart = ChartFactory.createXYAreaChart("titulo","Eje x","eje Y", dataset2, PlotOrientation.VERTICAL,true, false,false);
chart.setBackgroundPaint(Color.white);
XYPlot xyplot= chart.getXYPlot();
//fondo
xyplot.setBackgroundPaint(Color.white);
//pone la serie de time en el formato area (pero pierde el formato tiempo)
xyplot.setDataset(dataset1);
but my chart is returned like a areachart with x-axis from 0 to 1.285......
I have a recommended solution here.
Don't limit your mind on making XYAreaChart with TimeSeries X axis. Why not make a TimeSeries chart and render it into XYAreaChart?
Here is how it can be done.
// Create TimeSeriesChart
JFreeChart localJFreeChart = createChart(createDataset());
// Set to be XYAreaChart
XYItemRenderer render = new XYAreaRenderer();
XYPlot plot = localJFreeChart.getXYPlot();
plot.setRenderer(render);
in which
private static JFreeChart createChart(XYDataset paramXYDataset) {
JFreeChart localJFreeChart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices", "Date", "Price Per Unit", paramXYDataset, true, true, false);
// ...
return (JFreeChart) localJFreeChart;
}
Full code can be seen here.
Most of the code is from TimeSeriesDemo1 in JFreeChart-1.0.14-demo.jar, and modified by myself to meet your requirements.
Hope this could help.
I'm assuming you're using TimeSeries and adding instances of RegularTimePeriod to the data set. By default, the method ChartFactory. createXYAreaChart() uses a NumberAxis for the domain. Instead, use a DateAxis.
XYPlot plot = chart.getXYPlot();
DateAxis domain = new DateAxis("Tiempo");
plot.setDomainAxis(domain);

Resources