0

I have a segmented time series data and I want to plot it as a bar chart in one of the subplots of combined domain XY plot. I have found that some of the bars are not rendering properly (they are thin w.r.t. others) and few of the bars are missing altogether. Is this a known problem? Please provide any pointers to fix this issue.

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.SegmentedTimeline;
import org.jfree.chart.fx.ChartViewer;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

public class VolumeBarChartJava extends Application {
    private List<java.util.Date> getHolidayList() {
        List<java.util.Date> holidayList = new ArrayList();
        holidayList.add(new java.util.Date(new Day(3,1,2017).getFirstMillisecond()));
        holidayList.add(new java.util.Date(new Day(4,1,2017).getFirstMillisecond()));
        holidayList.add(new java.util.Date(new Day(10,1,2017).getFirstMillisecond()));
        holidayList.add(new java.util.Date(new Day(11,1,2017).getFirstMillisecond()));
        holidayList.add(new java.util.Date(new Day(16,1,2017).getFirstMillisecond()));
        holidayList.add(new java.util.Date(new Day(17,1,2017).getFirstMillisecond()));
        holidayList.add(new java.util.Date(new Day(18,1,2017).getFirstMillisecond()));
        holidayList.add(new java.util.Date(new Day(24,1,2017).getFirstMillisecond()));
        holidayList.add(new java.util.Date(new Day(25,1,2017).getFirstMillisecond()));

        return holidayList;
    }

    private TimeSeriesCollection createDataset() {
        TimeSeries ts = new TimeSeries("Data");
        ts.add(new Day(1,1,2017), 100.0);
        ts.add(new Day(2,1,2017), 135.0);
        ts.add(new Day(5,1,2017), 140.0);
        ts.add(new Day(6,1,2017), 109.0);
        ts.add(new Day(7,1,2017), 167.0);
        ts.add(new Day(8,1,2017), 201.0);
        ts.add(new Day(9,1,2017), 87.0);
        ts.add(new Day(12,1,2017), 56.0);
        ts.add(new Day(13,1,2017), 88.0);
        ts.add(new Day(14,1,2017), 125.0);
        ts.add(new Day(15,1,2017), 180.0);
        ts.add(new Day(19,1,2017), 115.0);
        ts.add(new Day(20,1,2017), 70.0);
        ts.add(new Day(21,1,2017), 62.0);
        ts.add(new Day(22,1,2017), 175.0);
        ts.add(new Day(23,1,2017), 22.0);
        ts.add(new Day(26,1,2017), 254.0);
        ts.add(new Day(27,1,2017), 134.0);
        ts.add(new Day(28,1,2017), 99.0);
        ts.add(new Day(29,1,2017), 149.0);

        TimeSeriesCollection tsc = new TimeSeriesCollection();
        tsc.addSeries(ts);

        return tsc;        
    }

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox();
        TimeSeriesCollection tsc = createDataset();
        List<java.util.Date> holidayList = getHolidayList();

        NumberAxis rangeAxis = new NumberAxis();
        XYBarRenderer renderer = new XYBarRenderer();
        renderer.setShadowVisible(false);
        renderer.setMargin(.5);

        XYPlot plot = new XYPlot(tsc, null, rangeAxis, renderer);

        DateAxis domainAxis = new DateAxis();
        SegmentedTimeline st = new SegmentedTimeline(SegmentedTimeline.DAY_SEGMENT_SIZE, 1, 0);
        st.addExceptions(holidayList);
        domainAxis.setTimeline(st);

        CombinedDomainXYPlot combinedPlot = new CombinedDomainXYPlot(domainAxis);
        combinedPlot.add(plot);

        JFreeChart chart = new JFreeChart("Volume Bar Chart", null, combinedPlot, false);
        ChartViewer chartViewer = new ChartViewer(chart);

        root.getChildren().add(chartViewer);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Volume Bar Chart");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Nimish Jain
  • 97
  • 3
  • 11
  • A `StandardXYBarPainter`, for [example](http://stackoverflow.com/a/28519356/230513), makes it easier to see; a similar problem is examined [here](http://stackoverflow.com/a/18421887/230513). – trashgod Feb 18 '17 at 02:37
  • @trashgod: The problem is still there even after setting the barPainter as suggested. It seems with segmented timeline, the bar rendering gets topsy turvy, may be because of some gap in width calculation of bar. I will try to debug the code. – Nimish Jain Feb 18 '17 at 04:54

0 Answers0