jfreechart柱状图自定义标题

package com.web.warning.util;

import java.awt.Color;
import java.awt.Font;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.LabelBlock;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.uintec.util.DateUtil;

import com.web.finplan.util.CaiwuCommon;

public class XYBarChartDemo2 extends ApplicationFrame
{
private static final long serialVersionUID = 1L;
private static final Color WHITE_BACKGROUND = new Color(255,255,255);
private static final Color BLACK_BACKGROUND = new Color(173,173,173);
private static final Font FONT10 = new Font("宋体",10,10);
private static final Font FONT20 = new Font("宋体",20,20);
private static final Font FONT15 = new Font("宋体",15,15);
public XYBarChartDemo2(String paramString)
{
super(paramString);
}
/**
* 获取数据源
* @param nameList
* @param numberList
* @return
*/
private static IntervalXYDataset createDataset(List<String> nameList,List<Object> numberList)
{
int[] date = getDay3();
if(nameList!=null && numberList!=null && nameList.size()==numberList.size())
{
TimeSeriesCollection localTimeSeriesCollection = new TimeSeriesCollection();
for(int i=0;i<nameList.size();i++)
{
TimeSeries localTimeSeries = new TimeSeries(nameList.get(i));
localTimeSeries.add(new Day(date[0]+i, date[1], date[2]), CaiwuCommon.isNullDouble(numberList.get(i)));
localTimeSeriesCollection.addSeries(localTimeSeries);
}
return localTimeSeriesCollection;
}
return null;

}
/**
* 开始
* @param paramIntervalXYDataset
* @return
*/
private static JFreeChart createChart(IntervalXYDataset paramIntervalXYDataset,List<Color> colorList)
{
double highValue = Double.MIN_VALUE;// 设置数据当中的最大值
double minValue = Double.MAX_VALUE;// 设置数据当中的最小值
TimeSeriesCollection localTimeSeriesCollection = (TimeSeriesCollection) paramIntervalXYDataset;
for(int i=0;i<localTimeSeriesCollection.getSeriesCount();i++)循环序列数
{
int seriesCount = localTimeSeriesCollection.getItemCount(i);
for(int j=0;j<seriesCount;j++)//循环柱子数量
{
if(highValue<localTimeSeriesCollection.getYValue(i, j))
highValue = localTimeSeriesCollection.getYValue(i, j);
if(minValue>localTimeSeriesCollection.getYValue(i, j))
minValue = localTimeSeriesCollection.getYValue(i, j);
}
}
JFreeChart jFreeChart = ChartFactory.createXYBarChart("即时涨跌幅", "", true, "", paramIntervalXYDataset, PlotOrientation.VERTICAL, true, true, false);//申明jfreechart对象
jFreeChart.getTitle().setPaint(BLACK_BACKGROUND);
jFreeChart.getTitle().setFont(FONT20);//头部标题字体,解决中文问题

[color=red]LegendTitle legendtitle = new LegendTitle(jFreeChart.getPlot());
BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
blockcontainer.setBorder(new BlockBorder(1.0D, 1.0D, 1.0D, 1.0D));
LabelBlock labelblock = new LabelBlock("即时涨跌幅:", new Font("宋体", 1, 12));
labelblock.setPadding(5D, 5D, 5D, 5D);
blockcontainer.add(labelblock, RectangleEdge.TOP);
LabelBlock labelblock1 = new LabelBlock(DateUtil.date2String(new Date(), "yyyy-MM-dd"));
labelblock1.setPadding(8D, 20D, 2D, 5D);
blockcontainer.add(labelblock1, RectangleEdge.BOTTOM);
BlockContainer blockcontainer1 = legendtitle.getItemContainer();
blockcontainer1.setPadding(2D, 10D, 5D, 2D);
blockcontainer.add(blockcontainer1);
legendtitle.setWrapper(blockcontainer);
legendtitle.setPosition(RectangleEdge.RIGHT);
legendtitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
jFreeChart.addSubtitle(legendtitle);
jFreeChart.getLegend().setVisible(false);//不显示系统生成的解释性语句[/color]
// jFreeChart.getLegend().setItemFont(FONT15);//底部标题字体,解决中文问题
// jFreeChart.getLegend().setPadding(50D, 15D, 50D, 15D);//设置解释性标题框的据窗体的高,据左,据上下,据右
// jFreeChart.getLegend().setPosition(RectangleEdge.RIGHT);//让 解释性标题 显示到窗体右边

XYPlot xyPlot = jFreeChart.getXYPlot();//创建画图布
xyPlot.setBackgroundPaint(WHITE_BACKGROUND);//设置面板背景色
xyPlot.setDomainGridlinesVisible(true);//设置Y轴线是否可以显示
xyPlot.setRangeGridlinesVisible(true);//设置X轴线是否可以显示
xyPlot.setRangeGridlinePaint(BLACK_BACKGROUND); //设置Y轴线的颜色
xyPlot.setDomainGridlinePaint(BLACK_BACKGROUND);//设置x轴线的颜色
//设置X轴
DateAxis x = (DateAxis)xyPlot.getDomainAxis();
x.setAutoRange(true);//X轴是否自动
x.setTickLabelFont(FONT10);
x.setTickLabelPaint(Color.red);
x.setTickLabelsVisible(false);//由于此图不需要X轴。所以设置为false

NumberAxis y = (NumberAxis)xyPlot.getRangeAxis();
y.setAutoRange(true);//X轴是否自动
y.setTickLabelFont(FONT10);
y.setTickLabelPaint(BLACK_BACKGROUND);
y.setRange(minValue-5,highValue+5);//设置Y轴范围
y.setTickUnit(new NumberTickUnit(5D, NumberFormat.getInstance()));//设置Y轴刻线密度

XYBarRenderer render = (XYBarRenderer)xyPlot.getRenderer();//用来设置每个柱子的属性
for(int i=0;i<colorList.size();i++)//设置颜色
{
render.setSeriesPaint(i,colorList.get(i));
}
render.setMargin(0.5);//柱子距离
ChartFrame frame = new ChartFrame("即时涨跌幅", jFreeChart);
// frame.setPreferredSize(new Dimension(400,500));
frame.pack();
frame.setVisible(true);
return jFreeChart;
}
/**
* 获取时间年月日
* @return
*/
public static int[] getDay3()
{
int[] date = new int[3];
Calendar canle = Calendar.getInstance();
int day = canle.get(Calendar.DATE);
int month = canle.get(Calendar.MONTH);
int year = canle.get(Calendar.YEAR)+1;
date[0] = day;
date[1] = month;
date[2] = year;
return date;
}
public static void main(String[] paramArrayOfString)
{
List<String> nameList = new ArrayList<String>();
nameList.add("股票");
nameList.add("基金");
nameList.add("债券");
nameList.add("黄金");
List<Object> numberList = new ArrayList<Object>();
numberList.add(10);
numberList.add(20);
numberList.add(-10);
numberList.add(-15);
List<Color> colorList = new ArrayList<Color>();
colorList.add(new Color(79,129,189));
colorList.add(new Color(192,80,77));
colorList.add(new Color(155,187,89));
colorList.add(new Color(128,100,162));
createChart(createDataset(nameList,numberList),colorList);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值