Use Drag-Zoom and zoom in to March 28 2003. Hover over the CandleStick at that point and you will get a tooltip.
If you read the comments in the code and use a normal ChartPanel you will find the tooltip will not display.
Code: Select all
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.SegmentedTimeline;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.TooltipChartPanel;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.AbstractXYDataset;
import org.jfree.data.xy.DefaultOHLCDataset;
import org.jfree.data.xy.OHLCDataItem;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Date;
import java.util.Collections;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class TooltipDemoCandle extends JFrame {
public TooltipDemoCandle(String symbol) {
super("TooltipDemoCandle");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DateAxis domainAxis = new DateAxis("Date");
NumberAxis rangeAxis = new NumberAxis("Price");
CandlestickRenderer renderer = new CandlestickRenderer();
XYDataset dataset = getDataSet(symbol);
XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);
//Uncomment this to test horizontal charts (Horizontal candlestick charts ?!?)
// mainPlot.setOrientation(PlotOrientation.HORIZONTAL);
//Do some setting up, see the API Doc
renderer.setSeriesPaint(0, Color.BLACK);
renderer.setDrawVolume(false);
renderer.setBaseCreateEntities(false); //**** BUG : Candlestick renderer ignores this (in vers 1.0.9)!!!
rangeAxis.setAutoRangeIncludesZero(false);
domainAxis.setTimeline( SegmentedTimeline.newMondayThroughFridayTimeline() );
JFreeChart chart = new JFreeChart(symbol, null, mainPlot, false);
//****** uncomment and use ChartPanel instead of TooltipChartPanel then zoom in to March 28 2003; the tooltip will not display (in vers 1.0.9)!!!
//ChartPanel chartPanel = new ChartPanel(chart);
ChartPanel chartPanel = new TooltipChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(600, 300));
this.add(chartPanel);
this.pack();
}
protected AbstractXYDataset getDataSet(String symbol) {
//This is the dataset we are going to create
DefaultOHLCDataset result = null;
//This is the data needed for the dataset
OHLCDataItem[] data;
//This is where we go get the data, replace with your own data source
data = getData(symbol);
//Create a dataset, an Open, High, Low, Close dataset
result = new DefaultOHLCDataset(symbol, data);
return result;
}
//This method uses yahoo finance to get the OHLC data
protected OHLCDataItem[] getData(String symbol) {
java.util.List<OHLCDataItem> dataItems = new ArrayList<OHLCDataItem>();
try {
String strUrl= "http://ichart.finance.yahoo.com/table.csv?s="+symbol+"&a=0&b=1&c=2000&d=3&e=30&f=2008&ignore=.csv";
URL url = new URL(strUrl);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
DateFormat df = new SimpleDateFormat("y-M-d");
String inputLine;
in.readLine();
while ((inputLine = in.readLine()) != null) {
StringTokenizer st = new StringTokenizer(inputLine, ",");
Date date = df.parse( st.nextToken() );
double open = Double.parseDouble( st.nextToken() );
double high = Double.parseDouble( st.nextToken() );
double low = Double.parseDouble( st.nextToken() );
double close = Double.parseDouble( st.nextToken() );
double volume = Double.parseDouble( st.nextToken() );
double adjClose = Double.parseDouble( st.nextToken() );
OHLCDataItem item = new OHLCDataItem(date, open, high, low, close, volume);
dataItems.add(item);
}
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
//Data from Yahoo is from newest to oldest. Reverse so it is oldest to newest
Collections.reverse(dataItems);
//Convert the list into an array
OHLCDataItem[] data = dataItems.toArray(new OHLCDataItem[dataItems.size()]);
return data;
}
public static void main(String[] args) {
new TooltipDemoCandle("MSFT").setVisible(true);
}
}