java代碼柱狀圖 java代碼生成柱狀圖

怎么用java做柱形圖?。?!

首先要有jfreechart.jar和jcommon-1.0.12.jar兩個包然后在web.xml配置

十多年的北安網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整北安建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“北安網(wǎng)站設(shè)計”,“北安網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

servlet

servlet-nameDisplayChart/servlet-name

servlet-classorg.jfree.chart.servlet.DisplayChart/servlet-class

/servlet

servlet-mapping

servlet-nameDisplayChart/servlet-name

url-pattern/DisplayChart/url-pattern

/servlet-mapping

最后是jsp代碼:

%@ page contentType="text/html;charset=GBK"%

%@ page import="org.jfree.chart.ChartFactory,

org.jfree.chart.JFreeChart,

org.jfree.chart.plot.PlotOrientation,

org.jfree.chart.servlet.ServletUtilities,

org.jfree.data.category.CategoryDataset,

org.jfree.data.general.DatasetUtilities"%

%

double[][] data = new double[][] {{1310}, {720}, {1130}, {440}};

String[] rowKeys = {"豬肉", "牛肉","雞肉", "魚肉"};

String[] columnKeys = {""};

CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);

JFreeChart chart = ChartFactory.createBarChart3D("廣州肉類銷量統(tǒng)計圖", "肉類",

"銷量",

dataset,

PlotOrientation.VERTICAL,

true,

false,

false);

String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);

String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;

%

img src="%= graphURL %"width=500 height=300 border=0 usemap="#%= filename %"

java 柱狀圖系統(tǒng)配置與實例

ChartDirector除了一個英文件的幫助以外,也沒有再提供Java DOC形式的文檔,為了方便,寫以下一個例子說明使用ChartDirector生成柱狀圖的方法.jsp方式實質(zhì)與JAVA方式?jīng)]有區(qū)別,這里是我從JSP中取的代碼(JSP改起來方便,不過手動)

代碼如下:

%@ page language="java" contentType="text/Html; charset=UTF-8"

pageEncoding="UTF-8" import="ChartDirector.*;"%

%

request.setCharacterEncoding("UTF-8");

//以兩個系列數(shù)據(jù)為例

double[] data = {185, 156, 179.5, 211, 123};

double[] data1 = {55, 76, 34.5, 88, 43};

//數(shù)據(jù)列名

String[] labels = {"一月", "二月", "三月", "四月", "五月"};

//生成圖片大小 250 x 250

XYChart c = new XYChart(550, 350);

//圖標(biāo)題

c.addTitle("第一個圖","",15);

//支持中文

c.setDefaultFonts("SIMSUN.TTC","simhei.ttf");

//圖表在圖片中的定位及區(qū)域大小

c.setPlotArea(30, 40, 400, 250);

//=========================

//加入單個數(shù)據(jù)

//BarLayer layer = c.addBarLayer(data,0xff3456,"我的測試");

//=========================

//加入多個BAR數(shù)據(jù)(多個datasets)

BarLayer layer = c.addBarLayer2(Chart.Side, 3);

layer.addDataSet(data, 0xff8080, "我測試1");

layer.addDataSet(data1, 0x008080, "你也測2");

//3d化

layer.set3D();

//設(shè)置BAR邊框形式

layer.setBarShape(0);

//bar寬度

layer.setBarWidth(50);

//設(shè)置BAR邊框顏色

//layer.setBorderColor(0xff9999);

//圖例形式

layer.setLegend(1);

//每個BAR頂部加入數(shù)據(jù)顯示

layer.setAggregateLabelStyle();

//設(shè)置BAR底部的名稱顯示

TextBox t = c.xAxis().setLabels(labels);

//名稱文字大小

t.setFontSize(9);

//加圖例

//LegendBox legend = c.addLegend(260, 120,true);

//legend.addKey("錢財",0xff8080);

//圖例位置

c.addLegend(450, 120,true);

//output the chart

String chart1URL = c.makeSession(request, "chart1");

//include tool tip for the chart

String imageMap1 = c.getHTMLImageMap("#", "", "title='{xLabel}: US${value}K'");

%!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

html

head

meta http-equiv="Content-Type" content="text/html; charset=UTF-8"

title圖表測試/title

/head

body

h1中文/h1

hr color="#000080"

br

img src='%=response.encodeURL("getchart.jsp?"+chart1URL)%'

usemap="#map1" border="0"

map name="map1"%=imageMap1%/map

/body

/html

資料引用:

eclipse中用JAVA代碼怎么畫柱形圖表

用jfreechart

jfreechart繪制柱狀圖

import java.io.File;

import java.io.IOException;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartUtilities;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.data.category.DefaultCategoryDataset;

/*

* 繪制柱狀圖

*你亮哥

* */

public class BarChart3DDemo

