記錄下畫折線(柱狀圖)的過程。先上圖:
JFreeCharts繪畫折線(柱狀圖)
畫這個圖的時候,並沒有用CombinedDomainXYPlot來結合。CombinedDomainXYPlot結合說到底還是屬於兩個圖片,只是將圖片合到了一張圖裏面。這裏是將數據畫在一張圖片中。所以用普通的方式生成chart就行了。
首先還是可以先定義好數據。這裏我用的條目數據集合(DefaultCategoryDataset),這裏沒什麼難度。需要注意的是,在此圖中,不同的折線可能會對應不同的y軸,但是兩條y軸的數據不一樣,所以,在設置好數據的之後要指定這條數據需要對應哪一條y軸。用plot.mapDatasetToRangeAxis(0,0)這裏的0,0代表你設置進plot中的數據的index和y軸的index。然後,一般折線都會要求在柱狀圖前面顯示。這裏用plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD)來設置,注意,參數根據你設置的先後順序來決定是要正序還是倒序。
貼下代碼:
//獲取畫圖數據
String dJson = params.get("data");
Map<String, Object> data = (Map<String, Object>) JsonUtil.parse(dJson);
String title = (String) data.get("title");
//使用y軸1
DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
DefaultCategoryDataset bardataset = new DefaultCategoryDataset();
//使用y軸2
DefaultCategoryDataset linedataset2 = new DefaultCategoryDataset();
DefaultCategoryDataset bardataset2 = new DefaultCategoryDataset();
List<String> dates = (List<String>) data.get("xAxis");//x軸信息
List<Map<String, Object>> yAxises = (List<Map<String, Object>>) data.get("yAxis");//y軸信息
List<Map<String, Object>> serieses = (List<Map<String, Object>>) data.get("series");
for(Map map : serieses){
List<String> lists = (List<String>) map.get("data");
if("line".equals(map.get("type")) && map.get("yAxis") == null){
for(int i = 0; i < lists.size(); i++){
linedataset.addValue(Double.valueOf(lists.get(i)), map.get("name").toString(), dates.get(i));
}
} else if("line".equals(map.get("type"))){
for(int i = 0; i < lists.size(); i++){
linedataset2.addValue(Double.valueOf(lists.get(i)), map.get("name").toString(), dates.get(i));
}
}
if("bar".equals(map.get("type")) && map.get("yAxis") == null){
for(int i = 0; i < lists.size(); i++){
bardataset.addValue(Double.valueOf(lists.get(i)), map.get("name").toString(), dates.get(i));
}
} else if("bar".equals(map.get("type"))){
for(int i = 0; i < lists.size(); i++){
bardataset2.addValue(Double.valueOf(lists.get(i)), map.get("name").toString(), dates.get(i));
}
}
}
//創建主題樣式
StandardChartTheme standardChartTheme=new StandardChartTheme("CN");
//設置標題字體
standardChartTheme.setExtraLargeFont(new Font("微軟雅黑",Font.BOLD,20));
//設置圖例的字體
standardChartTheme.setRegularFont(new Font("微軟雅黑",Font.PLAIN,15));
//設置軸向的字體
standardChartTheme.setLargeFont(new Font("微軟雅黑",Font.PLAIN,15));
//應用主題樣式
ChartFactory.setChartTheme(standardChartTheme);
//定義圖標對象
JFreeChart chart = ChartFactory.createLineChart(title,// 報表題目,字符串類型
null, // 橫軸
null, // 縱軸
null, // 數據集
PlotOrientation.VERTICAL, // 圖標方向垂直
true, // 顯示圖例
false, // 不用生成工具
false // 不用生成URL地址
);
LineAndShapeRenderer lineRenderer = new LineAndShapeRenderer();//默認折線圖畫圖器
BarRenderer barRenderer = new BarRenderer();//默認柱狀圖畫圖器
barRenderer.setItemMargin(0.1);
CategoryPlot plot = chart.getCategoryPlot();//獲取畫板
CategoryAxis xAxis = plot.getDomainAxis();//獲取x軸
xAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);//設置x軸標籤往上倒90度
xAxis.setLowerMargin(0);
xAxis.setCategoryMargin(0.3);
NumberAxis yAxis1 = new NumberAxis();//y軸1
yAxis1.setLabel(yAxises.get(0).get("name").toString());
yAxis1.setAutoRange(false);
yAxis1.setRange(Double.valueOf(yAxises.get(0).get("min").toString()), Double.valueOf(yAxises.get(0).get("max").toString()));
yAxis1.setLabelFont(new Font("微軟雅黑",Font.PLAIN,15));
if(yAxises.size() > 1){//如果有需要雙y軸,則添加一條y軸
NumberAxis yAxis2 = new NumberAxis();
yAxis2.setAutoRange(false);
yAxis2.setLabel(yAxises.get(1).get("name").toString());
yAxis2.setRange(Double.valueOf(yAxises.get(1).get("min").toString()), Double.valueOf(yAxises.get(1).get("max").toString()));
yAxis2.setLabelFont(new Font("微軟雅黑",Font.PLAIN,15));
plot.setRangeAxis(1, yAxis2);
}
plot.setRangeAxis(0, yAxis1);
//如果有數據,則寫入數據,並設置數據對應軸線
if(bardataset.getColumnCount() > 0){
plot.setDataset(1, bardataset);
plot.setRenderer(1, barRenderer);
plot.mapDatasetToRangeAxis(1, 0);
plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);//折線在柱面前面顯示
}
if(linedataset.getColumnCount() > 0){
plot.setDataset(0, linedataset);
plot.setRenderer(0, lineRenderer);
plot.mapDatasetToRangeAxis(0, 0);
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);//折線在柱面前面顯示
}
if(bardataset2.getColumnCount() > 0){
plot.setDataset(3, bardataset2);
plot.setRenderer(3, barRenderer);
plot.mapDatasetToRangeAxis(3, 1);
plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);//折線在柱面前面顯示
}
if(linedataset2.getColumnCount() > 0){
plot.setDataset(2, linedataset2);
plot.setRenderer(2, lineRenderer);
plot.mapDatasetToRangeAxis(2, 1);
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);//折線在柱面前面顯示
}
chart.getLegend().setItemLabelPadding(new RectangleInsets(0, 0, 0, 50D));//設置圖例間距
chart.getTitle().setMargin(0, -800, 0, 0);//設置標題居左
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int day = calendar.get(Calendar.DAY_OF_MONTH);
int month = calendar.get(Calendar.MONTH) + 1;
int year = calendar.get(Calendar.YEAR);
String file_path = PropertiesRead.getinstance().getValue("PATH");
path = year + "-" + month + "-" + day + "/" + Uuid.getUUID() + "lineChart.png";
file_path = file_path + path;
saveChartAsJPEG(chart, file_path);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.