前台:

<?page title="Grid使用" contentType="text/html;charset=UTF-8"?>
<zk xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.zkoss.org/2005/zul"
xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul"> <window border="none" apply="test.GrdiCtrl">
<grid >
<columns sizable="true" >
<column label="图表" align="center" />
</columns>
<rows id="rows"> </rows>
</grid> </window>
</zk>

后台:

package test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.util.Random; import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.CategoryModel;
import org.zkoss.zul.Cell;
import org.zkoss.zul.Chart;
import org.zkoss.zul.PieModel;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.SimpleCategoryModel;
import org.zkoss.zul.SimplePieModel; import chart.LineChartEngine;
import chart.TestEngine; public class GrdiCtrl extends GenericAutowireComposer<Component>{ private static final long serialVersionUID = 1L; // colors
public static Color COLOR_1 = new Color(0x38EB38);
public static Color COLOR_2 = new Color(0xFE9900);
public static Color COLOR_3 = new Color(0xEF6AD5);
public static Color COLOR_4 = new Color(0xFFFF00);
public static Color COLOR_5 = new Color(0x66CBFF);
public static Color COLOR_6 = new Color(0x9FFE80); private static Paint[] colors1 = new Paint[]{COLOR_2, COLOR_1}; private Rows rows; @Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp); rows.appendChild(addRow());
} private Row addRow() {
int count = 1;
Row row = new Row(); //饼状图
Chart mychart1 = new Chart(); PieModel model = new SimplePieModel();
model.setValue("已使用", new Double(2.0));
model.setValue("未使用", new Double(3.0)); TestEngine engine1 = new TestEngine();
engine1.setColors(colors1);
mychart1.setModel(model);
mychart1.setEngine(engine1); mychart1.setId("chart" + (3*count+1)); mychart1.setWidth("250px");
mychart1.setHeight("125px"); Cell cell2 = new Cell();
cell2.appendChild(mychart1); //折线图
CategoryModel model1 = new SimpleCategoryModel();
Chart linechart1 = new Chart();
linechart1.setType("line");
linechart1.setHeight("250px");
linechart1.setWidth("500px");
linechart1.setThreeD(false);
linechart1.setFgAlpha(128); model1.setValue("CPU使用率", "Category 1", 10 * new Random().nextInt(7));
model1.setValue("CPU使用率", "Category 2", 10 * new Random().nextInt(7));
model1.setValue("CPU使用率", "Category 3", 10 * new Random().nextInt(7));
model1.setValue("CPU使用率", "Category 4", 10 * new Random().nextInt(7));
model1.setValue("CPU使用率", "Category 5", 10 * new Random().nextInt(7)); model1.setValue("CPU使用率2", "Category 1", 10 * new Random().nextInt(7));
model1.setValue("CPU使用率2", "Category 2", 10 * new Random().nextInt(7));
model1.setValue("CPU使用率2", "Category 3", 10 * new Random().nextInt(7));
model1.setValue("CPU使用率2", "Category 4", 10 * new Random().nextInt(7));
model1.setValue("CPU使用率2", "Category 5", 10 * new Random().nextInt(7)); linechart1.setModel(model1);
LineChartEngine lce = new LineChartEngine(2,5);
lce.setMaxRange(100);
linechart1.setEngine(lce);
Font titlefont = new Font("黑体", Font.PLAIN, 12); linechart1.setTitle("CPU(%)");//设置top标题 Font subfont = new Font("黑体", Font.PLAIN, 12);
linechart1.setYAxisFont(subfont);//设置字体,防中文乱码
linechart1.setYAxis("数值");//设置left标题 Cell cell3 = new Cell();
cell3.appendChild(linechart1);
//
linechart1.setXAxis("08 09 10 11 12");
//Font f = new Font("Serif",Font.PLAIN,11);
//linechart1.setXAxisFont(f);
linechart1.setTitleFont(titlefont); row.appendChild(cell2);
row.appendChild(cell3); row.setStyle("background:#ffffff");
return row;
} }