{

public static void main(String[] args)

{

try

{

//設(shè)置主題

ChartFactory.setChartTheme(Theme.getTheme());

//構(gòu)造數(shù)據(jù)

DefaultCategoryDataset dataset = new DefaultCategoryDataset();

dataset.addValue(100, "JAVA","1");

dataset.addValue(200, "js","1");

dataset.addValue(200, "C++", "2");

dataset.addValue(300, "C", "3");

dataset.addValue(400, "HTML", "4");

dataset.addValue(400, "CSS", "5");

/*

* public static JFreeChart createBarChart3D(

* java.lang.String title, 設(shè)置圖表的標(biāo)題

* java.lang.String categoryAxisLabel, 設(shè)置分類軸的標(biāo)示

* java.lang.String valueAxisLabel, 設(shè)置值軸的標(biāo)示

* CategoryDataset dataset, 設(shè)置數(shù)據(jù)

* PlotOrientation orientation, 設(shè)置圖表的方向

* boolean legend, 設(shè)置是否顯示圖例

* boolean tooltips,設(shè)置是否生成熱點工具

* boolean urls) 設(shè)置是否顯示url

*/

JFreeChart chart = ChartFactory.createBarChart3D("編程語言統(tǒng)計", "語言",

"學(xué)習(xí)人數(shù)", dataset, PlotOrientation.VERTICAL, true, false,

false);

//保存圖表

ChartUtilities.saveChartAsPNG(new File("E:/chart/BarChart3D.png"), chart, 800, 500);

System.out.println("繪圖完成");

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

===================================================================================

//一條線 有點 有數(shù)

package Test;

import java.awt.Color;

import java.awt.Font;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartFrame;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.AxisSpace;

import org.jfree.chart.labels.ItemLabelAnchor;

import org.jfree.chart.labels.ItemLabelPosition;

import org.jfree.chart.labels.StandardXYItemLabelGenerator;

import org.jfree.chart.plot.XYPlot;

import org.jfree.chart.renderer.xy.XYItemRenderer;

import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

import org.jfree.chart.title.TextTitle;

import org.jfree.data.time.Month;

import org.jfree.data.time.TimeSeries;

import org.jfree.data.time.TimeSeriesCollection;

import org.jfree.ui.RectangleInsets;

import org.jfree.ui.TextAnchor;

public class try123 {

public static void main(String[] args){

//首先構(gòu)造數(shù)據(jù)

TimeSeries timeSeries = new TimeSeries("BMI", Month.class);

// 時間曲線數(shù)據(jù)集合

TimeSeriesCollection lineDataset = new TimeSeriesCollection();

// 構(gòu)造數(shù)據(jù)集合

timeSeries.add(new Month(1, 2009), 45);

timeSeries.add(new Month(2, 2009), 46);

timeSeries.add(new Month(3, 2009), 1);

timeSeries.add(new Month(4, 2009), 500);

timeSeries.add(new Month(5, 2009), 43);

timeSeries.add(new Month(6, 2009), 324);

timeSeries.add(new Month(7, 2009), 632);

timeSeries.add(new Month(8, 2009), 34);

timeSeries.add(new Month(9, 2009), 12);

timeSeries.add(new Month(10, 2009), 543);

timeSeries.add(new Month(11, 2009), 32);

timeSeries.add(new Month(12, 2009), 225);

lineDataset.addSeries(timeSeries);

JFreeChart chart = ChartFactory.createTimeSeriesChart("", "date", "bmi", lineDataset, true, true, true);

//增加標(biāo)題

chart.setTitle(new TextTitle("XXXBMI指數(shù)", new Font("隸書", Font.ITALIC, 15)));

chart.setAntiAlias(true);

XYPlot plot = (XYPlot) chart.getPlot();

plot.setAxisOffset(new RectangleInsets(10,10,10,10));//圖片區(qū)與坐標(biāo)軸的距離

plot.setOutlinePaint(Color.PINK);

plot.setInsets(new RectangleInsets(15,15,15,15));//坐標(biāo)軸與最外延的距離

// plot.setOrientation(PlotOrientation.HORIZONTAL);//圖形的方向,包括坐標(biāo)軸。

AxisSpace as = new AxisSpace();

as.setLeft(25);

as.setRight(25);

plot.setFixedRangeAxisSpace(as);

chart.setPadding(new RectangleInsets(5,5,5,5));

chart.setNotify(true);

// 設(shè)置曲線是否顯示數(shù)據(jù)點

XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();

xylineandshaperenderer.setBaseShapesVisible(true);

// 設(shè)置曲線顯示各數(shù)據(jù)點的值

XYItemRenderer xyitem = plot.getRenderer();

xyitem.setBaseItemLabelsVisible(true);

xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.INSIDE10, TextAnchor.BASELINE_LEFT));

xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());

xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 14));

plot.setRenderer(xyitem);

//顯示

ChartFrame frame = new ChartFrame("try1", chart);

frame.pack();

frame.setVisible(true);

}

}

怎樣用JAVA來實現(xiàn)在網(wǎng)頁中制作柱狀圖

JFreeChart是JAVA平臺上的一個開放的圖表繪制類庫。它完全使用JAVA語言編寫,是為applications, applets, servlets 以及JSP等使用所設(shè)計。JFreeChart可生成餅圖(pie charts)、柱狀圖(bar charts)、散點圖(scatter plots)、時序圖(time series)、甘特圖(Gantt charts)等等多種圖表,并且可以產(chǎn)生PNG和JPEG格式的輸出,還可以與PDF和EXCEL關(guān)聯(lián)。

JFreeChart的主頁地址為:

在這里可以找到最新版本的JFreeChart的相關(guān)信息,如說明文檔、下載連接以及示例圖表等。

JFreeChart目前是最好的java圖形解決方案,基本能夠解決目前的圖形方面的需求。

IBM文檔:

Javaeye社區(qū):

當(dāng)前標(biāo)題:java代碼柱狀圖 java代碼生成柱狀圖
分享路徑:http://muchs.cn/article44/dosjihe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、域名注冊搜索引擎優(yōu)化、微信公眾號、、用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)