嗨,我想在这个 barchar 上画十字准线。有人能帮帮我吗。我只希望十字准线在鼠标指向或单击的地方打印 X 和 y 坐标。即使十字准线在上面打印 X 和 Y 坐标,我也很好控制台。这是我的代码
import java.awt.Color;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import javax.swing.JPanel;
import org.jfree.chart.ChartColor;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.HighLowRenderer;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class PriceVolumeChart2 extends ApplicationFrame {
final static String filename = "D:\\CL.txt";
/**
* Default constructor
*/
public PriceVolumeChart2(String title) {
super(title);
JPanel panel = createDemoPanel();
panel.setPreferredSize(new Dimension(1200, 800));
setContentPane(panel);
}
private static OHLCDataset createPriceDataset(String filename) {
// the following data is taken from http://finance.yahoo.com/
// for demo purposes...
OHLCSeries s1 = new OHLCSeries(filename);
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
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());
s1.add(new Day(date), open, high, low, close);
//t1.add(new Day(date), close);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
OHLCSeriesCollection dataset = new OHLCSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
private static JFreeChart createCombinedChart() {
OHLCDataset data1 = createPriceDataset(filename);
System.out.println(data1.getItemCount(0));
HighLowRenderer renderer1 = new HighLowRenderer();
renderer1.setTickLength(3);
renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator
(StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
renderer1.setSeriesPaint(0, Color.blue);
DateAxis domainAxis = new DateAxis("Date");
NumberAxis rangeAxis = new NumberAxis("Price");
rangeAxis.setNumberFormatOverride(new DecimalFormat("$0.00"));
rangeAxis.setAutoRange(true);
rangeAxis.setAutoRangeIncludesZero(false);
ChartColor chartColor=new ChartColor(0, 0, 0);
XYPlot plot1 = new XYPlot(data1, domainAxis, rangeAxis, renderer1);
plot1.getRangeCrosshairPaint();
plot1.setRangePannable(true);
JFreeChart chart = new JFreeChart("NSE NIFTY", JFreeChart.DEFAULT_TITLE_FONT, plot1, false);
//ChartUtilities.applyCurrentTheme(chart);
return chart;
}
// create a panel
public static JPanel createDemoPanel() {
JFreeChart chart = createCombinedChart();
return new ChartPanel(chart);
}
public static void main(String[] args) {
// TODO code application logic here
PriceVolumeChart2 demo = new PriceVolumeChart2("JFreeChart");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
// Download data from web
}
这是我的输出。我希望十字准线根据 X 和 Y 轴上的给定值告诉我 x 和 y 坐标