Example usage for org.jfree.chart.axis DateAxis setTickUnit

List of usage examples for org.jfree.chart.axis DateAxis setTickUnit

Introduction

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

Prototype

public void setTickUnit(DateTickUnit unit, boolean notify, boolean turnOffAutoSelection) 

Source Link

Document

Sets the tick unit attribute and, if requested, sends an AxisChangeEvent to all registered listeners.

Usage

From source file:de.iteratec.visualizationmodel.jfreechart.AxisFactory.java

public DateAxis createDateAxis(String label) {

    DateAxis axis = new DateAxis(label);
    axis.setTickUnit(tickUnit == null ? defaultDateTickUnit : (DateTickUnit) tickUnit, false, false);
    axis.setDateFormatOverride(new SimpleDateFormat(MessageAccess.getString("DATE_FORMAT", locale), locale));
    return axis;/*  w  ww. j a v a2  s .com*/
}

From source file:daylightchart.daylightchart.chart.DaylightChart.java

@SuppressWarnings("deprecation")
private void createMonthsAxis(final XYPlot plot) {
    final DateAxis axis = new DateAxis();
    axis.setTickMarkPosition(DateTickMarkPosition.START);
    axis.setLowerMargin(0.0f);/*  w ww .j ava2s .co  m*/
    axis.setUpperMargin(0.0f);
    axis.setTickLabelFont(ChartConfiguration.chartFont.deriveFont(Font.PLAIN, 12));
    axis.setDateFormatOverride(ChartConfiguration.monthsFormat);
    axis.setVerticalTickLabels(true);
    axis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1), true, true);
    // Fix the axis range for all the months in the year
    final int dateYear = riseSetData.getYear() - 1900;
    axis.setRange(new Date(dateYear, 0, 1), new Date(dateYear, 11, 31));
    //
    plot.setDomainAxis(axis);
}

From source file:edu.jhuapl.graphs.jfreechart.JFreeChartTimeSeriesGraphSource.java

/**
 * Initializes the graph.  This method generates the backing {@link JFreeChart} from the time series and graph
 * parameter data./*  w  w  w. j  a  v a  2  s . com*/
 *
 * @throws GraphException if the initialization fails
 */
