Example usage for org.jfree.chart.axis NumberTickUnit NumberTickUnit

List of usage examples for org.jfree.chart.axis NumberTickUnit NumberTickUnit

Introduction

In this page you can find the example usage for org.jfree.chart.axis NumberTickUnit NumberTickUnit.

Prototype

public NumberTickUnit(double size, NumberFormat formatter) 

Source Link

Document

Creates a new number tick unit.

Usage

From source file:org.pentaho.plugin.jfreereport.reportcharts.CategoricalChartExpression.java

protected void configureRangeAxis(final CategoryPlot cpl, final Font labelFont) {
    final ValueAxis rangeAxis = cpl.getRangeAxis();
    if (rangeAxis instanceof NumberAxis) {
        final NumberAxis numberAxis = (NumberAxis) rangeAxis;
        numberAxis.setAutoRangeIncludesZero(isRangeIncludesZero());
        numberAxis.setAutoRangeStickyZero(isRangeStickyZero());

        if (getRangePeriodCount() > 0) {
            if (getRangeTickFormat() != null) {
                numberAxis.setTickUnit(new NumberTickUnit(getRangePeriodCount(), getRangeTickFormat()));
            } else if (getRangeTickFormatString() != null) {
                final FastDecimalFormat formatter = new FastDecimalFormat(getRangeTickFormatString(),
                        getResourceBundleFactory().getLocale());
                numberAxis.setTickUnit(new FastNumberTickUnit(getRangePeriodCount(), formatter));
            } else {
                numberAxis.setTickUnit(new FastNumberTickUnit(getRangePeriodCount()));
            }//from  w  w  w .  j av a2  s.c om
        } else {
            if (getRangeTickFormat() != null) {
                numberAxis.setNumberFormatOverride(getRangeTickFormat());
            } else if (getRangeTickFormatString() != null) {
                final DecimalFormat formatter = new DecimalFormat(getRangeTickFormatString(),
                        new DecimalFormatSymbols(getResourceBundleFactory().getLocale()));
                numberAxis.setNumberFormatOverride(formatter);
                standardTickUnitsApplyFormat(numberAxis, formatter);
            }
        }
    } else if (rangeAxis instanceof DateAxis) {
        final DateAxis numberAxis = (DateAxis) rangeAxis;

        if (getRangePeriodCount() > 0 && getRangeTimePeriod() != null) {
            if (getRangeTickFormatString() != null) {
                final SimpleDateFormat formatter = new SimpleDateFormat(getRangeTickFormatString(),
                        new DateFormatSymbols(getResourceBundleFactory().getLocale()));
                numberAxis.setTickUnit(new DateTickUnit(getDateUnitAsInt(getRangeTimePeriod()),
                        (int) getRangePeriodCount(), formatter));
            } else {
                numberAxis.setTickUnit(
                        new DateTickUnit(getDateUnitAsInt(getRangeTimePeriod()), (int) getRangePeriodCount()));
            }
        } else if (getRangeTickFormatString() != null) {
            final SimpleDateFormat formatter = new SimpleDateFormat(getRangeTickFormatString(),
                    new DateFormatSymbols(getResourceBundleFactory().getLocale()));
            numberAxis.setDateFormatOverride(formatter);
        }

    }

    if (rangeAxis != null) {
        rangeAxis.setLabelFont(labelFont);
        rangeAxis.setTickLabelFont(labelFont);

        if (getRangeTitleFont() != null) {
            rangeAxis.setLabelFont(getRangeTitleFont());
        }
        if (getRangeTickFont() != null) {
            rangeAxis.setTickLabelFont(getRangeTickFont());
        }
        final int level = getRuntime().getProcessingContext().getCompatibilityLevel();
        if (ClassicEngineBoot.isEnforceCompatibilityFor(level, 3, 8)) {
            if (getRangeMinimum() != 0) {
                rangeAxis.setLowerBound(getRangeMinimum());
            }
            if (getRangeMaximum() != 1) {
                rangeAxis.setUpperBound(getRangeMaximum());
            }
            if (getRangeMinimum() == 0 && getRangeMaximum() == 0) {
                rangeAxis.setAutoRange(true);
            }
        } else {
            if (isAutoRange()) {
                rangeAxis.setAutoRange(isAutoRange());
            } else {
                double factor = getScaleFactor();
                if (factor > DEFAULT_SCALE_FACTOR) {
                    // PRD-5340 hack
                    // this method is invoked after all series were populated
                    // hence the axis already has the graph's max and min values;
                    double lower = rangeAxis.getLowerBound();
                    if (lower < 0) {
                        lower *= factor;
                    } else if (lower > 0) {
                        lower /= factor;
                    }

                    double upper = rangeAxis.getUpperBound();
                    if (upper > 0) {
                        upper *= factor;
                    } else if (upper < 0) {
                        upper /= factor;
                    }
                    rangeAxis.setRange(lower, upper);
                } else {
                    // the 'scaleFactor' property is left intact or has an incorrect value
                    rangeAxis.setUpperBound(getRangeMaximum());
                    rangeAxis.setLowerBound(getRangeMinimum());
                }
            }
        }
    }
}