饼图引擎:

package com.zk.ctrl.chart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke; import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PiePlot;
import org.zkoss.zkex.zul.impl.JFreeChartEngine;
import org.zkoss.zul.Chart; public class TestEngine extends JFreeChartEngine { private static final long serialVersionUID = 1L; Paint[] colors; public Paint[] getColors() {
return colors;
} public void setColors(Paint[] colors) {
this.colors = colors;
} public boolean prepareJFreeChart(JFreeChart jfchart, Chart chart) {
jfchart.setBorderPaint(Color.white);
jfchart.setBorderVisible(true);
Stroke s = new BasicStroke(10.0f);
jfchart.setBorderStroke(s); PiePlot piePlot = (PiePlot) jfchart.getPlot();
// 璁剧疆鑳屾櫙閫忔槑搴?
piePlot.setBackgroundAlpha(1.0f); piePlot.setBaseSectionOutlinePaint(new Color(0xEEEEEE));
piePlot.setSectionOutlinesVisible(false);
piePlot.setLabelBackgroundPaint(Color.WHITE);
piePlot.setLabelOutlinePaint(null);
piePlot.setLabelShadowPaint(null);
Font axisFont = new Font("榛戜綋", Font.PLAIN, 12);
jfchart.getLegend().setVisible(false);
piePlot.setLabelFont(axisFont); piePlot.setOutlinePaint(null); // override some default colors
DefaultDrawingSupplier defaults = new DefaultDrawingSupplier();
piePlot.setDrawingSupplier(new DefaultDrawingSupplier(colors,
new Paint[] { defaults.getNextFillPaint() },//
new Paint[] { defaults.getNextOutlinePaint() }, //
new Stroke[] { defaults.getNextStroke() },//
new Stroke[] { defaults.getNextOutlineStroke() },//
new Shape[] { defaults.getNextShape() }));// piePlot.setShadowPaint(Color.white); //
piePlot.setSectionOutlinesVisible(false); return false;
}
}

拆线图引擎