public void initialize() throws GraphException {
    String title = getParam(GraphSource.GRAPH_TITLE, String.class, DEFAULT_TITLE);
    String xLabel = getParam(GraphSource.GRAPH_X_LABEL, String.class, DEFAULT_DOMAIN_LABEL);
    String yLabel = getParam(GraphSource.GRAPH_Y_LABEL, String.class, DEFAULT_RANGE_LABEL);
    Shape graphShape = getParam(GraphSource.GRAPH_SHAPE, Shape.class, DEFAULT_GRAPH_SHAPE);
    Paint graphColor = getParam(GraphSource.GRAPH_COLOR, Paint.class, DEFAULT_GRAPH_COLOR);
    boolean legend = getParam(GraphSource.GRAPH_LEGEND, Boolean.class, DEFAULT_GRAPH_LEGEND);
    boolean graphToolTip = getParam(GraphSource.GRAPH_TOOL_TIP, Boolean.class, DEFAULT_GRAPH_TOOL_TIP);
    Stroke graphStroke = getParam(GraphSource.GRAPH_STROKE, Stroke.class, DEFAULT_GRAPH_STROKE);
    Font titleFont = getParam(GraphSource.GRAPH_FONT, Font.class, DEFAULT_GRAPH_TITLE_FONT);
    boolean graphBorder = getParam(GraphSource.GRAPH_BORDER, Boolean.class, DEFAULT_GRAPH_BORDER);
    boolean legendBorder = getParam(GraphSource.LEGEND_BORDER, Boolean.class, DEFAULT_LEGEND_BORDER);
    Double offset = getParam(GraphSource.AXIS_OFFSET, Double.class, DEFAULT_AXIS_OFFSET);

    checkSeriesType(data);
    @SuppressWarnings("unchecked")
    List<? extends TimeSeriesInterface> timeData = (List<? extends TimeSeriesInterface>) data;

    TimeSeriesCollection dataset = new TimeSeriesCollection();
    int seriesCount = 1;
    for (TimeSeriesInterface series : timeData) {
        dataset.addSeries(buildTimeSeries(series, seriesCount));
        seriesCount += 1;
    }

    // actually create the chart
    this.chart = ChartFactory.createTimeSeriesChart(title, xLabel, yLabel, dataset, false, graphToolTip, false);

    // start customizing it
    Paint backgroundColor = getParam(GraphSource.BACKGROUND_COLOR, Paint.class, DEFAULT_BACKGROUND_COLOR);
    Paint plotColor = getParam(JFreeChartTimeSeriesGraphSource.PLOT_COLOR, Paint.class, backgroundColor);
    Paint graphDomainGridlinePaint = getParam(GraphSource.GRAPH_DOMAIN_GRIDLINE_PAINT, Paint.class,
            backgroundColor);
    Paint graphRangeGridlinePaint = getParam(GraphSource.GRAPH_RANGE_GRIDLINE_PAINT, Paint.class,
            backgroundColor);

    this.chart.setBackgroundPaint(backgroundColor);
    XYPlot plot = chart.getXYPlot();
    plot.setBackgroundPaint(plotColor);
    plot.setAxisOffset(new RectangleInsets(offset, offset, offset, offset));
    plot.setDomainGridlinePaint(graphDomainGridlinePaint);
    plot.setRangeGridlinePaint(graphRangeGridlinePaint);

    if (graphBorder) {

    } else {
        plot.setOutlinePaint(null);
    }

    //Use a TextTitle to change the font of the graph title
    TextTitle title1 = new TextTitle();
    title1.setText(title);
    title1.setFont(titleFont);
    chart.setTitle(title1);

    //Makes a wrapper for the legend to remove the border around it
    if (legend) {
        LegendTitle legend1 = new LegendTitle(chart.getPlot());
        BlockContainer wrapper = new BlockContainer(new BorderArrangement());
        if (legendBorder) {
            wrapper.setFrame(new BlockBorder(1, 1, 1, 1));
        } else {
            wrapper.setFrame(new BlockBorder(0, 0, 0, 0));
        }
        BlockContainer items = legend1.getItemContainer();
        items.setPadding(2, 10, 5, 2);
        wrapper.add(items);
        legend1.setWrapper(wrapper);
        legend1.setPosition(RectangleEdge.BOTTOM);
        legend1.setHorizontalAlignment(HorizontalAlignment.CENTER);

        if (params.get(GraphSource.LEGEND_FONT) instanceof Font) {
            legend1.setItemFont(((Font) params.get(GraphSource.LEGEND_FONT)));
        }

        chart.addSubtitle(legend1);
    }

    boolean include0 = getParam(GraphSource.GRAPH_RANGE_INCLUDE_0, Boolean.class, true);
    NumberAxis numAxis = (NumberAxis) plot.getRangeAxis();
    double rangeLower = getParam(GraphSource.GRAPH_RANGE_LOWER_BOUND, Double.class, numAxis.getLowerBound());
    double rangeUpper = getParam(GraphSource.GRAPH_RANGE_UPPER_BOUND, Double.class, numAxis.getUpperBound());
    boolean graphRangeIntegerTick = getParam(GraphSource.GRAPH_RANGE_INTEGER_TICK, Boolean.class, false);
    boolean graphRangeMinorTickVisible = getParam(GraphSource.GRAPH_RANGE_MINOR_TICK_VISIBLE, Boolean.class,
            true);

    if (include0) {
        rangeLower = 0;
    }

    numAxis.setRange(rangeLower, rangeUpper);

    if (graphRangeIntegerTick) {
        numAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    }

    numAxis.setMinorTickMarksVisible(graphRangeMinorTickVisible);
    setupFont(numAxis, GraphSource.GRAPH_Y_AXIS_FONT);

    if (params.get(GraphSource.GRAPH_Y_AXIS_LABEL_FONT) instanceof Font) {
        numAxis.setLabelFont(((Font) params.get(GraphSource.GRAPH_Y_AXIS_LABEL_FONT)));
    }

    TimeResolution minimumResolution = getMinimumResolution(timeData);
    DateFormat dateFormat = getParam(GraphSource.GRAPH_DATE_FORMATTER, DateFormat.class,
            new DefaultDateFormatFactory().getFormat(minimumResolution));

    if (params.get(DATE_AXIS) instanceof DateAxis) {
        DateAxis dateAxis = (DateAxis) params.get(DATE_AXIS);
        dateAxis.setLabel(xLabel);
        plot.setDomainAxis(dateAxis);
    }
    DateAxis dateAxis = ((DateAxis) plot.getDomainAxis());
    dateAxis.setDateFormatOverride(dateFormat);

    if (params.get(GraphSource.GRAPH_X_AXIS_LABEL_FONT) instanceof Font) {
        dateAxis.setLabelFont(((Font) params.get(GraphSource.GRAPH_X_AXIS_LABEL_FONT)));
    }

    int minTick = getParam(GraphSource.GRAPH_MIN_DOMAIN_TICK, Integer.class, 1);
    if (minTick <= 0) {
        minTick = 1;
    }

    dateAxis.setTickUnit(getDateTickUnit(minimumResolution, minTick), false, false);
    //dateAxis.setMinorTickMarksVisible(true);
    //dateAxis.setMinorTickCount(7);
    dateAxis.setMinorTickMarkOutsideLength(2);

    Integer minorTick = getParam(GraphSource.GRAPH_MINOR_TICKS, Integer.class, null);
    if (minorTick != null) {
        int minorVal = minorTick;
        if (minorVal > 0) {
            dateAxis.setMinorTickCount(minorVal);
        }
    }

    setupFont(dateAxis, GraphSource.GRAPH_X_AXIS_FONT);

    //double lowerMargin = getParam(DOMAIN_AXIS_LOWER_MARGIN, Double.class, DEFAULT_DOMAIN_AXIS_LOWER_MARGIN);
    double lowerMargin = getParam(DOMAIN_AXIS_LOWER_MARGIN, Double.class, DEFAULT_DOMAIN_AXIS_LOWER_MARGIN);
    dateAxis.setLowerMargin(lowerMargin);

    //double upperMargin = getParam(DOMAIN_AXIS_UPPER_MARGIN, Double.class, DEFAULT_DOMAIN_AXIS_UPPER_MARGIN);
    double upperMargin = getParam(DOMAIN_AXIS_UPPER_MARGIN, Double.class, DEFAULT_DOMAIN_AXIS_UPPER_MARGIN);
    dateAxis.setUpperMargin(upperMargin);

    Date domainLower = getParam(GraphSource.GRAPH_DOMAIN_LOWER_BOUND, Date.class, dateAxis.getMinimumDate());
    Date domainUpper = getParam(GraphSource.GRAPH_DOMAIN_UPPER_BOUND, Date.class, dateAxis.getMaximumDate());

    dateAxis.setRange(domainLower, domainUpper);

    // depending on the domain axis range, display either 1 tick per day, week, month or year
    TickUnits standardUnits = new TickUnits();
    standardUnits.add(new DateTickUnit(DateTickUnitType.DAY, 1));
    standardUnits.add(new DateTickUnit(DateTickUnitType.DAY, 7));
    standardUnits.add(new DateTickUnit(DateTickUnitType.MONTH, 1));
    standardUnits.add(new DateTickUnit(DateTickUnitType.YEAR, 1));
    dateAxis.setStandardTickUnits(standardUnits);

    TimeSeriesRenderer renderer = new TimeSeriesRenderer(dataset);
    setupRenderer(renderer, graphColor, graphShape, graphStroke);
    renderer.setBaseFillPaint(Color.BLACK);
    renderer.setSeriesOutlinePaint(0, Color.WHITE);

    //renderer.setUseOutlinePaint(true);

    plot.setRenderer(renderer);
    this.initialized = true;
}