| 1 |
/* ============================ |
| 2 |
* SVGChartWithAnnotations.java |
| 3 |
* ============================ |
| 4 |
* |
| 5 |
* Copyright (c) 2013-2021, Object Refinery Limited. |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions are met: |
| 10 |
* - Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* - Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* - Neither the name of the Object Refinery Limited nor the |
| 16 |
* names of its contributors may be used to endorse or promote products |
| 17 |
* derived from this software without specific prior written permission. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 |
* ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY |
| 23 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 26 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
* |
| 30 |
*/ |
| 31 |
|
| 32 |
package org.jfree.svg.demo; |
| 33 |
|
| 34 |
import java.awt.BasicStroke; |
| 35 |
import java.awt.Color; |
| 36 |
import java.awt.Font; |
| 37 |
import java.awt.Rectangle; |
| 38 |
import java.awt.geom.Ellipse2D; |
| 39 |
import java.io.File; |
| 40 |
import java.io.IOException; |
| 41 |
import java.text.DateFormat; |
| 42 |
import java.text.SimpleDateFormat; |
| 43 |
import org.jfree.chart.ChartFactory; |
| 44 |
import org.jfree.chart.JFreeChart; |
| 45 |
import org.jfree.chart.annotations.XYDrawableAnnotation; |
| 46 |
import org.jfree.chart.axis.DateAxis; |
| 47 |
import org.jfree.chart.axis.DateTickUnit; |
| 48 |
import org.jfree.chart.axis.DateTickUnitType; |
| 49 |
import org.jfree.chart.axis.NumberAxis; |
| 50 |
import org.jfree.chart.axis.TickUnitSource; |
| 51 |
import org.jfree.chart.axis.TickUnits; |
| 52 |
//import org.jfree.chart.drawable.ColorPainter; |
| 53 |
import org.jfree.chart.labels.StandardXYToolTipGenerator; |
| 54 |
import org.jfree.chart.plot.PiePlot; |
| 55 |
import org.jfree.chart.plot.XYPlot; |
| 56 |
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; |
| 57 |
import org.jfree.chart.ui.RectangleInsets; |
| 58 |
import org.jfree.data.category.DefaultCategoryDataset; |
| 59 |
import org.jfree.data.general.DefaultPieDataset; |
| 60 |
import org.jfree.data.time.Month; |
| 61 |
import org.jfree.data.time.TimeSeries; |
| 62 |
import org.jfree.data.time.TimeSeriesCollection; |
| 63 |
import org.jfree.data.time.Year; |
| 64 |
import org.jfree.data.xy.XYDataset; |
| 65 |
import org.jfree.svg.SVGGraphics2D; |
| 66 |
import org.jfree.svg.SVGUtils; |
| 67 |
|
| 68 |
/** |
| 69 |
* A demo/test for a pie chart. |
| 70 |
*/ |
| 71 |
public class SVGChartWithAnnotationsDemo1 { |
| 72 |
|
| 73 |
/** |
| 74 |
* Creates a sample chart. |
| 75 |
* |
| 76 |
* @param dataset a dataset for the chart. |
| 77 |
* |
| 78 |
* @return A sample chart. |
| 79 |
*/ |
| 80 |
private static JFreeChart createChart(XYDataset dataset) { |
| 81 |
JFreeChart chart = ChartFactory.createTimeSeriesChart( |
| 82 |
"XYDrawableAnnotationDemo1", |
| 83 |
null, "$ million", dataset); |
| 84 |
XYPlot plot = (XYPlot) chart.getPlot(); |
| 85 |
plot.setDomainPannable(true); |
| 86 |
plot.setRangePannable(true); |
| 87 |
DateAxis xAxis = (DateAxis) plot.getDomainAxis(); |
| 88 |
xAxis.setLowerMargin(0.2); |
| 89 |
xAxis.setUpperMargin(0.2); |
| 90 |
xAxis.setStandardTickUnits(createStandardDateTickUnits()); |
| 91 |
|
| 92 |
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis(); |
| 93 |
yAxis.setLowerMargin(0.2); |
| 94 |
yAxis.setUpperMargin(0.2); |
| 95 |
|
| 96 |
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); |
| 97 |
renderer.setDefaultShapesVisible(true); |
| 98 |
renderer.setDefaultLinesVisible(true); |
| 99 |
renderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0)); |
| 100 |
renderer.setSeriesShape(1, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0)); |
| 101 |
renderer.setSeriesStroke(0, new BasicStroke(3.0f)); |
| 102 |
renderer.setSeriesStroke(1, new BasicStroke(3.0f, BasicStroke.CAP_ROUND, |
| 103 |
BasicStroke.JOIN_ROUND, 5.0f, new float[] {10.0f, 5.0f}, 0.0f)); |
| 104 |
renderer.setSeriesFillPaint(0, Color.WHITE); |
| 105 |
renderer.setSeriesFillPaint(1, Color.WHITE); |
| 106 |
renderer.setUseFillPaint(true); |
| 107 |
|
| 108 |
renderer.setDefaultToolTipGenerator(new StandardXYToolTipGenerator()); |
| 109 |
renderer.setDefaultEntityRadius(6); |
| 110 |
|
| 111 |
renderer.addAnnotation(new XYDrawableAnnotation( |
| 112 |
new Month(4, 2005).getFirstMillisecond(), 600, 180, 100, 3.0, |
| 113 |
createPieChart())); |
| 114 |
renderer.addAnnotation(new XYDrawableAnnotation( |
| 115 |
new Month(9, 2007).getFirstMillisecond(), 1250, 120, 100, 2.0, |
| 116 |
createBarChart())); |
| 117 |
plot.setRenderer(renderer); |
| 118 |
return chart; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Creates a sample dataset. |
| 123 |
* |
| 124 |
* @return A dataset. |
| 125 |
*/ |
| 126 |
private static XYDataset createDataset() { |
| 127 |
TimeSeries series1 = new TimeSeries("Division A"); |
| 128 |
series1.add(new Year(2005), 1520); |
| 129 |
series1.add(new Year(2006), 1132); |
| 130 |
series1.add(new Year(2007), 450); |
| 131 |
series1.add(new Year(2008), 620); |
| 132 |
TimeSeries series2 = new TimeSeries("Division B"); |
| 133 |
series2.add(new Year(2005), 1200); |
| 134 |
series2.add(new Year(2006), 1300); |
| 135 |
series2.add(new Year(2007), 640); |
| 136 |
series2.add(new Year(2008), 520); |
| 137 |
TimeSeriesCollection dataset = new TimeSeriesCollection(); |
| 138 |
dataset.addSeries(series1); |
| 139 |
dataset.addSeries(series2); |
| 140 |
return dataset; |
| 141 |
} |
| 142 |
|
| 143 |
private static JFreeChart createPieChart() { |
| 144 |
DefaultPieDataset dataset = new DefaultPieDataset(); |
| 145 |
dataset.setValue("Engineering", 43.2); |
| 146 |
dataset.setValue("Research", 13.2); |
| 147 |
dataset.setValue("Advertising", 20.9); |
| 148 |
PiePlot plot = new PiePlot(dataset); |
| 149 |
// plot.setBackgroundPainter(null); |
| 150 |
plot.setBackgroundPaint(null); |
| 151 |
// plot.setBorderPainter(null); |
| 152 |
plot.setOutlineVisible(false); |
| 153 |
plot.setDefaultSectionOutlinePaint(Color.WHITE); |
| 154 |
plot.setDefaultSectionOutlineStroke(new BasicStroke(2.0f)); |
| 155 |
plot.setLabelFont(new Font("Dialog", Font.PLAIN, 18)); |
| 156 |
plot.setMaximumLabelWidth(0.25); |
| 157 |
JFreeChart chart = new JFreeChart(plot); |
| 158 |
// chart.setBackgroundPainter(null); |
| 159 |
chart.setBackgroundPaint(null); |
| 160 |
chart.removeLegend(); |
| 161 |
chart.setPadding(RectangleInsets.ZERO_INSETS); |
| 162 |
return chart; |
| 163 |
} |
| 164 |
|
| 165 |
private static JFreeChart createBarChart() { |
| 166 |
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); |
| 167 |
dataset.addValue(10.0, "R1", "Q1"); |
| 168 |
dataset.addValue(7.0, "R1", "Q2"); |
| 169 |
dataset.addValue(8.0, "R1", "Q3"); |
| 170 |
dataset.addValue(4.0, "R1", "Q4"); |
| 171 |
dataset.addValue(10.6, "R2", "Q1"); |
| 172 |
dataset.addValue(6.1, "R2", "Q2"); |
| 173 |
dataset.addValue(8.5, "R2", "Q3"); |
| 174 |
dataset.addValue(4.3, "R2", "Q4"); |
| 175 |
JFreeChart chart = ChartFactory.createBarChart("Sales 2008", null, |
| 176 |
null, dataset); |
| 177 |
chart.removeLegend(); |
| 178 |
// chart.setBackgroundPainter(null); |
| 179 |
// chart.getPlot().setBackgroundPainter(new ColorPainter( |
| 180 |
// new Color(200, 200, 255, 60))); |
| 181 |
return chart; |
| 182 |
} |
| 183 |
|
| 184 |
private static TickUnitSource createStandardDateTickUnits() { |
| 185 |
TickUnits units = new TickUnits(); |
| 186 |
DateFormat df = new SimpleDateFormat("yyyy"); |
| 187 |
units.add(new DateTickUnit(DateTickUnitType.YEAR, 1, |
| 188 |
DateTickUnitType.YEAR, 1, df)); |
| 189 |
units.add(new DateTickUnit(DateTickUnitType.YEAR, 2, |
| 190 |
DateTickUnitType.YEAR, 1, df)); |
| 191 |
units.add(new DateTickUnit(DateTickUnitType.YEAR, 5, |
| 192 |
DateTickUnitType.YEAR, 5, df)); |
| 193 |
return units; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Starting point for the demo. |
| 198 |
* |
| 199 |
* @param args ignored. |
| 200 |
* |
| 201 |
* @throws IOException |
| 202 |
*/ |
| 203 |
public static void main(String[] args) throws IOException { |
| 204 |
JFreeChart chart = createChart(createDataset()); |
| 205 |
SVGGraphics2D g2 = new SVGGraphics2D(500, 300); |
| 206 |
Rectangle r = new Rectangle(0, 0, 500, 300); |
| 207 |
chart.draw(g2, r); |
| 208 |
File f = new File("SVGChartWithAnnotationsDemo1.svg"); |
| 209 |
SVGUtils.writeToSVG(f, g2.getSVGElement()); |
| 210 |
} |
| 211 |
} |