From source file:net.sf.jasperreports.engine.fill.DefaultChartTheme.java

/**
 * For a given axis, adjust the tick unit size, in order to 
 * have a customizable number of ticks on that axis
 *//*from  w ww  .ja v a2  s.  co  m*/
protected void calculateTickUnits(Axis axis, boolean isRangeAxis) {
    Integer tickCount = null;
    Number tickInterval = null;
    boolean axisIntegerUnit = false;

    if (getChart().hasProperties()) {
        String tickCountProperty = null;
        String tickIntervalProperty = null;
        String axisIntegerUnitProperty = null;

        if (isRangeAxis) {
            tickCountProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_TICK_COUNT);
            tickIntervalProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_TICK_INTERVAL);
            axisIntegerUnitProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_INTEGER_UNIT);
        } else {
            tickCountProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_TICK_COUNT);
            tickIntervalProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_TICK_INTERVAL);
            axisIntegerUnitProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_INTEGER_UNIT);
        }
        if (tickCountProperty != null && tickCountProperty.trim().length() > 0) {
            tickCount = Integer.valueOf(tickCountProperty);
        }
        if (tickIntervalProperty != null && tickIntervalProperty.trim().length() > 0) {
            tickInterval = Double.valueOf(tickIntervalProperty);
        }
        if (axisIntegerUnitProperty != null && axisIntegerUnitProperty.trim().length() > 0) {
            axisIntegerUnit = Boolean.valueOf(axisIntegerUnitProperty);
        }
    }

    if (axis instanceof NumberAxis) {
        NumberAxis numberAxis = (NumberAxis) axis;
        int axisRange = (int) numberAxis.getRange().getLength();

        if (axisIntegerUnit) {
            ChartUtil chartUtil = ChartUtil.getInstance(chartContext.getJasperReportsContext());
            numberAxis.setStandardTickUnits(chartUtil.createIntegerTickUnits(getLocale()));
            chartUtil.setAutoTickUnit(numberAxis);
        } else if (axisRange > 0) {
            if (tickInterval != null) {
                if (numberAxis.getNumberFormatOverride() != null) {
                    numberAxis.setTickUnit(new NumberTickUnit(tickInterval.doubleValue(),
                            numberAxis.getNumberFormatOverride()));
                } else {
                    numberAxis.setTickUnit(new NumberTickUnit(tickInterval.doubleValue(),
                            NumberFormat.getNumberInstance(getLocale())));
                }
            } else if (tickCount != null) {
                if (numberAxis.getNumberFormatOverride() != null) {
                    numberAxis.setTickUnit(
                            new NumberTickUnit(axisRange / tickCount, numberAxis.getNumberFormatOverride()));
                } else {
                    numberAxis.setTickUnit(new NumberTickUnit(axisRange / tickCount,
                            NumberFormat.getNumberInstance(getLocale())));
                }
            } else {
                ChartUtil chartUtil = ChartUtil.getInstance(chartContext.getJasperReportsContext());
                numberAxis.setStandardTickUnits(chartUtil.createStandardTickUnits(getLocale()));
                chartUtil.setAutoTickUnit(numberAxis);
            }
        }
    }
    //      else if (axis instanceof DateAxis)
    //      {
    //         DateAxis dateAxis = (DateAxis)axis;
    //         int axisRange = (int)dateAxis.getRange().getLength();
    //         if (dateAxis.getDateFormatOverride() != null)
    //         {
    //            dateAxis.setTickUnit(new DateTickUnit(timePeriodUnit, axisRange/tickCount, dateAxis.getDateFormatOverride()));
    //         }
    //         else
    //         {
    //            dateAxis.setTickUnit(new DateTickUnit(timePeriodUnit, axisRange/tickCount));
    //         }
    //      }
}

