/** Replaces the chart's dataset and then checks that the new dataset is OK. */ public void testReplaceDataset() { // create a dataset... Number[][] data = new Integer[][] { {new Integer(-30), new Integer(-20)}, {new Integer(-10), new Integer(10)}, {new Integer(20), new Integer(30)} }; CategoryDataset newData = DatasetUtilities.createCategoryDataset("S", "C", data); LocalListener l = new LocalListener(); this.chart.addChangeListener(l); CategoryPlot plot = (CategoryPlot) this.chart.getPlot(); plot.setDataset(newData); assertEquals(true, l.flag); ValueAxis axis = plot.getRangeAxis(); Range range = axis.getRange(); assertTrue( "Expecting the lower bound of the range to be around -30: " + range.getLowerBound(), range.getLowerBound() <= -30); assertTrue( "Expecting the upper bound of the range to be around 30: " + range.getUpperBound(), range.getUpperBound() >= 30); }
/** * 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; }
/** * A check for the datasetIndex and seriesIndex fields in the LegendItem returned by the * getLegendItem() method. */ @Test public void testGetLegendItemSeriesIndex() { DefaultCategoryDataset dataset0 = new DefaultCategoryDataset(); dataset0.addValue(21.0, "R1", "C1"); dataset0.addValue(22.0, "R2", "C1"); DefaultCategoryDataset dataset1 = new DefaultCategoryDataset(); dataset1.addValue(23.0, "R3", "C1"); dataset1.addValue(24.0, "R4", "C1"); dataset1.addValue(25.0, "R5", "C1"); BarRenderer r = new BarRenderer(); CategoryPlot plot = new CategoryPlot(dataset0, new CategoryAxis("x"), new NumberAxis("y"), r); plot.setDataset(1, dataset1); /*JFreeChart chart =*/ new JFreeChart(plot); LegendItem li = r.getLegendItem(1, 2); assertEquals("R5", li.getLabel()); assertEquals(1, li.getDatasetIndex()); assertEquals(2, li.getSeriesIndex()); }
/** * A check for the interaction between the 'autoRangeIncludesZero' flag and the base setting in * the BarRenderer. */ @Test public void testAutoRange4() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.setValue(100.0, "Row 1", "Column 1"); dataset.setValue(200.0, "Row 1", "Column 2"); JFreeChart chart = ChartFactory.createBarChart( "Test", "Categories", "Value", dataset, PlotOrientation.VERTICAL, false, false, false); CategoryPlot plot = (CategoryPlot) chart.getPlot(); NumberAxis axis = (NumberAxis) plot.getRangeAxis(); axis.setAutoRangeIncludesZero(false); BarRenderer br = (BarRenderer) plot.getRenderer(); br.setIncludeBaseInRange(false); assertEquals(95.0, axis.getLowerBound(), EPSILON); assertEquals(205.0, axis.getUpperBound(), EPSILON); br.setIncludeBaseInRange(true); assertEquals(0.0, axis.getLowerBound(), EPSILON); assertEquals(210.0, axis.getUpperBound(), EPSILON); axis.setAutoRangeIncludesZero(true); assertEquals(0.0, axis.getLowerBound(), EPSILON); assertEquals(210.0, axis.getUpperBound(), EPSILON); br.setIncludeBaseInRange(true); assertEquals(0.0, axis.getLowerBound(), EPSILON); assertEquals(210.0, axis.getUpperBound(), EPSILON); // now replacing the dataset should update the axis range... DefaultCategoryDataset dataset2 = new DefaultCategoryDataset(); dataset2.setValue(900.0, "Row 1", "Column 1"); dataset2.setValue(1000.0, "Row 1", "Column 2"); plot.setDataset(dataset2); assertEquals(0.0, axis.getLowerBound(), EPSILON); assertEquals(1050.0, axis.getUpperBound(), EPSILON); br.setIncludeBaseInRange(false); assertEquals(0.0, axis.getLowerBound(), EPSILON); assertEquals(1050.0, axis.getUpperBound(), EPSILON); axis.setAutoRangeIncludesZero(false); assertEquals(895.0, axis.getLowerBound(), EPSILON); assertEquals(1005.0, axis.getUpperBound(), EPSILON); }
/** シリーズ(縦)軸を設定するメソッド */ 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 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 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; }