JFreechart TimeSeries在DateAxis上关闭自动范围

时间:2013-06-24 07:25:08

标签: java jfreechart time-series

我有一个TimeSeries图表,单位是天数。当我创建图表时,一切都很好,当数据跨越8天或更长时间。绘制线条,日期轴上的每个刻度标签显示一天 但是当数据只包含四天或更短的时间时,Ticks或TickLabels会重复。所以我在同一天有两个Ticks。我想说明一点:

数据跨越8天:

标签:星期一,星期二,星期三,星期四,星期五,星期六,星期日,星期一,

数据跨越4天:

标签:周一,周一,周二,周二,周三,周三,周四,周四,周五

除了缩小图表的大小以仅使用单个Tick和Label作为日期之外,还有其他方法吗?

     private void presentCharts(final MapDataParameter mapDataParameter)
{
    if (getRestrictionHistoryMap() != null && !getRestrictionHistoryMap().isEmpty())
    {
        final JFreeChart restrChart = ChartFactory.createTimeSeriesChart(
                X_AXIS_LABEL_TOP,
                X_AXIS_LABEL_BOTTOM,
                Y_AXIS_LABEL_LEFT,
                createCummulatedDataset(),
                true,
                true,
                false);

        final XYPlot pplot = restrChart.getXYPlot();
        setPlot(pplot);
        getPlot().setDataset(1, createCountDataset());
        getPlot().mapDatasetToRangeAxis(1, 1);
        getPlot().setDomainGridlinesVisible(true);
        getPlot().setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
        Color plotBackgroundColor = new Color(208, 205, 205);
        getPlot().setBackgroundPaint(plotBackgroundColor);
        // valueAxis is for cummulated (right y axis)
        final ValueAxis valueAxis = new NumberAxis(Y_AXIS_LABEL_RIGHT);
        valueAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        getPlot().setRangeAxis(1, valueAxis);


        final XYBarRenderer xyBarRenderer = new XYBarRenderer();
        getPlot().setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
        // Hintergrund stylen
        Color boundingBackgroundColor = new Color(238, 238, 238);
        restrChart.setBackgroundPaint(boundingBackgroundColor);
        // Datensätze den renderern zuweisen
        final StandardXYItemRenderer standardXyItemRenderer = new StandardXYItemRenderer();
        getPlot().setRenderer(0, standardXyItemRenderer);
        getPlot().setRenderer(1, xyBarRenderer);
        // nur Ganzzahlige Schritte
        ValueAxis valueAxis2 = getPlot().getRangeAxis(0);
        valueAxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        // Datumsformat einstellen
        final DateAxis dateAxis = (DateAxis) pplot.getDomainAxis();
        dateAxis.setDateFormatOverride(new SimpleDateFormat("dd.MMM.yyyy", mapDataParameter.getLocale()));
        // test
        Range range = new Range(1.1d, 19.9d);
        dateAxis.setRange(range, true, true);
        // Zeige die Anzahl über dem Balken
        XYItemLabelGenerator labelGenerator = new XYItemLabelGenerator()
        {
            @Override
            public String generateLabel(XYDataset xyd, int i, int i1)
            {
                Number tmp = xyd.getY(i, i1);
                String label = "" + tmp.intValue();
                return label;
            }
        };
        // set this at the renderer of the count dataset
        if (getPlot().getRendererCount() > 1)
        {
            getPlot().getRenderer(0).setSeriesPaint(0, CUMMULATED_COLOR);
            getPlot().getRenderer(1).setPaint(COUNTCOLOR);
            getPlot().getRenderer(1).setBaseItemLabelGenerator(labelGenerator);
            getPlot().getRenderer(1).setBaseItemLabelsVisible(true);
        }
        // dauert länger, als ohne, macht aber nichts
        restrChart.setAntiAlias(true);
        setChart(restrChart);

0 个答案:

没有答案