Example usage for org.jfree.chart.title LegendTitle setWrapper

List of usage examples for org.jfree.chart.title LegendTitle setWrapper

Introduction

In this page you can find the example usage for org.jfree.chart.title LegendTitle setWrapper.

Prototype

public void setWrapper(BlockContainer wrapper) 

Source Link

Document

Sets the wrapper container for the legend.

Usage

From source file:org.gaixie.micrite.jfreechart.style.PieStyle.java

/**
 * ?2???block//w w  w .  j ava  2s .com
 * <p>
 * ? LegendTitle
 * @param chart JFreeChart
 */
public static void styleTwo(JFreeChart chart) {
    PiePlot plot = (PiePlot) chart.getPlot();

    PieStyle.setBackground(chart);

    // LegendTitleblockblock
    BlockContainer block = new BlockContainer(new BorderArrangement());
    block.setBorder(new BlockBorder(1.0D, 1.0D, 1.0D, 1.0D));

    // LegendTitleblock
    LegendTitle legend = new LegendTitle(plot);
    BlockContainer legendBlock = legend.getItemContainer();

    // legendBlockblock?
    block.add(legendBlock);
    legend.setWrapper(block);
    legend.setPosition(RectangleEdge.BOTTOM);

    chart.addSubtitle(legend);
    //label
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({2})"));

}

From source file:org.jfree.chart.demo.LegendWrapperDemo1.java

private static JFreeChart createChart(PieDataset piedataset) {
    JFreeChart jfreechart = ChartFactory.createPieChart("Legend Wrapper Demo 1", piedataset, false, true,
            false);//from  www. j  ava2  s .c  o m
    PiePlot pieplot = (PiePlot) jfreechart.getPlot();
    pieplot.setLabelFont(new Font("SansSerif", 0, 12));
    pieplot.setNoDataMessage("No data available");
    pieplot.setCircular(true);
    pieplot.setLabelGap(0.02D);
    LegendTitle legendtitle = new LegendTitle(jfreechart.getPlot());
    BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
    blockcontainer.setFrame(new BlockBorder(1.0D, 1.0D, 1.0D, 1.0D));
    LabelBlock labelblock = new LabelBlock("Legend Items:", new Font("SansSerif", 1, 12));
    labelblock.setPadding(5D, 5D, 5D, 5D);
    blockcontainer.add(labelblock, RectangleEdge.TOP);
    LabelBlock labelblock1 = new LabelBlock("Source: http://www.jfree.org");
    labelblock1.setPadding(8D, 20D, 2D, 5D);
    blockcontainer.add(labelblock1, RectangleEdge.BOTTOM);
    BlockContainer blockcontainer1 = legendtitle.getItemContainer();
    blockcontainer1.setPadding(2D, 10D, 5D, 2D);
    blockcontainer.add(blockcontainer1);
    legendtitle.setWrapper(blockcontainer);
    legendtitle.setPosition(RectangleEdge.RIGHT);
    legendtitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
    jfreechart.addSubtitle(legendtitle);
    return jfreechart;
}

From source file:org.codehaus.mojo.dashboard.report.plugin.chart.time.MarkerTimeChartDecorator.java

/**
 * Creates a legend item block./*from www .  ja v a 2s  .co  m*/
 * 
 * @param item
 *            the legend item.
 * 
 * @return The block.
 */
