【问题标题】:Scrollable JFree domain axis and custom marker label可滚动的 JFree 域轴和自定义标记标签
【发布时间】:2017-05-22 00:29:41
【问题描述】:

我有这段代码来绘制图形,这很好用。我这里需要两件事

  1. 在域轴 (x) 上我希望能够滚动。
  2. 在标记上我看到一条粗粗线。我希望能够看到这个标记的一些可读文本。

现在我看到了这个输出

放大后我看到了这个

  1. 在域轴上我也有毫秒值。我可以将它映射到人类可读的日期吗?

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

【问题讨论】:

  • 您是否尝试过将chartPane 放入JScrollPane 中?
  • @MadProgrammer 我该怎么做?
  • @MadProgrammer:在滚动窗格之前,我会看一些其他的东西;更多内容如下。
  • @trashgod 不得不承认我并没有真正使用过 JFreeChart

标签: java swing jfreechart


【解决方案1】:

您必须结合几种方法:

  1. 域滚动替代方案

  2. 标记文本:使用XYTextAnnotation,代表example

  3. 格式化日期:将工厂坐标轴替换为DateAxis 并使用setDateFormatOverride(),即example

【讨论】:

  • 您是建议滚动需要所有 4 个点还是这些是选项?
  • 它们代表替代品;我通常只启用平移。
  • 谢谢,在我的情况下,时间在 X 轴上。它只允许 Y 上的 DateAxis。
  • 我不明白。上面引用的SlidingXYDataset 的实现在第一个演示中显示NumberAxis,在第二个演示中显示DateAxis;两者都是域轴。
  • 谢谢@trashgod。你也可以在这里帮忙stackoverflow.com/questions/44150322/… 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-03
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多