private void saveChart(final DefaultCategoryDataset dataset) throws IOException { final JFreeChart chart = ChartFactory.createBarChart( "HtmlUnit implemented properties and methods for " + browserVersion_.getNickname(), "Objects", "Count", dataset, PlotOrientation.HORIZONTAL, true, true, false); final CategoryPlot plot = (CategoryPlot) chart.getPlot(); final NumberAxis axis = (NumberAxis) plot.getRangeAxis(); axis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); final LayeredBarRenderer renderer = new LayeredBarRenderer(); plot.setRenderer(renderer); plot.setRowRenderingOrder(SortOrder.DESCENDING); renderer.setSeriesPaint(0, new GradientPaint(0, 0, Color.green, 0, 0, new Color(0, 64, 0))); renderer.setSeriesPaint(1, new GradientPaint(0, 0, Color.blue, 0, 0, new Color(0, 0, 64))); renderer.setSeriesPaint(2, new GradientPaint(0, 0, Color.red, 0, 0, new Color(64, 0, 0))); ImageIO.write( chart.createBufferedImage(1200, 2400), "png", new File( getArtifactsDirectory() + "/properties-" + browserVersion_.getNickname() + ".png")); }
private static JFreeChart finishBarChart(JFreeChart chart, Color... colors) { CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT); categoryplot.setRangePannable(true); if (colors.length > 0) categoryplot.setRenderer(new CustomRenderer(colors)); BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer(); barrenderer.setItemLabelAnchorOffset(9D); barrenderer.setBaseItemLabelsVisible(true); barrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); barrenderer.setMaximumBarWidth(0.05); barrenderer.setItemMargin(0.03D); ItemLabelPosition itemlabelposition = new ItemLabelPosition(INSIDE12, CENTER_RIGHT, CENTER_RIGHT, -1.5707963267948966D); barrenderer.setBasePositiveItemLabelPosition(itemlabelposition); ItemLabelPosition itemlabelposition1 = new ItemLabelPosition(OUTSIDE12, CENTER_LEFT, CENTER_LEFT, -1.5707963267948966D); barrenderer.setPositiveItemLabelPositionFallback(itemlabelposition1); CategoryAxis categoryaxis = categoryplot.getDomainAxis(); categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45); categoryaxis.setCategoryMargin(0.25D); categoryaxis.setUpperMargin(0.02D); categoryaxis.setLowerMargin(0.02D); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); numberaxis.setUpperMargin(0.10000000000000001D); ChartUtilities.applyCurrentTheme(chart); return chart; }
private static JFreeChart createChart(CategoryDataset categorydataset) { JFreeChart jfreechart = ChartFactory.createLineChart( "Statistical Line Chart Demo 1", "Type", "Value", categorydataset, PlotOrientation.VERTICAL, true, true, false); jfreechart.setBackgroundPaint(Color.white); CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot(); categoryplot.setBackgroundPaint(Color.lightGray); categoryplot.setRangeGridlinePaint(Color.white); CategoryAxis categoryaxis = categoryplot.getDomainAxis(); categoryaxis.setUpperMargin(0.0D); categoryaxis.setLowerMargin(0.0D); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); numberaxis.setAutoRangeIncludesZero(true); StatisticalLineAndShapeRenderer statisticallineandshaperenderer = new StatisticalLineAndShapeRenderer(true, false); statisticallineandshaperenderer.setUseSeriesOffset(true); categoryplot.setRenderer(statisticallineandshaperenderer); return jfreechart; }
public JFreeChart createChart(int size) { CategoryDataset paramCategoryDataset = createDataset(size); // 创建主题样式 StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); // 设置标题字体 standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20)); // 设置图例的字体 standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 12)); // 设置轴向的字体 standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 12)); // 应用主题样式 ChartFactory.setChartTheme(standardChartTheme); JFreeChart localJFreeChart = ChartFactory.createLineChart( title, "期号", yTitle, paramCategoryDataset, PlotOrientation.VERTICAL, true, true, false); CategoryPlot localCategoryPlot = (CategoryPlot) localJFreeChart.getPlot(); // SymbolAxis localSymbolAxis = new SymbolAxis("个数", new String[] { // "0", "1", "2", "3", "4", "5", "6" }); // localCategoryPlot.setRangeAxis(localSymbolAxis); ChartUtilities.applyCurrentTheme(localJFreeChart); LineAndShapeRenderer xyitem = (LineAndShapeRenderer) localCategoryPlot.getRenderer(); CategoryPlot plot = (CategoryPlot) localJFreeChart.getPlot(); // 设置网格背景颜色 plot.setBackgroundPaint(Color.white); // 设置网格竖线颜色 plot.setDomainGridlinePaint(Color.black); // 设置网格横线颜色 plot.setRangeGridlinePaint(Color.black); // 设置曲线图与xy轴的距离 plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 0D)); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); rangeAxis.setAutoRangeIncludesZero(true); if ("和值".equals(type)) { rangeAxis.setLowerBound(70); rangeAxis.setTickUnit(new NumberTickUnit(10)); } else { rangeAxis.setLowerBound(0); rangeAxis.setTickUnit(new NumberTickUnit(1)); } // rangeAxis.setUpperMargin(0.20); // 设置曲线显示各数据点的值 xyitem.setBaseItemLabelsVisible(true); xyitem.setBasePositiveItemLabelPosition( new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); xyitem.setSeriesStroke(0, new BasicStroke(1.5F)); xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 14)); xyitem.setSeriesShapesVisible(0, true); plot.setRenderer(xyitem); return localJFreeChart; }
/** * Creates a chart. * * @param dataset a dataset. * @return A chart. */ private JFreeChart createChart(Gantt gantt, IntervalCategoryDataset dataset) { JFreeChart chart = ChartFactory.createGanttChart( "Solution Gantt", // title "Operators", // x-axis label "Time", // y-axis label null, // data true, // create legend? true, // generate tooltips? false // generate URLs? ); CategoryPlot plot = (CategoryPlot) chart.getPlot(); Paint p = getBackgroundColorGradient(); chart.setBackgroundPaint(p); plot.setBackgroundPaint(Color.white); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.black); plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0)); plot.setOrientation(PlotOrientation.HORIZONTAL); DateAxis xaxis = (DateAxis) plot.getRangeAxis(); xaxis.setDateFormatOverride(new VertexDateFormat()); xaxis.setPositiveArrowVisible(true); DefaultDrawingSupplier d = new DefaultDrawingSupplier(); plot.setDrawingSupplier(d); GanttRenderer ren = new MyGanttRenderer(); for (GanttElement element : gantt.getElementSet()) { ((MyGanttRenderer) ren).addColor(element.getTitle(), element.getColor()); } ren.setSeriesItemLabelsVisible(0, false); ren.setSeriesVisibleInLegend(0, false); ren.setSeriesItemLabelGenerator(0, new IntervalCategoryItemLabelGenerator()); ren.setSeriesToolTipGenerator(0, new MapperGanttToolTipGenerator()); ren.setAutoPopulateSeriesShape(false); plot.setRenderer(ren); plot.setDataset(dataset); return chart; }
public void addDataset(String type, String series, List<Double> values) { if (values == null) { values = new ArrayList<Double>(); } if (values.size() == 0) { values.add(0.0); } this.dataset.add(values, series, type); Color color = colorGenerator.getNext(); CategoryPlot plot = (CategoryPlot) chart.getPlot(); BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer(); renderer.setSeriesPaint(0, color); renderer.setMeanVisible(false); plot.setRenderer(datasetIndex, renderer); datasetIndex++; // Update legends, remove those from variance. LegendItemCollection legendItemsOld = plot.getLegendItems(); this.updateAndSortLegend(legendItemsOld); }
public static void barchart() { String title = "title"; String categoryAxisLabel = "label"; String valueAxisLabel = "value"; DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i = 1; i <= 288; i++) { if (i % 10 == 0) { dataset.addValue(0, "row", "a" + i); } else { dataset.addValue(i, "row", "a" + i); } } JFreeChart chart = ChartFactory.createBarChart( title, categoryAxisLabel, valueAxisLabel, dataset, PlotOrientation.VERTICAL, true, true, false); CategoryPlot cplot = chart.getCategoryPlot(); // cplot.setDomainGridlinesVisible(false); BarRenderer renderer = new BarRenderer(); renderer.setShadowVisible(false); // 不显示阴影 cplot.setRenderer(renderer); // 输出图片文件 int width = 600; int height = 300; try { ChartUtilities.saveChartAsJPEG(new File("/home/a2.jpeg"), chart, width, height); } catch (IOException e) { e.printStackTrace(); } }
private BufferedImage generarGraficoBarrasDistribucionResultados( String titulo, String ejeHorizontal, String ejeVertical, double[] valores, String[] funciones, boolean porcentaje, boolean leyenda, int intervaloAprobacion, int anchoImagen, int altoImagen) { if (valores.length != funciones.length) { return null; } DefaultCategoryDataset pieDataset = new DefaultCategoryDataset(); for (int i = 0; i < valores.length; i++) { pieDataset.addValue(valores[i], "Serie 1", funciones[i]); } JFreeChart chart = ChartFactory.createBarChart( titulo, ejeHorizontal, ejeVertical, pieDataset, PlotOrientation.VERTICAL, leyenda, true, false); chart.setBackgroundPaint(null); chart.setBorderVisible(false); chart.getTitle().setPaint(LookAndFeelEntropy.COLOR_FUENTE_TITULO_PANEL); chart.getTitle().setFont(LookAndFeelEntropy.FUENTE_TITULO_GRANDE); if (leyenda) { chart.getLegend().setItemFont(LookAndFeelEntropy.FUENTE_REGULAR); chart.getLegend().setBackgroundPaint(LookAndFeelEntropy.COLOR_TABLA_PRIMARIO); } CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setBackgroundPaint(null); plot.setBackgroundImageAlpha(0.0f); plot.setOutlineVisible(false); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); if (porcentaje) { rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance()); } else { rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); } final AprobadoDesaprobadoBarRenderer renderer = new AprobadoDesaprobadoBarRenderer(); renderer.setIntervaloAprobacion(intervaloAprobacion); plot.setRenderer(renderer); final CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setCategoryLabelPositions( CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)); this.lastChart = chart; // Generamos una imagen return chart.createBufferedImage(anchoImagen, altoImagen); }
/** Comienzo de mètodos básicos. */ private BufferedImage generarGraficoBarrasApiladas( String titulo, String ejeHorizontal, String ejeVertical, ArrayList<Serie> series, boolean porcentaje, boolean leyenda, PlotOrientation orientacion, int anchoImagen, int altoImagen) { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (Serie serie : series) { if (serie.valores.length != serie.funciones.length) { continue; } for (int i = 0; i < serie.valores.length; i++) { dataset.addValue(serie.valores[i], serie.funciones[i], serie.nombre); } } JFreeChart chart = ChartFactory.createStackedBarChart( titulo, ejeHorizontal, ejeVertical, dataset, orientacion, leyenda, true, // tooltips false // urls ); chart.setBackgroundPaint(null); chart.getTitle().setPaint(LookAndFeelEntropy.COLOR_FUENTE_TITULO_PANEL); chart.getTitle().setFont(LookAndFeelEntropy.FUENTE_TITULO_GRANDE); chart.setBorderVisible(false); if (leyenda) { chart.getLegend().setItemFont(LookAndFeelEntropy.FUENTE_REGULAR); chart.getLegend().setBackgroundPaint(LookAndFeelEntropy.COLOR_TABLA_PRIMARIO); } GroupedStackedBarRenderer renderer = new GroupedStackedBarRenderer(); KeyToGroupMap map = new KeyToGroupMap(series.get(0).nombre); for (Serie serie : series) { map.mapKeyToGroup(serie.nombre, serie.nombre); } renderer.setSeriesToGroupMap(map); renderer.setDrawBarOutline(false); renderer.setRenderAsPercentages(porcentaje); renderer.setItemLabelsVisible(true); renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setToolTipGenerator(new StandardCategoryToolTipGenerator()); for (Serie serie : series) { for (int i = 0; i < serie.valores.length; i++) { if (Serie.colores.size() - 1 < i || Serie.colores.get(i) == null) { Color color = generarColorAleatorio(Color.orange); GradientPaint gp = new GradientPaint(0.0f, 0.0f, color, 0.0f, 0.0f, color); Serie.colores.add(gp); } renderer.setSeriesPaint(i, Serie.colores.get(i)); } } renderer.setGradientPaintTransformer( new StandardGradientPaintTransformer(GradientPaintTransformType.HORIZONTAL)); CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setBackgroundPaint(null); plot.setBackgroundImageAlpha(0.0f); plot.setOutlineVisible(false); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); if (porcentaje) { rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance()); } else { rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); } CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setCategoryLabelPositions( CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)); domainAxis.setCategoryMargin(0.05); plot.setRenderer(renderer); // Generamos una imagen this.lastChart = chart; return chart.createBufferedImage(anchoImagen, altoImagen); }
@Override public JFreeChart createChart(int nbr) { DefaultCategoryDataset dataset0 = new DefaultCategoryDataset(); // DefaultCategoryDataset dataset1 = new DefaultCategoryDataset(); bias for (Tuple<String, double[]> tuple : this.mreDblFileList) { for (int h = 0; h < 24; h++) { dataset0.addValue(tuple.getSecond()[h], tuple.getFirst(), Integer.toString(h + 1)); } } this.chart_ = ChartFactory.createLineChart( "", "Hour", "Mean rel error [%]", dataset0, PlotOrientation.VERTICAL, true, // legend? true, // tooltips? false // URLs? ); CategoryPlot plot = this.chart_.getCategoryPlot(); plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); final LineAndShapeRenderer renderer = new LineAndShapeRenderer(); renderer.setSeriesPaint(0, Color.RED); renderer.setSeriesPaint(1, Color.GREEN); renderer.setSeriesPaint(2, Color.BLUE); renderer.setSeriesToolTipGenerator(0, new StandardCategoryToolTipGenerator()); plot.setRenderer(0, renderer); Color transparent = new Color(0, 0, 255, 0); this.chart_.setBackgroundPaint( transparent); // pink : Color.getHSBColor((float) 0.0, (float) 0.0, (float) 0.93) plot.setBackgroundPaint(Color.getHSBColor((float) 0.0, (float) 0.0, (float) 0.93)); plot.setRangeGridlinePaint(Color.gray); plot.setRangeGridlinesVisible(true); final CategoryAxis axisX = new CategoryAxis("Hour"); axisX.setTickLabelFont(new Font("SansSerif", Font.BOLD, 15)); plot.setDomainAxis(axisX); // final ValueAxis axis2 = new NumberAxis("Mean abs bias [agent/h]"); // plot.setRangeAxis(1, axis2); final ValueAxis axisY = plot.getRangeAxis(0); axisY.setRange(0.0, 100.0); axisY.setTickLabelFont(new Font("SansSerif", Font.BOLD, 21)); final LineAndShapeRenderer renderer2 = new LineAndShapeRenderer(); renderer2.setSeriesToolTipGenerator(0, new StandardCategoryToolTipGenerator()); renderer2.setSeriesToolTipGenerator(1, new StandardCategoryToolTipGenerator()); plot.setRenderer(1, renderer2); plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE); this.chart_.getLegend().setItemFont(new Font("SansSerif", Font.BOLD, 17)); this.chart_.getLegend().setVisible(false); return this.chart_; }
/** シリーズ(縦)軸を設定するメソッド */ public void setSeriesAxisByDoc(Document doc) throws IllegalAccessException, NoSuchFieldException { CategoryPlot categoryPlot = (CategoryPlot) this.getPlot(); Element root = doc.getDocumentElement(); Element chartInfo = (Element) root.getElementsByTagName("ChartInfo").item(0); // データセットリスト ArrayList<Dataset> dataSetList = this.getDataSetList(); int listSize = dataSetList.size(); // データセットの数だけループ for (int i = 0; i < listSize; i++) { // プロットに2番目以降のデータセットを追加 // (1番目のデータセットは、ChartFactory.create** メソッドでチャートオブジェクト作成時に追加済み) if (i != 0) { DefaultCategoryDataset subDataset = (DefaultCategoryDataset) dataSetList.get(i); categoryPlot.setDataset(i, subDataset); // データセットをシリーズ軸に追加 categoryPlot.mapDatasetToRangeAxis(i, 1); } // シリーズ軸のValueAxisオブジェクトをラベル指定で生成 Element series = (Element) chartInfo.getElementsByTagName("Series").item(i); String seriesLabel = series.getElementsByTagName("Label").item(0).getFirstChild().getNodeValue(); ValueAxis valueAxis = null; if (i == 0) { valueAxis = categoryPlot.getRangeAxis(); } else { valueAxis = new NumberAxis(seriesLabel); } // シリーズ軸のラベルカラー、フォントを設定 String seriesLabelColor = series.getElementsByTagName("LabelColor").item(0).getFirstChild().getNodeValue(); valueAxis.setLabelPaint(this.createColor(seriesLabelColor)); // シリーズラベルカラー valueAxis.setLabelFont(this.getFont(doc)); // フォント valueAxis.setTickLabelFont(this.getFont(doc)); // Tickラベルフォント String isAutoRangeEnable = series.getElementsByTagName("isAutoRangeEnable").item(0).getFirstChild().getNodeValue(); // シリーズレンジ手動設定 if ("0".equals(isAutoRangeEnable)) { String maxRange = series.getElementsByTagName("MaxRange").item(0).getFirstChild().getNodeValue(); String minRange = series.getElementsByTagName("MinRange").item(0).getFirstChild().getNodeValue(); // レンジ最大値 valueAxis.setUpperBound(Double.parseDouble(maxRange)); // レンジ最小値 valueAxis.setLowerBound(Double.parseDouble(minRange)); } // プロットに生成したシリーズ軸を設定 if (i != 0) { categoryPlot.setRangeAxis(i, valueAxis); } // レンダラーを取得 CategoryItemRenderer renderer = null; if (i == 0) { renderer = this.createRenderer("byPlot"); } else { renderer = this.createRenderer("create"); } // ツールチップ作成 setToolTip(doc, renderer); // //ドリルダウン設定 // enableDrillDown(LSRenderer,(DefaultCategoryDataset)helper.codeDatasetList.elementAt(i)); if (i != 0) { categoryPlot.setRenderer(i, renderer); // オリジナルのプロットにレンダラーを追加 categoryPlot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE); } } // ループ終了 }
/** * Returns a sequence plot as a ChartPanel. * * @param aProteinSequencePanelParent the protein sequence panel parent * @param sparklineDataset the dataset * @param proteinAnnotations the protein annotations * @param addReferenceLine if true, a reference line is added * @param allowZooming if true, the user can zoom in the created plot/chart * @return a sequence plot */ public ChartPanel getSequencePlot( ProteinSequencePanelParent aProteinSequencePanelParent, JSparklinesDataset sparklineDataset, HashMap<Integer, ArrayList<ResidueAnnotation>> proteinAnnotations, boolean addReferenceLine, boolean allowZooming) { this.proteinSequencePanelParent = aProteinSequencePanelParent; DefaultCategoryDataset barChartDataset = new DefaultCategoryDataset(); StackedBarRenderer renderer = new StackedBarRenderer(); renderer.setShadowVisible(false); CategoryToolTipGenerator myTooltips = new ProteinAnnotations(proteinAnnotations); // add the data for (int i = 0; i < sparklineDataset.getData().size(); i++) { JSparklinesDataSeries sparklineDataSeries = sparklineDataset.getData().get(i); for (int j = 0; j < sparklineDataSeries.getData().size(); j++) { barChartDataset.addValue(sparklineDataSeries.getData().get(j), "" + i, "" + j); renderer.setSeriesPaint(i, sparklineDataSeries.getSeriesColor()); renderer.setSeriesToolTipGenerator(i, myTooltips); } } // create the chart JFreeChart chart = ChartFactory.createStackedBarChart( null, null, null, barChartDataset, PlotOrientation.HORIZONTAL, false, false, false); // fine tune the chart properites CategoryPlot plot = chart.getCategoryPlot(); // remove space before/after the domain axis plot.getDomainAxis().setUpperMargin(0); plot.getDomainAxis().setLowerMargin(0); // remove space before/after the range axis plot.getRangeAxis().setUpperMargin(0); plot.getRangeAxis().setLowerMargin(0); renderer.setRenderAsPercentages(true); renderer.setBaseToolTipGenerator(new IntervalCategoryToolTipGenerator()); // add the dataset to the plot plot.setDataset(barChartDataset); // hide unwanted chart details plot.getRangeAxis().setVisible(false); plot.getDomainAxis().setVisible(false); plot.setRangeGridlinesVisible(false); plot.setDomainGridlinesVisible(false); // add a reference line in the middle of the dataset if (addReferenceLine) { DefaultCategoryDataset referenceLineDataset = new DefaultCategoryDataset(); referenceLineDataset.addValue(1.0, "A", "B"); plot.setDataset(1, referenceLineDataset); LayeredBarRenderer referenceLineRenderer = new LayeredBarRenderer(); referenceLineRenderer.setSeriesBarWidth(0, referenceLineWidth); referenceLineRenderer.setSeriesFillPaint(0, referenceLineColor); referenceLineRenderer.setSeriesPaint(0, referenceLineColor); plot.setRenderer(1, referenceLineRenderer); } // set up the chart renderer plot.setRenderer(0, renderer); // hide the outline chart.getPlot().setOutlineVisible(false); // make sure the background is the same as the panel chart.getPlot().setBackgroundPaint(backgroundColor); chart.setBackgroundPaint(backgroundColor); final HashMap<Integer, ArrayList<ResidueAnnotation>> blockTooltips = proteinAnnotations; // create the chart panel ChartPanel chartPanel = new ChartPanel(chart); chartPanel.addChartMouseListener( new ChartMouseListener() { @Override public void chartMouseClicked(ChartMouseEvent cme) { if (cme.getEntity() != null && cme.getTrigger().getButton() == MouseEvent.BUTTON1) { ((CategoryItemEntity) cme.getEntity()).getDataset(); Integer blockNumber = new Integer((String) ((CategoryItemEntity) cme.getEntity()).getRowKey()); ArrayList<ResidueAnnotation> annotation = blockTooltips.get(blockNumber); if (annotation != null) { proteinSequencePanelParent.annotationClicked(annotation, cme); } } } @Override public void chartMouseMoved(ChartMouseEvent cme) { cme.getTrigger() .getComponent() .setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); if (cme.getEntity() != null && cme.getEntity() instanceof CategoryItemEntity) { ((CategoryItemEntity) cme.getEntity()).getDataset(); Integer blockNumber = new Integer((String) ((CategoryItemEntity) cme.getEntity()).getRowKey()); ArrayList<ResidueAnnotation> annotation = blockTooltips.get(blockNumber); if (annotation != null && !annotation.isEmpty()) { if (blockTooltips.get(blockNumber).get(0).isClickable()) { cme.getTrigger() .getComponent() .setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); } } } } }); if (!allowZooming) { chartPanel.setPopupMenu(null); chartPanel.setRangeZoomable(false); } chartPanel.setBackground(Color.WHITE); return chartPanel; }
public ChartPanel getChartPanel() { JFreeChart c = null; CombinedDomainCategoryPlot combinedP = null; for (final String valueProperty : valueP) { final DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i = 0; i < set.getNumResults(); i++) dataset.addValue( (Double) set.getResultValue(i, valueProperty), set.getResultValue(i, seriesP).toString(), set.getResultValue(i, categoryP).toString()); JFreeChart chart = ChartFactory.createLineChart( title, xAxisLabel, ((yAxisLabel == null) ? set.getNiceProperty(valueProperty) : yAxisLabel) + yAxisLabelSuffix, dataset, PlotOrientation.VERTICAL, showLegend, tooltips, false /*urls*/); chart.getPlot().setBackgroundPaint(Color.WHITE); ((CategoryPlot) chart.getPlot()).setRangeGridlinePaint(Color.GRAY); // ((CategoryPlot) chart.getPlot()).setDomainGridlinesVisible(true); // ((CategoryPlot) chart.getPlot()).setDomainGridlinePaint(Color.GRAY); chart.setBackgroundPaint(new Color(0, 0, 0, 0)); CategoryPlot plot = (CategoryPlot) chart.getPlot(); Color cols[]; Shape shapes[] = null; if (dataset.getRowCount() == 2) { cols = new Color[] { ColorUtil.bright(FreeChartUtil.COLORS[0]), ColorUtil.dark(FreeChartUtil.COLORS[1]) }; shapes = new Shape[] {ShapeUtilities.createDiamond(4.5f), new Ellipse2D.Float(-3f, -3f, 6f, 6f)}; } else if (dataset.getRowCount() == 3) { Color orange = new Color(244, 125, 43); // Color orange = new Color(255, 145, 63); Color red = new Color(238, 46, 47); Color blue = new Color(24, 90, 169); // Color blue = new Color(4, 70, 149); cols = new Color[] {red, orange, blue}; // // Color bright = Color.MAGENTA; // Color medium = Color.RED; // Color dark = new Color(0, 0, 75); // cols = new Color[] { bright, medium, dark }; } else cols = FreeChartUtil.COLORS; // for (int i = 0; i < cols.length; i++) // { // cols[i] = ColorUtil.grayscale(cols[i]); // int color = cols[i].getRGB(); // int red = (color >>> 16) & 0xFF; // int green = (color >>> 8) & 0xFF; // int blue = (color >>> 0) & 0xFF; // float luminance = (red * 0.2126f + green * 0.7152f + blue * 0.0722f) / 255; // System.out.println(luminance); // } // for (int i = 0; i < dataset.getColumnCount(); i++) // { // // int seriesIdx = -1; // // for (int j = 0; j < dataset.getRowCount(); j++) // // { // // String seriesValue = dataset.getRowKey(j).toString(); // // String categoryValue = dataset.getColumnKey(i).toString(); // // if ((drawShape.containsKey(valueProperty) && // seriesValue.equals(drawShape.get(valueProperty).get( // // categoryValue)))) // // { // // seriesIdx = j; // // break; // // } // // } // // // // if (seriesIdx != -1) // // { // CategoryLineAnnotation anno = new CategoryLineAnnotation(dataset.getColumnKey(i), // dataset // .getValue(0, i).doubleValue(), dataset.getColumnKey(i), dataset.getValue(1, // i).doubleValue(), // ColorUtil.transparent(Color.GRAY, 200), new BasicStroke(1.0f, BasicStroke.CAP_ROUND, // BasicStroke.JOIN_ROUND, 5.0f, new float[] { 2.0f, 2.0f }, 5.0f)); // int thick = 10; // Color back = new Color(255, 255, 255); // // // CategoryLineAnnotation anno = new CategoryLineAnnotation(dataset.getColumnKey(i), // dataset // // .getValue(0, i).doubleValue(), dataset.getColumnKey(i), dataset.getValue(1, // i).doubleValue(), // // ColorUtil.transparent(back, 200), new BasicStroke(thick, BasicStroke.CAP_ROUND, // // BasicStroke.JOIN_ROUND)); // plot.addAnnotation(anno); // // if (dataset.getValue(0, i).doubleValue() > dataset.getValue(1, i).doubleValue()) // { // addAnnotation(plot, dataset, i, 1, cols, back, thick, true, valueProperty); // addAnnotation(plot, dataset, i, 0, cols, back, thick, false, valueProperty); // } // else // { // addAnnotation(plot, dataset, i, 0, cols, back, thick, false, valueProperty); // addAnnotation(plot, dataset, i, 1, cols, back, thick, true, valueProperty); // // } // } LineAndShapeRenderer renderer = new LineAndShapeRenderer() { @Override public boolean getItemShapeVisible(int series, int item) { // // return dataset.getValue(series, item).doubleValue() > dataset.getValue(1 // - series, item) // // .doubleValue(); // // String seriesValue = dataset.getRowKey(series).toString(); // String categoryValue = dataset.getColumnKey(item).toString(); // return (drawShape.containsKey(valueProperty) && // seriesValue.equals(drawShape.get(valueProperty) // .get(categoryValue))); // // return new Random().nextBoolean(); return false; } @Override public boolean getItemShapeFilled(int series, int item) { String seriesValue = dataset.getRowKey(series).toString(); String categoryValue = dataset.getColumnKey(item).toString(); return (drawShape.containsKey(valueProperty) && seriesValue.equals(drawShape.get(valueProperty).get(categoryValue))); } @Override public Boolean getSeriesLinesVisible(int series) { return true; } }; // Polygon p1 = new Polygon(); // p1.addPoint(0, -9); // p1.addPoint(-3, -5); // p1.addPoint(3, -5); // renderer.setBaseShape(p1); plot.setRenderer(renderer); // LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); // renderer.set if (markers != null && markers.containsKey(valueProperty)) for (String category : markers.keySet2(valueProperty)) { CategoryMarker marker = new CategoryMarker(category); marker.setOutlinePaint(null); marker.setPaint(new Color(150, 150, 150)); marker.setDrawAsLine(true); marker.setLabel(" " + markers.get(valueProperty, category)); // marker.setLabelFont(plot.getDomainAxis().getLabelFont()); // marker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT); // marker.setLabelOffset(new RectangleInsets(10.0, 0.0, 0.0, 0.0)); marker.setLabelAnchor(RectangleAnchor.BOTTOM); marker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT); marker.setLabelOffsetType(LengthAdjustmentType.CONTRACT); plot.addDomainMarker(marker, Layer.BACKGROUND); } // // marker = new CategoryMarker("elephant"); // marker.setOutlinePaint(null); // marker.setPaint(new Color(150, 150, 150)); // plot.addDomainMarker(marker, Layer.BACKGROUND); // CategoryPointerAnnotation p = new CategoryPointerAnnotation("", "mouse", 0.5, // Math.toRadians(270.0)); // p.setPaint(Color.RED); // plot.addAnnotation(p); // for (int i = 0; i < cols.length; i++) // cols[i] = ColorUtil.grayscale(cols[i]); for (int i = 0; i < dataset.getRowCount(); i++) { // renderer.setSeriesShape(i, p1); // double ratio = i / (double) (dataset.getRowCount() - 1); // System.out.println(i + " " + ratio); // // renderer.setSeriesPaint(i, ColorGradient.get2ColorGradient(ratio, Color.BLACK, // Color.LIGHT_GRAY)); // // renderer.setSeriesPaint(i, new ColorGradient(new Color(200, 0, 0), // Color.LIGHT_GRAY, new Color(150, // // 150, 255)).getColor(ratio)); // renderer.setSeriesPaint(i, new ColorGradient(new Color(0, 0, 200), Color.LIGHT_GRAY, // new Color(150, // 150, 255)).getColor(ratio)); renderer.setSeriesPaint(i, cols[i]); renderer.setSeriesOutlinePaint(i, Color.BLACK); renderer.setSeriesFillPaint(i, Color.BLACK); if (shapes != null) renderer.setSeriesShape(i, shapes[i]); float thick = 1.0f; // if ((dataset.getRowCount() == 2 || dataset.getRowCount() == 3) && i == 1) // renderer.setSeriesStroke(i, new BasicStroke(thick, BasicStroke.CAP_ROUND, // BasicStroke.JOIN_ROUND, // 1.0f, new float[] { 6f * thick, 1.5f * thick }, 3f * thick)); // else if ((dataset.getRowCount() == 2 || dataset.getRowCount() == 3) && i == 2) // renderer.setSeriesStroke(i, new BasicStroke(thick, BasicStroke.CAP_ROUND, // BasicStroke.JOIN_ROUND, // 1.0f, new float[] { 1.5f * thick, 1.5f * thick }, 3f * thick)); // else renderer.setSeriesStroke( i, new BasicStroke(thick, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); // , 1.0f,new float[] { dash, 2 * thick }, dash / 2)); } if (yAxisRange != null) ((NumberAxis) plot.getRangeAxis()).setRange(yAxisRange[0], yAxisRange[1]); if (yAxisRangePerValue.containsKey(valueProperty)) ((NumberAxis) plot.getRangeAxis()) .setRange( yAxisRangePerValue.get(valueProperty)[0], yAxisRangePerValue.get(valueProperty)[1]); if (yAxisTickUnitsPerValue.containsKey(valueProperty)) ((NumberAxis) plot.getRangeAxis()) .setTickUnit(new NumberTickUnit(yAxisTickUnitsPerValue.get(valueProperty))); // ((NumberAxis) plot.getRangeAxis()).setAutoRangeIncludesZero(true); CategoryAxis axis = plot.getDomainAxis(); // axis.setTickLabelsVisible(true); if (c == null) { c = chart; } else if (combinedP == null) { combinedP = new CombinedDomainCategoryPlot(new CategoryAxis(xAxisLabel)); combinedP.setOrientation(PlotOrientation.VERTICAL); combinedP.add(c.getCategoryPlot()); combinedP.add(chart.getCategoryPlot()); c = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, combinedP, false); c.setBackgroundPaint(new Color(0, 0, 0, 0)); c.addLegend(chart.getLegend()); axis = plot.getDomainAxis(); } else { combinedP.add(chart.getCategoryPlot()); } if (rotateXLabels == XLabelsRotation.diagonal) axis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); if (rotateXLabels == XLabelsRotation.vertical) { axis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90); axis.setMaximumCategoryLabelWidthRatio(1.0f); } axis.setLowerMargin(0); axis.setUpperMargin(0); if (!showDomainAxis) axis.setVisible(false); } ChartPanel cp = new ChartPanel(c); return cp; }
public static String getBarSeries(Map dataSource, String objectName, HttpSession session) throws Exception { DefaultKeyedValues barValues = new DefaultKeyedValues(); DefaultKeyedValues seriesValues = new DefaultKeyedValues(); Element chartObject = XMLHandler.getElementByAttribute(getChartObjectList(), "name", objectName); Element barField = chartObject.getChild("BarFields").getChild("Field"); Element seriesField = chartObject.getChild("SeriesFields").getChild("Field"); for (int i = 0; i < dataSource.size(); i++) { Map rec = (Map) dataSource.get("ROW" + i); barValues.addValue( DataFilter.show(rec, chartObject.getChildText("ColumnLabel")), Double.parseDouble(rec.get(barField.getAttributeValue("name")).toString())); seriesValues.addValue( DataFilter.show(rec, chartObject.getChildText("ColumnLabel")), Double.parseDouble(rec.get(seriesField.getAttributeValue("name")).toString())); } CategoryDataset dataset = DatasetUtilities.createCategoryDataset(barField.getAttributeValue("label"), barValues); PlotOrientation plotOrientation = chartObject.getAttributeValue("plotOrientation").equalsIgnoreCase("VERTICAL") ? PlotOrientation.VERTICAL : PlotOrientation.HORIZONTAL; JFreeChart chart = ChartFactory.createBarChart3D( chartObject.getAttributeValue("title"), chartObject.getAttributeValue("categoryAxisLabel"), chartObject.getAttributeValue("valueAxisLabel"), dataset, plotOrientation, chartObject.getAttribute("showLegend").getBooleanValue(), chartObject.getAttribute("showToolTips").getBooleanValue(), chartObject.getAttribute("urls").getBooleanValue()); CategoryPlot categoryplot = chart.getCategoryPlot(); LineRenderer3D lineRenderer = new LineRenderer3D(); CategoryDataset datasetSeries = DatasetUtilities.createCategoryDataset( seriesField.getAttributeValue("label"), seriesValues); categoryplot.setDataset(1, datasetSeries); categoryplot.setRangeAxis(1, new NumberAxis3D(seriesField.getAttributeValue("label"))); categoryplot.setRenderer(1, lineRenderer); categoryplot.mapDatasetToRangeAxis(1, 1); BarRenderer3D barrenderer = (BarRenderer3D) categoryplot.getRenderer(); barrenderer.setLabelGenerator(new StandardCategoryLabelGenerator()); barrenderer.setItemLabelsVisible(true); barrenderer.setPositiveItemLabelPosition( new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BASELINE_CENTER)); // lineRenderer.setLabelGenerator(new StandardCategoryLabelGenerator()); // lineRenderer.setItemLabelsVisible(true); // lineRenderer.setPositiveItemLabelPosition( // new ItemLabelPosition(ItemLabelAnchor.OUTSIDE10, TextAnchor.CENTER)); float alpha = 0.7F; if (chartObject.getAttribute("alpha") != null) { alpha = chartObject.getAttribute("alpha").getFloatValue(); } categoryplot.setForegroundAlpha(alpha); int width, height; if (chartObject.getAttributeValue("width").equalsIgnoreCase("auto")) { width = (50 * dataSource.size()) + 100; } else { width = Integer.parseInt(chartObject.getAttributeValue("width")); } if (chartObject.getAttributeValue("height").equalsIgnoreCase("auto")) { height = (50 * dataSource.size()) + 100; } else { height = Integer.parseInt(chartObject.getAttributeValue("height")); } return ServletUtilities.saveChartAsPNG(chart, width, height, session); }
public Drawable createChart(ADCDataset dataset, Dimension dimension) { JFreeChart chart = ChartFactory.createBarChart( "", // chart title "", // domain axis label "", // range axis label dataset, // data PlotOrientation.VERTICAL, // the plot orientation false, // legend false, // tooltips false // urls ); TextTitle textTitle = new TextTitle(dataset.get(Attribute.TITLE), TITLE_FONT); textTitle.setPadding(new RectangleInsets(10, 0, 0, 0)); chart.setTitle(textTitle); chart.addLegend(createLegend(dataset.getRowKey(0).toString(), dataset.getRowKey(1).toString())); CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setBackgroundPaint(Color.white); plot.setOutlineVisible(false); plot.setAxisOffset(new RectangleInsets(0, 0, 0, 0)); plot.setDomainGridlinesVisible(false); plot.setRangeGridlinesVisible(true); plot.setRangeGridlinePaint(Color.gray); plot.setRangeGridlineStroke(new BasicStroke(2)); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setAutoTickUnitSelection(true); rangeAxis.setTickUnit(new NumberTickUnit(0.2, percentFormatter())); rangeAxis.setAxisLineVisible(true); rangeAxis.setLabel(dataset.get(Attribute.Y_AXIS_LABEL)); rangeAxis.setAxisLineStroke(new BasicStroke(2)); rangeAxis.setAxisLinePaint(Color.black); rangeAxis.setTickMarksVisible(false); rangeAxis.setLabelPaint(AXIS_LABEL_COLOR); rangeAxis.setLabelFont(AXIS_LABEL_FONT); rangeAxis.setLabelInsets(new RectangleInsets(0, 0, 0, 0)); rangeAxis.setUpperMargin(0); rangeAxis.setAutoRange(false); rangeAxis.setRange(0, 1); CategoryAxis cAxis = plot.getDomainAxis(); cAxis.setTickMarksVisible(false); cAxis.setAxisLinePaint(Color.black); cAxis.setAxisLineStroke(new BasicStroke(2)); cAxis.setLabel(dataset.get(Attribute.X_AXIS_LABEL)); cAxis.setTickLabelsVisible(true); cAxis.setUpperMargin(0.05); cAxis.setLowerMargin(0.05); cAxis.setTickLabelFont(CAXIS_LABEL_FONT); cAxis.setTickLabelPaint(Color.black); CustomBarRenderer renderer = new CustomBarRenderer(); plot.setRenderer(renderer); renderer.setDrawBarOutline(false); renderer.setBaseItemLabelsVisible(false); renderer.setShadowVisible(false); renderer.setBarPainter(new StandardBarPainter()); renderer.setMaximumBarWidth(0.08); renderer.setItemMargin(0.01); return new JFreeChartDrawable(chart, dimension); }
public static JFreeChart createChart() { DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset(); defaultcategorydataset.addValue(1.0D, "S1", "Category 1"); defaultcategorydataset.addValue(4D, "S1", "Category 2"); defaultcategorydataset.addValue(3D, "S1", "Category 3"); defaultcategorydataset.addValue(5D, "S1", "Category 4"); defaultcategorydataset.addValue(5D, "S1", "Category 5"); defaultcategorydataset.addValue(7D, "S1", "Category 6"); defaultcategorydataset.addValue(7D, "S1", "Category 7"); defaultcategorydataset.addValue(8D, "S1", "Category 8"); defaultcategorydataset.addValue(5D, "S2", "Category 1"); defaultcategorydataset.addValue(7D, "S2", "Category 2"); defaultcategorydataset.addValue(6D, "S2", "Category 3"); defaultcategorydataset.addValue(8D, "S2", "Category 4"); defaultcategorydataset.addValue(4D, "S2", "Category 5"); defaultcategorydataset.addValue(4D, "S2", "Category 6"); defaultcategorydataset.addValue(2D, "S2", "Category 7"); defaultcategorydataset.addValue(1.0D, "S2", "Category 8"); StandardCategoryItemLabelGenerator standardcategoryitemlabelgenerator = new StandardCategoryItemLabelGenerator(); BarRenderer barrenderer = new BarRenderer(); barrenderer.setBaseItemLabelGenerator(standardcategoryitemlabelgenerator); barrenderer.setBaseItemLabelsVisible(true); CategoryPlot categoryplot = new CategoryPlot(); categoryplot.setDataset(defaultcategorydataset); categoryplot.setRenderer(barrenderer); categoryplot.setDomainAxis(new CategoryAxis("Category")); categoryplot.setRangeAxis(new NumberAxis("Value")); categoryplot.setOrientation(PlotOrientation.VERTICAL); categoryplot.setRangeGridlinesVisible(true); categoryplot.setDomainGridlinesVisible(true); DefaultCategoryDataset defaultcategorydataset1 = new DefaultCategoryDataset(); defaultcategorydataset1.addValue(9D, "T1", "Category 1"); defaultcategorydataset1.addValue(7D, "T1", "Category 2"); defaultcategorydataset1.addValue(2D, "T1", "Category 3"); defaultcategorydataset1.addValue(6D, "T1", "Category 4"); defaultcategorydataset1.addValue(6D, "T1", "Category 5"); defaultcategorydataset1.addValue(9D, "T1", "Category 6"); defaultcategorydataset1.addValue(5D, "T1", "Category 7"); defaultcategorydataset1.addValue(4D, "T1", "Category 8"); LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer(); categoryplot.setDataset(1, defaultcategorydataset1); categoryplot.setRenderer(1, lineandshaperenderer); NumberAxis numberaxis = new NumberAxis("Axis 2"); categoryplot.setRangeAxis(1, numberaxis); DefaultCategoryDataset defaultcategorydataset2 = new DefaultCategoryDataset(); defaultcategorydataset2.addValue(94D, "R1", "Category 1"); defaultcategorydataset2.addValue(75D, "R1", "Category 2"); defaultcategorydataset2.addValue(22D, "R1", "Category 3"); defaultcategorydataset2.addValue(74D, "R1", "Category 4"); defaultcategorydataset2.addValue(83D, "R1", "Category 5"); defaultcategorydataset2.addValue(9D, "R1", "Category 6"); defaultcategorydataset2.addValue(23D, "R1", "Category 7"); defaultcategorydataset2.addValue(98D, "R1", "Category 8"); categoryplot.setDataset(2, defaultcategorydataset2); LineAndShapeRenderer lineandshaperenderer1 = new LineAndShapeRenderer(); categoryplot.setRenderer(2, lineandshaperenderer1); categoryplot.mapDatasetToRangeAxis(2, 1); categoryplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); categoryplot.getDomainAxis().setCategoryLabelPositions(CategoryLabelPositions.UP_45); JFreeChart jfreechart = new JFreeChart(categoryplot); jfreechart.setTitle("Overlaid Bar Chart"); return jfreechart; }