Example usage for org.jfree.chart.axis NumberAxis getTickUnit

List of usage examples for org.jfree.chart.axis NumberAxis getTickUnit

Introduction

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

Prototype

public NumberTickUnit getTickUnit() 

Source Link

Document

Returns the tick unit for the axis.

Usage

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
 *///w w w .  j a va2s  . c  o m
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.spring.GenericChartTheme.java

/**
 * For a given axis, adjust the tick unit size, in order to 
 * have a customizable number of ticks on that axis
 *//*www .  j a  v  a 2 s . c  o 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));
    //         }
    //      }
}