【问题标题】:JFreechart candlestick chart weird behaviour on dragJFreechart烛台图表拖动时的奇怪行为
【发布时间】:2015-05-23 19:40:59
【问题描述】:

这是来自this question 的后续问题。

会发生以下情况:

当我启动图表并拖动图表时,会发生一些奇怪的事情:在某个时间间隔内,似乎每 7 个周期,烛台就会变得越来越小,直到它们变成一条条纹。然后当我进一步拖动时,它们再次变厚,直到它们再次恢复正常大小。这似乎每 7 个周期发生一次。

以下 3 张图片显示了这种现象的一个示例:

下面的代码将准确地说明我的意思。只需编译并运行它。 然后按住CTRL 并在图表上单击并按住鼠标。现在尝试将图表向右或向左拖动。在一定的“拖动距离”后,您会注意到该错误。

我的问题:如何预防/解决这个问题?

代码:

    import org.jfree.chart.*;
    import org.jfree.chart.axis.*;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.CandlestickRenderer;
    import org.jfree.data.xy.*;

    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import java.net.URL;
    import java.text.*;
    import java.util.*;
    import java.util.List;

    public class CandlestickDemo2 extends JFrame {
        public CandlestickDemo2(String stockSymbol) {
            super("CandlestickDemo");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            DateAxis    domainAxis       = new DateAxis("Date");
            NumberAxis  rangeAxis        = new NumberAxis("Price");
            CandlestickRenderer renderer = new CandlestickRenderer();
            XYDataset   dataset          = getDataSet(stockSymbol);

            XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);

            //Do some setting up, see the API Doc
            renderer.setSeriesPaint(0, Color.BLACK);
            renderer.setDrawVolume(false);
            rangeAxis.setAutoRangeIncludesZero(false);
            domainAxis.setTimeline( SegmentedTimeline.newMondayThroughFridayTimeline() );

            //Now create the chart and chart panel
            JFreeChart chart = new JFreeChart(stockSymbol, null, mainPlot, false);
            ChartPanel chartPanel = new ChartPanel(chart, false);
            chartPanel.setPreferredSize(new Dimension(600, 300));

            mainPlot.setDomainPannable(true);
            mainPlot.setRangePannable(true);

            this.add(chartPanel);
            this.pack();
        }
        protected AbstractXYDataset getDataSet(String stockSymbol) {
            //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(stockSymbol);

            //Create a dataset, an Open, High, Low, Close dataset
            result = new DefaultOHLCDataset(stockSymbol, data);

            return result;
        }
        //This method uses yahoo finance to get the OHLC data
        protected OHLCDataItem[] getData(String stockSymbol) {
            List<OHLCDataItem> dataItems = new ArrayList<OHLCDataItem>();
            try {
                String strUrl= "http://ichart.yahoo.com/table.csv?s=GOOG&a=2&b=1&c=2013&d=4&e=24&f=2013&g=d&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) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new CandlestickDemo2("GOOG").setVisible(true);
                }
            });
        }
    }

更新

这个错误现在是 JFreeChart 的 sourceforge 页面上报告的错误问题。

这个bug可以追踪here

【问题讨论】:

  • @assylias:所以唯一的缺点是轴分布不均匀?你能告诉我你是如何修改课程的吗?
  • 我以为我已经解决了一些问题,但是当我尝试使用您的示例时,我得到了类似的行为...忘记我说的话。对不起。
  • @assylias:没关系。我想这只是一个非常烦人的问题,没有简单的解决方法。
  • 前段时间我在它上面花了很长时间,这让我很头疼 - 每次我修复一些东西时,它都会破坏其他东西。我最后的想法(我从未尝试过)是使用文本轴而不是日期轴并手动填充比例图例。
  • @assylias:它可以工作。不幸的是,我的程序使用dataset.getXValue(0, k),并且因为我使用的是TimeSeries,所以XValue 准确(以毫秒为单位)非常重要。因此,用字符串填充比例图例(又名 Strings 模仿 Dates)会导致重大问题。

标签: java jfreechart drag candlestick-chart


【解决方案1】:

我能够重现所描述的效果。如before,只有SegmentedTimeline才能看到效果; DefaultTimeline 并不明显。它似乎与拖过周一至周五时间线的“隐藏”周末同时发生,但我没有看到明显的错误。

