java代码显示柱状图 java图表控件

java中使用jfreechart做柱状图,在jsp中显示有问题 , jfreechart中显示柱状图到jsp页面:
package com.test.jfreechart;
import java.awt.Font;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
【java代码显示柱状图 java图表控件】import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
public class JFreeChartTest2 extends ApplicationFrame
{
public JFreeChartTest2(String title)
{
super(title);
this.setContentPane(createPanel()); //构造函数中自动创建Java的panel面板
}
public static CategoryDataset createDataset() //创建柱状图数据集
{
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
dataset.setValue(10,"a","管理人员");
dataset.setValue(20,"b","市场人员");
dataset.setValue(40,"c","开发人员");
dataset.setValue(15,"d","其他人员");
return dataset;
}
public static JFreeChart createChart(CategoryDataset dataset) //用数据集创建一个图表
{
JFreeChart chart=ChartFactory.createBarChart("hi", "人员分布",
"人员数量", dataset, PlotOrientation.VERTICAL, true, true, false); //创建一个JFreeChart
chart.setTitle(new TextTitle("某公司组织结构图",new Font("宋体",Font.BOLD Font.ITALIC,20)));//可以重新设置标题 , 替换“hi”标题
CategoryPlot plot=(CategoryPlot)chart.getPlot();//获得图标中间部分,即plot
CategoryAxis categoryAxis=plot.getDomainAxis();//获得横坐标
categoryAxis.setLabelFont(new Font("微软雅黑",Font.BOLD,12));//设置横坐标字体
return chart;
}
public static JPanel createPanel()
{
JFreeChart chart =createChart(createDataset());
return new ChartPanel(chart); //将chart对象放入Panel面板中去,ChartPanel类已继承Jpanel
}
public static void main(String[] args)
{
JFreeChartTest2 chart=new JFreeChartTest2("某公司组织结构图");
chart.pack();//以合适的大小显示
chart.setVisible(true);
}
}
运行结果:
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;
/*
* 绘制柱状图
*java代码显示柱状图你亮哥
* */
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代码显示柱状图的标题
* java.lang.String categoryAxisLabel, 设置分类轴java代码显示柱状图的标示
* java.lang.String valueAxisLabel, 设置值轴java代码显示柱状图的标示
* 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);//图形的方向java代码显示柱状图,包括坐标轴 。
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 柱状图系统配置与实例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 = https://www.04ip.com/post/{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='https://www.04ip.com/post/%=response.encodeURL("getchart.jsp?" chart1URL)%'
usemap="#map1" border="0"
map name="map1"%=imageMap1%/map
/body
/html
资料引用:
怎么用java做柱形图?。。?/h2>首先要有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 = https://www.04ip.com/post/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="https://www.04ip.com/post/%= graphURL %"width=500 height=300usemap="#%= filename %"
关于java代码显示柱状图和java图表控件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读