0

我已经生成了一个完美的图表,除了 Y 轴以秒为单位,如图所示。

我不想更改刻度单位或范围,我只想以 HH:mm:ss 共振峰打印

例如

7500 秒 = 02:05:00

不确定在绘制图形时是否可以将 7500 秒值修改为 HH:mm:ss 值

我尝试了 JFreeChart - SymbolAxis - NumberAxis 但找不到解决方法。谁能指出我正确的 API 或类似的问题。

在此处输入图像描述

4

2 回答 2

1

您可以使用 NumberAxis 类中的 setNumberFormatOverride() 方法来提供自定义格式化程序。唯一的问题是您需要一个 NumberFormat 子类,它可以将表示秒的数字转换为 HH:MM:SS 格式。不知道有没有现成的,但是自己写也不难。这是我的第一次尝试,它似乎可以工作,但需要进行一些测试(我可能会在下一个 JFreeChart 版本中包含它):

package org.jfree.chart.util;

import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;

/**
 * A custom number formatter that formats numbers (in seconds) as HH:MM:SS.
 *
 * @since 1.0.17
 */
public class HMSNumberFormat extends NumberFormat {

    private NumberFormat hourFormatter = new DecimalFormat("00");
    private NumberFormat minuteFormatter = new DecimalFormat("00");
    private NumberFormat secondFormatter = new DecimalFormat("00");

    /**
     * Creates a new instance.
     */
    public HMSNumberFormat() {

    }

    /**
     * Formats the specified number as a string of the form HH:MM:SS.  The 
     * decimal fraction is ignored.
     *
     * @param number  the number to format.
     * @param toAppendTo  the buffer to append to (ignored here).
     * @param pos  the field position (ignored here).
     *
     * @return The string buffer.
     */
    @Override
    public StringBuffer format(double number, StringBuffer toAppendTo,
            FieldPosition pos) {
        return format((long) number, toAppendTo, pos);
    }

    /**
     * Formats the specified number as a string of the form HH:MM:SS.
     *
     * @param number  the number to format.
     * @param toAppendTo  the buffer to append to (ignored here).
     * @param pos  the field position (ignored here).
     *
     * @return The string buffer.
     */
    @Override
    public StringBuffer format(long number, StringBuffer toAppendTo,
            FieldPosition pos) {
        StringBuffer sb = new StringBuffer();
        long hours = number / 3600;
        sb.append(this.hourFormatter.format(hours)).append(":");
        long remaining = number - (hours * 3600);
        long minutes = remaining / 60;
        sb.append(this.minuteFormatter.format(minutes)).append(":");
        long seconds = remaining - (minutes * 60);
        sb.append(this.secondFormatter.format(seconds));
        return sb;
    }

    /**
     * Parsing is not implemented, so this method always returns
     * <code>null</code>.
     *
     * @param source  ignored.
     * @param parsePosition  ignored.
     *
     * @return Always <code>null</code>.
     */
    @Override
    public Number parse (String source, ParsePosition parsePosition) {
        return null; // don't bother with parsing
    }

}
于 2013-09-28T05:51:08.707 回答
0

没有您的代码很难提供帮助,但我怀疑您需要为您的 XYPlot 创建一个 StandardXYItemRenderer,然后创建一个适合您需要的 StandardXYTooltipGenerator。这是我的一张图表的复制粘贴,我必须在其中操作 X 轴的工具提示。也许它有帮助:

 public JFreeChart createChart() {

    String title = "MyChart";

    IntervalXYDataset dataset1;
    dataset1 = createDataset();
    XYBarRenderer renderer1 = new XYBarRenderer(0.20000000000000001D);
    renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0}: ({1}, {2})", new SimpleDateFormat("EE, d-MMM-yyyy"), new DecimalFormat("0.00")));
    renderer1.setSeriesPaint(0, Color.BLUE);
    renderer1.setDefaultShadowsVisible(false);
    DateAxis domainAxis = new DateAxis("Datum");
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    NumberAxis valueAxis = new NumberAxis("Anzahl");
    XYPlot plot = new XYPlot(dataset1, domainAxis, valueAxis, renderer1);
    XYDataset dataset2 = createBettenDataset();
    StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
    renderer2.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0}: ({1}, {2})", new SimpleDateFormat("EE, d-MMM-yyyy"), new DecimalFormat("0.00")));
    renderer2.setSeriesPaint(0, Color.CYAN);
    renderer2.setSeriesStroke(0, new BasicStroke(2));
    plot.setDataset(1, dataset2);
    plot.setRenderer(1, renderer2);
    XYDataset dataset3 = createMaxBelegungDataset();
    StandardXYItemRenderer renderer3 = new StandardXYItemRenderer();
    renderer3.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0}: ({1}, {2})", new SimpleDateFormat("EE, d-MMM-yyyy"), new DecimalFormat("0.00")));
    renderer3.setSeriesPaint(0, Color.RED);
    renderer3.setSeriesStroke(0, new BasicStroke(4));
    plot.setDataset(2, dataset3);
    plot.setRenderer(2, renderer3);

    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

    JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);

    return chart;

}
于 2013-09-26T14:47:12.437 回答