jfreechart需要自定义Y轴才能进行打印

时间:2013-09-26 12:54:46

标签: java jfreechart

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

我不想更改刻度单位或范围,我只想打印出HH:mm:ss formant

例如

  

7500秒= 02:05:00

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

我尝试了两个JFreeChart - SymbolAxis - NumberAxis但找不到解决方法。任何人都可以指出我正确的API或类似的问题。

enter image description here

2 个答案:

答案 0 :(得分: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
    }

}

答案 1 :(得分: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;

}