protected void createLegendBlock(BlockContainer blockcontainerLabel) {

    XYPlot xyplot = (XYPlot) report.getPlot();

    int nbRenderer = xyplot.getDatasetCount();
    BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
    if (nbRenderer > 1) {
        BlockContainer oldLegendBlockContainer = new BlockContainer(
                new FlowArrangement(HorizontalAlignment.LEFT, VerticalAlignment.TOP, 2.0D, 2.0D));
        for (int i = 0; i < nbRenderer; i++) {
            LegendTitle legendtitle = new LegendTitle(xyplot.getRenderer(i));
            legendtitle.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
            legendtitle.setFrame(new LineBorder());
            legendtitle.setBackgroundPaint(ChartColor.WHITE);
            oldLegendBlockContainer.add(legendtitle);
        }

        blockcontainer.add(oldLegendBlockContainer, RectangleEdge.LEFT);
    } else {
        LegendTitle legendtitle = new LegendTitle(xyplot.getRenderer(0));
        legendtitle.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
        legendtitle.setFrame(new LineBorder());
        legendtitle.setBackgroundPaint(ChartColor.WHITE);
        blockcontainer.add(legendtitle, RectangleEdge.LEFT);
    }

    LegendTitle legendtitle1 = new LegendTitle(xyplot.getRenderer());
    legendtitle1.setMargin(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
    legendtitle1.setFrame(new LineBorder());
    legendtitle1.setBackgroundPaint(ChartColor.WHITE);
    legendtitle1.setWrapper(blockcontainerLabel);

    blockcontainer.add(legendtitle1, RectangleEdge.RIGHT);
    blockcontainer.add(new EmptyBlock(1000D, 0.0D));

    CompositeTitle compositetitle = new CompositeTitle(blockcontainer);
    compositetitle.setPosition(RectangleEdge.BOTTOM);
    report.clearSubtitles();
    report.addSubtitle(compositetitle);
}

From source file:org.ash.history.TopActivityPreview.java

/**
 * Add legend to chart panel.//from ww w  . j a va2 s  . c om
 * 
 * @param fontSize
 */
public void addLegend(int fontSize) {

    LegendTitle legend = new LegendTitle(chart.getPlot());

    BlockContainer wrapper = new BlockContainer(new BorderArrangement());
    wrapper.setFrame(new BlockBorder(1.0, 1.0, 1.0, 1.0));

    BlockContainer itemss = legend.getItemContainer();
    itemss.setPadding(2, 10, 5, 2);
    wrapper.add(itemss);
    legend.setWrapper(wrapper);

    legend.setItemFont(new Font(LegendTitle.DEFAULT_ITEM_FONT.getFontName(),
            LegendTitle.DEFAULT_ITEM_FONT.getStyle(), fontSize));

    legend.setPosition(RectangleEdge.RIGHT);
    legend.setHorizontalAlignment(HorizontalAlignment.LEFT);

    chart.addSubtitle(legend);

}

From source file:org.ash.gui.StackedChart.java

/**
 * Creates the chart.//  w  ww.jav a  2 s  . c om
 * 
 * @return the j free chart
 */
private JFreeChart createChart() {

    xAxis = new DateAxis("time");
    xAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

    currentDate = new Date();
    updatexAxisLabel(new Long(currentDate.getTime()).doubleValue());

    chart = ChartFactory.createStackedXYAreaChart("Top activity", // chart title
            "X Value", // domain axis label
            "Active Sessions", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // the plot orientation
            xAxis, false, // legend
            true, // tooltips
            false // urls
    );

    plot = (XYPlot) chart.getPlot();
    StackedXYAreaRenderer3 renderer = new StackedXYAreaRenderer3();
    renderer.setRoundXCoordinates(true);
    renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0} ({1}, {2})",
            new SimpleDateFormat("HH:mm"), new DecimalFormat("0.0")));
    plot.setRenderer(0, renderer);
    plot.getRangeAxis().setLowerBound(0.0);
    plot.getRangeAxis().setAutoRange(true);

    // add a labelled marker for the cpu_count
    thresholdMaxCpu = new ValueMarker(this.maxCpu);
    thresholdMaxCpu.setLabelOffsetType(LengthAdjustmentType.EXPAND);
    thresholdMaxCpu.setPaint(Color.red);
    thresholdMaxCpu.setStroke(new BasicStroke(1.0f));
    thresholdMaxCpu.setLabel("Maximum CPU");
    thresholdMaxCpu.setLabelFont(new Font("SansSerif", Font.PLAIN, 11));
    thresholdMaxCpu.setLabelPaint(Color.red);
    thresholdMaxCpu.setLabelAnchor(RectangleAnchor.TOP_LEFT);
    thresholdMaxCpu.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
    plot.addRangeMarker(thresholdMaxCpu);

    renderer.setSeriesPaint(0,
            Options.getInstance().getColor(Options.getInstance().getResource("cpuLabel.text")), true);
    renderer.setSeriesPaint(1,
            Options.getInstance().getColor(Options.getInstance().getResource("schedulerLabel.text")), true);
    renderer.setSeriesPaint(2,
            Options.getInstance().getColor(Options.getInstance().getResource("userIOLabel.text")), true);
    renderer.setSeriesPaint(3,
            Options.getInstance().getColor(Options.getInstance().getResource("systemIOLabel.text")), true);
    renderer.setSeriesPaint(4,
            Options.getInstance().getColor(Options.getInstance().getResource("concurrencyLabel.text")), true);
    renderer.setSeriesPaint(5,
            Options.getInstance().getColor(Options.getInstance().getResource("applicationsLabel.text")), true);
    renderer.setSeriesPaint(6,
            Options.getInstance().getColor(Options.getInstance().getResource("commitLabel.text")), true);
    renderer.setSeriesPaint(7,
            Options.getInstance().getColor(Options.getInstance().getResource("configurationLabel.text")), true);
    renderer.setSeriesPaint(8,
            Options.getInstance().getColor(Options.getInstance().getResource("administrativeLabel.text")),
            true);
    renderer.setSeriesPaint(9,
            Options.getInstance().getColor(Options.getInstance().getResource("networkLabel.text")), true);
    renderer.setSeriesPaint(11,
            Options.getInstance().getColor(Options.getInstance().getResource("queueningLabel.text")), true);//que
    renderer.setSeriesPaint(10,
            Options.getInstance().getColor(Options.getInstance().getResource("clusterLabel.text")), true);//cluster
    renderer.setSeriesPaint(12,
            Options.getInstance().getColor(Options.getInstance().getResource("otherLabel.text")), true);

    // Set format for x axis
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));
    // Add legend to right
    LegendTitle legend = new LegendTitle(chart.getPlot());

    BlockContainer wrapper = new BlockContainer(new BorderArrangement());
    wrapper.setFrame(new BlockBorder(1.0, 1.0, 1.0, 1.0));

    BlockContainer itemss = legend.getItemContainer();
    itemss.setPadding(2, 10, 5, 2);
    wrapper.add(itemss);
    legend.setWrapper(wrapper);

    legend.setPosition(RectangleEdge.RIGHT);
    legend.setHorizontalAlignment(HorizontalAlignment.LEFT);

    chart.addSubtitle(legend);

    return chart;
}

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

