JFreeChart - Timeseries and CandleStick on the same chart
This is possible using JFreeChart, the key is to create an additional dataset and renderer
You will need to create a new TimeSeriesCollection
to hold the data for the three additional series
TimeSeriesCollection otherDataSet = new TimeSeriesCollection();
TimeSeries ts1 = new TimeSeries("Series 1");
otherDataSet.addSeries(ts1);
TimeSeries ts2 = new TimeSeries("Series 2");
otherDataSet.addSeries(ts2);
TimeSeries ts3 = new TimeSeries("Series 2");
otherDataSet.addSeries(ts3);
Then add the data to the TimeSeries
as normal.
You will then need to add the otherDataSet
to the Plot
in OhlcChart
map it to the same axis at the original plot (mapDatasetToRangeAxis
) and provide a Renderer
//Add the otherDataSet to the plot and map it to the same axis at the original plot
int index = 1;
plot.setDataset(index, otherDataSet);
plot.mapDatasetToRangeAxis(index, 0);
XYItemRenderer renderer2 = new XYLineAndShapeRenderer();
plot.setRenderer(1, renderer2);
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
Here is an example using an OHLCDataset
rather than a BoxAndWhiskerXYDataset
Is it possible to show hollow candle in Candlestick jfreechart?
I've done it. Here is the complete own renderer:
public class CandlestickRenderer extends org.jfree.chart.renderer.xy.CandlestickRenderer {
private final Paint colorRaising = Color.GREEN;
private final Paint colorFalling = Color.RED;
private final Paint colorUnknown = Color.GRAY;
private final Paint colorTransparent = Color.BLACK;
public CandlestickRenderer() {
setDrawVolume(false);
setUpPaint(colorUnknown); // use unknown color if error
setDownPaint(colorUnknown); // use unknown color if error
}
@Override
public Paint getItemPaint(int series, int item) {
OHLCDataset highLowData = (OHLCDataset) getPlot().getDataset(series);
Number curClose = highLowData.getClose(series, item);
Number prevClose = highLowData.getClose(series, item>0 ? item-1 : 0);
if (prevClose.doubleValue() <= curClose.doubleValue()) {
return Color.GREEN;
} else {
return Color.RED;
}
}
@Override
public void drawItem(Graphics2D g2, XYItemRendererState state,
Rectangle2D dataArea, PlotRenderingInfo info, XYPlot plot,
ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset,
int series, int item, CrosshairState crosshairState, int pass) {
OHLCDataset highLowData = (OHLCDataset) dataset;
double yOpen = highLowData.getOpenValue(series, item);
double yClose = highLowData.getCloseValue(series, item);
// set color for filled candle
if (yClose >= yOpen) {
setUpPaint(colorRaising);
setDownPaint(colorFalling);
}
// set color for hollow (not filled) candle
else {
setUpPaint(colorTransparent);
setDownPaint(colorTransparent);
}
// call parent method
super.drawItem(g2, state, dataArea, info, plot, domainAxis, rangeAxis, dataset, series, item, crosshairState, pass);
}
}
And resulting chart:
Don't hesitate to contact me if you'll want all the example code.
JFreechart stop unzoom when drag left
I made an assumption (based on your short question) that you want to disable the "zoom restore" feature when invoked by the mouse being dragged to the left.
If so, you simply need to override the restoreAutobounds()
method like this:
JFreeChart chart = /* create your chart here*/
// add the chart to the panel and override
// the zoom restore behavior
chartPanel = new ChartPanel(chart){
@Override
public void restoreAutoBounds(){
// Do nothing
}
};
Or, event better is to extend the ChartPanel
object and override it there.
NOTE: the zoom out feature will still be available from the context menu of the chart so the user still has meaningful ways to zoom out
Create Fatter Candlesticks in JFreeChart
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
, andsetting its
setAutoWidthMethod()
method toCandlestickRenderer.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);
JGraph - overriding cell drag
You would be better off posting at the JGraph forum.
Add horizontal scroll to JFreeChart
You can actually move it left and right if you call setDomainPannable(true)
on XYPlot
. Looks like chart.getXYPlot().setDomainPannable(true)
in your case. There is also a method setRangePannable(boolean pannable)
for panning in range direction respectively. When I used it long time ago, this resulted in being able to move chart by dragging the middle mouse button. I don't know what the behavior will be in your case nowadays :) But this method is a good place to start if you have no other ideas. May be if you look inside the source code there, then you can create your own custom scrollbar for the same functionality.
Upd.: Alternatively, you can just increase/decrease the size of ChartPanel by yourself on mouse wheel or whatever without using zooming functionality of JFreeChart. Then JScrollPane will do the job. Which is fine and easier if you just want to scale globally. But if you want that nice zooming of user selected area, then I would look into customizing of JFreeChart's "panning".
Upd2.: I might be wrong about middle mouse button. Probably it was CTRL
+ mouse dragging like in the link provided by @trashgod
Related Topics
How to Use Yamlpropertiesfactorybean to Load Yaml Files Using Spring Framework 4.1
Jfreechart with Selectable Domain Axis and Zoom
Get Cell Value from Excel Sheet with Apache Poi
How to Open a Windows Named Pipe from Java
How to Round a Double to Two Decimal Places in Java
Escaping Special Characters in Java Regular Expressions
Java Generics Wildcard Question: List<? Extends A>
Apache PDFbox: Problems with Encoding
Rotate Bufferedimage Inside JPAnel
Java - How to Drag and Drop JPAnel with Its Components
Browse for Image File and Display It Using Java Swing
How to Identify End of Inputstream in Java
How to Make a Jbutton in a Jtable Cell Click-Able
How to Parse Month Full Form String Using Dateformat in Java