导入包:
java代码:
public class LineChart extends ActionSupport{
private static final long serialVersionUID = 1L;
JFreeChart chart_drought;
public JFreeChart getChart() {
String[] x_axis={"苹果","橘子","香蕉","橙子","柚子","葡萄","车厘子"};
double[][] y_data={{2000,1900,1800,1700,1600,1500,1400},{1400,1500,1600,1700,1800,1900,2000}};
String x_axis_title="水果种类";//x轴变量
String[] lines_name={"夏天","冬天"};//折线名
double[][] data = y_data;
String[] rowKeys = lines_name;
String[] columnKeys = x_axis;
CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
createTimeXYChar("水果销量折线图", x_axis_title, "销量", dataset, "");
return chart_drought;
}
public CategoryDataset getBarData(double[][] data, String[] rowKeys, String[] columnKeys)
{
return DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);
}
public void createTimeXYChar(String chartTitle, String x, String y, CategoryDataset xyDataset, String charName) {
chart_drought = ChartFactory.createLineChart(chartTitle, x, y, xyDataset, PlotOrientation.VERTICAL, true, true, false);
Font font00 = new Font("宋体",Font.BOLD,10);
LegendTitle legend = chart_drought.getLegend();
legend.setItemFont(font00);//设置注释字体
chart_drought.setTextAntiAlias(false);
// 设置图标题的字体重新设置title
Font font = new Font("宋体", Font.BOLD, 15);
TextTitle title = new TextTitle(chartTitle);
title.setFont(font);
chart_drought.setTitle(title);
CategoryPlot categoryplot = (CategoryPlot) chart_drought.getPlot();
// x轴分类轴网格是否可见
categoryplot.setDomainGridlinesVisible(true);
// y轴数据轴网格是否可见
categoryplot.setRangeGridlinesVisible(true);
categoryplot.setRangeGridlinePaint(Color.gray);// 虚线色彩
categoryplot.setDomainGridlinePaint(Color.gray);// 虚线色彩
categoryplot.setBackgroundPaint(Color.white);
// 设置轴和面板之间的距离
categoryplot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 0D));
CategoryAxis domainAxis = categoryplot.getDomainAxis();
domainAxis.setLabelFont(new Font("宋体" , Font.BOLD , 15));// 轴标题
domainAxis.setTickLabelFont(new Font("宋体" , Font.ITALIC , 15));// 轴数值
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90); // 横轴上的
// 设置距离图片左端距离
domainAxis.setLowerMargin(0.0);
// 设置距离图片右端距离
domainAxis.setUpperMargin(0.0);
NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);
numberaxis.setLabelFont(new Font("宋体" , Font.BOLD , 20));
//设置最高的一个值与图片顶端的距离
numberaxis.setUpperMargin(0.05);
//设置最低的一个值与图片底端的距离
numberaxis.setLowerMargin(0.05);
// 获得renderer
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();
lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见
lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见
// 显示折点数据
lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
lineandshaperenderer.setBaseItemLabelsVisible(true);
}
}
struts.xml:
<action name="lineChart_fruit" class="jfreechartfruit.LineChart" >
<result type="chart">
<param name="width">850</param>
<param name="height">700</param>
</result>
</action>
jap:
<a href="lineChart_fruit.action">
<img src="lineChart_fruit.action" style="width:auto;height: 550px">
显示效果如下: