Author: echatellier Date: 2010-01-07 18:39:23 +0100 (Thu, 07 Jan 2010) New Revision: 352 Added: misctestproject/trunk/src/main/java/org/nuiton/test/chart/ misctestproject/trunk/src/main/java/org/nuiton/test/chart/TestMultiChart.java Modified: misctestproject/trunk/pom.xml Log: Add jfreechart sample Modified: misctestproject/trunk/pom.xml =================================================================== --- misctestproject/trunk/pom.xml 2010-01-07 11:19:05 UTC (rev 351) +++ misctestproject/trunk/pom.xml 2010-01-07 17:39:23 UTC (rev 352) @@ -89,5 +89,11 @@ <artifactId>jxlayer</artifactId> <version>3.0.4</version> </dependency> + + <dependency> + <groupId>jfree</groupId> + <artifactId>jfreechart</artifactId> + <version>1.0.12</version> + </dependency> </dependencies> </project> \ No newline at end of file Added: misctestproject/trunk/src/main/java/org/nuiton/test/chart/TestMultiChart.java =================================================================== --- misctestproject/trunk/src/main/java/org/nuiton/test/chart/TestMultiChart.java (rev 0) +++ misctestproject/trunk/src/main/java/org/nuiton/test/chart/TestMultiChart.java 2010-01-07 17:39:23 UTC (rev 352) @@ -0,0 +1,154 @@ +/* *##% + * Copyright (C) 2010 Code Lutin, Chatellier Eric + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package org.nuiton.test.chart; + +import java.io.File; +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; + +import org.jfree.chart.ChartUtilities; +import org.jfree.chart.JFreeChart; +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.PlotOrientation; +import org.jfree.chart.renderer.category.BarRenderer; +import org.jfree.data.category.DefaultCategoryDataset; + +/** + * TODO add comment here. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class TestMultiChart { + + public static void main(String[] args) { + + List<SortedMap<Date, Integer>> results = new ArrayList<SortedMap<Date, Integer>>(); + + SortedMap<Date, Integer> map1 = new TreeMap<Date, Integer>(); + + Date date = new GregorianCalendar(2010, 0, 1).getTime(); + map1.put(date, 12); + date = new GregorianCalendar(2010, 1, 1).getTime(); + map1.put(date, 2); + date = new GregorianCalendar(2010, 2, 1).getTime(); + map1.put(date, 18); + date = new GregorianCalendar(2010, 3, 1).getTime(); + map1.put(date, 0); + date = new GregorianCalendar(2010, 4, 1).getTime(); + map1.put(date, 25); + date = new GregorianCalendar(2010, 5, 1).getTime(); + map1.put(date, 36); + date = new GregorianCalendar(2010, 6, 1).getTime(); + map1.put(date, 4); + date = new GregorianCalendar(2010, 7, 1).getTime(); + map1.put(date, 18); + date = new GregorianCalendar(2010, 8, 1).getTime(); + map1.put(date, 7); + date = new GregorianCalendar(2010, 9, 1).getTime(); + map1.put(date, 2); + date = new GregorianCalendar(2010, 10, 1).getTime(); + map1.put(date, 0); + date = new GregorianCalendar(2010, 11, 1).getTime(); + map1.put(date, 60); + + results.add(map1); + + SortedMap<Date, Integer> map2 = new TreeMap<Date, Integer>(); + + date = new GregorianCalendar(2010, 0, 1).getTime(); + map2.put(date, 7); + date = new GregorianCalendar(2010, 1, 1).getTime(); + map2.put(date, 8); + date = new GregorianCalendar(2010, 2, 1).getTime(); + map2.put(date, 15); + date = new GregorianCalendar(2010, 3, 1).getTime(); + map2.put(date, 3); + date = new GregorianCalendar(2010, 4, 1).getTime(); + map2.put(date, 32); + date = new GregorianCalendar(2010, 5, 1).getTime(); + map2.put(date, 36); + date = new GregorianCalendar(2010, 6, 1).getTime(); + map2.put(date, 3); + date = new GregorianCalendar(2010, 7, 1).getTime(); + map2.put(date, 17); + date = new GregorianCalendar(2010, 8, 1).getTime(); + map2.put(date, 6); + date = new GregorianCalendar(2010, 9, 1).getTime(); + map2.put(date, 1); + date = new GregorianCalendar(2010, 10, 1).getTime(); + map2.put(date, 0); + date = new GregorianCalendar(2010, 11, 1).getTime(); + map2.put(date, 30); + + results.add(map2); + + displayGraph(results); + } + + public static void displayGraph(List<SortedMap<Date, Integer>> results) { + + DateFormat format = new SimpleDateFormat("MM/yyyy"); + + // create a chart with the dataset + DefaultCategoryDataset dataset = new DefaultCategoryDataset(); + + int i = 0; + for (Map<Date, Integer> map : results) { + for (Map.Entry<Date, Integer> data : map.entrySet()) { + dataset.setValue(data.getValue(), "Serie " + i, format.format(data.getKey())); + } + i++; + } + + CategoryAxis categoryAxis = new CategoryAxis("Date"); + // label horizontaux + //categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90); + ValueAxis valueAxis = new NumberAxis("Bla"); + + BarRenderer renderer = new BarRenderer(); + //StackedBarRenderer renderer = new StackedBarRenderer(); + + CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer); + plot.setOrientation(PlotOrientation.VERTICAL); + JFreeChart chart = new JFreeChart("Legende (test)", JFreeChart.DEFAULT_TITLE_FONT, + plot, true); + + // create and return the image + try { + ChartUtilities.saveChartAsJPEG(new File("/tmp", "chart.jpg"), chart, 800, 400); + } catch (IOException e) { + e.printStackTrace(); + } + } +} Property changes on: misctestproject/trunk/src/main/java/org/nuiton/test/chart/TestMultiChart.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL"
participants (1)
-
echatellier@users.nuiton.org