jFreeChart: DateAxis to behave like a CategoryAxis. Only discrete datevalues to be printed - jfreechart

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);

Related

anychart stock x-axis tick configuration. Change color and interval.

I've been scratching my head and reading the documentation a lot, trying to understand when using anychart stock how i can get control over the tick ( major/minor) display ? i.e i'd like to change the tick stroke color, the interval and make it rotate so text is vertical.
this is example data i'm playing with.
https://playground.anychart.com/sTrncP0D
Nothing special, i just want the major tick per minute and minor every 10 seconds if possible. I tried many variations but fail to get working combination and i think it's because the Stock axis differences.
Can you someone help how this is done ? Or if this is even possible.
thanks.
You can set ticks and minor ticks interval with the following lines:
var scale = chart.xScale();
scale.ticks([{major: {unit: 'minute', count: 1}, minor : {unit:'second', count: 10}}]);
You can rotate axis labels like this:
var labels = chart.plot().xAxis().labels();
minorLabels = chart.scroller().xAxis().minorLabels();
labels.rotation(90);
minorLabels.rotation(90);
You can adjust ticks appearance with the following lines:
var ticks = chart.plot().xAxis().ticks();
var minorTicks = chart.plot().xAxis().minorTicks();
ticks.stroke("red", 0.9);
minorTicks.stroke("blue", 0.9);

Create Fatter Candlesticks in JFreeChart

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);

XYSeries autorange loss of precision?

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.

JFreeChart - Determine the moment when chart is not visible because of big amount of range axes

I am using JFreeChart library to create Chart. I need to present big amount of data on the same chart. Because of that I have many range axes descriptions.
Unfortunately when there are too many range axes, chart is no longer visible. It is possible to make chart visible by calling this simple lines (Where plot is an XYPlot instance):
int axises = plot.getRangeAxisCount();
for (int i = 0; i < axises; i++) {
plot.getRangeAxis(i).setVisible(false);
}
This simple portion of code hides all RangeAxes. After that code execution, chart does not have any description for range axes but it is visible.
But unfortunately I am unable to figure out how can I determine does the chart is visible or not during the processing time.
Information that is important to me is in:
chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea().getWidth()
But unfortunately I am performing many operations on the chart (for example zoom, move, etc.) and because of that I need to have this information everytime, when state of the chart changes. I am unable to take that information whenever plotChanged() method of the PlotChangeListener interface is called, because there is no plot (this event is not fired). chartChanged() method from ChartChangeListener is fired too early - chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea().getWidth() returns old values. Any ideas?
You can add a ChartProgressListener to the ChartPanel.
chart.addProgressListener(new ChartProgressListener() {
#Override
public void chartProgress(ChartProgressEvent event) {
System.out.println(event.getType() + " "
+ event.getPercent() + " "
+ chartPanel.getChartRenderingInfo()
.getPlotInfo().getDataArea().getWidth());
}
});

jFreeChart: Possible to combine StatisticalBarRenderer and LayeredBarRenderer?

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".

Resources