I'm creating an XYSeries in JFreeChart 1.0.17 as shown below, and building a chart out of it dynamically. The x, y data that are added can be of any magnitude, but when the x and y values are very small fractions -- 1e-5 or less -- the autorange calculations seem not to work, and the plot produced will be just a flat line. In this case multiplying all the data by a factor of 10,000 before adding them to the series leads to a correct plot. The data are added as doubles, and I've looked at the XYSeries object in a debugger to make sure the correct values are ending up there. Am I missing something? Is there some way to help the autorange calculations do the right thing?
series = new XYSeries("Heartbeat");
final XYSeriesCollection data = new XYSeriesCollection(series);
chart = ChartFactory.createXYLineChart("", "", "", data, PlotOrientation.VERTICAL, false, true, false);
final XYPlot plot = chart.getXYPlot();
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
((NumberAxis) domain).setAutoRangeIncludesZero(false);
ValueAxis range = plot.getRangeAxis();
range.setAutoRange(true);
((NumberAxis) range).setAutoRangeIncludesZero(false);
The ValueAxis class has an autoRangeMinimumSize attribute which defaults to 0.00000001. When the range is automatically calculated, the axis won't let it go below this size. It is intended for the case where all your data values are the same, in which case it determines the axis length. But as you've found, it causes problems if all your data values are less than 0.00000001, so you should set it to something lower instead of using the default.
Related
I'm developing an app that displays daily financial data, and have chosen to use JFreeChart. I was able to learn how to create a candlestick chart, but my problem lies in customization.
You see, what I'm aiming for is more along the lines of
While, so far all I've been able to manage is
.
No matter how far I zoom in, the candlesticks do not increase in width.
I'm fairly certain that somehow the thin candlesticks have something to do with being bound to a certain time range.. I've tried to remedy that but am not sure what I'm doing wrong here.
SSCE
public void showStockHistory(OHLCDataset dataset, String stockName) {
JFreeChart candleChart = ChartFactory.createCandlestickChart("History of " + stockName, "Date", "Stock Points", dataset, true);
XYPlot plot = candleChart.getXYPlot();
plot.setDomainPannable(true);
plot.setRangePannable(true);
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
NumberAxis range = (NumberAxis)plot.getRangeAxis();
range.setUpperMargin(0.0D);
range.setLowerMargin(0.0D);
range.setAutoRange(true);
range.setAutoRangeIncludesZero(false);
ChartPanel chartPanel = new ChartPanel(candleChart);
chartPanel.setMouseWheelEnabled(true);
chartPanel.setMouseZoomable(true);
getViewport().add(chartPanel);
}
Although my given example seems to have to no differing method calls from the demo in the first picture's code above, it nevertheless only shows thin candlesticks. I assume this to be some kind of bug.
However, I was able to rectify the issue as follows:
getting the renderer for the chart,
casting it to a type of CandlestickRenderer, and
setting its setAutoWidthMethod() method to CandlestickRenderer.WIDTHMETHOD_SMALLEST.
This is how you do it:
JFreeChart candleChart = ChartFactory.createCandlestickChart(
"History of " + stockName, "Date", "Stock Points", dataset, true);
XYPlot plot = candleChart.getXYPlot();
CandlestickRenderer renderer = (CandlestickRenderer) plot.getRenderer();
renderer.setAutoWidthMethod(CandlestickRenderer.WIDTHMETHOD_SMALLEST);
I've got a box and whisker chart being populated like so:
JFreeChart chart = ChartFactory.createBoxAndWhiskerChart(
"Average Fitness",
"Generation",
"Fitness",
aveDataSet,
true);
ChartFrame frame = new ChartFrame("Average Fitness", chart);
frame.pack();
frame.setVisible(true);
But the scale of the data makes the chart hard to read. At the left of the x-axis, the values are in the neighborhood of 250 000 000, but by about the halfway point the values are below 10 (but still converging toward 0). This long-tail convergence is impossible to see with the linear y-axis, but I can't figure if I'm able to replace it with a log scale.
I have created a function that processes some value.
Now, my problem is that the graph i create displays the values generated after ALL the values have been calculated. i want to generate the graph such that the as and when the data is calculated; it must be plotted on the data.
The calculation and the display of data in the graph must appear parallel.
I have generated the graph as:
dataset.addSeries(series1);
dataset = new XYSeriesCollection();
series1 = new XYSeries("% ERROR");
chart = ChartFactory.createXYLineChart("Percent Error", "Number of Records", "% Error", dataset,PlotOrientation.VERTICAL,true, true, true);
chart.setBackgroundPaint(Color.WHITE);
frame = new ChartFrame("Error graph", chart);
frame.pack();
frame.setVisible(true);
The series1 is plotted on the graph.the values in series1 is added on every iteration of the loop which generates some value.
Can it be done using wait and notify? I am not really sure about this functions. Please help
The add() method of XYSeries "Adds a data item to the series and sends a SeriesChangeEvent to all registered listeners." One approach is to invoke add() using javax.swing.Timer, as shown in this example.
If your data are computed within a fix period what about using DinamycTimeSeriesCollection?
They are very usefull.
You can find an example in this question where I managed the problem of having periods multiple of a millisecond.
I have a TimeSeries Chart with a XYdataset which has the milliseconds of each dates.
Now I want to have a chart that only displays each date one time on the axis, no matter what the range of dates is. I already got it so far, that only the date is printed, not the time (via setNumberFormatOverride() method).
What I have now, is this:
alt text http://dl.getdropbox.com/u/1144075/Bildschirmfoto%202010-08-05%20um%2016.51.39.JPG
What I want, is this:
alt text http://dl.getdropbox.com/u/1144075/Bildschirmfoto%202010-08-05%20um%2016.54.54.JPG
This is a snippet of my code. also, the setLabelAngle() method don't work?!
JFreeChart chart = ChartFactory.createTimeSeriesChart(...);
XYPlot xyplot = (XYPlot)chart.getPlot();
DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis();
dateaxis.setLabelAngle(Math.PI / 6.0);`
numberaxis.setNumberFormatOverride((DecimalFormat)DecimalFormat.getInstance(Locale.GERMAN));
You can set the TickUnit to any convenient DateTickUnitType. I use DAY in this example and MMM to verify the Locale:
axis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 1,
new SimpleDateFormat("dd.MMM.yy", Locale.GERMAN)));
As you are a returning customer, I'll address the second question, too: setLabelAngle() works perfectly well, but it changes the axis label in the chart legend. Using setVerticalTickLabels() is an alternative for the tick labels themselves:
axis.setVerticalTickLabels(true);
I want to create a chart that consists of couples of two bars.
one of them a regular bar, the second one is layered and has a deviation.
This is a snippet of my code:
private static JFreeChart createStatisticalBarChart(DefaultStatisticalCategoryDataset dataset, String chartTitle, String domainAxisLabel, String rangeAxisLabel, List<String> sunndayMap) {
CategoryAxis xAxis = new CategoryAxis(domainAxisLabel);
ValueAxis yAxis = new NumberAxis(rangeAxisLabel);
CategoryItemRenderer renderer = new StatisticalBarRenderer();
CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
JFreeChart chart = new JFreeChart(chartTitle,
new Font("Arial", Font.PLAIN, 10),
plot,
true);
My dataset has mean+standarddeviatin records with every second record's deviation being "0", so that no range indicators will be displayed.
How can I accomplish that?
You can use a small negative value in setItemMargin() to achieve an overlap, for example
renderer.setItemMargin(-0.10f);
It appears that standard deviation lines are not drawn if the getStdDevValue() method returns null. You might try that value instead of "0".