jFreechat 生成双Y轴折线图
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.util.Map;
import java.util.TreeMap;
import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYSeriesCollection;
import com.mln.frame.BaseAction;
/**
* 柱状图基类
* @author mln-wyf
*/
public abstract class StackedBarChartAction extends BaseAction
{
public JFreeChart chart;
private String title; // 标题
private String x; // 横坐标名称
private String y; // 纵坐标名称
private String y2;// 纵坐标右边名称
/**
*双Y轴折线图
* @param datasetLeft Y轴左边的数据集
* @param datasetRight Y轴右边的数据集
* @return
*/
protected JFreeChart createLineChart(XYSeriesCollection datasetLeft,XYSeriesCollection datasetRight) {
// 设置字体样式
Font fs = new Font("微软雅黑", Font.BOLD, 12);
Font f = new Font("微软雅黑", Font.PLAIN, 12);
chart = ChartFactory.createXYLineChart(title,
x,
y,
datasetLeft,
PlotOrientation.VERTICAL,
true,
true,
true);
XYPlot xyplot = (XYPlot)chart.getPlot();
xyplot.setDomainPannable(true);
xyplot.setRangePannable(true);
// 设置X轴显示方式
//NumberAxis domainAxis = new NumberAxis(this.x);
NumberAxis domainAxis= (NumberAxis)xyplot.getDomainAxis();
domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); //设置x轴数字为整数
domainAxis.setVisible(true);
domainAxis.setAutoRangeIncludesZero(false);
xyplot.setRangeAxis(0, domainAxis);
// 左边y轴显示方式
//NumberAxis numberaxis= (NumberAxis)plotxy.getRangeAxis();//y轴整数显示
NumberAxis numberaxis = new NumberAxis(this.y);
numberaxis.setLabelFont(fs);
numberaxis.setTickLabelFont(f);
xyplot.setRangeAxis(0, numberaxis);
// 右边y轴显示方式
NumberAxis numberaxis2 = new NumberAxis(this.y2);
numberaxis2.setLabelFont(fs);
numberaxis2.setTickLabelFont(f);
xyplot.setRangeAxis(1, numberaxis2);
xyplot.setDataset(1, datasetRight);
xyplot.mapDatasetToRangeAxis(1, 1);
// 左边y轴
XYItemRenderer renderer = xyplot.getRenderer();
//xylineandshaperenderer1.setBaseToolTipGenerator(StandardXYToolTipGenerator.getTimeSeriesInstance()); //数据显示格式
if (renderer instanceof XYLineAndShapeRenderer) //显示数据
{
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)renderer;
xylineandshaperenderer.setBaseShapesVisible(true);
xylineandshaperenderer.setBaseShapesFilled(true);
}
// 右边y轴
XYLineAndShapeRenderer renderer1 = new XYLineAndShapeRenderer();
renderer1.setBaseShapesVisible(true);
renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator()); //数据显示格式
renderer1.setSeriesPaint(0, Color.green);
renderer1.setOutlinePaint(Color.green);
renderer1.setUseOutlinePaint(true);
renderer1.setUseFillPaint(true);
renderer1.setDrawOutlines(true);
xyplot.setRenderer(1, renderer1);
xyplot.setBackgroundPaint(new Color(238, 244, 255));//设置图表的颜色
xyplot.setDomainGridlinePaint(Color.lightGray);// 设置垂直网格线的颜色
xyplot.setRangeGridlinePaint(Color.lightGray);// 设置水平网格线的颜色
xyplot.setDomainGridlinesVisible(true); // 设置垂直网格线是否显示
xyplot.setRangeGridlinesVisible(true); // 设置水平网格线是否显示
setChartLineFont();
return chart;
}
}
这个是双Y轴折线图的基类,可以在action中将 * @param datasetLeft Y轴左边的数据集
* @param datasetRight Y轴右边的数据集 这两个数据集生成之后调用。