JfreeChart折线图工具类

    /**
     * @param chartLineMap  多条线的横纵坐标数据集合
     * @param yLineUnit y轴值的单位
     * @return 生成图像的byte数据
     * @throws IOException 生成数据异常
     */
    public static byte[] getChartLineUrlUtil(String title, ChartLineMap>> chartLineMap, String yLineUnit, String xLineUnit,Integer imgWidth,Integer imgHeight) throws IOException {
        //点集
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        Set>>> entries = chartLineMap.entrySet();
        //多条线,循环画
        for (Map.Entry>> entry : entries) {
            String lineName = entry.getKey();
            ChartLine> linePoints = entry.getValue();

            //每条线的点需要根据时间重新排序,否则偶尔会导致时间轴部分错乱,此行代码重要
            linePoints.sort(
                    Comparator.comparing(
                            ChartLinePoint::getXLineName));

            //点连线
            for (ChartLinePoint linePoint : linePoints) {
                //System.out.print(linePoint.xLineName+"------");
                dataset.addValue(
                        (Number) linePoint.getYLineValue(),
                        lineName,
                        linePoint.getXLineName()
                );
            }
            //System.out.println();
        }

        //以下为具体画图代码
        JFreeChart chart = ChartFactory.createLineChart(
                title,
                xLineUnit,
                yLineUnit,
                dataset,
                PlotOrientation.VERTICAL,
                true,
                false,
                false
        );

        CategoryPlot categoryPlot = chart.getCategoryPlot();

        categoryPlot.setBackgroundPaint(Color.WHITE); // 设置绘图区背景色
        categoryPlot.setRangeCrosshairLockedOnData(true);
        categoryPlot.setRangeGridlinePaint(Color.GRAY); // 设置水平方向背景线颜色
        categoryPlot.setRangeGridlinesVisible(true);// 设置是否显示水平方向背景线,默认值为true
        categoryPlot.setDomainGridlinePaint(Color.GRAY); // 设置垂直方向背景线颜色
        //categoryPlot.setDomainGridlinesVisible(true); // 设置是否显示垂直方向背景线,默认值为false
        //categoryPlot.setRangeMinorGridlinesVisible(true);

        Font mainTitle = FontLoadUtil.getInstanceTitleFont();
        Font contentTitle = FontLoadUtil.getInstanceTextFont();

        //设置标题颜色
        TextTitle textTitle = chart.getTitle();
        textTitle.setFont(mainTitle);

        //设置x坐标字体
        CategoryAxis categoryAxis = categoryPlot.getDomainAxis();
        categoryAxis.setTickLabelFont(contentTitle);
        categoryAxis.setLabelFont(contentTitle);
        //很坐标时间旋转显示
        categoryAxis.setCategoryLabelPositions(CategoryLabelPositions
                .createUpRotationLabelPositions(Math.PI / 6.0));
        //横坐标时间疏密调整
        setDomainAxis(categoryAxis,dataset);
        //设置y坐标字体
        ValueAxis rangeAxis = categoryPlot.getRangeAxis();
        rangeAxis.setTickLabelFont(contentTitle);
        rangeAxis.setLabelFont(contentTitle);

        //rangeAxis.setAxisLineStroke(new BasicStroke(5.0f)); // Y坐标轴粗细
        rangeAxis.setAxisLineVisible(true);
        rangeAxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());// Y轴显示数字
        rangeAxis.setAutoRangeMinimumSize(2); // 最小跨度
        rangeAxis.setUpperMargin(0.18);// 上边距,防止最大的一个数据靠近了坐标轴。
        rangeAxis.setLowerBound(0); // 最小值显示0
        rangeAxis.setAutoRange(false); // 不自动分配Y轴数据
        rangeAxis.setTickMarkStroke(new BasicStroke(3.0f)); // 设置坐标标记大小
        rangeAxis.setTickMarkPaint(Color.BLACK); // 设置坐标标记颜色

        chart.getLegend().setItemFont(contentTitle);


        LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryPlot.getRenderer();
        //lineandshaperenderer.setSeriesShapesVisible(true); // series 点(即数据点)可见
        lineandshaperenderer.setDefaultLinesVisible(true);


        for (int i = 0; i < entries.size(); i++) {
            lineandshaperenderer.setSeriesStroke(i, new BasicStroke(4F));
        }
//        //使阈值线细
//        lineandshaperenderer.setSeriesStroke(entries.size()-1, new BasicStroke(1F));
//        //使最高值线细
//        lineandshaperenderer.setSeriesStroke(0, new BasicStroke(1F));
        // 显示折点数据
        lineandshaperenderer
                .setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        lineandshaperenderer.setDataBoundsIncludesVisibleSeriesOnly(true);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ChartUtils.writeChartAsJPEG(byteArrayOutputStream, chart, imgWidth, imgHeight);

        return byteArrayOutputStream.toByteArray();
    }

你可能感兴趣的:(Java,java,开发语言)