From source file:com.afunms.system.manage.equipManager.java

/**
 * Creates a sample chart.//from   w  w w .  j av  a2 s  . co m
 * 
 * @return a sample chart.
 */
private JFreeChart createChart() {
    final XYDataset direction = createDirectionDataset(600);
    final JFreeChart chart = ChartFactory.createTimeSeriesChart("", "", "", direction,
            true, true, false);

    final XYPlot plot = chart.getXYPlot();
    plot.getDomainAxis().setLowerMargin(0.0);
    plot.getDomainAxis().setUpperMargin(0.0);
    plot.setRangeCrosshairVisible(true);
    plot.setDomainCrosshairVisible(true);
    plot.setBackgroundPaint(Color.WHITE);
    plot.setForegroundAlpha(0.8f);
    plot.setRangeGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.darkGray);
    plot.setDomainGridlinesVisible(true);
    plot.setDomainGridlinePaint(new Color(139, 69, 19));
    XYLineAndShapeRenderer render0 = (XYLineAndShapeRenderer) plot.getRenderer(0);
    render0.setSeriesPaint(0, Color.BLUE);

    XYAreaRenderer xyarearenderer = new XYAreaRenderer();
    xyarearenderer.setSeriesPaint(1, Color.GREEN); // 
    xyarearenderer.setSeriesFillPaint(1, Color.GREEN);
    xyarearenderer.setPaint(Color.GREEN);

    // configure the range axis to display directions...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setAutoRangeIncludesZero(false);
    final TickUnits units = new TickUnits();
    units.add(new NumberTickUnit(180.0, new CompassFormat()));
    units.add(new NumberTickUnit(90.0, new CompassFormat()));
    units.add(new NumberTickUnit(45.0, new CompassFormat()));
    units.add(new NumberTickUnit(22.5, new CompassFormat()));
    rangeAxis.setStandardTickUnits(units);

    // add the wind force with a secondary dataset/renderer/axis
    plot.setRangeAxis(rangeAxis);
    final XYItemRenderer renderer2 = new XYAreaRenderer();
    final ValueAxis axis2 = new NumberAxis("");
    axis2.setRange(0.0, 12.0);
    xyarearenderer.setSeriesPaint(1, new Color(0, 204, 0)); // 
    xyarearenderer.setSeriesFillPaint(1, Color.GREEN);
    xyarearenderer.setPaint(Color.GREEN);
    plot.setDataset(1, createForceDataset(600));
    plot.setRenderer(1, xyarearenderer);
    plot.setRangeAxis(1, axis2);
    plot.mapDatasetToRangeAxis(1, 1);

    return chart;
}

From source file:net.sf.fspdfs.chartthemes.simple.SimpleChartTheme.java

/**
 * For a given axis, adjust the tick unit size, in order to 
 * have a customizable number of ticks on that axis
 *//*from  w  w  w.j a  va2 s  . co  m*/
