jfreechart-xylineandshaperender getitemlinevisible()不工作

mum43rcc  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(283)

我用dynamicTimesRiesCollection模拟虚拟实时数据,就像这样。在随机间隔期间,传递到绘图的数据应“丢失”,以模拟网络连接丢失。此时,此绘图应停止绘制,并且仅在丢失消退后才开始绘制数据。
我是亚类的 XYLineAndShapeRenderer 并超越了 getItemLineVisible() 方法:

@Override
        public boolean getItemLineVisible(int series, int item){
            if(offline){
                return false;
            }else{
                return true;
            }
        }

但是,当offline为true时,所有点仍在图形上绘制。

public DTSCTest(final String title) throws ParseException {
        super(title);

        SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy HH:mm", Locale.ENGLISH);
        parser = new PriceParser();
        List<List<String>> priceData = parser.parse();
        Date date = formatter.parse(priceData.get(0).get(0));
        startDate = Calendar.getInstance();
        startDate.setTime(date);
        Calendar timeBaseStartDate = Calendar.getInstance();
        timeBaseStartDate.setTime(startDate.getTime());
        timeBaseStartDate.add(Calendar.SECOND, -COUNT);

        final DynamicTimeSeriesCollection dataset =
                new DynamicTimeSeriesCollection(1, COUNT, new Second());
        dataset.setTimeBase(new Second(timeBaseStartDate.getTime()));
        dataset.addSeries(new float[1], 0, "Longwall Data");
        dataset.addValue(0, 0, (float) gateStart);

        JFreeChart chart = createChart(dataset);
        final JComboBox combo = new JComboBox();
        combo.addItem("Fast");
        combo.addItem("Real-time");
        combo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if ("Fast".equals(combo.getSelectedItem())) {
                    timer.setDelay(FAST);
                } else {
                    timer.setDelay(REALTIME);
                }
            }
        });

        JFrame frame = new JFrame("Test");
        JLabel label = new JLabel("Network connectivity lost.");

        this.add(new ChartPanel(chart), BorderLayout.CENTER);
        JPanel btnPanel = new JPanel(new FlowLayout());
        btnPanel.add(combo);
        JPanel test = new JPanel();
        test.add(label);
        this.add(btnPanel, BorderLayout.SOUTH);

       // frame.add(btnPanel);
        //frame.add(test);

        timer = new Timer(FAST, new ActionListener() {
            Date timeToCheck = formatter.parse(priceData.get(0).get(0));
            Calendar pauseResume = Calendar.getInstance();
            Calendar offlineTime = Calendar.getInstance();
            boolean paused = false;
            boolean waiting = false;
            //boolean offline = false;
            double currentPrice;
            float[] newData = new float[1];
            PopupFactory pf = PopupFactory.getSharedInstance();
            Popup popup;

            @Override
            public void actionPerformed(ActionEvent e) {
                Date datasetTime = dataset.getNewestTime().getStart();
                //System.out.println(gateStart);
                System.out.println(btnPanel.getX());
                System.out.println(btnPanel.getY());
                if(offline){
                    System.out.println("Offline: "+offlineTime.getTime());
                    System.out.println("Current: "+datasetTime);
                    if(offlineTime.getTime().compareTo(datasetTime) == 0){
                        offline = false;
                        System.out.println("Im no longer offline");
                        popup.hide();
//                        renderer.setSeriesLinesVisible(0, true);
                    }
                }

                if(ThreadLocalRandom.current().nextInt(0, 100) > 98 && !offline){
                    offline = true;
                    offlineTime.setTime(datasetTime);
                    offlineTime.add(Calendar.SECOND, ThreadLocalRandom.current().nextInt(1, 10)*10);
//                    renderer.setSeriesLinesVisible(0, false);
                    popup = pf.getPopup(btnPanel, label, 600, 400);
                    popup.show();

                }

                if(timeToCheck.compareTo(datasetTime) == 0){
                    currentPrice = Double.valueOf(priceData.get(0).get(1));
                    System.out.println(currentPrice);
                    paused = currentPrice >= threshold;
                    priceData.remove(0);
                    try {
                        timeToCheck = formatter.parse(priceData.get(0).get(0));
                    } catch (ParseException ex) {
                        ex.printStackTrace();
                    }
                }

                if(!paused) {
                    if (Math.round(gateStart) * 10 / 10.0 == 100d) {
                        returning = true;
                    } else if (Math.round(gateStart) * 10 / 10.0 == 0) {
                        returning = false;
                    }
                    if (returning) {
                        gateStart -= 0.1d;
                    } else {
                        gateStart += 0.1d;
                    }
                }else{
                    if(datasetTime.compareTo(pauseResume.getTime()) == 0 && currentPrice < threshold){
                        paused = false;
                        waiting = false;
                    }else{
                        if(Math.round(gateStart)*10/10.0 == 0 || Math.round(gateStart)*10/10.0 == 100){
                            if(!waiting){
                                pauseResume.setTime(datasetTime);
                                pauseResume.add(Calendar.SECOND, 120);
                            }
                            waiting = true;
                        }else{
                            if(Math.round(gateStart)*10/10.0 >= 50){
                                gateStart += 0.1d;
                            }else if(Math.round(gateStart)*10/10.0 < 50){
                                gateStart -= 0.1d;
                            }
                        }
                    }
                }
                newData[0] = (float)gateStart;
                dataset.advanceTime();
//                if(!offline){
                    dataset.appendData(newData);
//                }
            }
        });
    }

    private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart result = ChartFactory.createTimeSeriesChart(
                TITLE, "Time", "Shearer Position", dataset, true, true, false);
        final XYPlot plot = result.getXYPlot();
        renderer = new XYLineAndShapeRendererTest();
        plot.setRenderer(renderer);
        DateAxis domain = (DateAxis)plot.getDomainAxis();

        Calendar endDate = Calendar.getInstance();
        endDate.setTime(startDate.getTime());
        endDate.add(Calendar.HOUR_OF_DAY, 12);

        domain.setRange(startDate.getTime(), endDate.getTime());
        domain.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 1));
        domain.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

        ValueAxis range = plot.getRangeAxis();
        range.setRange(0, 100);

        return result;
    }
    public void start() {
    timer.start();
}

   public static void main(final String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            DTSCTest demo = null; //pass date in from csv
            try {
                demo = new DTSCTest(TITLE);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            demo.pack();
            RefineryUtilities.centerFrameOnScreen(demo);
            demo.setVisible(true);
            demo.start();
        }
    });
}

我做错什么了?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题