public static void main(String[] args) throws IOException { JFreeChart chart = ChartFactory.createBarChart3D( "图书销量统计图", "图书", // 目录轴的显示标签 "销量", // 数值轴的显示标签 getDataSet(), PlotOrientation.VERTICAL, // 设置图表方向 false, false, false); // 设置标题 chart.setTitle(new TextTitle("图书销量统计图", new Font("黑体", Font.ITALIC, 22))); // 设置图表部分 CategoryPlot plot = (CategoryPlot) chart.getPlot(); CategoryAxis categoryAxis = plot.getDomainAxis(); // 取得横轴 categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 22)); // 设置横轴显示标签的字体 categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 分类标签以45度倾斜 categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 18)); // 分类标签字体 NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis(); // 取得纵轴 numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 42)); // 设置纵轴显示标签字体 FileOutputStream fos = null; fos = new FileOutputStream("book1.jpg"); ChartUtilities.writeChartAsJPEG(fos, 1, chart, 800, 600, null); fos.close(); }
@Override public JFreeChart createChart(FacesContext context) { JFreeChart chart; if (!getIs3D()) { chart = ChartFactory.createBarChart( getTitle(), getDomainAxisLabel(), getRangeAxisLabel(), (CategoryDataset) dataset, plotOrientation(getOrientation()), getLegend(), false, false); } else { chart = ChartFactory.createBarChart3D( getTitle(), getDomainAxisLabel(), getRangeAxisLabel(), (CategoryDataset) dataset, plotOrientation(getOrientation()), getLegend(), false, false); } configureTitle(chart.getTitle()); configureLegend(chart.getLegend()); return chart; }
/** * Erstellt ein Stabdiagramm aus den Parametern. * * @param dataset Datenset * @param titel Titel * @return {@link JFreeChart} */ protected JFreeChart erstelleDiagramm( CategoryDataset dataset, String titel, String reihenBeschriftung, String spaltenBeschriftung) { // create the chart... JFreeChart chart = ChartFactory.createBarChart3D( titel, reihenBeschriftung, spaltenBeschriftung, dataset, PlotOrientation.VERTICAL, true, true, false); TextTitle t = chart.getTitle(); t.setHorizontalAlignment(HorizontalAlignment.CENTER); t.setFont(new Font("Arial", Font.CENTER_BASELINE, 26)); chart.setBackgroundPaint(Color.white); CategoryPlot plot = (CategoryPlot) chart.getPlot(); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setDrawBarOutline(false); for (int i = 0; i < parteienListe.size(); i++) { if (parteienListe.get(i).getFarbe() != null) { GradientPaint gp = new GradientPaint( 0.0f, 0.0f, parteienListe.get(i).getFarbe(), 0.0f, 0.0f, new Color(parteienListe.get(i).getFarbe().getRGB())); renderer.setSeriesPaint(i, gp); } } CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setCategoryLabelPositions( CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)); return chart; }
private JFreeChart createChart(CategoryDataset dataset) { JFreeChart chart = ChartFactory.createBarChart3D( "", // Título "Planejamento", // Label X "Valores", // Label Y dataset, // Dados PlotOrientation.VERTICAL, // Orientação true, // Legenda false, // Tooltips false // urls ); chart.setBackgroundPaint(new Color(255, 255, 255)); CategoryPlot plot = chart.getCategoryPlot(); plot.setForegroundAlpha(0.6f); return chart; }
/** * Create a horizontal bar chart with sample data in the range -3 to +3. * * @return the chart. */ private static JFreeChart createBarChart3D() { // create a dataset... Number[][] data = new Integer[][] { {new Integer(-3), new Integer(-2)}, {new Integer(-1), new Integer(1)}, {new Integer(2), new Integer(3)} }; CategoryDataset dataset = DatasetUtilities.createCategoryDataset("S", "C", data); // create the chart... return ChartFactory.createBarChart3D( "Bar Chart 3D", "Domain", "Range", dataset, PlotOrientation.HORIZONTAL, true, // include legend true, false); }
public JFreeChart getChart() { JFreeChart chart = null; try { Font font = new Font("宋体", 0, 20); // 字体 QuestionStatisticsModel qsm = ss.statistics(qid); DefaultPieDataset pieds = null; // 饼图的数据集 DefaultCategoryDataset cateds = null; // 种类数据集 if (chartType < 2) { pieds = new DefaultPieDataset(); for (OptionStatisticsModel os : qsm.getOsms()) { pieds.setValue(os.getOptionLabel(), os.getCount()); } } else { cateds = new DefaultCategoryDataset(); for (OptionStatisticsModel os : qsm.getOsms()) { cateds.setValue(os.getCount(), os.getOptionLabel(), ""); } } switch (chartType) { case CHARTTYPE_PIE_2D: chart = ChartFactory.createPieChart(qsm.getQuestion().getTitle(), pieds, true, false, false); break; case CHARTTYPE_PIE_3D: chart = ChartFactory.createPieChart3D(qsm.getQuestion().getTitle(), pieds, true, true, true); // 设置前景色透明度 chart.getPlot().setForegroundAlpha(0.6f); break; case CHARTTYPE_BAR_2D_H: // 平面条形图 chart = ChartFactory.createBarChart( qsm.getQuestion().getTitle(), "", "", cateds, PlotOrientation.HORIZONTAL, true, true, true); break; case CHARTTYPE_BAR_2D_V: // 平面条形图 chart = ChartFactory.createBarChart( qsm.getQuestion().getTitle(), "", "", cateds, PlotOrientation.VERTICAL, true, true, true); case CHARTTYPE_BAR_3D_H: // 平面条形图 chart = ChartFactory.createBarChart3D( qsm.getQuestion().getTitle(), "", "", cateds, PlotOrientation.HORIZONTAL, true, true, true); case CHARTTYPE_BAR_3D_V: // 平面条形图 chart = ChartFactory.createBarChart3D( qsm.getQuestion().getTitle(), "", "", cateds, PlotOrientation.VERTICAL, true, true, true); break; // case CHARTTYPE_LINE_2D: // 平面条形图 chart = ChartFactory.createLineChart( qsm.getQuestion().getTitle(), "", "", cateds, PlotOrientation.VERTICAL, true, true, true); break; case CHARTTYPE_LINE_3D: // 平面条形图 chart = ChartFactory.createLineChart3D( qsm.getQuestion().getTitle(), "", "", cateds, PlotOrientation.HORIZONTAL, true, true, true); break; } chart.getTitle().setFont(font); chart.getLegend().setItemFont(font); // 设置饼图特效 if (chart.getPlot() instanceof PiePlot) { PiePlot pieplot = (PiePlot) chart.getPlot(); pieplot.setLabelFont(font); pieplot.setExplodePercent(0, 0.1); pieplot.setStartAngle(-15); pieplot.setDirection(Rotation.CLOCKWISE); pieplot.setNoDataMessage("No data to display"); // pieplot.setForegroundAlpha(0.5f); // pieplot.setBackgroundImageAlpha(0.3f); } // 设置非饼图效果 else { chart.getCategoryPlot().getRangeAxis().setLabelFont(font); chart.getCategoryPlot().getRangeAxis().setTickLabelFont(font); chart.getCategoryPlot().getDomainAxis().setLabelFont(font); chart.getCategoryPlot().getDomainAxis().setTickLabelFont(font); } } catch (Exception e) { e.printStackTrace(); } return chart; }
/** JFreeChartオブジェクトを作成する */ private JFreeChart createChartObject(Document doc) { Element root = doc.getDocumentElement(); Element chartInfo = (Element) root.getElementsByTagName("ChartInfo").item(0); String chartTitle = chartInfo.getElementsByTagName("Title").item(0).getFirstChild().getNodeValue(); this.chartType = chartInfo.getElementsByTagName("Type").item(0).getFirstChild().getNodeValue(); String categoryLabel = ((Element) chartInfo.getElementsByTagName("Category").item(0)) .getElementsByTagName("Label") .item(0) .getFirstChild() .getNodeValue(); Element firstSeries = (Element) ((Element) chartInfo.getElementsByTagName("SeriesList").item(0)) .getElementsByTagName("Series") .item(0); String firstSeriesLabel = firstSeries.getElementsByTagName("Label").item(0).getFirstChild().getNodeValue(); // データセットのリストを取得 this.dataSetList = this.getDatasetList(doc); // 棒チャート(Series数に関わらない) if (this.chartType.equals("VerticalBar") || this.chartType.equals("HorizontalBar") || this.chartType.equals("VerticalMultiBar") || this.chartType.equals("HorizontalMultiBar")) { chart = ChartFactory.createBarChart( chartTitle, categoryLabel, firstSeriesLabel, (CategoryDataset) this.dataSetList.get(0), getLayoutFromDoc(doc), false, false, false); } // 3D棒チャート(Series数に関わらない) else if (this.chartType.equals("Vertical3D_Bar") || this.chartType.equals("Horizontal3D_Bar") || this.chartType.equals("VerticalMulti3D_Bar") || this.chartType.equals("HorizontalMulti3D_Bar")) { chart = ChartFactory.createBarChart3D( chartTitle, categoryLabel, firstSeriesLabel, (CategoryDataset) this.dataSetList.get(0), getLayoutFromDoc(doc), false, false, false); } // 積み上げ棒チャート else if ((this.chartType.equals("VerticalStackedBar")) || (this.chartType.equals("HorizontalStackedBar"))) { chart = ChartFactory.createStackedBarChart( chartTitle, categoryLabel, firstSeriesLabel, (CategoryDataset) this.dataSetList.get(0), getLayoutFromDoc(doc), false, false, false); } // 3D積み上げ棒チャート else if ((this.chartType.equals("VerticalStacked3D_Bar")) || (this.chartType.equals("HorizontalStacked3D_Bar"))) { chart = ChartFactory.createStackedBarChart3D( chartTitle, categoryLabel, firstSeriesLabel, (CategoryDataset) this.dataSetList.get(0), getLayoutFromDoc(doc), false, false, false); } // 折れ線チャート(Series数に関わらない) else if ((this.chartType.equals("Line")) || (this.chartType.equals("MultiLine"))) { chart = ChartFactory.createLineChart( chartTitle, categoryLabel, firstSeriesLabel, (CategoryDataset) this.dataSetList.get(0), PlotOrientation.VERTICAL, false, false, false); } // 面チャート(Series数に関わらない) else if ((this.chartType.equals("Area")) || (this.chartType.equals("MultiArea"))) { chart = ChartFactory.createAreaChart( chartTitle, categoryLabel, firstSeriesLabel, (CategoryDataset) this.dataSetList.get(0), PlotOrientation.VERTICAL, false, false, false); } // 積み上げ面チャート else if (this.chartType.equals("StackedArea")) { chart = ChartFactory.createStackedAreaChart( chartTitle, categoryLabel, firstSeriesLabel, (CategoryDataset) this.dataSetList.get(0), PlotOrientation.VERTICAL, false, false, false); } // 円チャート(Series数に関わらない) else if (this.chartType.equals("Pie")) { chart = ChartFactory.createPieChart( chartTitle, (PieDataset) this.dataSetList.get(0), false, false, false); } // 3D円チャート(Series数に関わらない) else if (this.chartType.equals("Pie_3D")) { chart = ChartFactory.createPieChart3D( chartTitle, (PieDataset) this.dataSetList.get(0), false, false, false); } // 複数円チャート(Series数が2以上) else if (this.chartType.equals("MultiPie")) { // 複数円チャート chart = ChartFactory.createMultiplePieChart( chartTitle, (CategoryDataset) this.dataSetList.get(0), TableOrder.BY_ROW, false, false, false); } return chart; }
public JFreeChart getChart(CategoryDataset dataset) { // create the chart... final JFreeChart chart = ChartFactory.createBarChart3D( "Загрузка данных", // chart title "Дата", // domain axis label "Значение (кб.)", // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips? false // URLs? ); // #44639C; TextTitle title = new TextTitle("Динамика загрузки данных", labelFont); // Paint paint = title.getPaint(); title.setPaint(new Color(68, 99, 156)); chart.setTitle(title); // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART... // set the background color for the chart... chart.setBackgroundPaint(Color.white); // get a reference to the plot for further customisation... final CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setRangeGridlinePaint(Color.white); // final IntervalMarker target = new IntervalMarker(2000000, 3000000); // target.setLabel("Ожидаемый диапазон"); // target.setLabelFont(new Font("SansSerif", Font.ITALIC, 11)); // target.setLabelAnchor(RectangleAnchor.LEFT); // target.setLabelTextAnchor(TextAnchor.CENTER_LEFT); // target.setPaint(new Color(222, 222, 255, 128)); // plot.addRangeMarker(target, Layer.BACKGROUND); // set the range axis to display integers only... final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); rangeAxis.setTickLabelFont(labelFont); // disable bar outlines... final BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setDrawBarOutline(false); renderer.setItemMargin(0.10); // set up gradient paints for series... final GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, Color.blue); final GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, Color.lightGray); final GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, Color.lightGray); renderer.setSeriesPaint(0, gp0); renderer.setSeriesPaint(1, gp1); renderer.setSeriesPaint(2, gp2); final CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setCategoryLabelPositions( CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4.0) // CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0) ); // OPTIONAL CUSTOMISATION COMPLETED. return chart; }
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 String getSimpleBarChart(Map dataSource, String objectName, HttpSession session) throws Throwable { Element chartObject = XMLHandler.getElementByAttribute(getChartObjectList(), "name", objectName); List invokeFields = chartObject.getChild("InvokeFields").getChildren("Field"); double[][] data = new double[invokeFields.size()][dataSource.size()]; String[] rowKeys = new String[invokeFields.size()]; String[] columnKeys = new String[dataSource.size()]; String columnLabel = chartObject.getChildText("ColumnLabel"); for (int i = 0; i < dataSource.size(); i++) { Map rec = (Map) dataSource.get("ROW" + i); columnKeys[i] = DataFilter.show(rec, columnLabel); for (int j = 0; j < invokeFields.size(); j++) { data[j][i] = Double.parseDouble( rec.get(((Element) invokeFields.get(j)).getAttributeValue("name")).toString()); } } for (int i = 0; i < invokeFields.size(); i++) { rowKeys[i] = ((Element) invokeFields.get(i)).getAttributeValue("label"); } CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); 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 C3dplot = (CategoryPlot) chart.getPlot(); if (chartObject.getAttribute("alpha") != null) { C3dplot.setForegroundAlpha(chartObject.getAttribute("alpha").getFloatValue()); } BarRenderer3D barrenderer = (BarRenderer3D) C3dplot.getRenderer(); barrenderer.setLabelGenerator(new StandardCategoryLabelGenerator()); barrenderer.setItemLabelsVisible(true); barrenderer.setPositiveItemLabelPosition( new ItemLabelPosition(ItemLabelAnchor.OUTSIDE1, TextAnchor.BASELINE_CENTER)); int width, height; if (chartObject.getAttributeValue("width").equalsIgnoreCase("auto")) { width = (50 * dataSource.size()) * invokeFields.size() + 100; } else { width = Integer.parseInt(chartObject.getAttributeValue("width")); } if (chartObject.getAttributeValue("height").equalsIgnoreCase("auto")) { height = (50 * dataSource.size()) * invokeFields.size() + 100; } else { height = Integer.parseInt(chartObject.getAttributeValue("height")); } return ServletUtilities.saveChartAsPNG(chart, width, height, session); }