protected void calculateTickUnits(Axis axis, AxisSettings axisSettings, int timePeriodUnit) {
    Integer tickCount = null;
    Number tickInterval = null;

    if (getChart().hasProperties()) {
        String tickCountProperty = null;
        String tickIntervalProperty = null;
        if (axisSettings == getChartThemeSettings().getRangeAxisSettings()) {
            tickCountProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_TICK_COUNT);
            tickIntervalProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_TICK_INTERVAL);
        } else {
            tickCountProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_TICK_COUNT);
            tickIntervalProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_TICK_INTERVAL);
        }
        if (tickCountProperty != null && tickCountProperty.trim().length() > 0) {
            tickCount = Integer.valueOf(tickCountProperty);
        }
        if (tickIntervalProperty != null && tickIntervalProperty.trim().length() > 0) {
            tickInterval = Double.valueOf(tickIntervalProperty);
        }
    } else {
        tickCount = axisSettings.getTickCount();
        tickInterval = axisSettings.getTickInterval();
    }

    if (tickInterval == null && tickCount == null) {
        return;
    }

    if (axis instanceof NumberAxis) {
        NumberAxis numberAxis = (NumberAxis) axis;
        int axisRange = (int) numberAxis.getRange().getLength();
        if (axisRange > 0) {
            if (tickInterval != null) {
                if (numberAxis.getNumberFormatOverride() != null) {
                    numberAxis.setTickUnit(new NumberTickUnit(tickInterval.doubleValue(),
                            numberAxis.getNumberFormatOverride()));
                } else {
                    numberAxis.setTickUnit(new NumberTickUnit(tickInterval.doubleValue()));
                }
            } else if (tickCount != null) {
                if (numberAxis.getNumberFormatOverride() != null) {
                    numberAxis.setTickUnit(new NumberTickUnit(axisRange / tickCount.intValue(),
                            numberAxis.getNumberFormatOverride()));
                } else {
                    numberAxis.setTickUnit(new NumberTickUnit(axisRange / tickCount.intValue()));
                }
            }
        }
    }
    //      else if(axis instanceof DateAxis)
    //      {
    //         DateAxis dateAxis = (DateAxis)axis;
    //         int axisRange = (int)dateAxis.getRange().getLength();
    //         if(dateAxis.getDateFormatOverride() != null)
    //         {
    //            dateAxis.setTickUnit(new DateTickUnit(timePeriodUnit, axisRange/tickCount, dateAxis.getDateFormatOverride()));
    //         }
    //         else
    //         {
    //            dateAxis.setTickUnit(new DateTickUnit(timePeriodUnit, axisRange/tickCount));
    //         }
    //      }
}

From source file:net.sf.fspdfs.chartthemes.spring.GenericChartTheme.java

/**
 * For a given axis, adjust the tick unit size, in order to 
 * have a customizable number of ticks on that axis
 *//*from   w w w.j a v  a  2  s  . c  om*/
