Scrollable JFree domain axis and custom marker label
Asked Answered
W

1

2

I have this code to draw graph, this works fine. I need two things here

  1. on domain axis (x) I want to be able to scroll.
  2. On markers I see a thick bold line. I want to be able to see some readable text for this marker.

For now I see this output enter image description here

and upon zooming I see this

enter image description here

  1. Also on domain axis I have millis values. can I map it to human readable dates ?

    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);
        }
    }
    
Watchmaker answered 22/5, 2017 at 0:29 Comment(5)
Have you tried putting the chartPane in a JScrollPane?Osteoporosis
@Osteoporosis how do I do it ?Watchmaker
How to use scroll panesOsteoporosis
@MadProgrammer: I'd look at a few other things before scroll pane; more below.Mordy
@Mordy Have to admit I've not really used JFreeChartOsteoporosis
M
3

You'll have to combine several approaches:

  1. Domain scrolling alternatives:

  2. Marker text: Use XYTextAnnotation, for example.

  3. Format dates: Replace the factory's axis with a DateAxis and use setDateFormatOverride(), for example.

Mordy answered 22/5, 2017 at 6:14 Comment(5)
are you suggesting all 4 points for scrolling are needed or these are options ?Watchmaker
They represent alternatives; I usually just enable panning.Mordy
Thanks, in my case time is on X axis. It only allows DateAxis on Y.Watchmaker
I don't understand. The implementation of SlidingXYDataset cited above shows NumberAxis in the first demo and DateAxis in the second demo; both are the domain axis.Mordy
Thanks @trashgod. can you help here too #44150822 ?Watchmaker

© 2022 - 2024 — McMap. All rights reserved.