@SuppressWarnings("unchecked")
@Override/*from  ww  w .j  a v  a 2 s  .co m*/
public void initialize() throws GraphException {
    String title = getParam(GraphSource.GRAPH_TITLE, String.class, DEFAULT_TITLE);
    boolean legend = getParam(GraphSource.GRAPH_LEGEND, Boolean.class, DEFAULT_GRAPH_LEGEND);
    boolean graphToolTip = getParam(GraphSource.GRAPH_TOOL_TIP, Boolean.class, DEFAULT_GRAPH_TOOL_TIP);
    boolean graphDisplayLabel = getParam(GraphSource.GRAPH_DISPLAY_LABEL, Boolean.class, false);
    boolean legendBorder = getParam(GraphSource.LEGEND_BORDER, Boolean.class, DEFAULT_LEGEND_BORDER);
    boolean graphBorder = getParam(GraphSource.GRAPH_BORDER, Boolean.class, DEFAULT_GRAPH_BORDER);
    Font titleFont = getParam(GraphSource.GRAPH_FONT, Font.class, DEFAULT_GRAPH_TITLE_FONT);
    String noDataMessage = getParam(GraphSource.GRAPH_NO_DATA_MESSAGE, String.class,
            DEFAULT_GRAPH_NO_DATA_MESSAGE);
    PieGraphData pieGraphData = makeDataSet();
    Map<Comparable, Paint> colors = pieGraphData.colors;

    this.chart = ChartFactory.createPieChart(title, pieGraphData.data, false, graphToolTip, false);

    Paint backgroundColor = getParam(GraphSource.BACKGROUND_COLOR, Paint.class, DEFAULT_BACKGROUND_COLOR);
    Paint plotColor = getParam(JFreeChartTimeSeriesGraphSource.PLOT_COLOR, Paint.class, backgroundColor);
    Paint labelColor = getParam(JFreeChartTimeSeriesGraphSource.GRAPH_LABEL_BACKGROUND_COLOR, Paint.class,
            DEFAULT_BACKGROUND_COLOR);

    this.chart.setBackgroundPaint(backgroundColor);
    PiePlot plot = (PiePlot) chart.getPlot();
    plot.setBackgroundPaint(plotColor);
    plot.setNoDataMessage(noDataMessage);

    if (!graphDisplayLabel) {
        plot.setLabelGenerator(null);
    } else {
        plot.setInteriorGap(0.001);
        plot.setMaximumLabelWidth(.3);
        //           plot.setIgnoreNullValues(true);
        plot.setIgnoreZeroValues(true);
        plot.setShadowPaint(null);
        //           plot.setOutlineVisible(false);
        //TODO use title font?
        Font font = plot.getLabelFont();
        plot.setLabelLinkStyle(PieLabelLinkStyle.CUBIC_CURVE);
        plot.setLabelFont(font.deriveFont(font.getSize2D() * .75f));
        plot.setLabelBackgroundPaint(labelColor);
        plot.setLabelShadowPaint(null);
        plot.setLabelOutlinePaint(null);
        plot.setLabelGap(0.001);
        plot.setLabelLinkMargin(0.0);
        plot.setLabelLinksVisible(true);
        //           plot.setSimpleLabels(true);
        //           plot.setCircular(true);
    }

    if (!graphBorder) {
        plot.setOutlineVisible(false);
    }

    if (title != null && !"".equals(title)) {
        TextTitle title1 = new TextTitle();
        title1.setText(title);
        title1.setFont(titleFont);
        title1.setPadding(3, 2, 5, 2);
        chart.setTitle(title1);
    } else {
        chart.setTitle((TextTitle) null);
    }
    plot.setLabelPadding(new RectangleInsets(1, 1, 1, 1));
    //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);
        plot.setLegendLabelGenerator(new PieGraphLabelGenerator());
    }

    for (Comparable category : colors.keySet()) {
        plot.setSectionPaint(category, colors.get(category));
    }

    plot.setToolTipGenerator(new PieGraphToolTipGenerator(pieGraphData));
    plot.setURLGenerator(new PieGraphURLGenerator(pieGraphData));

    initialized = true;
}

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