protected void calculateTickUnits(Axis axis, boolean isRangeAxis, String timePeriodUnit) {
    Integer tickCount = null;
    Number tickInterval = null;

    if (getChart().hasProperties()) {
        String tickCountProperty = null;
        String tickIntervalProperty = null;
        if (isRangeAxis) {
            tickCountProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_TICK_COUNT);
            tickIntervalProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_TICK_INTERVAL);
        } else {
            tickCountProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_TICK_COUNT);
            tickIntervalProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_TICK_INTERVAL);
        }
        if (tickCountProperty != null && tickCountProperty.trim().length() > 0) {
            tickCount = Integer.valueOf(tickCountProperty);
        }
        if (tickIntervalProperty != null && tickIntervalProperty.trim().length() > 0) {
            tickInterval = Double.valueOf(tickIntervalProperty);
        }
    } else {
        if (isRangeAxis) {
            tickCount = (Integer) getDefaultValue(defaultAxisPropertiesMap,
                    ChartThemesConstants.RANGE_AXIS_TICK_COUNT);
            tickInterval = (Number) getDefaultValue(defaultAxisPropertiesMap,
                    ChartThemesConstants.RANGE_AXIS_TICK_INTERVAL);
        } else {
            tickCount = (Integer) getDefaultValue(defaultAxisPropertiesMap,
                    ChartThemesConstants.DOMAIN_AXIS_TICK_COUNT);
            tickInterval = (Number) getDefaultValue(defaultAxisPropertiesMap,
                    ChartThemesConstants.DOMAIN_AXIS_TICK_INTERVAL);
        }
    }

    if (tickInterval == null && tickCount == null) {
        return;
    }
    //      if(axis instanceof NumberAxis)
    //      {
    //         NumberAxis numberAxis = (NumberAxis)axis;
    //         int axisRange = (int)numberAxis.getRange().getLength();
    //         if(numberAxis.getNumberFormatOverride() != null)
    //         {
    //            if(tickInterval != null && tickInterval.length() > 0)
    //               numberAxis.setTickUnit(new NumberTickUnit(Double.valueOf(tickInterval).doubleValue(), numberAxis.getNumberFormatOverride()));
    //            else
    //               numberAxis.setTickUnit(new NumberTickUnit( axisRange/tickCount, numberAxis.getNumberFormatOverride()));
    //         }
    //         else
    //         {
    //            if(tickInterval != null && tickInterval.length() > 0)
    //               numberAxis.setTickUnit(new NumberTickUnit(Double.valueOf(tickInterval).doubleValue()));
    //            else
    //               numberAxis.setTickUnit(new NumberTickUnit(axisRange/tickCount));
    //         }
    //      }
    if (axis instanceof NumberAxis) {
        NumberAxis numberAxis = (NumberAxis) axis;
        int axisRange = (int) numberAxis.getRange().getLength();
        if (axisRange > 0) {
            if (tickInterval != null) {
                if (numberAxis.getNumberFormatOverride() != null) {
                    numberAxis.setTickUnit(new NumberTickUnit(tickInterval.doubleValue(),
                            numberAxis.getNumberFormatOverride()));
                } else {
                    numberAxis.setTickUnit(new NumberTickUnit(tickInterval.doubleValue()));
                }
            } else if (tickCount != null) {
                int newTickUnitSize = axisRange / tickCount.intValue();
                if (newTickUnitSize > numberAxis.getTickUnit().getSize()) {
                    int tickUnitSize = newTickUnitSize;

                    //preferably multiple of 5 values should be used as tick units lengths:
                    int i = 1;
                    while (tickUnitSize > 9) {
                        tickUnitSize /= 10;
                        i *= 10;
                    }
                    tickUnitSize *= i;
                    newTickUnitSize = tickUnitSize + i / 2;

                    if (newTickUnitSize > 0 && axisRange / newTickUnitSize > tickCount.intValue()) {
                        newTickUnitSize += i / 2;
                    }
                    if (numberAxis.getNumberFormatOverride() != null) {
                        numberAxis.setTickUnit(
                                new NumberTickUnit(newTickUnitSize, numberAxis.getNumberFormatOverride()));
                    } else {
                        numberAxis.setTickUnit(new NumberTickUnit(newTickUnitSize));
                    }
                }
            }
        }
    }
    //      else if(axis instanceof DateAxis)
    //      {
    //         DateAxis dateAxis = (DateAxis)axis;
    //         int axisRange = (int)dateAxis.getRange().getLength();
    //         int timeUnit = getTimePeriodUnit(timePeriodUnit);
    //         
    //         if(dateAxis.getDateFormatOverride() != null)
    //         {
    //            if(tickInterval != null && tickInterval.length() > 0)
    //               dateAxis.setTickUnit(new DateTickUnit(timeUnit, Integer.valueOf(tickInterval).intValue(), dateAxis.getDateFormatOverride()));
    //            else
    //               dateAxis.setTickUnit(new DateTickUnit(timeUnit, axisRange/tickCount, dateAxis.getDateFormatOverride()));
    //         }
    //         else
    //         {
    //            if(tickInterval != null && tickInterval.length() > 0)
    //               dateAxis.setTickUnit(new DateTickUnit(timeUnit, Integer.valueOf(tickInterval).intValue()));
    //            else
    //               dateAxis.setTickUnit(new DateTickUnit(timeUnit, axisRange/tickCount));
    //         }
    //      }
}

