我有这个代码来绘制图形,这很好用。我在这里需要两件事
在缩放时我看到了这个
同样在域轴上,我有毫秒值。我可以将它映射到人类可读的日期吗?
public class Grapher extends ApplicationFrame {
public Grapher(final String title, List<PriceModel> priceModels) {
super(title);
final XYSeries series = new XYSeries("foo");
double max = Double.MIN_VALUE, min = Double.MAX_VALUE;
for (int i = 0; i < priceModels.size(); i++) {
double price = priceModels.get(i).getPrice();
if (price < min) {
min = price;
}
if (price > max) {
max = price;
}
series.add((double) priceModels.get(i).getDate(), price);
}
final XYSeriesCollection data = new XYSeriesCollection(series);
final JFreeChart chart = ChartFactory.createXYLineChart(
"XY Series Demo",
"X",
"Y",
data,
PlotOrientation.VERTICAL,
true,
true,
false
);
for (int i = 0; i < priceModels.size(); i++) {
if (priceModels.get(i).getAction() != null) {
Marker marker = new ValueMarker((double) priceModels.get(i).getDate());
marker.setLabelAnchor(RectangleAnchor.BOTTOM_RIGHT);
marker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
if (priceModels.get(i).getAction() == Types.Action.SELL) {
marker.setPaint(Color.green);
marker.setLabel("SELL");
} else {
marker.setPaint(Color.red);
marker.setLabel("BUY");
}
marker.setStroke(new BasicStroke(10.0f));
chart.getXYPlot().addDomainMarker(marker);
}
}
chart.getXYPlot().setBackgroundPaint(Color.white);
chart.getXYPlot().getRenderer().setPaint(Color.BLUE);
chart.getXYPlot().getRangeAxis().setRange(min - 1, max + 1);
final ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setBackground(Color.WHITE);
chartPanel.setRangeZoomable(true);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
public static void draw(List<PriceModel> priceModels) {
final Grapher demo = new Grapher("foo", priceModels);
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
答案 0 :(得分:3)
您必须结合以下几种方法:
域名滚动替代方案:
SlidingXYDataset
,实施here并说明here。plot.setDomainPannable(true)
,如建议here。JScrollPane
,例如add(new JScrollPane(chartPanel);
。 标记文字:对example使用XYTextAnnotation
。
格式化日期:使用DateAxis
替换工厂的轴,并使用setDateFormatOverride()
替换example。