JFreeChart DateTickUnit 无法正常工作

发布于 2025-01-07 18:07:38 字数 2735 浏览 3 评论 0原文

我为用户提供了过滤图形结果的更改能力。例如,用户可以选择显示 2 天的结果。然后 TimeUnitType 是天,timeAmount 是 2。但这不能正常工作。对于这种情况,它必须只有 2 个厚单位.与第一天相关的日期必须以相关的刻度单位显示。与第二天相关的其他数据必须以第二个粗单位显示。

例如,默认选择 1 天。它在域轴下显示一条黑线。

Domain Axis view

我选择 1 个月间隔。DomainAxis 不显示任何厚度。

    private void customizeGraphView(JFreeChart chart, TimeUnitType timeUnit, Integer timeAmount) {
    CombinedRangeXYPlot combinedPlot = (CombinedRangeXYPlot) chart.getPlot();
    @SuppressWarnings("unchecked")
    /** read 2 subplot*/
    List<XYPlot> subPlots = combinedPlot.getSubplots();
    for (int plotIndex = 0; plotIndex < subPlots.size(); plotIndex++) {
        /** get plot */
        XYPlot plot = subPlots.get(plotIndex);
        plot.setDomainGridlinesVisible(false);
        // plot.setRangeGridlinesVisible(false);
        XYItemRenderer itemRenderer = plot.getRenderer();
        /** if line and shape rendere */
        if (itemRenderer instanceof StandardXYItemRenderer) {
            StandardXYItemRenderer renderer = (StandardXYItemRenderer) itemRenderer;
            /** show shapes in time series */
            renderer.setBaseShapesVisible(true);
            /** fill shapes in time series */
            renderer.setBaseShapesFilled(true);
            renderer.setBaseFillPaint(Color.BLACK);
            // addItemLabels(renderer);
        }
        /** change DateAxis specific vaues */
        DateAxis dateAxis = (DateAxis) plot.getDomainAxis();
        /** change Date Axis Time Unit format */
        dateAxis.setTickLabelsVisible(true);
        /** add severity bar */
        if (plotIndex == 0) {
            dateAxis.setAutoRange(true);
            if(getFirstGraphMinRange() != null && getFirstGraphMaxRange() != null)
            {
                dateAxis.setRange(getHistoryGraphMinRange(), getHistoryGraphMaxRange());
                dateAxis.setTickUnit(new DateTickUnit(getTickUnitType(timeUnit), timeAmount));
            }
        } else {
            dateAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm", Locale.getDefault()));
            dateAxis.setRange(getSecondGraphMinRange(), getSecondGraphMaxRange());
            dateAxis.setTickUnit(new DateTickUnit(DateTickUnitType.MINUTE, 5));
        }
    }

}

    /**
 * @param timeUnit
 * @return
 */
private DateTickUnitType getTickUnitType(TimeUnitType timeUnit) {
    DateTickUnitType unitType = null;
    switch (timeUnit) {
    case HOUR:
        unitType = DateTickUnitType.HOUR;
    break;
    case DAY:
        unitType = DateTickUnitType.DAY;
    break;
    case MONTH:
        unitType = DateTickUnitType.MONTH;
    break;
    default:
    break;
    }
    return unitType;
}

I am giving user change ability to filter graph result.For example user can select to show results for 2 day.Then TimeUnitType is day, timeAmount is 2.But This is not work properly.For this case, it must be only 2 thick unit.Date related to first day must be shown in related tick unit. Another data related to day 2 must be shown in second thick unit.

For example by default 1 day is selected.It shows a black line under domain axis.

Domain Axis view

