日期:2014-05-20  浏览次数:20737 次

困扰了一个月的问题, 请高手解惑!
研究了JFreeChart一个月了, 按照官方提供的demo一些简单的图表到是可以实现了. 但有一个问题一直困扰着我, 在做一个时序图, X轴是日期轴, 从1号到30/31号, Y轴表示每一天的出货量. 数据从数据库中获得大体sql应该是
select date, count(*) c from where date like '...' t group by date. 结果集就包含日期与数量两列. 

然后使用JFreeChart进行绘制, 发现如果检索出来的数据不是连续的(中间有几天没记录), 或者只有半个月的(或更少), 这个时候X轴上的日期显示就乱套了, 不知道是为什么, 请高手指教....

------解决方案--------------------
源码实例,请参考,若有帮助麻烦给下分,谢谢!

package JFreeChartDemo;

import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartFactory;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;

import java.awt.Font;

public class JFreeCharTest1 {

/**
* @param args
*/
public static CategoryDataset CreateCategory(){
DefaultCategoryDataset cds=new DefaultCategoryDataset();
cds.setValue(10,"开发人员","beyondsoft");
cds.setValue(18,"初级开发人员","beyondsoft");
cds.setValue(22,"高级开发人员","beyondsoft");
cds.setValue(26,"测试人员","hisoft");
cds.setValue(33,"黑盒测试人员","hisoft");
cds.setValue(44,"白盒测试人员","hisoft");
return cds;
}

public static JFreeChart getfreechart(){
JFreeChart chart=ChartFactory.createBarChart("IT市场形式分析","公司名","人数",CreateCategory(), PlotOrientation.VERTICAL, true, true, true);
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
// 设置图表的纵轴和横轴org.jfree.chart.axis.CategoryAxis
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
CategoryAxis domainAxis = categoryplot.getDomainAxis();
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
return chart;
}

public static void main(String[] args) {

ChartFrame frame=new ChartFrame("IT市场形式分析",getfreechart());
frame.pack();
frame.setVisible(true);
}
}

------解决方案--------------------
中间不连续的数据用0,折线图必须是横纵坐标一一对应的