001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.admin.common;
005:
006: import org.jfree.chart.ChartFactory;
007: import org.jfree.chart.JFreeChart;
008: import org.jfree.chart.axis.DateAxis;
009: import org.jfree.chart.axis.NumberAxis;
010: import org.jfree.chart.axis.ValueAxis;
011: import org.jfree.chart.plot.PlotOrientation;
012: import org.jfree.chart.plot.XYPlot;
013: import org.jfree.data.RangeType;
014: import org.jfree.data.time.TimeSeries;
015: import org.jfree.data.time.TimeSeriesCollection;
016: import org.jfree.data.xy.IntervalXYDataset;
017: import org.jfree.data.xy.XYDataset;
018:
019: import java.text.SimpleDateFormat;
020:
021: public class DemoChartFactory {
022: public static JFreeChart getXYBarChart(String header,
023: String xLabel, String yLabel, TimeSeries ts) {
024: IntervalXYDataset dataset = createTimeSeriesDataset(ts);
025: return createXYBarChart(header, xLabel, yLabel, dataset, false);
026: }
027:
028: public static JFreeChart getXYBarChart(String header,
029: String xLabel, String yLabel, TimeSeries[] timeseries) {
030: IntervalXYDataset dataset = createTimeSeriesDataset(timeseries);
031: return createXYBarChart(header, xLabel, yLabel, dataset, true);
032: }
033:
034: private static JFreeChart createXYBarChart(String header,
035: String xLabel, String yLabel, IntervalXYDataset dataset,
036: boolean legend) {
037: JFreeChart chart = ChartFactory.createXYBarChart(header,
038: xLabel, true, yLabel, dataset,
039: PlotOrientation.VERTICAL, legend, true, false);
040:
041: XYPlot plot = (XYPlot) chart.getPlot();
042:
043: DateAxis axis = (DateAxis) plot.getDomainAxis();
044: axis.setFixedAutoRange(30000.0);
045: axis.setDateFormatOverride(new SimpleDateFormat("kk:mm:ss"));
046:
047: NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
048: numberAxis.setRangeType(RangeType.POSITIVE);
049: numberAxis.setAutoRangeMinimumSize(50.0);
050:
051: return chart;
052: }
053:
054: public static JFreeChart getXYLineChart(String header,
055: String xLabel, String yLabel, TimeSeries ts) {
056: XYDataset dataset = createTimeSeriesDataset(ts);
057: return createXYLineChart(header, xLabel, yLabel, dataset, false);
058: }
059:
060: public static JFreeChart getXYLineChart(String header,
061: String xLabel, String yLabel, TimeSeries[] timeseries) {
062: XYDataset dataset = createTimeSeriesDataset(timeseries);
063: return createXYLineChart(header, xLabel, yLabel, dataset, true);
064: }
065:
066: private static JFreeChart createXYLineChart(String header,
067: String xLabel, String yLabel, XYDataset dataset,
068: boolean legend) {
069: JFreeChart chart = ChartFactory.createTimeSeriesChart(header,
070: xLabel, yLabel, dataset, legend, true, false);
071:
072: XYPlot plot = (XYPlot) chart.getPlot();
073:
074: ValueAxis axis = plot.getDomainAxis();
075: axis.setFixedAutoRange(30000.0);
076: if (axis instanceof DateAxis) {
077: ((DateAxis) axis)
078: .setDateFormatOverride(new SimpleDateFormat(
079: "kk:mm:ss"));
080: }
081:
082: NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
083: numberAxis.setRangeType(RangeType.POSITIVE);
084: numberAxis.setAutoRangeMinimumSize(50.0);
085:
086: return chart;
087: }
088:
089: public static JFreeChart getXYStepChart(String header,
090: String xLabel, String yLabel, TimeSeries ts) {
091: XYDataset dataset = createTimeSeriesDataset(ts);
092: return createXYStepChart(header, xLabel, yLabel, dataset, false);
093: }
094:
095: public static JFreeChart getXYStepChart(String header,
096: String xLabel, String yLabel, TimeSeries[] timeseries) {
097: XYDataset dataset = createTimeSeriesDataset(timeseries);
098: return createXYStepChart(header, xLabel, yLabel, dataset, true);
099: }
100:
101: private static JFreeChart createXYStepChart(String header,
102: String xLabel, String yLabel, XYDataset dataset,
103: boolean legend) {
104: JFreeChart chart = ChartFactory.createXYStepChart(header,
105: xLabel, yLabel, dataset, PlotOrientation.VERTICAL,
106: legend, true, false);
107:
108: XYPlot plot = (XYPlot) chart.getPlot();
109:
110: ValueAxis axis = plot.getDomainAxis();
111: axis.setFixedAutoRange(30000.0);
112: if (axis instanceof DateAxis) {
113: ((DateAxis) axis)
114: .setDateFormatOverride(new SimpleDateFormat(
115: "kk:mm:ss"));
116: }
117:
118: NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
119: numberAxis.setRangeType(RangeType.POSITIVE);
120: numberAxis.setAutoRangeMinimumSize(50.0);
121:
122: return chart;
123: }
124:
125: private static TimeSeriesCollection createTimeSeriesDataset(
126: TimeSeries s1) {
127: return createTimeSeriesDataset(new TimeSeries[] { s1 });
128: }
129:
130: private static TimeSeriesCollection createTimeSeriesDataset(
131: TimeSeries[] series) {
132: TimeSeriesCollection dataset = new TimeSeriesCollection();
133:
134: for (int i = 0; i < series.length; i++) {
135: dataset.addSeries(series[i]);
136: }
137:
138: dataset.setDomainIsPointsInTime(true);
139:
140: return dataset;
141: }
142: }
|