org.xwiki.chart.internal.plot.BarPlotGenerator.java Source code

Java tutorial

Introduction

Here is the source code for org.xwiki.chart.internal.plot.BarPlotGenerator.java

Source

/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.chart.internal.plot;

import java.util.Map;

import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.xwiki.chart.model.ChartModel;

/**
 * A {@link PlotGenerator} for generating bar charts.
 * 
 * @version $Id: 48d7b86cb67755c8ad6c653813266518edc9a1ff $
 * @since 2.0M1
 */
public class BarPlotGenerator implements PlotGenerator {
    /**
     * {@inheritDoc}
     */
    public Plot generate(ChartModel model, Map<String, String> parameters) {
        CategoryItemRenderer renderer = new BarRenderer();
        CategoryAxis domainAxis = new CategoryAxis();
        ValueAxis rangeAxis = new NumberAxis();
        return new CategoryPlot(buildCategoryDataset(model, parameters), domainAxis, rangeAxis, renderer);
    }

    /**
     * Builds a new {@link DefaultCategoryDataset} corresponding to the provided {@link ChartModel}.
     * 
     * @param model the {@link ChartModel} instance.
     * @param parameters additional parameters.
     * @return a {@link DefaultCategoryDataset} corresponding to the provided {@link ChartModel}.
     */
    public DefaultCategoryDataset buildCategoryDataset(ChartModel model, Map<String, String> parameters) {
        String dataSeries = parameters.get("series");
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        if ("rows".equals(dataSeries)) {
            extractRows(model, dataset);
        } else {
            extractColumns(model, dataset);
        }
        return dataset;
    }

    /**
     * Extracts data rows from the {@link ChartModel} provided and populates the {@link DefaultCategoryDataset}
     * accordingly.
     * 
     * @param model the {@link ChartModel} instance.
     * @param dataset the {@link DefaultCategoryDataset} to be populated.
     */
    private void extractRows(ChartModel model, DefaultCategoryDataset dataset) {
        for (int row = 0; row < model.getRowCount(); row++) {
            for (int column = 0; column < model.getColumnCount(); column++) {
                dataset.addValue(model.getCellValue(row, column), model.getRowHeader(row),
                        model.getColumnHeader(column));
            }
        }
    }

    /**
     * Extracts data columns from the {@link ChartModel} provided and populates the {@link DefaultCategoryDataset}
     * accordingly.
     * 
     * @param model the {@link ChartModel} instance.
     * @param dataset the {@link DefaultCategoryDataset} to be populated.
     */
    private void extractColumns(ChartModel model, DefaultCategoryDataset dataset) {
        for (int row = 0; row < model.getRowCount(); row++) {
            for (int column = 0; column < model.getColumnCount(); column++) {
                dataset.addValue(model.getCellValue(row, column), model.getColumnHeader(column),
                        model.getRowHeader(row));
            }
        }
    }
}