RCP项目应用jfree实现图形报表中的柱状图与曲线图
RCP项目使用jfree实现图形报表中的柱状图与曲线图
此案例摘取自工作中的功能案例,简单使用不需要进行如果封装数据.如参数类.
RCP项目使用jfree实现图形报表中的柱状图与曲线图.
结构说明:
1. JfreechartUtil.java 实现生成图表数据及生成图表.
2. PlotTempDialog.java 图表显示窗体.
3. PlotParameterVO.java 图表参数类.
4. PlotData.java 封装从显示的数据,从数据库获得
5. 需要相关的JAR文件支持,于附件.
执行PlotTempDialog类的main入口测试.
JfreechartUtil.java
package com.yfkj.shop.util; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.lang.reflect.Method; import java.text.DecimalFormat; import java.util.List; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.axis.ValueAxis; import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.category.BarRenderer; import org.jfree.chart.renderer.category.LineAndShapeRenderer; import org.jfree.chart.title.LegendTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.general.PieDataset; import org.jfree.experimental.chart.swt.ChartComposite; import com.yfkj.shop.ui.util.DialogFactory; import com.yfkj.shop.ui.util.RefMethod; /** * 图形报表工具类 * * @author YinDang CreateTime:2011-7-15 * */ public class JfreechartUtil { /** 饼状图 **/ public static final int PIE = 1; /** 柱状图 **/ public static final int BAR = 2; /** 曲线图 **/ public static final int LINEANDAREA = 3; /** * 创建柱状图 * * @param dataset * 数据集 * @param xName * @param yName * @param chartTitle * 标题 * @param charName * 名称 * @param parent * 父容器 * @param chartType * 创建类型 */ public static ChartComposite createBarChart(CategoryDataset dataset, String xName, String yName, String chartTitle, String charName, Composite parent) { JFreeChart chart = ChartFactory.createBarChart(chartTitle, // 图表标题 // xName, // 目录轴的显示标签 yName, // 数值轴的显示标签 dataset, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例(对于简单的柱状图必须是false) true, // 是否生成工具 false // 是否生成URL链接 ); // 为一个页面上面两个图表,隐藏上面图表的X. ((CategoryPlot) chart.getPlot()).getDomainAxis().setVisible(false); chart.getLegend().setVisible(false); Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); //$NON-NLS-1$ /* * VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭, * 使用的关闭抗锯齿后,字体尽量选择12到14号的宋体字,这样文字最清晰好看 */ // chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); chart.setTextAntiAlias(false);// 字体模糊边界 chart.setBackgroundPaint(Color.white); // create plot CategoryPlot plot = chart.getCategoryPlot(); // 设置横虚线可见 plot.setRangeGridlinesVisible(true); // 虚线色彩 plot.setRangeGridlinePaint(Color.gray); // 数据轴精度 NumberAxis vn = (NumberAxis) plot.getRangeAxis(); // vn.setAutoRangeIncludesZero(true); DecimalFormat df = new DecimalFormat("#0.00"); //$NON-NLS-1$ vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式 // x轴设置 CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setLabelFont(labelFont);// 轴标题 domainAxis.setTickLabelFont(labelFont);// 轴数值 // 设置图例中的字体 LegendTitle legend = chart.getLegend(); legend.setItemFont(labelFont); // 图示项字体 // Lable(Math.PI/3.0)度倾斜 domainAxis.setCategoryLabelPositions(CategoryLabelPositions .createUpRotationLabelPositions(Math.PI / 3.0)); domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);// 横轴上的 Lable 是否完整显示 // 设置距离图片左端距离 domainAxis.setLowerMargin(0.1); // 设置距离图片右端距离 domainAxis.setUpperMargin(0.1); // 设置 columnKey 是否间隔显示 // domainAxis.setSkipCategoryLabelsToFit(true); plot.setDomainAxis(domainAxis); // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确) plot.setBackgroundPaint(new Color(255, 255, 204)); // y轴设置 ValueAxis rangeAxis = plot.getRangeAxis(); rangeAxis.setLabelFont(labelFont); rangeAxis.setTickLabelFont(labelFont); // 设置最高的一个 Item 与图片顶端的距离 rangeAxis.setUpperMargin(0.15); // 设置最低的一个 Item 与图片底端的距离 rangeAxis.setLowerMargin(0.15); plot.setRangeAxis(rangeAxis); BarRenderer renderer = new BarRenderer(); // 设置柱子宽度 renderer.setMaximumBarWidth(0.1); // 设置柱子高度 renderer.setMinimumBarLength(0.2); // 设置柱子边框颜色 renderer.setBaseOutlinePaint(Color.BLACK); // 设置柱子边框可见 renderer.setDrawBarOutline(true); // // 设置柱的颜色 // renderer.setSeriesPaint(0, new Color(204, 255, 255)); // renderer.setSeriesPaint(1, new Color(153, 204, 255)); // renderer.setSeriesPaint(2, new Color(51, 204, 204)); // 设置每个地区所包含的平行柱的之间距离 renderer.setItemMargin(0.0); // 显示每个柱的数值,并修改该数值的字体属性 renderer.setIncludeBaseInRange(true); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); plot.setRenderer(renderer); // 设置柱的透明度 plot.setForegroundAlpha(1.0f); if (parent.getChildren().length > 0) { Control[] cons = parent.getChildren(); for (Control control : cons) { control.dispose(); } } ChartComposite composite = new ChartComposite(parent, SWT.BORDER, chart, true); composite.setLayout(new FillLayout()); composite.layout(); parent.layout(); parent.update(); return composite; } /** * 创建不带横向X轴的柱状图 * * @param dataset * 数据集 * @param xName * @param yName * @param chartTitle * 标题 * @param charName * 名称 * @param parent * 父容器 * @param chartType * 创建类型 */ public static ChartComposite createBarChartNoX(CategoryDataset dataset, String xName, String yName, String chartTitle, String charName, Composite parent) { JFreeChart chart = ChartFactory.createBarChart(chartTitle, // 图表标题 // xName, // 目录轴的显示标签 yName, // 数值轴的显示标签 dataset, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例(对于简单的柱状图必须是false) true, // 是否生成工具 false // 是否生成URL链接 ); // 为一个页面上面两个图表,隐藏上面图表的X. ((CategoryPlot) chart.getPlot()).getDomainAxis().setVisible(false); chart.getLegend().setVisible(false); Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); //$NON-NLS-1$ /* * VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭, * 使用的关闭抗锯齿后,字体尽量选择12到14号的宋体字,这样文字最清晰好看 */ // chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); chart.setTextAntiAlias(false);// 字体模糊边界 chart.setBackgroundPaint(Color.white); // create plot CategoryPlot plot = chart.getCategoryPlot(); // 设置横虚线可见 plot.setRangeGridlinesVisible(true); // 虚线色彩 plot.setRangeGridlinePaint(Color.gray); // 数据轴精度 NumberAxis vn = (NumberAxis) plot.getRangeAxis(); // vn.setAutoRangeIncludesZero(true); DecimalFormat df = new DecimalFormat("#0.00"); //$NON-NLS-1$ vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式 // x轴设置 CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setLabelFont(labelFont);// 轴标题 domainAxis.setTickLabelFont(labelFont);// 轴数值 // 设置图例中的字体 LegendTitle legend = chart.getLegend(); legend.setItemFont(labelFont); // 图示项字体 // Lable(Math.PI/3.0)度倾斜 domainAxis.setCategoryLabelPositions(CategoryLabelPositions .createUpRotationLabelPositions(Math.PI / 3.0)); domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);// 横轴上的 Lable 是否完整显示 // 设置距离图片左端距离 domainAxis.setLowerMargin(0.1); // 设置距离图片右端距离 domainAxis.setUpperMargin(0.1); // 设置 columnKey 是否间隔显示 // domainAxis.setSkipCategoryLabelsToFit(true); plot.setDomainAxis(domainAxis); // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确) plot.setBackgroundPaint(new Color(255, 255, 204)); // y轴设置 ValueAxis rangeAxis = plot.getRangeAxis(); rangeAxis.setLabelFont(labelFont); rangeAxis.setTickLabelFont(labelFont); // 设置最高的一个 Item 与图片顶端的距离 rangeAxis.setUpperMargin(0.15); // 设置最低的一个 Item 与图片底端的距离 rangeAxis.setLowerMargin(0.15); plot.setRangeAxis(rangeAxis); BarRenderer renderer = new BarRenderer(); // 设置柱子宽度 renderer.setMaximumBarWidth(0.1); // 设置柱子高度 renderer.setMinimumBarLength(0.2); // 设置柱子边框颜色 renderer.setBaseOutlinePaint(Color.BLACK); // 设置柱子边框可见 renderer.setDrawBarOutline(true); // // 设置柱的颜色 // renderer.setSeriesPaint(0, new Color(204, 255, 255)); // renderer.setSeriesPaint(1, new Color(153, 204, 255)); // renderer.setSeriesPaint(2, new Color(51, 204, 204)); // 设置每个地区所包含的平行柱的之间距离 renderer.setItemMargin(0.0); // 显示每个柱的数值,并修改该数值的字体属性 renderer.setIncludeBaseInRange(true); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); plot.setRenderer(renderer); // 设置柱的透明度 plot.setForegroundAlpha(1.0f); if (parent.getChildren().length > 0) { Control[] cons = parent.getChildren(); for (Control control : cons) { control.dispose(); } } ChartComposite composite = new ChartComposite(parent, SWT.BORDER, chart, true); composite.setLayout(new FillLayout()); composite.layout(); parent.layout(); parent.update(); return composite; } /*** * 创建柱状图数据集 * * @param list * @param strs * 字段属性 {值,名称1,名称2} * @param doub * 是否是Double类型 * @return */ public static CategoryDataset createDateSet(List<?> list, String[] strs, boolean doub) { if (list != null && list.size() > 0) { try { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); Double str; String str1; String str2; if (strs.length < 3) { return null; } for (Object obj : list) { str = Double.valueOf(getString(invokeGetMethod(obj, RefMethod.gainGetMethod(strs[0])))); str1 = (String) invokeGetMethod(obj, RefMethod.gainGetMethod(strs[1])); str2 = (String) invokeGetMethod(obj, RefMethod.gainGetMethod(strs[2])); dataset.setValue(str, str1, str2); } return dataset; } catch (Exception e) { DialogFactory.openWarning("请检查传入的数据是否正确!\r" + e.getMessage()); } } return null; } private static String getString(Object obj) { if (obj == null) return ""; if (obj.getClass() == Double.class) return NumberUtil.getSimFormatValue((Double) obj, 2); return obj.toString(); } /** * 创建曲线图 * * @param dataset * 数据集 * @param xName * x轴显示的名称 * @param yName * y轴显示的名称 * @param chartTitle * @param charName * 图标名称 * @param parent * 父容器 */ @SuppressWarnings({ "deprecation" }) public static ChartComposite createLineChart(CategoryDataset dataset, String xName, String yName, String chartTitle, String charName, Composite parent) { JFreeChart chart = ChartFactory.createLineChart(charName, xName, yName, dataset, PlotOrientation.VERTICAL, true, true, false); chart.setBackgroundPaint(Color.white); CategoryPlot categoryplot = (CategoryPlot) chart.getPlot(); categoryplot.setBackgroundPaint(Color.lightGray); categoryplot.setRangeGridlinePaint(Color.white); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis(); numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); numberaxis.setAutoRangeIncludesZero(true); // x轴设置 CategoryAxis domainAxis = categoryplot.getDomainAxis(); // Lable(Math.PI/3.0)度倾斜 domainAxis.setCategoryLabelPositions(CategoryLabelPositions .createUpRotationLabelPositions(Math.PI / 3.0)); // 获得renderer 注意这里是下嗍造型到lineandshaperenderer!! LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot .getRenderer(); lineandshaperenderer.setShapesVisible(true); // series 点(即数据点)可见 lineandshaperenderer.setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] { 10F, 6F }, 0.0F)); // 定义series为”First”的(即series1)点之间的连线 // ,这里是虚线,默认是直线 lineandshaperenderer.setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] { 6F, 6F }, 0.0F)); // 定义series为”Second”的(即series2)点之间的连线 lineandshaperenderer.setSeriesStroke(2, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] { 2.0F, 6F }, 0.0F)); // 定义series为”Third”的(即series3)点之间的连线 if (parent.getChildren().length > 0) { Control[] cons = parent.getChildren(); for (Control control : cons) { control.dispose(); } } ChartComposite composite = new ChartComposite(parent, SWT.BORDER, chart, true); composite.setLayout(new FillLayout()); composite.layout(); parent.layout(); parent.update(); return composite; } /** * 创建曲线图数据集 * * @param fname * 显示名称 * @param list * @param strs * 要显示的字段名称 不少于2个 顺序{值 ,名称} * @return */ public static CategoryDataset createLineDataset(String fname, List<?> list, String[] strs) { if (list != null && list.size() > 0) { try { DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset(); for (Object obj : list) { Double value = Double.valueOf(invokeGetMethod(obj, RefMethod.gainGetMethod(strs[0])).toString()); String str = (String) invokeGetMethod(obj, RefMethod.gainGetMethod(strs[1])); defaultcategorydataset.addValue(value, fname, str); } return defaultcategorydataset; } catch (Exception e) { DialogFactory.openWarning("请检查传入的数据是否正确!\r" + e.getMessage()); return null; } } return null; } /** * 执行持久类中Get方法 * * @param owner * @param methodName */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Object invokeGetMethod(Object owner, String methodName) { Class ownerClass = owner.getClass(); Object result = null; Method method; try { method = ownerClass.getMethod(methodName, new Class[] {}); result = method.invoke(owner, new Object[] {}); } catch (Exception e) { // e.printStackTrace(); } return result; } /** * 创建饼状图 * * @param dataset * @param xName * @param yName * @param chartTitle * @param charName * @param parent */ public static ChartComposite createPieChart(PieDataset dataset, String xName, String yName, String chartTitle, String charName, Composite parent) { JFreeChart chart = ChartFactory.createPieChart(charName, // 图形标题名称 dataset, // dataset true, // legend? true, // tooltips? false); // URLs? PiePlot pieplot = (PiePlot) chart.getPlot(); // 通过JFreeChart 对象获得 // plot:PiePlot!! pieplot.setNoDataMessage("没有数据"); // 没有数据的时候显示的内容 if (parent.getChildren().length > 0) { Control[] cons = parent.getChildren(); for (Control control : cons) { control.dispose(); } } ChartComposite composite = new ChartComposite(parent, SWT.BORDER, chart, true); composite.setLayout(new FillLayout()); composite.layout(); parent.layout(); parent.update(); return composite; } /** * 创建饼状图数据集 * * @param fname * 显示名称 * @param list * @param strs * 要显示的字段名称 不少于2个 顺序为 {名称 , 值} * @return */ public static PieDataset createPieDataset(List<?> list, String[] strs) { if (list != null && list.size() > 0) { try { DefaultPieDataset defaultcategorydataset = new DefaultPieDataset(); for (Object obj : list) { String str = (String) RefMethod.invokeGetMethod(obj, RefMethod.gainGetMethod(strs[0])); Double value = Double.valueOf(RefMethod.invokeGetMethod( obj, RefMethod.gainGetMethod(strs[1])).toString()); defaultcategorydataset.setValue(str, value); } return defaultcategorydataset; } catch (Exception e) { DialogFactory.openWarning("请检查传入的数据是否正确!\r" + e.getMessage()); return null; } } return null; } }
PlotTempDialog.java
package com.yfkj.shop.ui.dialog; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.DateTime; import org.eclipse.swt.widgets.Dialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.jfree.data.category.CategoryDataset; import com.yfkj.shop.ui.util.LayoutUtil; import com.yfkj.shop.ui.util.TimeHelper; import com.yfkj.shop.ui.vo.PlotData; import com.yfkj.shop.ui.vo.PlotParameterVO; import com.yfkj.shop.uicustom.ctable.core.framework.resource.RcpResourceManager; import com.yfkj.shop.util.JfreechartUtil; /** * 综合查询图形报表窗体 ,测试功能直接执行此类的main方法. * * @author yindang CreateTime:2011-7-13 * */ public class PlotTempDialog extends Dialog { protected Object result; protected Shell shell; public DateTime beginTime; public DateTime endTime; public Combo cbo_sort; public Button btn_ok; public PlotParameterVO plotParameterVO; private Composite composite_qxt; private Composite composite_zxt; public PlotParameterVO getPlotParameterVO() { return plotParameterVO; } public void setPlotParameterVO(PlotParameterVO plotParameterVO) { this.plotParameterVO = plotParameterVO; } public PlotTempDialog(Shell parent, int style) { super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); } public PlotTempDialog(Shell parent) { super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); } public PlotTempDialog(Shell parent, String title, List<Object> list, String[] propertys, String[] displays, Date beginDate, Date endDate) { super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); setText(title); } public Object open() { Display display = getParent().getDisplay(); createContents(); shell.open(); LayoutUtil.centerShell(display, shell); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } return result; } private void createContents() { shell = new Shell(getParent(), getStyle()); shell.setSize(1024, 768); shell.setText(getText()); shell.setLayout(new FormLayout()); Composite composite_top = new Composite(shell, SWT.NONE); { FormData fd_top = new FormData(); fd_top.left = new FormAttachment(0); fd_top.top = new FormAttachment(0); fd_top.right = new FormAttachment(100); fd_top.height = 45; composite_top.setLayoutData(fd_top); composite_top.setBackgroundMode(SWT.INHERIT_DEFAULT); composite_top.setLayout(new FormLayout()); composite_top.setForeground(RcpResourceManager .getColor(SWT.COLOR_BLUE)); { Label l1 = new Label(composite_top, SWT.NONE); l1.setText("查询日期:"); FormData fdl1 = new FormData(); fdl1.top = new FormAttachment(0, 17); fdl1.left = new FormAttachment(1); l1.setLayoutData(fdl1); beginTime = new DateTime(composite_top, SWT.DROP_DOWN | SWT.BORDER); FormData fdbeginTime = new FormData(); fdbeginTime.top = new FormAttachment(0, 12); fdbeginTime.left = new FormAttachment(l1, 5); beginTime.setLayoutData(fdbeginTime); if (null != plotParameterVO.getBeginDate()) { setDate(beginTime, TimeHelper.stringToTime( plotParameterVO.getBeginDate(), "yyyy-MM-dd")); } Label l2 = new Label(composite_top, SWT.NONE); l2.setText("至"); FormData fdl2 = new FormData(); fdl2.top = new FormAttachment(0, 17); fdl2.left = new FormAttachment(beginTime, 3); l2.setLayoutData(fdl2); endTime = new DateTime(composite_top, SWT.DROP_DOWN | SWT.BORDER); FormData fdendTime = new FormData(); fdendTime.top = new FormAttachment(0, 12); fdendTime.left = new FormAttachment(l2, 3); endTime.setLayoutData(fdendTime); if (null != plotParameterVO.getEndDate()) { setDate(endTime, TimeHelper.stringToTime( plotParameterVO.getEndDate(), "yyyy-MM-dd")); } Label l3 = new Label(composite_top, SWT.NONE); l3.setText("排序方式:"); FormData fdl3 = new FormData(); fdl3.top = new FormAttachment(0, 17); fdl3.left = new FormAttachment(endTime, 20); l3.setLayoutData(fdl3); cbo_sort = new Combo(composite_top, SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY); FormData fdcbo_sort = new FormData(); fdcbo_sort.top = new FormAttachment(0, 13); fdcbo_sort.left = new FormAttachment(l3, 3); cbo_sort.setLayoutData(fdcbo_sort); cbo_sort.setItems(new String[] { "按日", "按月", "按年" }); cbo_sort.setData("按日", "day"); cbo_sort.setData("按月", "month"); cbo_sort.setData("按年", "year"); cbo_sort.select(0); btn_ok = new Button(composite_top, SWT.PUSH | SWT.CENTER); FormData fdbtn_ok = new FormData(); fdbtn_ok.top = new FormAttachment(0, 12); fdbtn_ok.left = new FormAttachment(cbo_sort, 25); fdbtn_ok.width = 75; btn_ok.setLayoutData(fdbtn_ok); btn_ok.setText("查询"); btn_ok.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { // 从后台查询数据集合,保存进list List<PlotData> list = new ArrayList<PlotData>(); String begin = getDateString(beginTime); StringBuilder builder = new StringBuilder(); String type = (String) cbo_sort.getData(cbo_sort .getText()); if ("month".equals(type) || "year".equals(type)) { builder.append(begin.substring(0, 6)); builder.append("1-01"); } else if ("day".equals(type)) { builder.append(begin); } plotParameterVO.setList(list); CategoryDataset set = JfreechartUtil.createDateSet( plotParameterVO.getList(), plotParameterVO.getPropertys(), true); JfreechartUtil.createBarChart(set, plotParameterVO.getxDisplayStr(), plotParameterVO.getyDisplayStr(), plotParameterVO.getTitle(), null, composite_qxt); CategoryDataset set2 = JfreechartUtil .createLineDataset(plotParameterVO.getTitle(), plotParameterVO.getList(), plotParameterVO.getPropertys()); JfreechartUtil.createLineChart(set2, plotParameterVO.getxDisplayStr(), plotParameterVO.getyDisplayStr(), plotParameterVO.getTitle(), null, composite_zxt); } @Override public void widgetDefaultSelected(SelectionEvent e) { } }); } } composite_qxt = new Composite(shell, SWT.NONE); { FormData fdcomposite_qxt = new FormData(); fdcomposite_qxt.left = new FormAttachment(0); fdcomposite_qxt.top = new FormAttachment(composite_top, 0); fdcomposite_qxt.right = new FormAttachment(100); fdcomposite_qxt.height = 300; composite_qxt.setLayoutData(fdcomposite_qxt); composite_qxt.setBackgroundMode(SWT.INHERIT_DEFAULT); composite_qxt.setLayout(new FillLayout()); { CategoryDataset set = JfreechartUtil.createDateSet( plotParameterVO.getList(), plotParameterVO.getPropertys(), true); JfreechartUtil.createBarChartNoX(set, plotParameterVO.getxDisplayStr(), plotParameterVO.getyDisplayStr(), plotParameterVO.getTitle(), null, composite_qxt); } } composite_zxt = new Composite(shell, SWT.NONE); { FormData fdcomposite_zxt = new FormData(); fdcomposite_zxt.left = new FormAttachment(0); fdcomposite_zxt.top = new FormAttachment(composite_qxt, -1); fdcomposite_zxt.right = new FormAttachment(100); fdcomposite_zxt.bottom = new FormAttachment(100); composite_zxt.setLayoutData(fdcomposite_zxt); composite_zxt.setBackgroundMode(SWT.INHERIT_DEFAULT); composite_zxt.setLayout(new FillLayout()); { CategoryDataset set = JfreechartUtil.createLineDataset( plotParameterVO.getTitle(), plotParameterVO.getList(), plotParameterVO.getPropertys()); JfreechartUtil.createLineChart(set, plotParameterVO.getxDisplayStr(), plotParameterVO.getyDisplayStr(), plotParameterVO.getTitle(), null, composite_zxt); } } } /** * 从DateTime日期控件取得值 格式为:yyyy-MM-dd */ public static String getDateString(DateTime dateTime) { String value = dateTime.getYear() + "-" + ((dateTime.getMonth() + 1) > 9 ? (dateTime.getMonth() + 1) : "0" + (dateTime.getMonth() + 1)) + "-" + (dateTime.getDay() > 9 ? dateTime.getDay() : "0" + dateTime.getDay()); return value; } /** * 给DateTime日期控件附值 */ public static void setDate(DateTime dateTime, Date date) { if (date == null) return; Calendar cal = Calendar.getInstance(); cal.setTime(date); dateTime.setDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)); dateTime.redraw(); } public static void main(String[] str) { PlotTempDialog dia = new PlotTempDialog(new Shell()); dia.initdata(); dia.open(); } private void initdata() { List<PlotData> list = new ArrayList<PlotData>(); for (int i = 0; i < 5; i++) { PlotData bean = new PlotData(); bean.setFje(new Double(i + 1)); bean.setFrq(i + "周"); list.add(bean); } this.plotParameterVO = new PlotParameterVO(); this.plotParameterVO.setTitle("welcome you!图表测试"); this.plotParameterVO.setList(list); this.plotParameterVO .setPropertys(new String[] { "fje", "frq", "frq" }); this.plotParameterVO.setxDisplayStr("周期"); this.plotParameterVO.setyDisplayStr("金额"); } } class Bean { private Double je; private String rq; private String name; public Double getJe() { return je; } public void setJe(Double je) { this.je = je; } public String getRq() { return rq; } public void setRq(String rq) { this.rq = rq; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
PlotParameterVO.java
package com.yfkj.shop.ui.vo; import java.io.Serializable; import java.util.List; /** * 综合信息图表参数实体 * * @author YinDang CreateTime:2011-7-14 * */ public class PlotParameterVO implements Serializable { /** * */ private static final long serialVersionUID = 5621514121349597113L; /** * 要显示的值集合 */ public List<PlotData> list; public List<PlotData> getList() { return list; } public void setList(List<PlotData> list) { this.list = list; } /** * 与集合对象匹配的属性 第一项数值类型 第二项为String 第三项为标题,不写取构造处的TITLE */ public String[] propertys; /** * 对应X轴的显示字样 */ public String xDisplayStr; /** * 对应Y轴的显示字样 */ public String yDisplayStr; public String[] getPropertys() { return propertys; } public void setPropertys(String[] propertys) { this.propertys = propertys; } public String getxDisplayStr() { return xDisplayStr; } public void setxDisplayStr(String xDisplayStr) { this.xDisplayStr = xDisplayStr; } public String getyDisplayStr() { return yDisplayStr; } public void setyDisplayStr(String yDisplayStr) { this.yDisplayStr = yDisplayStr; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } /** * 主窗体的名称 */ public String title; /** * 起始时间 */ public String beginDate; /** * 结束时间 */ public String endDate; public String getBeginDate() { return beginDate; } public void setBeginDate(String beginDate) { this.beginDate = beginDate; } public String getEndDate() { return endDate; } public void setEndDate(String endDate) { this.endDate = endDate; }; }
PlotData.java
package com.yfkj.shop.ui.vo; import java.io.Serializable; /** * 图形报表数据封装类 * * @author YinDang CreateTime:2011-7-15 * */ public class PlotData implements Serializable { /** * */ private static final long serialVersionUID = -8805815579861940941L; private String frq; private Double fje; private String fsl; public String getFrq() { return frq; } public void setFrq(String frq) { this.frq = frq; } public Double getFje() { return fje; } public void setFje(Double fje) { this.fje = fje; } public String getFsl() { return fsl; } public void setFsl(String fsl) { this.fsl = fsl; } }
----------------工作积累 尹当