一种解决方法可能是让用户使用相邻控件选择TimeLine,正如example 中所建议的那样。因为DefaultTimelineprivate,所以在控件的处理程序中调用setTimeline() 之前,您需要保存getTimeline() 的结果。

附录:这是使用JCheckBox 切换Timeline 的程序的变体。点击复选框启用SegmentedTimeline;水平平移以查看效果(在 Windows 上按住 Control 键单击;在 Mac 上按住 Option 键单击)。

import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.data.xy.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.*;
import java.net.URL;
import java.text.*;
import java.util.*;
import java.util.List;

/**
 * @see https://stackoverflow.com/a/18421887/230513
 * @see http://www.jfree.org/forum/viewtopic.php?f=10&t=24521
 */
public class CandlestickDemo2 extends JFrame {

    public CandlestickDemo2(String stockSymbol) {
        super("CandlestickDemo2");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final DateAxis domainAxis = new DateAxis("Date");
        NumberAxis rangeAxis = new NumberAxis("Price");
        CandlestickRenderer renderer = new CandlestickRenderer();
        XYDataset dataset = getDataSet(stockSymbol);
        XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);
        //Do some setting up, see the API Doc
        renderer.setSeriesPaint(0, Color.BLACK);
        renderer.setDrawVolume(false);
        rangeAxis.setAutoRangeIncludesZero(false);
        //Now create the chart and chart panel
        JFreeChart chart = new JFreeChart(stockSymbol, null, mainPlot, false);
        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setPreferredSize(new Dimension(600, 300));
        mainPlot.setDomainPannable(true);
        mainPlot.setRangePannable(true);
        this.add(chartPanel);
        // Add tiemline toggle
        final Timeline oldTimeline = domainAxis.getTimeline();
        final Timeline newTimeline = SegmentedTimeline.newMondayThroughFridayTimeline();
        this.add(new JCheckBox(new AbstractAction("Segmented Timeline") {
            @Override
            public void actionPerformed(ActionEvent e) {
                JCheckBox jcb = (JCheckBox) e.getSource();
                if (jcb.isSelected()) {
                    domainAxis.setTimeline(newTimeline);
                } else {
                    domainAxis.setTimeline(oldTimeline);
                }
            }
        }), BorderLayout.SOUTH);
        this.pack();
        this.setLocationRelativeTo(null);
    }

    private AbstractXYDataset getDataSet(String stockSymbol) {
        //This is the dataset we are going to create
        DefaultOHLCDataset result;
        //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(stockSymbol);
        //Create a dataset, an Open, High, Low, Close dataset
        result = new DefaultOHLCDataset(stockSymbol, data);
        return result;
    }
    //This method uses yahoo finance to get the OHLC data

    protected OHLCDataItem[] getData(String stockSymbol) {
        List<OHLCDataItem> dataItems = new ArrayList<OHLCDataItem>();
        try {
            String strUrl = "http://ichart.yahoo.com/table.csv?s=" + stockSymbol
                + "&a=4&b=1&c=2013&d=6&e=1&f=2013&g=d&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(System.err);
        }
        //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) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CandlestickDemo2("AAPL").setVisible(true);
            }
        });
    }
}

【讨论】:

  • 但是因为我没有周末的数据,不会更改为DefaultTimeline 更改x axis 上的单位,这样就会出现差距吗?也许想了解更多信息:我刚刚在jfree 上发现了一个类似的问题。
  • 是的;没有修复,这是一个权衡。我不是SegmentedTimeline 的粉丝,但无论如何,偏好可能会很好。我注意到newFifteenMinuteTimeline() 平底锅OK。
  • 好的,谢谢。你认为修复存在还是不可能通过构建? (如果可能,我将等待 2 天开始赏金)
  • 我不确定这是错误还是只是父级实现 pan() 的异常。随意留下这个问题。由于您有一个带有插图的sscce,您可以在论坛或项目的错误跟踪器中发帖。使用上面的share 链接获得announcer 徽章积分。
  • @assylias:我发现合成数据存在同样的问题。我不明白“文本”轴。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 2020-06-29
  • 2011-11-04
相关资源
最近更新 更多