From source file:net.sf.jasperreports.chartthemes.simple.SimpleChartTheme.java

/**
 * For a given axis, adjust the tick unit size, in order to 
 * have a customizable number of ticks on that axis
 *///from w  w  w .  jav a 2 s.co  m
protected void calculateTickUnits(Axis axis, AxisSettings axisSettings, DateTickUnitType timePeriodUnitx) {
    Integer tickCount = null;
    Number tickInterval = null;
    boolean axisIntegerUnit = false;

    if (getChart().hasProperties()) {
        String tickCountProperty = null;
        String tickIntervalProperty = null;
        String axisIntegerUnitProperty = null;

        if (axisSettings == getChartThemeSettings().getRangeAxisSettings()) {
            tickCountProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_TICK_COUNT);
            tickIntervalProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_TICK_INTERVAL);
            axisIntegerUnitProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_INTEGER_UNIT);
        } else {
            tickCountProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_TICK_COUNT);
            tickIntervalProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_TICK_INTERVAL);
            axisIntegerUnitProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_INTEGER_UNIT);
        }
        if (tickCountProperty != null && tickCountProperty.trim().length() > 0) {
            tickCount = Integer.valueOf(tickCountProperty);
        }
        if (tickIntervalProperty != null && tickIntervalProperty.trim().length() > 0) {
            tickInterval = Double.valueOf(tickIntervalProperty);
        }
        if (axisIntegerUnitProperty != null && axisIntegerUnitProperty.trim().length() > 0) {
            axisIntegerUnit = Boolean.valueOf(axisIntegerUnitProperty);
        }
    } else {
        tickCount = axisSettings.getTickCount();
        tickInterval = axisSettings.getTickInterval();
        if (axisSettings.getAxisIntegerUnit() != null) {
            axisIntegerUnit = axisSettings.getAxisIntegerUnit();
        }
    }

    if (axis instanceof NumberAxis) {
        NumberAxis numberAxis = (NumberAxis) axis;
        int axisRange = (int) numberAxis.getRange().getLength();
        if (axisIntegerUnit) {
            ChartUtil chartUtil = ChartUtil.getInstance(getChartContext().getJasperReportsContext());
            numberAxis.setStandardTickUnits(chartUtil.createIntegerTickUnits(getLocale()));
            chartUtil.setAutoTickUnit(numberAxis);
        } else if (axisRange > 0) {
            if (tickInterval != null) {
                if (numberAxis.getNumberFormatOverride() != null) {
                    numberAxis.setTickUnit(new NumberTickUnit(tickInterval.doubleValue(),
                            numberAxis.getNumberFormatOverride()));
                } else {
                    numberAxis.setTickUnit(new NumberTickUnit(tickInterval.doubleValue(),
                            NumberFormat.getNumberInstance(getLocale())));
                }
            } else if (tickCount != null) {
                if (numberAxis.getNumberFormatOverride() != null) {
                    numberAxis.setTickUnit(
                            new NumberTickUnit(axisRange / tickCount, numberAxis.getNumberFormatOverride()));
                } else {
                    numberAxis.setTickUnit(new NumberTickUnit(axisRange / tickCount,
                            NumberFormat.getNumberInstance(getLocale())));
                }
            } else {
                ChartUtil chartUtil = ChartUtil.getInstance(getChartContext().getJasperReportsContext());
                numberAxis.setStandardTickUnits(chartUtil.createStandardTickUnits(getLocale()));
                chartUtil.setAutoTickUnit(numberAxis);
            }
        }
    }
    //      else if (axis instanceof DateAxis)
    //      {
    //         DateAxis dateAxis = (DateAxis)axis;
    //         int axisRange = (int)dateAxis.getRange().getLength();
    //         if (dateAxis.getDateFormatOverride() != null)
    //         {
    //            dateAxis.setTickUnit(new DateTickUnit(timePeriodUnit, axisRange/tickCount, dateAxis.getDateFormatOverride()));
    //         }
    //         else
    //         {
    //            dateAxis.setTickUnit(new DateTickUnit(timePeriodUnit, axisRange/tickCount));
    //         }
    //      }
}

