KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > userinterface > LineChart


1 package com.calipso.reportgenerator.userinterface;
2
3 import org.jfree.chart.JFreeChart;
4 import org.jfree.chart.ChartFactory;
5 import org.jfree.chart.ChartPanel;
6 import org.jfree.chart.renderer.category.LineAndShapeRenderer;
7 import org.jfree.chart.axis.*;
8 import org.jfree.chart.plot.CategoryPlot;
9 import org.jfree.chart.plot.Marker;
10 import org.jfree.chart.plot.ValueMarker;
11 import org.jfree.ui.RectangleAnchor;
12 import org.jfree.ui.TextAnchor;
13 import org.jfree.text.TextBlockAnchor;
14 import org.jfree.data.category.CategoryDataset;
15
16 import java.awt.*;
17
18 import com.calipso.reportgenerator.common.LanguageTraslator;
19
20 /**
21  * Representa un grafico de Lineas
22  */

23
24 public class LineChart extends Charts{
25
26
27     /**
28      * Inicializa una instancia de LineChart
29      * @param dataset
30      * @param tittle
31      * @param color
32      * @param legend
33      * @param toolTips
34      */

35     public LineChart(CategoryDataset dataset, String JavaDoc tittle, Color color, boolean legend, boolean toolTips, Dimension size, boolean multipleAxis) {
36         super(dataset, tittle, color, legend, toolTips, size, multipleAxis);
37         previewLineChart();
38         this.setVisible(true);
39     }
40
41     /**
42      * Crea el chart correspondiente a partir de un <code>CategoryDataset</code>,
43      * titulo, leyenda y toolTips
44      */

45     void previewLineChart(){
46
47
48         // create the chart...
49
JFreeChart chart = ChartFactory.createLineChart(tittle, // chart title
50
LanguageTraslator.traslate("336"), // domain axis label
51
LanguageTraslator.traslate("337"), // range axis label
52
dataset, // data
53
org.jfree.chart.plot.PlotOrientation.VERTICAL,
54                                                         legend, // include legend
55
toolTips, // tooltips
56
true // urls
57
);
58
59         // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
60

61         chart.setBackgroundPaint(color);
62
63         CategoryPlot plot = chart.getCategoryPlot();
64
65         // set the stroke for each series...
66
plot.getRenderer().setSeriesStroke(0, new BasicStroke(2.0f,
67                                                               BasicStroke.CAP_ROUND,
68                                                               BasicStroke.JOIN_ROUND,
69                                                               1.0f,
70                                                               new float[] { 10.0f, 6.0f },
71                                                               0.0f));
72         plot.getRenderer().setSeriesStroke(1, new BasicStroke(2.0f,
73                                                               BasicStroke.CAP_ROUND,
74                                                               BasicStroke.JOIN_ROUND,
75                                                               1.0f,
76                                                               new float[] { 6.0f, 6.0f },
77                                                               0.0f));
78         plot.getRenderer().setSeriesStroke(2, new BasicStroke(2.0f,
79                                                               BasicStroke.CAP_ROUND,
80                                                               BasicStroke.JOIN_ROUND,
81                                                               1.0f,
82                                                               new float[] { 2.0f, 6.0f },
83                                                               0.0f));
84
85         // label data points with values...
86
// plot.setItemsabelsVisible(true);
87

88         // add a range marker...
89
plot.addRangeMarker(new ValueMarker(8.0));
90
91         // customise the renderer...
92
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
93        renderer.setDrawOutlines(true);
94        renderer.setItemLabelsVisible(true);
95
96         // customise the domain axis...
97
CategoryAxis domainAxis = plot.getDomainAxis();
98       CategoryLabelPosition position = new CategoryLabelPosition(
99             RectangleAnchor.TOP, TextBlockAnchor.TOP_RIGHT, TextAnchor.TOP_RIGHT,-70, CategoryLabelWidthType.RANGE,
100  70);
101       domainAxis.setCategoryLabelPositions(new CategoryLabelPositions(position,position,position,position));
102       domainAxis.setCategoryLabelPositionOffset(1);
103
104         // customise the range axis...
105
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
106         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
107         rangeAxis.setAutoRangeIncludesZero(false);
108         rangeAxis.setUpperMargin(0.12);
109
110
111         // OPTIONAL CUSTOMISATION COMPLETED.
112

113         // add the chart to a panel...
114
ChartPanel chartPanel = new ChartPanel(chart);
115       chartPanel.setPreferredSize(getSize());
116         add(chartPanel);
117
118     }
119 }
120
121
122
Popular Tags