@Override
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);
    CategoryDataset dataset = makeDataSet();

    chart = createChart(title, xLabel, yLabel, dataset, false, false);

    // start customizing the graph
    Paint backgroundColor = getParam(GraphSource.BACKGROUND_COLOR, Paint.class, DEFAULT_BACKGROUND_COLOR);
    Paint plotColor = getParam(JFreeChartTimeSeriesGraphSource.PLOT_COLOR, Paint.class, backgroundColor);
    Double offset = getParam(GraphSource.AXIS_OFFSET, Double.class, DEFAULT_AXIS_OFFSET);
    Paint graphDomainGridlinePaint = getParam(GraphSource.GRAPH_DOMAIN_GRIDLINE_PAINT, Paint.class,
            backgroundColor);//from w  ww . j ava 2 s. c  o m
    Paint graphRangeGridlinePaint = getParam(GraphSource.GRAPH_RANGE_GRIDLINE_PAINT, Paint.class,
            backgroundColor);
    boolean graphBorder = getParam(GraphSource.GRAPH_BORDER, Boolean.class, DEFAULT_GRAPH_BORDER);

    chart.setBackgroundPaint(backgroundColor);
    CategoryPlot plot = chart.getCategoryPlot();
    plot.setBackgroundPaint(plotColor);
    plot.setAxisOffset(new RectangleInsets(offset, offset, offset, offset));
    plot.setDomainGridlinePaint(graphDomainGridlinePaint);
    plot.setDomainGridlinesVisible(true);
    plot.setRangeGridlinePaint(graphRangeGridlinePaint);
    plot.setOutlineVisible(graphBorder);

    // set the axis location
    AxisLocation axisLocation = getParam(GraphSource.GRAPH_RANGE_AXIS_LOCATION, AxisLocation.class,
            AxisLocation.TOP_OR_LEFT);
    plot.setRangeAxisLocation(axisLocation);

    // customize the y-axis
    if (params.get(RANGE_AXIS) instanceof ValueAxis) {
        ValueAxis valueAxis = (ValueAxis) params.get(RANGE_AXIS);
        plot.setRangeAxis(valueAxis);
    }

    ValueAxis valueAxis = plot.getRangeAxis();
    Object yAxisFont = params.get(GraphSource.GRAPH_Y_AXIS_FONT);
    Object yAxisLabelFont = params.get(GraphSource.GRAPH_Y_AXIS_LABEL_FONT);
    Double rangeLowerBound = getParam(GraphSource.GRAPH_RANGE_LOWER_BOUND, Double.class, null);
    Double rangeUpperBound = getParam(GraphSource.GRAPH_RANGE_UPPER_BOUND, Double.class, null);
    boolean graphRangeIntegerTick = getParam(GraphSource.GRAPH_RANGE_INTEGER_TICK, Boolean.class, false);
    boolean graphRangeMinorTickVisible = getParam(GraphSource.GRAPH_RANGE_MINOR_TICK_VISIBLE, Boolean.class,
            true);

    if (yAxisFont instanceof Font) {
        valueAxis.setTickLabelFont((Font) yAxisFont);
    }

    if (yAxisLabelFont instanceof Font) {
        valueAxis.setLabelFont((Font) yAxisLabelFont);
    }

    if (rangeLowerBound != null) {
        valueAxis.setLowerBound(rangeLowerBound);
    }

    if (rangeUpperBound != null) {
        valueAxis.setUpperBound(rangeUpperBound);
    }

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

    valueAxis.setMinorTickMarksVisible(graphRangeMinorTickVisible);

    // customize the x-axis
    if (params.get(DOMAIN_AXIS) instanceof CategoryAxis) {
        CategoryAxis domainAxis = (CategoryAxis) params.get(DOMAIN_AXIS);
        plot.setDomainAxis(domainAxis);
    }

    CategoryAxis domainAxis = plot.getDomainAxis();
    Object xAxisFont = params.get(GraphSource.GRAPH_X_AXIS_FONT);
    Object xAxisLabelFont = params.get(GraphSource.GRAPH_X_AXIS_LABEL_FONT);

    if (xAxisFont instanceof Font) {
        domainAxis.setTickLabelFont((Font) xAxisFont);
    }

    if (xAxisLabelFont instanceof Font) {
        domainAxis.setLabelFont((Font) xAxisLabelFont);
    }

    domainAxis.setLabel(xLabel);
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    plot.setDomainAxis(domainAxis);

    // change the font of the graph title
    Font titleFont = getParam(GraphSource.GRAPH_FONT, Font.class, DEFAULT_GRAPH_TITLE_FONT);
    TextTitle textTitle = new TextTitle();
    textTitle.setText(title);
    textTitle.setFont(titleFont);
    chart.setTitle(textTitle);

    // makes a wrapper for the legend to remove the border around it
    boolean legend = getParam(GraphSource.GRAPH_LEGEND, Boolean.class, DEFAULT_GRAPH_LEGEND);
    boolean legendBorder = getParam(GraphSource.LEGEND_BORDER, Boolean.class, DEFAULT_LEGEND_BORDER);
    Object legendFont = params.get(GraphSource.LEGEND_FONT);

    if (legend) {
        LegendTitle legendTitle = 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 = legendTitle.getItemContainer();
        items.setPadding(2, 10, 5, 2);
        wrapper.add(items);
        legendTitle.setWrapper(wrapper);
        legendTitle.setPosition(RectangleEdge.BOTTOM);
        legendTitle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        if (legendFont instanceof Font) {
            legendTitle.setItemFont((Font) legendFont);
        }

        chart.addSubtitle(legendTitle);
    }

    this.initialized = true;
}

