1 package dinamica.charts; 2 3 import java.awt.Color ; 4 import dinamica.*; 5 import org.jfree.chart.*; 6 import org.jfree.data.category.*; 7 import org.jfree.chart.plot.*; 8 import org.jfree.chart.renderer.category.LineAndShapeRenderer; 9 10 11 16 public class LineChart extends AbstractChartPlugin 17 { 18 19 22 public JFreeChart getChart(Recordset chartInfo, Recordset data) 23 throws Throwable 24 { 25 26 27 String dateFormat = (String )chartInfo.getValue("dateformat"); 28 29 30 DefaultCategoryDataset chartdata = new DefaultCategoryDataset(); 31 32 33 String series[] = null; 34 String seriesLabels = chartInfo.getString("title-series"); 35 if (seriesLabels!=null) 36 series = StringUtil.split(seriesLabels, ";"); 37 else 38 { 39 series = new String [1]; 40 series[0] = ""; 41 } 42 43 44 String dataCols[] = null; 45 String coly = (String )chartInfo.getValue("column-y"); 46 if (coly.indexOf(";")>0) 47 dataCols = StringUtil.split(coly, ";"); 48 else 49 { 50 dataCols = new String [1]; 51 dataCols[0] = coly; 52 } 53 54 55 56 data.top(); 57 while (data.next()) 58 { 59 60 61 String colx = (String )chartInfo.getValue("column-x"); 62 RecordsetField f = data.getField(colx); 63 String label = null; 64 if (f.getType()==java.sql.Types.DATE) 65 label = StringUtil.formatDate((java.util.Date )data.getValue(colx), dateFormat); 66 else 67 label = String.valueOf(data.getValue(colx)); 68 69 70 for (int i=0;i<dataCols.length;i++) 71 { 72 Double value = new Double (String.valueOf(data.getValue(dataCols[i]))); 73 if (value==null) 74 value = new Double (0); 75 76 chartdata.addValue(value.doubleValue(), series[i], label); 77 78 } 79 80 } 81 82 83 String title = (String )chartInfo.getValue("title"); 84 String titlex = (String )chartInfo.getValue("title-x"); 85 String titley = (String )chartInfo.getValue("title-y"); 86 87 88 boolean useLegend = (dataCols.length>1); 89 90 91 JFreeChart chart = ChartFactory.createLineChart( 92 title, titlex, titley, chartdata, PlotOrientation.VERTICAL, useLegend, false, false ); 101 102 103 configurePlot( chart.getPlot() ); 104 105 String color = chartInfo.getString("color"); 108 if (!useLegend && color!=null) 109 { 110 CategoryPlot p = (CategoryPlot)chart.getPlot(); 111 p.getRenderer().setSeriesPaint(0, Color.decode(color)); 112 } 113 114 115 return chart; 116 117 118 } 119 120 123 public void configurePlot(Plot p) 124 { 125 126 CategoryPlot plot = (CategoryPlot)p; 127 128 plot.setBackgroundPaint(Color.WHITE); 129 plot.setRangeGridlinePaint(Color.BLACK); 130 plot.setDomainGridlinePaint(Color.BLACK); 131 plot.setDomainGridlinesVisible(true); 132 plot.setRangeGridlinesVisible(true); 133 134 LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)plot.getRenderer(); 135 lineandshaperenderer.setShapesVisible(true); 136 lineandshaperenderer.setShapesFilled(true); 137 138 } 139 140 } 141 | Popular Tags |