/* * Metodo que aplica o filtro */ public static void Apply(BufferedImage image, String savePath) { // Chamada do metodo que gera os dados do histograma GetData(image); // Dataset que gera o grafico DefaultCategoryDataset ds = new DefaultCategoryDataset(); // Loop que percorre o array de dados do histograma for (int k = 0; k < data.length; k++) { // Adiciona o valor ao dataset ds.setValue(data[k], "Imagem", Integer.toString(data[k])); } // Gera o grafico JFreeChart grafico = ChartFactory.createLineChart( "Histograma", "Tons de cinza", "Valor", ds, PlotOrientation.VERTICAL, true, true, false); // Salva o grafico em um arquivo de png try { OutputStream arquivo = new FileOutputStream(savePath); ChartUtilities.writeChartAsPNG(arquivo, grafico, 550, 400); arquivo.close(); } catch (IOException e) { e.printStackTrace(); } }
private JFreeChart formChart() { DefaultCategoryDataset data = new DefaultCategoryDataset(); String dateString = request.getParameter("date"); Date date = null; if (dateString != null && (!dateString.equals("null"))) { date = DayTransformer.transform(dateString); } else { date = new Date(System.currentTimeMillis()); } String[][] storeSellingDis = saleperformanceManage.getStoreSellingDis(DayTransformer.transformToMonth(date)); for (int i = 0; i < storeSellingDis.length; i++) { data.setValue( Double.parseDouble(storeSellingDis[i][1]), storeSellingDis[i][0], storeSellingDis[i][0]); } JFreeChart barChart = ChartFactory.createBarChart( "店铺销售分布图", "店铺名称", "销售额", data, PlotOrientation.VERTICAL, true, true, false); CategoryPlot plot = (CategoryPlot) barChart.getCategoryPlot(); BarRenderer renderer = (BarRenderer) plot.getRenderer(); // 显示条目标签 renderer.setBaseItemLabelsVisible(true); // 设置条目标签生成器,在JFreeChart1.0.6之前可以通过renderer.setItemLabelGenerator(CategoryItemLabelGenerator // generator)方法实现,但是从版本1.0.6开始有下面方法代替 renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); // 设置条目标签显示的位置,outline表示在条目区域外,baseline_center表示基于基线且居中 renderer.setBasePositiveItemLabelPosition( new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER)); barChart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15)); // 设置引用标签字体 barChart.getTitle().setFont(new Font("华文行楷", Font.BOLD, 32)); return barChart; }
private void btnPictorialViewActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btnPictorialViewActionPerformed // TODO add your handling code here: DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset(); for (Product product : supplier.getProductCatalog().getProductCatalog()) { categoryDataset.setValue(product.getSoldQuantity(), "Units", product.getProdName()); } JFreeChart freeChart = ChartFactory.createBarChart( "Sales Report", "Product", "Units", categoryDataset, PlotOrientation.VERTICAL, false, true, false); CategoryPlot categoryPlot = freeChart.getCategoryPlot(); categoryPlot.setRangeGridlinePaint(Color.BLACK); ChartFrame cf = new ChartFrame("Sales Report", freeChart); cf.setVisible(true); cf.setSize(450, 300); } // GEN-LAST:event_btnPictorialViewActionPerformed
public NetflowTotalByIPChart(String title, String filename) { TotalsCSVReader reader = new TotalsCSVReader(filename, TotalsCSVReader.IP, TotalsCSVReader.OCTETS); HashMap<String, Integer> mappeddata = reader.getTotalsItems(); Integer val; /* Create a simple Bar chart */ DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (String protocol : mappeddata.keySet()) { val = mappeddata.get(protocol); dataset.setValue(val.intValue(), "Netflow", protocol); } JFreeChart chart = ChartFactory.createBarChart( "Total by IP", "IP Address", "Octets", dataset, PlotOrientation.VERTICAL, false, true, false); // create and display a frame... frame = new ChartFrame(title, chart); frame.pack(); }
/** * Erstellt einen Datensatz aus den Parametern. * * @param daten Daten * @param kopfZeile Kopfzeile * @param start Start der Daten * @return {@link DefaultCategoryDataset} */ protected DefaultCategoryDataset erstelleDatensatz( Object[][] daten, Object[] kopfZeile, int start) { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i = 0; i < daten.length; i++) { for (int j = 0; j < daten[i].length - start; j++) { int help = (Integer) daten[i][j + start]; double d = help; String k = (String) kopfZeile[0]; if (k.endsWith("Gesamtsitzzahl")) { dataset.setValue(d, (String) kopfZeile[j + start], "Bundestag"); } else { dataset.setValue(d, (String) kopfZeile[j + start], (String) daten[i][1]); } } } return dataset; }
public void incrementValue(double value, Comparable rowKey, Comparable columnKey) { double existing = 0.0d; Number n = getValue(rowKey, columnKey); if (n != null) { existing = n.doubleValue(); } setValue(existing + value, rowKey, columnKey); }
/** * 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 JFreeChart getChart() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); dataset.setValue(5, "Sales", "2007"); dataset.setValue(6, "Sales", "2008"); JFreeChart chart = ChartFactory.createBarChart( "BarChart", "Year", "Sales", dataset, PlotOrientation.VERTICAL, false, true, true); return chart; }
/** * A simple test for the auto-range calculation looking at a NumberAxis used as the range axis for * a CategoryPlot. */ @Test public void testAutoRange1() { 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); CategoryPlot plot = (CategoryPlot) chart.getPlot(); NumberAxis axis = (NumberAxis) plot.getRangeAxis(); assertEquals(axis.getLowerBound(), 0.0, EPSILON); assertEquals(axis.getUpperBound(), 210.0, EPSILON); }
/** * A simple test for the auto-range calculation looking at a NumberAxis used as the range axis for * a CategoryPlot. In this case, the 'autoRangeIncludesZero' flag is set to false. */ public void testAutoRange2() { 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); assertEquals(axis.getLowerBound(), 95.0, EPSILON); assertEquals(axis.getUpperBound(), 205.0, EPSILON); }
@Override protected DefaultCategoryDataset getDataOfChat() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (Map.Entry<File, List<QueryFile>> map : getResults4File().entrySet()) { for (QueryFile qf : map.getValue()) { if (qf.getAverageTime() != 0) dataset.setValue( Long.valueOf(qf.getAverageTime()), QueryType.getNameByType(qf.getQueryType()), qf.getFileName()); } } return dataset; }
private PanelTopPadecimientos() { setBackground(Color.WHITE); setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null)); estadisticaModelo = ModeloEstadistica.getInstancia((Manejador.getInstancia(false, false))); DefaultCategoryDataset dataset = new DefaultCategoryDataset(); DefaultCategoryDataset datasetPadecimiento = new DefaultCategoryDataset(); ArrayList<Estadistica> estadisticaPadecimiento = (ArrayList<Estadistica>) estadisticaModelo.getEstadisticaPadecimiento(); for (Estadistica estadist : estadisticaPadecimiento) { System.out.println(estadist.getTotal() + "\t" + estadist.getMedicoId()); long val; val = (long) estadist.getTotal(); String val2 = ""; val2 += estadist.getMedicoId(); dataset.setValue(val, "Padecimiento", val2); } JFreeChart chartPadecimiento = ChartFactory.createBarChart( "Top 10 padecimientos más frecuentados", "", "Padecimientos", dataset, PlotOrientation.VERTICAL, false, true, false); chartPadecimiento.setBackgroundPaint(Color.white); chartPadecimiento.getTitle().setPaint(Color.blue); CategoryPlot pa = chartPadecimiento.getCategoryPlot(); pa.setRangeGridlinePaint(Color.red); setLayout(null); ChartPanel panelPadecimiento = new ChartPanel(chartPadecimiento); panelPadecimiento.setBounds(47, 41, 837, 551); panelPadecimiento.setBackground(SystemColor.control); add(panelPadecimiento); }
/** * Updates the dataset values based on the scale info. * * @param chartDocument -- Current chart document. * @param data -- Data for the current chart. * @param dataset -- Dataset to be updated. * @param row -- Curent row nuber in the data. * @param column -- Current column number in the data. * @param noRowNameSpecified -- Default row name if row name is not specified. * @param noColumnName -- Default column name if column name is not specified. * @param scale -- Scale to be used to modify the dataset. */ private void updateDatasetBasedOnScale( final ChartDocument chartDocument, final ChartTableModel data, final DefaultCategoryDataset dataset, final int row, final int column, final String noRowNameSpecified, final String noColumnName, final double scale) { final String rawColumnName = JFreeChartUtils.getColumnName(data, column); final String columnName = rawColumnName != null ? rawColumnName : noColumnName + column; final Object rawRowName = JFreeChartUtils.getRawRowName(data, chartDocument, row); final String rowName = rawRowName != null ? String.valueOf(rawRowName) : (noRowNameSpecified + row); final Object rawValue = data.getValueAt(row, column); if (rawValue instanceof Number) { final Number number = (Number) rawValue; double value = number.doubleValue(); value *= scale; dataset.setValue(value, rowName, columnName); } }
public static JFreeChart generateBarChart(JSONArray IndividualPDFChart) { DefaultCategoryDataset dataSet = new DefaultCategoryDataset(); for (int i = 0; i < IndividualPDFChart.size(); i++) { JSONObject jsonChart = IndividualPDFChart.getJSONObject(i); dataSet.setValue( Double.parseDouble(jsonChart.getString(PDFConstants.PieData2)), PDFConstants.BarChartBarTitle, jsonChart.getString(PDFConstants.PieData1)); } JFreeChart chart = ChartFactory.createBarChart( PDFConstants.BarChartTitle, PDFConstants.BarChartXTitle, PDFConstants.BarChartYTitle, dataSet, PlotOrientation.VERTICAL, false, true, false); return chart; }
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; }
public void setValue(double value, Comparable rowKey, Comparable columnKey) { setValue(new Double(value), rowKey, columnKey); }
private String geraTabelaMeses( String tituloSLA, Integer idAcordoNivelServico, HttpServletRequest request, UsuarioDTO usuarioDto) throws ParseException, IOException { ControleGenerateSLAPorAcordoNivelServicoByMesAno controleGenerateSLAPorAcordoNivelServicoByMesAno = new ControleGenerateSLAPorAcordoNivelServicoByMesAno(); int m = UtilDatas.getMonth(UtilDatas.getDataAtual()); int y = UtilDatas.getYear(UtilDatas.getDataAtual()); int mPesq = (m + 1); // Faz este incremento de 1, pois logo que entrar no laço, faz um -1 String strTable = "<table width='100%' border='1'>"; String strHeader = ""; String strDados = ""; strHeader += "<tr>"; strDados += "<tr>"; DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (int i = 0; i < 6; i++) { mPesq = (mPesq - 1); if (mPesq <= 0) { mPesq = 12; y = y - 1; } strHeader = strHeader + "<td colspan='2' style='border:1px solid black; text-align:center'>"; strHeader = strHeader + (mPesq + "/" + y); strHeader = strHeader + "</td>"; List lst = controleGenerateSLAPorAcordoNivelServicoByMesAno.execute(idAcordoNivelServico, y, mPesq); double qtdeDentroPrazo = 0; double qtdeForaPrazo = 0; if (lst != null && lst.size() > 0) { for (Iterator itSLA = lst.iterator(); itSLA.hasNext(); ) { Object[] objs = (Object[]) itSLA.next(); if (((String) objs[0]).indexOf("Fora") > -1 || ((String) objs[0]).indexOf("Out") > -1) { qtdeForaPrazo = (Double) objs[2]; } else { qtdeDentroPrazo = (Double) objs[2]; } } } double qtdeDentroPrazoPerc = 0; if ((qtdeDentroPrazo + qtdeForaPrazo) > 0) { qtdeDentroPrazoPerc = (qtdeDentroPrazo / (qtdeDentroPrazo + qtdeForaPrazo)) * 100; } double qtdeForaPrazoPerc = 0; if ((qtdeDentroPrazo + qtdeForaPrazo) > 0) { qtdeForaPrazoPerc = (qtdeForaPrazo / (qtdeDentroPrazo + qtdeForaPrazo)) * 100; } strDados = strDados + "<td style='border:1px solid black'>"; strDados = strDados + UtilFormatacao.formatDouble(qtdeDentroPrazoPerc, 2) + "%"; strDados = strDados + "</td>"; strDados = strDados + "<td style='border:1px solid black'>"; strDados = strDados + UtilFormatacao.formatDouble(qtdeForaPrazoPerc, 2) + "%"; strDados = strDados + "</td>"; dataset.setValue( new Double(qtdeDentroPrazoPerc), UtilI18N.internacionaliza(request, "sla.avaliacao.noprazo"), "" + (mPesq + "/" + y)); dataset.setValue( new Double(qtdeForaPrazoPerc), UtilI18N.internacionaliza(request, "sla.avaliacao.foraprazo"), "" + (mPesq + "/" + y)); } strHeader += "</tr>"; strDados += "</tr>"; // create the chart... JFreeChart chart = ChartFactory.createBarChart( tituloSLA, // chart title UtilI18N.internacionaliza(request, "sla.avaliacao.indicadores"), // domain axis label UtilI18N.internacionaliza(request, "sla.avaliacao.resultado"), // range axis label dataset, // data PlotOrientation.VERTICAL, // orientation true, // include legend true, // tooltips? false // URLs? ); // set the background color for the chart... chart.setBackgroundPaint(Color.white); // get a reference to the plot for further customisation... CategoryPlot plot = chart.getCategoryPlot(); plot.setBackgroundPaint(Color.lightGray); plot.setDomainGridlinePaint(Color.white); plot.setDomainGridlinesVisible(true); plot.setRangeGridlinePaint(Color.white); // set the range axis to display integers only... final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); // disable bar outlines... BarRenderer renderer = (BarRenderer) plot.getRenderer(); renderer.setDrawBarOutline(false); // set up gradient paints for series... GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, new Color(0, 0, 64)); GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, new Color(0, 64, 0)); renderer.setSeriesPaint(0, gp0); renderer.setSeriesPaint(1, gp1); CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setCategoryLabelPositions( CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)); String nomeImgAval = CITCorporeUtil.caminho_real_app + "/tempFiles/" + usuarioDto.getIdUsuario() + "/avalSLAXX_" + idAcordoNivelServico + ".png"; String nomeImgAvalRel = br.com.citframework.util.Constantes.getValue("SERVER_ADDRESS") + br.com.citframework.util.Constantes.getValue("CONTEXTO_APLICACAO") + "/tempFiles/" + usuarioDto.getIdUsuario() + "/avalSLAXX_" + idAcordoNivelServico + ".png"; File arquivo2 = new File(nomeImgAval); if (arquivo2.exists()) { arquivo2.delete(); } ChartUtilities.saveChartAsPNG(arquivo2, chart, 500, 200); strTable += "<tr>"; strTable += "<td colspan='12'>"; strTable += "<img src='" + nomeImgAvalRel + "' border='0'/>"; strTable += "</td>"; strTable += "</tr>"; strTable += strHeader; strTable += strDados; strTable += "</table>"; return strTable; }