From source file:org.ash.history.detail.StackedChartDetail.java

/**
 * Creates the chart./* w w  w.  jav  a2s  .  co m*/
 * 
 * @return the jfreechart
 */
private JFreeChart createChart() {

    xAxis = new DateAxis("time");
    xAxis.setLabel(null);

    chart = ChartFactory.createStackedXYAreaChart("", // chart title
            "X Value", // domain axis label
            "Active Sessions", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // the plot orientation
            xAxis, // xAxis
            false, // legend
            true, // tooltips
            false // urls
    );

    chart.getTitle()
            .setFont(new Font(TextTitle.DEFAULT_FONT.getFontName(), TextTitle.DEFAULT_FONT.getStyle(), 14));

    plot = (XYPlot) chart.getPlot();
    renderer = new StackedXYAreaRenderer3();
    renderer.setRoundXCoordinates(true);
    renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0} ({1}, {2})",
            new SimpleDateFormat("HH:mm"), new DecimalFormat("0.0")));
    plot.setRenderer(0, renderer);
    plot.getRangeAxis().setLowerBound(0.0);
    plot.getRangeAxis().setAutoRange(true);

    // Set format for x axis
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

    // Add legend to right
    LegendTitle legend = new LegendTitle(chart.getPlot());

    BlockContainer wrapper = new BlockContainer(new BorderArrangement());
    wrapper.setFrame(new BlockBorder(1.0, 1.0, 1.0, 1.0));

    BlockContainer itemss = legend.getItemContainer();
    itemss.setPadding(2, 10, 5, 2);
    wrapper.add(itemss);
    legend.setWrapper(wrapper);

    legend.setPosition(RectangleEdge.RIGHT);
    legend.setHorizontalAlignment(HorizontalAlignment.LEFT);

    legend.setItemFont(new Font(LegendTitle.DEFAULT_ITEM_FONT.getFontName(),
            LegendTitle.DEFAULT_ITEM_FONT.getStyle(), 10));

    chart.addSubtitle(legend);

    return chart;
}

