java实现柱状图代码,java 柱状图

怎样用JAVA来实现在网页中制作柱状图

JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计。JFreeChart可生成饼图(pie charts)、柱状图(bar charts)、散点图(scatter plots)、时序图(time series)、甘特图(Gantt charts)等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联。

10年的安丘网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整安丘建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“安丘网站设计”,“安丘网站推广”以来,每个客户项目都认真落实执行。

JFreeChart的主页地址为:

在这里可以找到最新版本的JFreeChart的相关信息,如说明文档、下载连接以及示例图表等。

JFreeChart目前是最好的java图形解决方案,基本能够解决目前的图形方面的需求。

IBM文档:

Javaeye社区:

java 柱状图系统配置与实例

ChartDirector除了一个英文件的帮助以外,也没有再提供Java DOC形式的文档,为了方便,写以下一个例子说明使用ChartDirector生成柱状图的方法.jsp方式实质与JAVA方式没有区别,这里是我从JSP中取的代码(JSP改起来方便,不过手动)

代码如下:

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

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

%

request.setCharacterEncoding("UTF-8");

//以两个系列数据为例

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

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

//数据列名

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

//生成图片大小 250 x 250

XYChart c = new XYChart(550, 350);

//图标题

c.addTitle("第一个图","",15);

//支持中文

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

//图表在图片中的定位及区域大小

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

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

//加入单个数据

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

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

//加入多个BAR数据(多个datasets)

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

layer.addDataSet(data, 0xff8080, "我测试1");

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

//3d化

layer.set3D();

//设置BAR边框形式

layer.setBarShape(0);

//bar宽度

layer.setBarWidth(50);

//设置BAR边框颜色

//layer.setBorderColor(0xff9999);

//图例形式

layer.setLegend(1);

//每个BAR顶部加入数据显示

layer.setAggregateLabelStyle();

//设置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

{

//设置主题

ChartFactory.setChartTheme(Theme.getTheme());

//构造数据

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, 设置图表的标题

* java.lang.String categoryAxisLabel, 设置分类轴的标示

* java.lang.String valueAxisLabel, 设置值轴的标示

* CategoryDataset dataset, 设置数据

* PlotOrientation orientation, 设置图表的方向

* boolean legend, 设置是否显示图例

* boolean tooltips,设置是否生成热点工具

* boolean urls) 设置是否显示url

*/

JFreeChart chart = ChartFactory.createBarChart3D("编程语言统计", "语言",

"学习人数", 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();

}

}

}

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

//一条线 有点 有数

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){

//首先构造数据

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

// 时间曲线数据集合

TimeSeriesCollection lineDataset = new TimeSeriesCollection();

// 构造数据集合

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);

//增加标题

chart.setTitle(new TextTitle("XXXBMI指数", new Font("隶书", Font.ITALIC, 15)));

chart.setAntiAlias(true);

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

plot.setAxisOffset(new RectangleInsets(10,10,10,10));//图片区与坐标轴的距离

plot.setOutlinePaint(Color.PINK);

plot.setInsets(new RectangleInsets(15,15,15,15));//坐标轴与最外延的距离

// plot.setOrientation(PlotOrientation.HORIZONTAL);//图形的方向,包括坐标轴。

AxisSpace as = new AxisSpace();

as.setLeft(25);

as.setRight(25);

plot.setFixedRangeAxisSpace(as);

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

chart.setNotify(true);

// 设置曲线是否显示数据点

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

xylineandshaperenderer.setBaseShapesVisible(true);

// 设置曲线显示各数据点的值

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做柱形图!!!

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

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("广州肉类销量统计图", "肉类",

"销量",

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实现柱状图代码,java 柱状图
文章URL:http://scjbc.cn/article/hshphg.html

其他资讯