Java tutorial
/* BarChart.java Copyright 2004-2007 KUBO Hiroya (hiroya@cuc.ac.jp). Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Created on 2005/02/26 */ package net.sqs2.omr.result.chart; import java.io.IOException; import java.io.OutputStream; import net.sqs2.omr.master.FormArea; import net.sqs2.omr.result.builder.StatisticsResult; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PlotOrientation; import org.jfree.data.category.DefaultCategoryDataset; public class BarChart { public static void write(OutputStream outputStream, int width, int height, Legend legend, StatisticsResult statisticsResult) { // (1)create dataset DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (FormArea formArea : legend.formAreaList) { int itemIndex = formArea.getItemIndex(); String key = legend.questionIndex + "," + itemIndex; dataset.addValue(statisticsResult.getCount(key), "", formArea.getItemLabel()); } if (legend.primaryFormArea.isSelectSingle()) { int count = statisticsResult.getCount(legend.questionIndex + ",-1"); dataset.addValue(count, "", ChartConstants.NO_ANSWER); } // String title = StringUtil.join(formAreaList.get(0).getHints(), "\n"); String title = ""; JFreeChart chart = ChartFactory.createBarChart(title, "", ChartConstants.NO_ANSWER, dataset, PlotOrientation.HORIZONTAL, false, true, false); try { ChartUtilities.writeChartAsPNG(outputStream, chart, width, height); } catch (IOException ioEx) { ioEx.printStackTrace(); } } }