From source file:org.ash.detail.StackedChartDetail.java

/**
 * Creates the chart./*from ww w.  j a  v  a 2s  . c o  m*/
 * 
 * @return the jfreechart
 */
private JFreeChart createChart() {

    xAxis = new DateAxis("time");
    xAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

    currentDate = new Date();
    updatexAxisLabel(new Long(currentDate.getTime()).doubleValue());

    chart = ChartFactory.createStackedXYAreaChart("", // chart title
            "X Value", // domain axis label
            "Active Sessions", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // the plot orientation
            xAxis, // xAxis
            false, // legend
            true, // tooltips
            false // urls
    );

    plot = (XYPlot) chart.getPlot();
    renderer = new StackedXYAreaRenderer3();
    renderer.setRoundXCoordinates(true);
    renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0} ({1}, {2})",
            new SimpleDateFormat("HH:mm"), new DecimalFormat("0.0")));
    plot.setRenderer(0, renderer);
    plot.getRangeAxis().setLowerBound(0.0);
    plot.getRangeAxis().setAutoRange(true);

    // Set format for x axis
    DateAxis axis = (DateAxis) plot.getDomainAxis();
    axis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));

    // Add legend to right
    LegendTitle legend = new LegendTitle(chart.getPlot());

    BlockContainer wrapper = new BlockContainer(new BorderArrangement());
    wrapper.setFrame(new BlockBorder(1.0, 1.0, 1.0, 1.0));

    BlockContainer itemss = legend.getItemContainer();
    itemss.setPadding(2, 10, 5, 2);
    wrapper.add(itemss);
    legend.setWrapper(wrapper);

    legend.setPosition(RectangleEdge.RIGHT);
    legend.setHorizontalAlignment(HorizontalAlignment.LEFT);

    chart.addSubtitle(legend);

    return chart;
}

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.//from  w  w  w  . java 2s  . c  om
 *
 * @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;
}