From source file:net.sf.jasperreports.chartthemes.spring.GenericChartTheme.java

/**
 * For a given axis, adjust the tick unit size, in order to 
 * have a customizable number of ticks on that axis
 *//*from   ww w  .j  a  v a 2s. co m*/
protected void calculateTickUnits(Axis axis, boolean isRangeAxis, String timePeriodUnit) {
    Integer tickCount = null;
    Number tickInterval = null;
    boolean axisIntegerUnit = false;

    if (getChart().hasProperties()) {
        String tickCountProperty = null;
        String tickIntervalProperty = null;
        String axisIntegerUnitProperty = null;

        if (isRangeAxis) {
            tickCountProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_TICK_COUNT);
            tickIntervalProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_TICK_INTERVAL);
            axisIntegerUnitProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_RANGE_AXIS_INTEGER_UNIT);
        } else {
            tickCountProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_TICK_COUNT);
            tickIntervalProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_TICK_INTERVAL);
            axisIntegerUnitProperty = getChart().getPropertiesMap()
                    .getProperty(DefaultChartTheme.PROPERTY_DOMAIN_AXIS_INTEGER_UNIT);
        }
        if (tickCountProperty != null && tickCountProperty.trim().length() > 0) {
            tickCount = Integer.valueOf(tickCountProperty);
        }
        if (tickIntervalProperty != null && tickIntervalProperty.trim().length() > 0) {
            tickInterval = Double.valueOf(tickIntervalProperty);
        }
        if (axisIntegerUnitProperty != null && axisIntegerUnitProperty.trim().length() > 0) {
            axisIntegerUnit = Boolean.valueOf(axisIntegerUnitProperty);
        }
    } else {
        if (isRangeAxis) {
            tickCount = (Integer) getDefaultValue(defaultAxisPropertiesMap,
                    ChartThemesConstants.RANGE_AXIS_TICK_COUNT);
            tickInterval = (Number) getDefaultValue(defaultAxisPropertiesMap,
                    ChartThemesConstants.RANGE_AXIS_TICK_INTERVAL);
            if (getDefaultValue(defaultAxisPropertiesMap,
                    ChartThemesConstants.RANGE_AXIS_INTEGER_UNIT) != null) {
                axisIntegerUnit = (Boolean) getDefaultValue(defaultAxisPropertiesMap,
                        ChartThemesConstants.RANGE_AXIS_INTEGER_UNIT);
            }
        } else {
            tickCount = (Integer) getDefaultValue(defaultAxisPropertiesMap,
                    ChartThemesConstants.DOMAIN_AXIS_TICK_COUNT);
            tickInterval = (Number) getDefaultValue(defaultAxisPropertiesMap,
                    ChartThemesConstants.DOMAIN_AXIS_TICK_INTERVAL);
            if (getDefaultValue(defaultAxisPropertiesMap,
                    ChartThemesConstants.DOMAIN_AXIS_INTEGER_UNIT) != null) {
                axisIntegerUnit = (Boolean) getDefaultValue(defaultAxisPropertiesMap,
                        ChartThemesConstants.DOMAIN_AXIS_INTEGER_UNIT);
            }
        }
    }

    //      if (axis instanceof NumberAxis)
    //      {
    //         NumberAxis numberAxis = (NumberAxis)axis;
    //         int axisRange = (int)numberAxis.getRange().getLength();
    //         if (numberAxis.getNumberFormatOverride() != null)
    //         {
    //            if (tickInterval != null && tickInterval.length() > 0)
    //               numberAxis.setTickUnit(new NumberTickUnit(Double.valueOf(tickInterval), numberAxis.getNumberFormatOverride()));
    //            else
    //               numberAxis.setTickUnit(new NumberTickUnit( axisRange/tickCount, numberAxis.getNumberFormatOverride()));
    //         }
    //         else
    //         {
    //            if (tickInterval != null && tickInterval.length() > 0)
    //               numberAxis.setTickUnit(new NumberTickUnit(Double.valueOf(tickInterval)));
    //            else
    //               numberAxis.setTickUnit(new NumberTickUnit(axisRange/tickCount));
    //         }
    //      }
    if (axis instanceof NumberAxis) {
        NumberAxis numberAxis = (NumberAxis) axis;
        int axisRange = (int) numberAxis.getRange().getLength();

        if (axisIntegerUnit) {
            ChartUtil chartUtil = ChartUtil.getInstance(getChartContext().getJasperReportsContext());
            numberAxis.setStandardTickUnits(chartUtil.createIntegerTickUnits(getLocale()));
            chartUtil.setAutoTickUnit(numberAxis);
        } else if (axisRange > 0) {
            if (tickInterval != null) {
                if (numberAxis.getNumberFormatOverride() != null) {
                    numberAxis.setTickUnit(new NumberTickUnit(tickInterval.doubleValue(),
                            numberAxis.getNumberFormatOverride()));
                } else {
                    numberAxis.setTickUnit(new NumberTickUnit(tickInterval.doubleValue(),
                            NumberFormat.getNumberInstance(getLocale())));
                }
            } else if (tickCount != null) {
                int newTickUnitSize = axisRange / tickCount;
                if (newTickUnitSize > numberAxis.getTickUnit().getSize()) {
                    int tickUnitSize = newTickUnitSize;

                    //preferably multiple of 5 values should be used as tick units lengths:
                    int i = 1;
                    while (tickUnitSize > 9) {
                        tickUnitSize /= 10;
                        i *= 10;
                    }
                    tickUnitSize *= i;
                    newTickUnitSize = tickUnitSize + i / 2;

                    if (newTickUnitSize > 0 && axisRange / newTickUnitSize > tickCount) {
                        newTickUnitSize += i / 2;
                    }
                    if (numberAxis.getNumberFormatOverride() != null) {
                        numberAxis.setTickUnit(
                                new NumberTickUnit(newTickUnitSize, numberAxis.getNumberFormatOverride()));
                    } else {
                        numberAxis.setTickUnit(new NumberTickUnit(newTickUnitSize,
                                NumberFormat.getNumberInstance(getLocale())));
                    }
                }
            } else {
                ChartUtil chartUtil = ChartUtil.getInstance(getChartContext().getJasperReportsContext());
                numberAxis.setStandardTickUnits(chartUtil.createStandardTickUnits(getLocale()));
                chartUtil.setAutoTickUnit(numberAxis);
            }
        }
    }
    //      else if (axis instanceof DateAxis)
    //      {
    //         DateAxis dateAxis = (DateAxis)axis;
    //         int axisRange = (int)dateAxis.getRange().getLength();
    //         int timeUnit = getTimePeriodUnit(timePeriodUnit);
    //         
    //         if (dateAxis.getDateFormatOverride() != null)
    //         {
    //            if (tickInterval != null && tickInterval.length() > 0)
    //               dateAxis.setTickUnit(new DateTickUnit(timeUnit, Integer.valueOf(tickInterval), dateAxis.getDateFormatOverride()));
    //            else
    //               dateAxis.setTickUnit(new DateTickUnit(timeUnit, axisRange/tickCount, dateAxis.getDateFormatOverride()));
    //         }
    //         else
    //         {
    //            if (tickInterval != null && tickInterval.length() > 0)
    //               dateAxis.setTickUnit(new DateTickUnit(timeUnit, Integer.valueOf(tickInterval)));
    //            else
    //               dateAxis.setTickUnit(new DateTickUnit(timeUnit, axisRange/tickCount));
    //         }
    //      }
}