jfreechart中画createTimeSeriesChart类型图,怎么让折点只显示最大值和最小值!??求教!
lars 2008-09-26 06:05:50
这是代码:
public static void drawSecLineChart(String seccode,String secname) throws IOException, ParseException {
TimeSeries ts =new TimeSeries(seccode, Day.class);
//(convert(varchar(7),getDate())+convert(varchar(5),datepart(year,getDate())-2))
List<SecQuote>list =em.findBySql(SecQuote.class, "select * from SecQuote");
for(SecQuote sq :list){
ts.add(new Day(sq.getId()), sq.getSecQClose());
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(ts);
System.out.println(dataset.getDomainUpperBound(true)+"------------:"+dataset.getDomainLowerBound(true));
JFreeChart chart = ChartFactory.createTimeSeriesChart(
secname,
//lt.XTitle,
"",
//lt.YTitle,
"",
dataset, //得到结果集!
false, // 是否显示图例(对于简单的柱状图必须是false)
true, // 是否生成工具
false // 是否生成URL链接
);
//true,false,false生成底部的标题"-600839.shse" //false,true,false不生成相对应的文件。
saveChart2(chart, seccode,secname);
}
//------------------
public static void saveChart2(JFreeChart chart, String names,String secname)throws IOException, ParseException {
chart.setBackgroundPaint(Color.black);
chart.setPadding(new RectangleInsets(3.0,3.0,3.0,10.0));//调整和xy的距离
// LegendTitle legend = chart.getLegend(0); // 修改图例的字体
// legend.setItemFont(new Font("幼圆", Font.BOLD, 14)); // 取得时间顺序图的Plot对象
// legend.setItemPaint(Color.white);
// legend.setBackgroundPaint(Color.black);
XYPlot plot = (XYPlot) chart.getPlot(); // 取得横轴
Image image = null;
image = ImageIO.read(new File(Messages.getString("vvbackimage1")));
plot.setBackgroundImage(image);
//使用这个方法显示值
XYItemRenderer xrend = (XYItemRenderer)plot.getRenderer();
//这里显示折点值----------怎么控制只显示最大值和最小值了?
xrend.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
xrend.setBaseItemLabelsVisible(true);
// xrend.setBaseToolTipGenerator(generator)
//xrend.setSeriesVisible(0, null, false); //这里是设置第几个集合的数据不显示。
// xrend.setBasePositiveItemLabelPosition(position, notify)
//xrend.setBaseNegativeItemLabelPosition(new ItemLabelPosition());
xrend.setBaseItemLabelFont(new Font("Impact", Font.ITALIC, 14)); //设置折点上的值的字体
xrend.setBaseItemLabelPaint(Color.yellow);
//
DateAxis categoryAxis = (DateAxis) plot.getDomainAxis(); // 设置横轴显示标签的字体
categoryAxis.setLabelFont(new Font(Messages.getString("fonttype"), Font.ITALIC, Integer.parseInt(Messages.getString("fontsize"))));
categoryAxis.setTickLabelFont(new Font(Messages.getString("fonttype"), Font.PLAIN, Integer.parseInt(Messages.getString("fontsize"))));
categoryAxis.setPositiveArrowVisible(false);
// Color col = new Color(234,123,62);
// categoryAxis.setTickLabelPaint(col);// 横轴下的字体颜色
// categoryAxis.setLabelPaint(col);// 横轴主字体颜色
// categoryAxis.setLabelAngle(0.1);
// categoryAxis.setVerticalTickLabels(true);
Color col = new Color(234,123,62);
categoryAxis.setTickLabelPaint(col);// 横轴下的字体颜色
categoryAxis.setLabelPaint(col);// 横轴主字体颜色
categoryAxis.setUpperMargin(0.05); //右间距
//设置时间的序列
// SegmentedTimeline s = SegmentedTimeline.newMondayThroughFridayTimeline();
// List el = holidays();
// s.addExceptions(el);
// Calendar c = Calendar.getInstance();
// c.set(c.YEAR, 2006);
//
// Calendar d = Calendar.getInstance();
// Range range = new Range(c.getTimeInMillis(),d.getTimeInMillis());
// categoryAxis.setRange(range);
// categoryAxis.setAutoRange(false);
// categoryAxis.setTimeline(s);
//DateTickUnit dt = new DateTickUnit(DateTickUnit.MONTH, Integer.parseInt(Messages.getString("interval_mon2")));//,DateTickUnit.DAY,10,new SimpleDateFormat("yyyy-MM"));
DateTickUnit dt = new DateTickUnit(DateTickUnit.DAY, 120);
categoryAxis.setTickUnit(dt);
categoryAxis.setDateFormatOverride(new SimpleDateFormat("yy-MM-dd"));
// 取得纵轴
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
// 设置纵轴显示标签的字体
//numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 22));
numberAxis.setLabelFont(new Font(Messages.getString("fonttype"), Font.PLAIN, Integer.parseInt(Messages.getString("fontsize"))));
numberAxis.setTickLabelFont(new Font(Messages.getString("fonttype"), Font.PLAIN, Integer.parseInt(Messages.getString("fontsize"))));
//numberAxis.setLabelAngle(1.5);
Color col2 = new Color(80,217,209);
numberAxis.setTickLabelPaint(col2);
numberAxis.setLabelPaint(col2);
chart.getTitle().setBackgroundPaint(Color.BLACK);
chart.getTitle().setPaint(Color.orange);
//设置标题颜色
plot.setBackgroundPaint(Color.black);
plot.setWeight(10);
plot.getRenderer().setSeriesItemLabelPaint(0, col2);
plot.getRenderer().setSeriesPaint(0, col2);
plot.setOutlinePaint(Color.white);
BasicStroke bs = new BasicStroke(1);//设置边框的粗细
//设置中央图的风格
plot.setOutlineStroke(bs);
plot.setDomainCrosshairPaint(Color.GREEN);
plot.setQuadrantPaint(0, Color.PINK);
plot.setDomainGridlinePaint(Color.yellow);
plot.setRangeGridlinePaint(Color.GREEN);
FileOutputStream fos = null;
String path = Messages.getString("savepath");
String name = names + ".png";
File f = new File(path + name);
if (!f.getParentFile().exists())
f.getParentFile().mkdirs();
fos = new FileOutputStream(f);// 将统计图标输出成JPG文件
ChartUtilities.writeChartAsPNG(fos, // 输出到哪个输出流
chart, // 统计图标对象
//800, // 宽
555,
//600,// 高
334,
null // ChartRenderingInfo 信息
);
fos.close();
}