Demo20200710.zip

  • 长庚123
    了解作者
  • Java
    开发工具
  • 116KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2020-09-27 08:08
    上传日期
网站信息抓取:选取一个特定的网站(豆瓣、安居客、雪球、B站或者游戏论坛等...),使用Java爬取10000个页面的数据后,对数据进行统计分析。
Demo20200710.zip
内容介绍
package url; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.StandardChartTheme; import org.jfree.chart.labels.*; import org.jfree.chart.plot.*; import org.jfree.chart.renderer.category.BarRenderer3D; import org.jfree.chart.renderer.xy.XYItemRenderer; import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer; import org.jfree.data.category.CategoryDataset; import org.jfree.data.general.DatasetUtilities; import org.jfree.data.general.DefaultPieDataset; import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeriesCollection; import org.jfree.ui.TextAnchor; import org.jfree.util.Rotation; import java.awt.*; import java.io.FileOutputStream; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Map; //生成柱状图 public class Chart { public String barChart() { FileOutputStream fos_jpg = null; String path = System.getProperty("user.dir") + "\\src\\main\\resources\\" + System.currentTimeMillis() + "barChart.jpg"; try { Map<String, Integer> map = DoubanUtils.Count();//从数据库获取数据 Integer one = map.get("one"); Integer two = map.get("two"); Integer three = map.get("three"); Integer four = map.get("four"); Integer five = map.get("five"); double[][] data = new double[][]{{one}, {two}, {three}, {four}, {five}}; String[] rowKeys = {">=8.5\">=9\", ", ">=8", ">=7.5", "<7.5"}; String[] columnKeys = {"评分"}; CategoryDataset dataset = DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); 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.createBarChart3D( "短片评分柱状图", // 图表标题 "短片", // 目录轴的显示标签 "数量", // 数值轴的显示标签 dataset, // 数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例(对于简单的柱状图必须是 false) false, // 是否创建工具提示 (tooltip) false // 是否生成 URL 链接 ); CategoryPlot plot = chart.getCategoryPlot(); // 设置网格背景颜色 plot.setBackgroundPaint(Color.white); // 设置网格竖线颜色 plot.setDomainGridlinePaint(Color.pink); // 设置网格横线颜色 plot.setRangeGridlinePaint(Color.pink); // 显示每个柱的数值,并修改该数值的字体属性 BarRenderer3D renderer = new BarRenderer3D(); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT)); renderer.setItemLabelAnchorOffset(10D); // 设置平行柱的之间距离 renderer.setItemMargin(0.4); plot.setRenderer(renderer); fos_jpg = new FileOutputStream(path); ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 700, 500, null); } catch (Exception e) { System.out.println("error"); } finally { try { fos_jpg.close(); } catch (Exception e) { System.out.println("error2"); } return path; } } public String pieChart() { FileOutputStream fos_jpg = null; String path = System.getProperty("user.dir") + "\\src\\main\\resources\\" + System.currentTimeMillis() + "pieChart.jpg"; try { Map<String, Integer> map = DoubanUtils.Count(); Integer one = map.get("one"); Integer two = map.get("two"); Integer three = map.get("three"); Integer four = map.get("four"); Integer five = map.get("five"); DefaultPieDataset data = new DefaultPieDataset(); data.setValue(">=9", one); data.setValue(">=8.5", two); data.setValue(">=8", three); data.setValue(">=7.5", four); data.setValue("<7.5", five); 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.createPieChart3D( "评分饼状图", // 图表标题 data, true, // 是否显示图例 false, // 是否创建工具提示 (tooltip) false // 是否生成 URL 链接 ); //显示百分比 PiePlot pieplot = (PiePlot) chart.getPlot(); pieplot.setLabelFont(new Font("宋体", 0, 12)); pieplot.setNoDataMessage("无数据"); pieplot.setCircular(true); pieplot.setLabelGap(0.02D); pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {2}", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot(); //设置开始角度 pieplot3d.setStartAngle(120D); //设置方向为”顺时针方向“ pieplot3d.setDirection(Rotation.CLOCKWISE); //设置透明度,0.5F为半透明,1为不透明,0为全透明 pieplot3d.setForegroundAlpha(0.7F); fos_jpg = new FileOutputStream(path); ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 700, 500, null); } catch (Exception e) { System.out.println("error"); } finally { try { fos_jpg.close(); } catch (Exception e) { System.out.println("error2"); } return path; } } public String lineChart() { FileOutputStream fos_jpg = null; String path = System.getProperty("user.dir") + "\\src\\main\\resources\\" + System.currentTimeMillis() + "lineChart.jpg"; try { XYSeriesCollection collection = new XYSeriesCollection(); XYSeries series = new XYSeries("折线"); Map<String, Integer> map2 = DoubanUtils.lineChart(); int number = 99; for (int i = 0; i < map2.size(); i++) { String s = number + ""; String score = s.charAt(0) + "." + s.charAt(1); series.add(Double.parseDouble(score), map2.get(score)); number--; } collection.addSeries(series); StandardChartTheme standardChartTheme = new St
评论
    相关推荐