package chart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font; import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;
import org.zkoss.zkex.zul.impl.JFreeChartEngine;
import org.zkoss.zul.Chart; public class LineChartEngine extends JFreeChartEngine { private static final long serialVersionUID = 1L; private int width = 5;
private boolean showLine = true;
private boolean showShape = true;
private int series;
private int total; public LineChartEngine() { } public LineChartEngine(int series, int total) {
this.series = series;
this.total = total;
} public float minRange = 0.0f;
public float maxRange = 100.0f; public boolean prepareJFreeChart(JFreeChart jfchart, Chart chart) {
jfchart.setBackgroundPaint(Color.WHITE);
TextTitle subtitle = new TextTitle("2007年度", new Font("黑体", Font.BOLD, 12));
jfchart.addSubtitle(subtitle);
jfchart.setBorderStroke(null); CategoryPlot plot = (CategoryPlot) jfchart.getPlot(); plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
plot.setBackgroundPaint(Color.WHITE);
plot.setOutlinePaint(Color.RED);// 边界线 LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); CategoryPlot plot1 = jfchart.getCategoryPlot(); plot1.setDomainGridlinesVisible(true);
plot1.setRangeGridlinesVisible(true);
plot1.setBackgroundPaint(Color.green);
// 设置曲线图与xy轴的距离
plot1.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 0d));
plot.setAxisOffset(new RectangleInsets(0d, 0d, 0d, 0d)); Font font = new Font("宋体", Font.PLAIN, 10);
CategoryAxis categoryAxis = (CategoryAxis) plot1.getDomainAxis();
categoryAxis.setTickLabelFont(font);
// Y轴
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
numberaxis.setTickUnit(new NumberTickUnit(2D));
// numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// 横轴
CategoryAxis dateaxis = (CategoryAxis) plot.getDomainAxis();
dateaxis.setMinorTickMarksVisible(false);
Font f = new Font("Serif", Font.PLAIN, 1);
plot.getDomainAxis().setTickLabelFont(f);
// plot.getDomainAxis(
// dateaxis.sett
numberaxis.setAutoRangeIncludesZero(true);
numberaxis.setAutoTickUnitSelection(true);
// numberaxis.setAutoRangeMinimumSize(0.01f);
numberaxis.setRange(minRange, maxRange);
// //获取同网卡的读或写
// int num=0;
// if(series>=total/2){
// num=series-total/2;
// }else{
// num=series+total/2;
// }
if (series >= 0) {
if (series >= total / 2) {
renderer.setSeriesLinesVisible(1, showLine);// 是否显示数据点连线
renderer.setSeriesShapesVisible(1, showShape);// 是否显示数据节点
renderer.setLegendTextFont(1, font);
renderer.setSeriesStroke(1, new BasicStroke(width)); renderer.setSeriesLinesVisible(0, showLine);// 0123,4567
renderer.setSeriesShapesVisible(0, showShape);
renderer.setLegendTextFont(0, font);
renderer.setSeriesStroke(0, new BasicStroke(1));
} else {
renderer.setSeriesLinesVisible(0, showLine);// 0123,4567
renderer.setSeriesShapesVisible(0, showShape);
renderer.setLegendTextFont(0, font);
renderer.setSeriesStroke(0, new BasicStroke(width)); renderer.setSeriesLinesVisible(1, showLine);// 0123,4567
renderer.setSeriesShapesVisible(1, showShape);
renderer.setLegendTextFont(1, font);
renderer.setSeriesStroke(1, new BasicStroke(1));
} } // 显示数据点数值
renderer.setBaseShapesVisible(true);
renderer.setBaseItemLabelsVisible(true);
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,
TextAnchor.BASELINE_LEFT));
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelFont(new Font("宋体", 5, 12));
// for (int i = 0; i < total; i++) {
// if(i==series||i==num){
// continue;
// }
// renderer.setSeriesStroke(i, new BasicStroke(1));//设置线条粗细
// renderer.setSeriesShapesVisible(i, showShape);//数据点不可见
// renderer.setSeriesVisibleInLegend(i, false);//设置线条的图例不可见
// renderer.setLegendTextFont(i, font);//设置图例字体样式
// renderer.setSeriesLinesVisible(i, false);//曲线是否可见
// }
return false;
} public void setWidth(int width) {
this.width = width;
} public void setShowLine(boolean showLine) {
this.showLine = showLine;
} public void setShowShape(boolean showShape) {
this.showShape = showShape;
} public void setMinRange(float minRange) {
this.minRange = minRange;
} public void setMaxRange(float maxRange) {
this.maxRange = maxRange;
}
}

效果图:

jar包下载

