O Jfreechart é uma biblioteca de classes de desenho de gráficos abertos na plataforma Java. Está escrito completamente no idioma Java e foi projetado para aplicativos, applets, servlets e JSP. O Jfreechart pode gerar gráficos de pizza, gráficos de barras, gráficos de dispersão, séries temporais, gráficos de Gantt e outros gráficos, e podem gerar saída nos formatos PNG e JPEG e também podem ser associados ao PDF e ao Excel.
Exemplo de gráfico de linha
pacote com.sprite.test; importar java.io.file; importar java.io.filenotfoundException; importar java.io.fileOutputStream; importar java.io.ioException; importar org.jfree.Chart.ChartFactory; importar org.jfree.Chart.Chartutities; importar org.jfree.chart.jfreechart; importar org.jfree.chart.labels.standardcategoryItemLabelGenerator; importar org.jfree.chart.plot.categoryplot; importar org.jfree.chart.plot.plotorientation; importar org.jfree.chart.renderer.category.LineAndshapenderer; importar org.jfree.data.category.categoryDataset; importar org.jfree.data.general.datasetutities; // gráfico de linha Jfreechart (gráfico de linha) classe pública TestJfreechart {/** * Crie o gráfico de linha Jfreechart (gráfico de linha) */public static void main (string [] args) {// etapa 1: crie categorydataset objeto (preparar dados) categorydataSet DATASET = criado (); // Etapa 2: Gere o objeto jfreechart de acordo com o DataSet e faça as configurações correspondentes jfreechart Freechart = createChart (DataSet); // Etapa 3: Saia o objeto jfreechart para um arquivo, fluxo de saída do servlet, etc. SAVEASFILE (FREECHART, "e: //line.jpg", 600, 400); } // Salvar como um arquivo public static void SAVEASFILE (gráfico Jfreechart, String OutputPath, int Weight, int Hight) {FileOutputStream Out = NULL; tente {arquivo outfile = novo arquivo (outputPath); if (! outfile.getParentFile (). Exist ()) {outfile.getParentFile (). mkdirs (); } out = new FileOutputStream (outputPath); // Salvar como png // Chartutilities.WritecharTaspng (OUT, Chart, 600, 400); // Salvar como JPEG Chartutilities.Writechartasjpeg (OUT, Chart, 600, 400); out.flush (); } catch (filenotfoundException e) {e.printStackTrace (); } catch (ioexception e) {e.printStackTrace (); } finalmente {if (out! = null) {try {out.close (); } catch (ioexception e) {// não faz nada}}}}} // Crie um objeto jfreechart baseado na categorydataSet public static jfreechart createChart (categorydataSetcoryDataSet) {// create jfreecht Object: ChartFactor por hora ", // title" Ano Divisão ", // categoryaxislabel (eixo da categoria, eixo horizontal, etiqueta x-exis)" Quantity ", // valueaxislabel (eixo de valor, eixo vertical) categorysets), // eixo de eixo vertical); // URLs // Use a categoryplot para definir vários parâmetros. As seguintes configurações podem ser omitidas. Plot categoryplot = (categoryplot) jfreechart.getplot (); // Plot de transparência de cor de fundo. // Plot de transparência de cor em primeiro plano. // Para outras configurações, consulte a classe CategoryPlot lineandshapenderer renderer = (linendshapenderer) plot.getRenderer (); renderer.setBaseshapeSvisible (true); // o ponto da série (ou seja, o ponto de dados) pode ser visível renderizador.setBaseLinesVisible (true); // Série Há uma conexão entre pontos (ou seja, pontos de dados) e pode ser visto renderer.setUSeserieffset (true); // Defina o renderizador de deslocamento.SetBaseItemLabelGenerator (novo StandardCategoryItemLabelGenerator ()); renderer.setBaseItemLabelsVisible (true); retornar Jfreechart; } / *** Crie um objeto de categoryDataSet** / public static categoryDataSet criouTASET () {String [] rowkeys = {"A Platform"}; String [] Colkeys = {"0:00", "1:00", "2:00", "7:00", "8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "16:00", "20:00", "21:00", "23:00"; duplo [] [] dados = {{4, 3, 1, 1, 1, 1, 2, 2, 2, 1, 8, 2, 2, 1, 1},}; // ou use código semelhante ao seguinte // DefaultCategoryDataSet CategoryDataSet = new // DefaultCategoryDataset (); // categorydataset.addvalue (10, "RowKey", "Colkey"); Retornar DataSeTutities.CreateCategoryDataSet (RowKeys, Colkeys, Data); }}Gerar renderizações:
O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.