给我一个物流订单数据集,并用java 对数据集进行清洗预处理设置特征值,对物流状态,已接单,未接单,已完成,派送中,用echarts 进行可视化饼图,折线图,柱形图,并展示代码
时间: 2023-12-11 18:05:59 浏览: 134
(疫情、物流)echarts数据可视化大屏源码
5星 · 资源好评率100%
好的,这里也推荐给您一个Kaggle上的物流订单数据集:https://www.kaggle.com/shashwatwork/logistics-industry-datasets
下面是对该数据集的清洗预处理、设置特征值以及可视化的Java代码:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject;
import echarts.Option;
import echarts.axis.AxisLabel;
import echarts.axis.CategoryAxis;
import echarts.axis.ValueAxis;
import echarts.charts.Bar;
import echarts.charts.Line;
import echarts.charts.Pie;
import echarts.option.Legend;
import echarts.option.Title;
import echarts.option.Tooltip;
import echarts.series.BarSeries;
import echarts.series.LineSeries;
import echarts.series.PieSeries;
public class LogisticsAnalysis {
public static void main(String[] args) throws IOException, ParseException {
// 读取数据集
BufferedReader reader = new BufferedReader(new FileReader("logistics_industry.csv"));
List<String> lines = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
// 去除标题行
lines.remove(0);
// 去除重复行
List<String> uniqueLines = lines.stream().distinct().collect(Collectors.toList());
// 处理日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
List<Map<String, Object>> dataList = new ArrayList<>();
for (String uniqueLine : uniqueLines) {
String[] fields = uniqueLine.split(",");
Map<String, Object> data = new HashMap<>();
data.put("Pickup - Date", dateFormat.parse(fields[1]));
if (!fields[2].isEmpty()) {
data.put("Pickup - Time", fields[2]);
}
data.put("Delivery - Date", dateFormat.parse(fields[3]));
if (!fields[4].isEmpty()) {
data.put("Delivery - Time", fields[4]);
}
data.put("Delivery - Status", fields[5]);
dataList.add(data);
}
// 设置特征值
for (Map<String, Object> data : dataList) {
String deliveryTime = (String) data.get("Delivery - Time");
if (deliveryTime == null) {
data.put("Delivery Status", "未完成");
data.put("接单状态", "未接单");
data.put("派送状态", "派送中");
} else {
data.put("Delivery Status", "已完成");
data.put("接单状态", "已接单");
data.put("派送状态", null);
}
}
// 绘制饼图
Map<String, Integer> statusCountMap = new HashMap<>();
for (Map<String, Object> data : dataList) {
String deliveryStatus = (String) data.get("Delivery Status");
int count = statusCountMap.getOrDefault(deliveryStatus, 0);
statusCountMap.put(deliveryStatus, count + 1);
}
List<PieSeries> pieSeries = new ArrayList<>();
for (Map.Entry<String, Integer> entry : statusCountMap.entrySet()) {
PieSeries series = new PieSeries();
series.setName(entry.getKey());
series.setValue(entry.getValue());
pieSeries.add(series);
}
Option pieOption = new Option();
pieOption.title(new Title().text("物流订单交付状态饼图"));
pieOption.tooltip(new Tooltip());
pieOption.legend(new Legend().setShow(true).setRight("right").setTop("middle"));
Pie pie = new Pie();
pie.series(pieSeries.toArray(new PieSeries[0]));
pieOption.series(pie);
String pieHtml = pieOption.toHtml();
// 绘制折线图
Map<String, Integer> orderCountMap = new HashMap<>();
for (Map<String, Object> data : dataList) {
Calendar calendar = Calendar.getInstance();
calendar.setTime((Date) data.get("Pickup - Date"));
String yearMonth = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1);
int count = orderCountMap.getOrDefault(yearMonth, 0);
orderCountMap.put(yearMonth, count + 1);
}
List<String> xData = new ArrayList<>(orderCountMap.keySet());
List<Integer> yData = new ArrayList<>(orderCountMap.values());
LineSeries lineSeries = new LineSeries();
lineSeries.setName("订单数");
lineSeries.setData(yData.toArray(new Integer[0]));
Line line = new Line();
line.xAxis(new CategoryAxis().setData(xData.toArray(new String[0])));
line.yAxis(new ValueAxis());
line.series(lineSeries);
Option lineOption = new Option();
lineOption.title(new Title().text("物流订单每月订单数折线图"));
lineOption.tooltip(new Tooltip());
lineOption.legend(new Legend().setShow(true).setRight("right").setTop("middle"));
lineOption.xAxis(new CategoryAxis().setAxisLabel(new AxisLabel().setRotate(-45)));
lineOption.series(line);
String lineHtml = lineOption.toHtml();
// 绘制柱形图
int orderCount = 0;
int shippingCount = 0;
for (Map<String, Object> data : dataList) {
String pickupTime = dateFormat.format(data.get("Pickup - Date"));
String deliveryTime = (String) data.get("Delivery - Time");
String deliveryStatus = (String) data.get("Delivery - Status");
if (deliveryTime == null) {
orderCount++;
if (deliveryStatus != null) {
shippingCount++;
}
}
}
List<String> categories = new ArrayList<>();
categories.add("接单状态");
categories.add("派送状态");
List<Integer> data = new ArrayList<>();
data.add(orderCount);
data.add(shippingCount);
BarSeries barSeries = new BarSeries();
barSeries.setName("订单数");
barSeries.setData(data.toArray(new Integer[0]));
Bar bar = new Bar();
bar.xAxis(new CategoryAxis().setData(categories.toArray(new String[0])));
bar.yAxis(new ValueAxis());
bar.series(barSeries);
Option barOption = new Option();
barOption.title(new Title().text("物流订单接单状态与派送状态柱形图"));
barOption.tooltip(new Tooltip());
barOption.legend(new Legend().setShow(true).setRight("right").setTop("middle"));
barOption.series(bar);
String barHtml = barOption.toHtml();
// 输出HTML文件
String htmlContent = "<html><head><meta charset=\"UTF-8\"></head><body>%s%s%s</body></html>";
System.out.println(String.format(htmlContent, pieHtml, lineHtml, barHtml));
}
}
```
以上代码中,使用了fastjson和echarts-java库进行可视化,同时需要注意Java版本的兼容性。需要自行安装echarts-java库和fastjson库。
阅读全文