jFreeChart:使用分段时间轴无法正确渲染条形图

时间:2017-02-17 20:19:26

标签: jfreechart

我有一个分段的时间序列数据,我想将其绘制为组合域XY图的一个子图中的条形图。我发现有些条形图没有正确渲染(它们很薄,而其他条形图很薄),很少有条形条纹完全丢失。这是known problem吗?请提供解决此问题的任何指示。

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

0 个答案:

没有答案