ZK 使用jfreeChart的更多相关文章

  1. zk 起别名时候碰到的问题

    第一次搭建时候都是用的ip,没什么问题,看到别人都是用的别名,于是也想试试把ip改成别名.然而 其中碰到的问题 ,快一周了才解决,现在记录下: 1.改主机别名 一直以为 修改 /etc/hosts 里 ...

  2. 基于ZK构建统一配置中心的方案和实践

    背景: 近期使用Zk实现了一个简单的配置管理的小东西,在此开源出来,有兴趣的希望提出您的宝贵意见.如果恰巧您也使用或者接触过类似的东西, 也希望您可以分享下您觉得现在这个项目可以优化和改进的地方. 项 ...

  3. 【Java EE 学习 74 下】【数据采集系统第六天】【使用Jfreechart的统计图实现】【将JFreechart整合到项目中】

    之前说了JFreechart的基本使用方法,包括生成饼图.柱状统计图和折线统计图的方法.现在需要将其整合到数据采集系统中根据调查结果生成三种不同的统计图. 一.统计模型的分析和设计 实现统计图显示的流 ...

  4. 【Java EE 学习 74 上】【数据采集系统第六天】【使用Jfreechart的统计图实现】【Jfreechart的基本使用方法】

    之前已经实现了数据的采集,现在已经有了基本的数据,下一步就需要使用这些数据实现统计图的绘制了.这里使用Jfreechart实现这些统计图的绘制.首先看一下Jfreechart的基本用法,只有知道了它的 ...

  5. JFreeChart

    花了四个小时给同学写的.还行吧,原来都没有用过到处找资料写的. package DrawLine; import org.jfree.chart.ChartFactory; import org.jf ...

  6. JFreechart在linux下不显示及中文乱码问题

    一.使用JFreeChart建的报表,在window下能正常显示,但是放到linux下就报错,而且有时候会把tomcat挂掉, 原因是jfreechart的在linux系统中需要访问java awt库 ...

  7. JFreechart 在linux下不显示及中文乱码问题

    一.使用JFreeChart建的报表,在window下能正常显示,但是放到linux下就报错,而且有时候会把tomcat挂掉, 原因是jfreechart的在linux系统中需要访问java awt库 ...

  8. dubbo zk 分布式服务项目搭建与配置

    1. 项目 jar  -----提供接口 2. 项目 jar  -----接口实现   provider启动zk main方法启动 start applicationContext.xml <b ...

  9. ZK 最少限度加载页面js文件

    官方文档说明: ZK Developer's Reference文档,章节为Minimize Number of JavaScript Files to Load,按照文档步骤执行,最后需在 web. ...

随机推荐

  1. 海量日志分析方案--logstash+kibnana+kafka

    下图为唯品会在qcon上面公开的日志处理平台架构图.听后觉得有些意思,好像也可以很容易的copy一个,就动手尝试了一下. 目前只对flume===>kafka===>elacsticSea ...

  2. 【翻译】Fluent NHibernate介绍和入门指南

    英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...

  3. NPA——.NET Persistence API

    你可曾听说过JPA. 有JPA那么就一定有NPA. 软件架构的路上一定少不了这个名词. —————————————————————————————————————————————— P Persist ...

  4. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

  5. [转]C/C++ 程序员必须收藏的资源大全

    from: https://github.com/jobbole/awesome-cpp-cn C++ 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome – XXX 系列 ...

  6. 使用Jenkins配置Git和Maven的自动化构建

    Jenkins是一个开源的持续集成工具,应用Jenkins搭建持续集成环境,可以进行自动构建.自动编译和部署,非常方便. 在服务器比较少的情况下,Jenkins的优势并不明显,但是随着项目发展,服务器 ...

  7. Html和CSS布局技巧

    单列布局水平居中 水平居中的页面布局中最为常见的一种布局形式,多出现于标题,以及内容区域的组织形式,下面介绍四种实现水平居中的方法(注:下面各个实例中实现的是child元素的对齐操作,child元素的 ...

  8. [BI项目记]-新任务创建

    上一篇介绍了如何处理一个Bug工作,此篇主要介绍如何借助TFS对于一个新需求创建一个新的工作项. 这里假定,有一个新的需求,需要创建五个报表. 然后开发的工作流程如下: 这个流程总结起来大致如下: 首 ...

  9. 基于Solr的空间搜索

    如果需要对带经纬度的数据进行检索,比如查找当前所在位置附近1000米的酒店,一种简单的方法就是:获取数据库中的所有酒店数据,按经纬度计算距离,返回距离小于1000米的数据. 这种方式在数据量小的时候比 ...

  10. NetMQ(二): 请求响应模式 Request-Reply

    ZeroMQ系列 之NetMQ 一:zeromq简介 二:NetMQ 请求响应模式 Request-Reply 三:NetMQ 发布订阅模式 Publisher-Subscriber 四:NetMQ ...