实现含有折线图的pdf下载

This commit is contained in:
沈健
2021-04-09 19:42:12 +08:00
parent 6d1e03c34a
commit ccfb806d6a
7 changed files with 248 additions and 36 deletions
@@ -312,25 +312,6 @@ public class DataUtil {
return ret; return ret;
} }
/**
* 加密图片url
*
* @param u
* @param s
* @param t
* @return
*/
public static String encodePicUrl(String u, String s, String t) {
if (StringUtils.isBlank(u)) {
return StringUtils.EMPTY;
}
try {
return SecurityUtil.encode("u=" + URLEncoder.encode(u, "UTF-8") + "&s=" + s + "&t=" + t);
} catch (UnsupportedEncodingException e) {
return SecurityUtil.encode("u=" + u + "&s=" + s + "&t=" + t);
}
}
/** /**
* 把字符串以16进制数值显示出来 * 把字符串以16进制数值显示出来
* *
@@ -1805,6 +1786,25 @@ public class DataUtil {
return b2.toString(); return b2.toString();
} }
/**
* 加密图片url
*
* @param u
* @param s
* @param t
* @return
*/
public static String encodePicUrl(String u, String s, String t) {
if (StringUtils.isBlank(u)) {
return StringUtils.EMPTY;
}
try {
return SecurityUtil.encode("u=" + URLEncoder.encode(u, "UTF-8") + "&s=" + s + "&t=" + t);
} catch (UnsupportedEncodingException e) {
return SecurityUtil.encode("u=" + u + "&s=" + s + "&t=" + t);
}
}
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(getFirstChar("文裕花园")); System.out.println(getFirstChar("文裕花园"));
System.out.println(getFirstChar("文怡花园")); System.out.println(getFirstChar("文怡花园"));
+7
View File
@@ -147,11 +147,18 @@
<version>1.0.0</version> <version>1.0.0</version>
<classifier>jdk18</classifier> <classifier>jdk18</classifier>
</dependency> </dependency>
<!-- html转pdf -->
<dependency> <dependency>
<groupId>org.xhtmlrenderer</groupId> <groupId>org.xhtmlrenderer</groupId>
<artifactId>core-renderer</artifactId> <artifactId>core-renderer</artifactId>
<version>R8</version> <version>R8</version>
</dependency> </dependency>
<!-- 绘制折线图等 -->
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.5.3</version>
</dependency>
<dependency> <dependency>
<groupId>org.sothis</groupId> <groupId>org.sothis</groupId>
<artifactId>sothis-core</artifactId> <artifactId>sothis-core</artifactId>
@@ -5103,8 +5103,8 @@ public class PropertyController {
//周边相似小区租金 //周边相似小区租金
Integer room = null; Integer room = null;
if(model.getHouseType()!=null){ if(model.getRoom()!=0){
room = model.getHouseType(); room = model.getRoom();
}else{ }else{
room = houseService.getDistrictRoomType((float)model.getSize()); room = houseService.getDistrictRoomType((float)model.getSize());
} }
@@ -1,7 +1,5 @@
package com.fangjia.open.controllers.weixin; package com.fangjia.open.controllers.weixin;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.client.identify.Base64;
import com.fangjia.core.util.*; import com.fangjia.core.util.*;
import com.fangjia.dal.QueryParam; import com.fangjia.dal.QueryParam;
import com.fangjia.dal.SdQuery; import com.fangjia.dal.SdQuery;
@@ -15,27 +13,29 @@ import com.fangjia.open.compatible.model.JsonModel;
import com.fangjia.open.dto.DistrictHouseDtao; import com.fangjia.open.dto.DistrictHouseDtao;
import com.fangjia.open.interceptors.AuthenticationBean; import com.fangjia.open.interceptors.AuthenticationBean;
import com.fangjia.open.model.BaseModel; import com.fangjia.open.model.BaseModel;
import com.fangjia.open.model.BasicModel;
import com.fangjia.open.model.weixin.EvaluationModel; import com.fangjia.open.model.weixin.EvaluationModel;
import com.fangjia.open.model.weixin.PropertyEvaluationModel; import com.fangjia.open.model.weixin.PropertyEvaluationModel;
import com.fangjia.open.util.LineChartsUtils;
import com.fangjia.service.api.AutoCompleteBo; import com.fangjia.service.api.AutoCompleteBo;
import com.fangjia.service.datastore.DistrictService; import com.fangjia.service.datastore.DistrictService;
import com.fangjia.service.datastore.EvaluationService; import com.fangjia.service.datastore.EvaluationService;
import com.fangjia.service.datastore.HouseService; import com.fangjia.service.datastore.HouseService;
import com.fangjia.service.district.DistrictRoomSqmetreRentService; import com.fangjia.service.district.DistrictRoomSqmetreRentService;
import com.fangjia.service.district.RentDealPriceService; import com.fangjia.service.district.RentDealPriceService;
import com.fangjia.service.mongo.fangjia.FileService;
import com.fangjia.service.mongo.fangjia.SeawaterService; import com.fangjia.service.mongo.fangjia.SeawaterService;
import com.fangjia.service.util.DistrictScoreUtil; import com.fangjia.service.util.DistrictScoreUtil;
import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.BaseFont;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sothis.core.util.CollectionUtils;
import org.sothis.web.mvc.ActionContext; import org.sothis.web.mvc.ActionContext;
import org.sothis.web.mvc.annotation.Param; import org.sothis.web.mvc.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.xhtmlrenderer.pdf.ITextFontResolver; import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer; import org.xhtmlrenderer.pdf.ITextRenderer;
@@ -55,12 +55,15 @@ import java.util.*;
*/ */
public class CcbRentController { public class CcbRentController {
private static final Logger logger = LoggerFactory.getLogger(CcbRentController.class);
private DistrictService districtService; private DistrictService districtService;
private SeawaterService seawaterService; private SeawaterService seawaterService;
private DistrictRoomSqmetreRentService districtRoomSqmetreRentService; private DistrictRoomSqmetreRentService districtRoomSqmetreRentService;
private EvaluationService evaluationService; private EvaluationService evaluationService;
private HouseService houseService; private HouseService houseService;
private RentDealPriceService rentDealPriceService; private RentDealPriceService rentDealPriceService;
private FileService fileService;
public Object indexAction(BaseModel result) { public Object indexAction(BaseModel result) {
return result; return result;
@@ -203,11 +206,18 @@ public class CcbRentController {
} }
} }
} }
if (CollectionUtils.isNotEmpty(houses)) {
for (Seawater seawater: houses) {
if (seawater.getpDate() != null) {
seawater.setRoot(DateUtil.formatByPattern(DateUtil.parseDate(String.valueOf(seawater.getpDate()), "yyyyMMdd"), "yyyy-MM-dd"));
}
}
}
districtPriceMap.put("houses", houses); districtPriceMap.put("houses", houses);
//周边相似小区租金 //周边相似小区租金
if(model.getHouseType()!=null){ if(model.getRoom() != 0){
room = model.getHouseType(); room = model.getRoom();
}else{ }else{
room = houseService.getDistrictRoomType((float)model.getSize()); room = houseService.getDistrictRoomType((float)model.getSize());
} }
@@ -250,16 +260,48 @@ public class CcbRentController {
Map<String, Object> trendsMap = districtRoomSqmetreRentService.getRentTrends(district.getCity(), district.getRegion(), district.getName(), room, -12); Map<String, Object> trendsMap = districtRoomSqmetreRentService.getRentTrends(district.getCity(), district.getRegion(), district.getName(), room, -12);
districtPriceMap.put("trends", trendsMap); districtPriceMap.put("trends", trendsMap);
// 生成走势图图片,一遍转pdf
String tradesImgUrl = buildTrendImage(trendsMap, room);
//成交的租房房源 //成交的租房房源
List<RentDealPrice> rentTrades = rentDealPriceService.getRentDealPrices(district.getCity(), district.getRegion(), district.getName(), 5); List<RentDealPrice> rentTrades = rentDealPriceService.getRentDealPrices(district.getCity(), district.getRegion(), district.getName(), 5);
if (CollectionUtils.isNotEmpty(rentTrades)) {
for (RentDealPrice price : rentTrades) {
// 返回成交日期到前端,直接取日期转pdf报错!!!!!!!!!!!!!!!!!
if (price.getTradeDate() != null) {
price.setSource(DateUtil.formatByPattern(price.getTradeDate(), "yyyy-MM-dd"));
}
}
}
districtPriceMap.put("trades", rentTrades); districtPriceMap.put("trades", rentTrades);
districtPriceMap.put("tradesImgUrl", tradesImgUrl);
result.setResult(districtPriceMap); result.setResult(districtPriceMap);
} }
// result.setResult(new HashMap<String, Object>());
return result; return result;
} }
private String buildTrendImage(Map<String, Object> trendsMap, Integer room) {
List<String> times = (List<String>) trendsMap.get("times");
Map<String, Object> distMap = (Map<String, Object>) ((Map<String, Object>) trendsMap.get("distMap")).get(String.valueOf(room));
if (distMap != null) {
List<Integer> values = new ArrayList<Integer>();
for (int i = 0; i < times.size(); i++) {
if (distMap.get(times.get(i)) != null) {
values.add((Integer) distMap.get(times.get(i)));
} else {
values.add(null);
}
}
// 保存到本地测试
//String url = LineChartsUtils.createChartAndSave2Png("d://test.png",room + "居室", times, values);
String url = LineChartsUtils.createChartAndUpload(fileService,room + "居室", times, values);
logger.info("租金估值图片上传至服务器:{}", url);
return url;
}
return null;
}
/** /**
* 通过EvaluationModel组装Evaluation实体 * 通过EvaluationModel组装Evaluation实体
* *
@@ -534,4 +576,8 @@ public class CcbRentController {
public void setRentDealPriceService(RentDealPriceService rentDealPriceService) { public void setRentDealPriceService(RentDealPriceService rentDealPriceService) {
this.rentDealPriceService = rentDealPriceService; this.rentDealPriceService = rentDealPriceService;
} }
public void setFileService(FileService fileService) {
this.fileService = fileService;
}
} }
@@ -0,0 +1,137 @@
package com.fangjia.open.util;
import com.fangjia.core.util.DataUtil;
import com.fangjia.service.mongo.fangjia.FileService;
import org.bson.types.ObjectId;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.ui.ApplicationFrame;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RefineryUtilities;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 折线图绘图工具类
*
* @author shenjian
* @since 2021/4/9
*/
public class LineChartsUtils {
public static void main(String[] args) throws IOException {
File file = new File("d://test.png");
List<String> columns = new ArrayList<String>();
columns.add("2020-02");
columns.add("2020-03");
columns.add("2020-04");
columns.add("2020-05");
columns.add("2020-06");
columns.add("2020-07");
columns.add("2020-08");
columns.add("2020-09");
columns.add("2020-10");
columns.add("2020-11");
columns.add("2020-12");
columns.add("2021-01");
List<Integer> values = new ArrayList<Integer>();
values.add(1000);
values.add(2000);
values.add(2000);
values.add(2000);
values.add(2000);
values.add(2000);
values.add(2000);
values.add(3000);
values.add(3000);
values.add(3000);
values.add(3000);
values.add(3000);
createChartAndSave2Png("d://test.png", "1居室", columns, values);
}
public static String createChartAndSave2Png(String path, String rowKey, List<String> columns, List<Integer> values) {
File file = new File(path);
try {
ChartUtilities.saveChartAsPNG(file, createChart(rowKey, columns, values),800,300);
} catch (IOException e) {
e.printStackTrace();
}
return path;
}
public static String createChartAndUpload(FileService fileService, String rowKey, List<String> columns, List<Integer> values) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ChartUtilities.writeChartAsPNG(os, createChart(rowKey, columns, values),800,300);
byte[] data = os.toByteArray();
String id = new ObjectId().toString();
fileService.save(id,"o", data, "image/png", "o");
String url = "http://img0.fangjia.com/stc/fs/" + DataUtil.encodePicUrl(id, "o", "house");
return url;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 定义图表对象
* @param rowKey 曲线名,如一居室
* @param columns 横坐标名
* @param values 纵坐标值
*/
public static JFreeChart createChart(String rowKey, List<String> columns, List<Integer> values) {
createDataset(rowKey, columns, values);
// 折线图名称, 横坐标名称, 纵坐标名称 数据水平竖直 PlotOrientation.HORIZONTAL, 水平显示图像 true, 水平显示图像 true, include legend true, tooltips false, urls
JFreeChart chart = ChartFactory.createLineChart("", "", "", createDataset(rowKey, columns, values), PlotOrientation.VERTICAL, true, true, false);
CategoryPlot plot = chart.getCategoryPlot();
plot.setRangeGridlinesVisible(false); // 是否显示格子线
plot.setBackgroundAlpha(0.3f); // 设置背景透明度
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setSeriesPaint(0, Color.BLUE);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setUpperMargin(0.20);
rangeAxis.setLabelAngle(Math.PI / 2.0);
return chart;
}
private static DefaultCategoryDataset createDataset(String rowKey, List<String> columns, List<Integer> values) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < columns.size(); i++) {
String column = columns.get(i);
if (values.get(i) != null) {
dataset.addValue(Double.valueOf(values.get(i)), rowKey, column);
} else {
dataset.addValue(null, rowKey, column);
}
}
return dataset;
}
}
@@ -106,7 +106,18 @@
</div> </div>
<div> <div>
<div class="title1">5 历史租金</div> <div class="title1">5 历史租金</div>
<div class="charts" id="districtRentTrendsChart"></div> <div>
<#if result.tradesImgUrl??>
<#-- <img width="800px" height="300px" src="../static/images/test.png"></img>-->
<img width="800px" height="300px" src="${result.tradesImgUrl}"></img>
<#else>
<div class="p table">
<table class="inline">
<tbody><tr><td>暂无数据</td></tr></tbody>
</table>
</div>
</#if>
</div>
</div> </div>
<div> <div>
<div class="title1">6 已租房源</div> <div class="title1">6 已租房源</div>
@@ -137,7 +148,7 @@
<td><#if bo.room??>${bo.room}</#if></td> <td><#if bo.room??>${bo.room}</#if></td>
<td><#if bo.hall??>${bo.hall}</#if></td> <td><#if bo.hall??>${bo.hall}</#if></td>
<td><#if bo.fitment??>${bo.fitment}</#if></td> <td><#if bo.fitment??>${bo.fitment}</#if></td>
<td><#if bo.tradeDate??>${bo.tradeDate}</#if></td> <td><#if bo.source??>${bo.source}</#if></td>
</tr> </tr>
</#list> </#list>
<#else> <#else>
@@ -176,7 +187,7 @@
<td><#if bo.room??>${bo.room}</#if></td> <td><#if bo.room??>${bo.room}</#if></td>
<td><#if bo.hall??>${bo.hall}</#if></td> <td><#if bo.hall??>${bo.hall}</#if></td>
<td><#if bo.fitment??>${bo.fitment}</#if></td> <td><#if bo.fitment??>${bo.fitment}</#if></td>
<td><#if bo.pDate??>${bo.pDate}</#if></td> <td><#if bo.root??>${bo.root}</#if></td>
</tr> </tr>
</#list> </#list>
<#else> <#else>
@@ -201,9 +212,20 @@
</tr> </tr>
</thead> </thead>
<tbody id="sameDistricts"> <tbody id="sameDistricts">
<tr class=""> <#if (result.districts??&&result.districts?size>0)>
<td>暂无数据</td> <#list result.districts as bo>
</tr> <tr class="">
<td class="td01"><#if bo.city??>${bo.city}</#if></td>
<td class="td02"><#if bo.region??>${bo.region}</#if></td>
<td><#if bo.districtName??>${bo.districtName}</#if></td>
<td><#if bo.totalPrice??>${bo.totalPrice}</#if></td>
<td><#if bo.room??>${bo.room}</#if></td>
<td><#if bo.hall??>${bo.hall}</#if></td>
</tr>
</#list>
<#else>
<tr><td>暂无数据</td></tr>
</#if>
</tbody> </tbody>
</table> </table>
</div> </div>
@@ -47,7 +47,7 @@ public class DistrictRoomSqmetreRentServiceImpl implements DistrictRoomSqmetreRe
rooms = new ArrayList<Integer>(); rooms = new ArrayList<Integer>();
for(DistrictRoomSqmetreRent rent : districtRents){ for(DistrictRoomSqmetreRent rent : districtRents){
Map<String, Integer> priceMap = distMap.get(room); Map<String, Integer> priceMap = distMap.get(String.valueOf(room));
if(null == priceMap){ if(null == priceMap){
priceMap = new HashMap<String, Integer>(); priceMap = new HashMap<String, Integer>();
distMap.put(String.valueOf(room), priceMap); distMap.put(String.valueOf(room), priceMap);