Java JfreeChart StackedAreaChart堆叠面积图x轴数据量过大时和密集怎么解决?
/**
* Creates a sample dataset.
*
* @return A sample dataset.
*/
public CategoryDataset createDataset(List<RowData> list) {
double[][] data = new double[list.size()][list.get(0).getColValMap().size()];
// 定义行键
Comparable[] rowKeys = new Comparable[list.size()];
// 定义列键
Comparable[] columnKeys = new Comparable[list.get(0).getColValMap().size()];
int c = 0;
for (Integer index : list.get(0).getColValMap().keySet()) {
columnKeys[c++] = index;
}
for (int i = 0; i < list.size(); i++) {
Map<Integer, Object> colValMap = list.get(i).getColValMap();
int j = 0;
for (Integer index : colValMap.keySet()) {
double value = ObjectUtils.isEmpty(colValMap.get(index)) ? 0 : Double.valueOf(colValMap.get(index) + "");
data[i][j++] = value;
}
rowKeys[i] = list.get(i).getName();
}
/*final double[][] data = new double[][] {
{1.0, 4.0, 3.0, 5.0, 5.0, 7.0, 7.0, 8.0 },
{5.0, 7.0, 6.0, 8.0, 4.0, 4.0, 2.0, 1.0 },
{4.0, 3.0, 2.0, 3.0, 6.0, 3.0, 4.0, 3.0 }
};*/
final CategoryDataset dataset = DatasetUtils.createCategoryDataset(
rowKeys, columnKeys, data
);
return dataset;
}
/**
* Creates a sample chart.
*
* @param dataset the dataset.
*
* @return A sample chart.
*/
public JFreeChart createChart(CategoryDataset dataset) {
final JFreeChart chart = ChartFactory.createStackedAreaChart(
"Stacked Area Chart", // chart title
"Category", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true,
false
);
chart.setBackgroundPaint(Color.white);
final CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setForegroundAlpha(0.5f);
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
//是否显示网格线
plot.setDomainGridlinesVisible(false); // hide horizontal gridlines
plot.setRangeGridlinesVisible(false); // hide vertical gridlines
// 设置图的背景为白色
plot.setBackgroundPaint(Color.WHITE);
// 设置背景虚线的颜色
plot.setRangeGridlinePaint(Color.decode("#B6A2DE"));
// 去掉堆叠面积图的背景边框,使边框不可见
plot.setOutlineVisible(false);
// 设置标题的字体样式
chart.getTitle().setFont(new Font("微软雅黑", Font.PLAIN, 24));
// 设置图表下方图例上的字体样式
chart.getLegend().setItemFont(new Font("微软雅黑", Font.PLAIN, 12));
final CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLowerMargin(0.0);
// change the auto tick unit selection to integer units only...
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
final CategoryItemRenderer renderer = plot.getRenderer();
//renderer.setItemLabelsVisible(true);
return chart;
}