Java代码生成统计图

引入依赖



    org.knowm.xchart
    xchart
    3.8.6

如果在Linux环境出现中文乱码就在Linux中执行该命令。安装字体库,安装完成后,可以在/usr/share目录下看到fonts和fontconfig文件夹

yum -y install fontconfig

单个条形统计图

Java代码生成统计图_第1张图片

import org.junit.Test;
import org.knowm.xchart.BitmapEncoder;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.style.CategoryStyler;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zmc
 */
public class Xchart {
    @Test
    public void Xchart1() throws IOException {
        CategoryChart chart = new CategoryChartBuilder()
                .width(700)
                .height(500)
                .title("柱状统计图")
                .xAxisTitle("日期")
                .yAxisTitle("y轴数据")
                .build();
        //防止中文乱码
        Font font = new Font("SimSun", Font.PLAIN, 12);
        chart.getStyler().setAxisTitleFont(font);
        chart.getStyler().setAxisTickLabelsFont(font);
        List yData = new ArrayList<>();
        yData.add(3.1);
        yData.add(4.2);
        yData.add(2.1);
        yData.add(8.3);
        yData.add(1.2);
        yData.add(0.3);
        yData.add(5.6);
        List xData = new ArrayList<>();
        xData.add("1月");
        xData.add("2月");
        xData.add("3月");
        xData.add("4月");
        xData.add("5月");
        xData.add("6月");
        xData.add("7月");
        chart.addSeries("统计", xData, yData);
        CategoryStyler styler = chart.getStyler();
        //条形间距
        styler.setAvailableSpaceFill(0.32);
        BitmapEncoder.saveBitmap(chart, "E:\\xchart\\测试1.png", BitmapEncoder.BitmapFormat.PNG);
    }
}

复式条形统计图

Java代码生成统计图_第2张图片

import org.junit.Test;
import org.knowm.xchart.BitmapEncoder;
import org.knowm.xchart.CategoryChart;
import org.knowm.xchart.CategoryChartBuilder;
import org.knowm.xchart.style.CategoryStyler;

import java.awt.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zmc
 */
public class Xchart {
    @Test
    public void Xchart2() throws IOException {
        CategoryChart chart = new CategoryChartBuilder()
                .width(700)
                .height(500)
                .title("柱状统计图")
                .xAxisTitle("日期")
                .yAxisTitle("y轴数据")
                .build();
        //防止中文乱码
        Font font = new Font("SimSun", Font.PLAIN, 12);
        chart.getStyler().setAxisTitleFont(font);
        chart.getStyler().setAxisTickLabelsFont(font);
        List yData1 = new ArrayList<>();
        yData1.add(3.1);
        yData1.add(4.2);
        yData1.add(2.1);
        yData1.add(8.3);
        yData1.add(1.2);
        yData1.add(0.3);
        yData1.add(5.6);
        List yData2 = new ArrayList<>();
        yData2.add(2.1);
        yData2.add(4.6);
        yData2.add(2.0);
        yData2.add(4.3);
        yData2.add(8.2);
        yData2.add(1.3);
        yData2.add(3.6);
        List xData = new ArrayList<>();
        xData.add("1月");
        xData.add("2月");
        xData.add("3月");
        xData.add("4月");
        xData.add("5月");
        xData.add("6月");
        xData.add("7月");
        chart.addSeries("统计1", xData, yData1);
        chart.addSeries("统计2", xData, yData2);
        CategoryStyler styler = chart.getStyler();
        //条形间距
        styler.setAvailableSpaceFill(0.32);
        BitmapEncoder.saveBitmap(chart, "E:\\xchart\\测试1.png", BitmapEncoder.BitmapFormat.PNG);
    }
}

借鉴

Index of /javadocs/xchart/org/knowm/xcharticon-default.png?t=N7T8https://knowm.org/javadocs/xchart/org/knowm/xchart/

你可能感兴趣的:(java)