user414977
user414977

Reputation: 275

jfreechart need to customize Y axis just for printing

I have generated a graph which is perfect except the Y axis is in seconds as shown in the figure.

I do not want to change the tick unit or range, I just want to print out in HH:mm:ss formant

for example

7500 seconds = 02:05:00

Not sure if I could modify the 7500 second value to HH:mm:ss value while plotting the graph

I tried both JFreeChart - SymbolAxis - NumberAxis but couldn't find a workaround. Can anybody point me to the correct API or similar question please.

enter image description here

Upvotes: 0

Views: 1222

Answers (2)

David Gilbert
David Gilbert

Reputation: 4477

You can use the setNumberFormatOverride() method in the NumberAxis class to provide a custom formatter. The only catch is that you need a NumberFormat subclass that can convert numbers representing seconds to the format HH:MM:SS. I don't know if there is an existing one, but it is not hard to write your own. Here is my first attempt, it seems to work but will need some testing (I might include this in the next JFreeChart release):

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
    }

}

Upvotes: 1

Jannis Alexakis
Jannis Alexakis

Reputation: 1319

It´s difficult to help without your code, but I suspect you need to create a StandardXYItemRenderer for your XYPlot, then create a StandardXYTooltipGenerator that suits you needs. Here´s a copy-paste from one of my charts where i had to manipulate the tooltips for the X-Axis. Maybe it helps :

 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;

}

Upvotes: 0

Related Questions