I am selecting 1 month interval .DomainAxis does not show any thick.

    private void customizeGraphView(JFreeChart chart, TimeUnitType timeUnit, Integer timeAmount) {
    CombinedRangeXYPlot combinedPlot = (CombinedRangeXYPlot) chart.getPlot();
    @SuppressWarnings("unchecked")
    /** read 2 subplot*/
    List<XYPlot> subPlots = combinedPlot.getSubplots();
    for (int plotIndex = 0; plotIndex < subPlots.size(); plotIndex++) {
        /** get plot */
        XYPlot plot = subPlots.get(plotIndex);
        plot.setDomainGridlinesVisible(false);
        // plot.setRangeGridlinesVisible(false);
        XYItemRenderer itemRenderer = plot.getRenderer();
        /** if line and shape rendere */
        if (itemRenderer instanceof StandardXYItemRenderer) {
            StandardXYItemRenderer renderer = (StandardXYItemRenderer) itemRenderer;
            /** show shapes in time series */
            renderer.setBaseShapesVisible(true);
            /** fill shapes in time series */
            renderer.setBaseShapesFilled(true);
            renderer.setBaseFillPaint(Color.BLACK);
            // addItemLabels(renderer);
        }
        /** change DateAxis specific vaues */
        DateAxis dateAxis = (DateAxis) plot.getDomainAxis();
        /** change Date Axis Time Unit format */
        dateAxis.setTickLabelsVisible(true);
        /** add severity bar */
        if (plotIndex == 0) {
            dateAxis.setAutoRange(true);
            if(getFirstGraphMinRange() != null && getFirstGraphMaxRange() != null)
            {
                dateAxis.setRange(getHistoryGraphMinRange(), getHistoryGraphMaxRange());
                dateAxis.setTickUnit(new DateTickUnit(getTickUnitType(timeUnit), timeAmount));
            }
        } else {
            dateAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm", Locale.getDefault()));
            dateAxis.setRange(getSecondGraphMinRange(), getSecondGraphMaxRange());
            dateAxis.setTickUnit(new DateTickUnit(DateTickUnitType.MINUTE, 5));
        }
    }

}

    /**
 * @param timeUnit
 * @return
 */
private DateTickUnitType getTickUnitType(TimeUnitType timeUnit) {
    DateTickUnitType unitType = null;
    switch (timeUnit) {
    case HOUR:
        unitType = DateTickUnitType.HOUR;
    break;
    case DAY:
        unitType = DateTickUnitType.DAY;
    break;
    case MONTH:
        unitType = DateTickUnitType.MONTH;
    break;
    default:
    break;
    }
    return unitType;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2025-01-14 18:07:38

很难找到您发布的内容的问题,因为还有其他参数可能会影响该行为。

例如,如果您使用的时间线排除了某些日期,则可能会解释为什么您看不到刻度。
例如,如果您使用 1 个月的刻度单位并使用排除星期日的时间线,则从星期日开始的所有月份都将没有有刻度。
您可以尝试使用包含所有日期的时间线来查看问题是否仍然发生。

编辑

根据您的评论,您尝试将日期范围为几个小时的图表的刻度单位设置为 4 天。我不确定这样的 seutp 的预期行为是什么。如果我在这样的图表上复制这些参数,我也不会在日期轴上看到任何刻度。如果我将刻度单位设置为 10 分钟(例如),我可以再次看到刻度。

我认为结论是您的刻度单位必须足够细化,以便图表上至少有 1 个刻度以确保其可见(换句话说,2 个刻度之间的持续时间必须小于日期范围)。

如果您想对域轴进行更多控制,您可能更愿意使用自定义 NumberAxis 而不是 DateAxis(请参阅 此处 例如)。

It is difficult to find the problem with what you posted as there are other parameters that might affect that behavior.

For example, if you are using a Timeline that excludes some dates, it may explain why you don't see the ticks.
For example, if you use a tickunit of 1 month and use a timeline that excludes sunday, all the month that started on a Sunday will not have a tick.
You can try using a timeline that includes all the dates to see if the problem still happens.

EDIT

Based on your comments, you are trying to set the tickunit to 4 days with a chart that has a date range of a few hours. I am not sure what is the expected behavior of such a seutp. If I replicate those parameters on such a chart, I don't see any ticks on the date axis either. If I setup the tickunit to 10 minutes instead (for example) I can see the ticks again.

I suppose the conclusion is that your tickunit has to be granular enough so that there is at least 1 tick on your chart to make sure it is seen (in other words, the duration between 2 ticks has to be less than the date range).

If you want more control on the domain axis, you might prefer to use a custom NumberAxis instead of a DateAxis (see here for example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文