Java 学习笔记之jfreechart 生成折线图,饼图, ...

本文介绍了如何使用Java的jfreechart库生成各种图表,包括折线图、饼图、单组柱状图、多组柱状图和堆积柱状图。通过示例代码详细展示了每种图表的生成过程,适用于数据可视化需求。
摘要由CSDN通过智能技术生成
package com.yyh.jfreechart;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

/**
 * 实际取色的时候一定要16位的,这样比较准确
 * 
 * @author new
 */
public class CreateChartServiceImpl {

    private static final String CHART_PATH = "E:/test/";

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CreateChartServiceImpl pm = new CreateChartServiceImpl();
        // 生成饼状图
        pm.makePieChart();
        // 生成单组柱状图
        pm.makeBarChart();
        // 生成多组柱状图
        pm.makeBarGroupChart();
        // 生成堆积柱状图
        pm.makeStackedBarChart();
        // 生成折线图
        pm.makeLineAndShapeChart();
    }

    /**
     * 生成折线图
     */
    public void makeLineAndShapeChart() {
        double[][] data = new double[][]{
            {672, 766, 223, 540, 126},
            {325, 521, 210, 340, 106},
            {332, 256, 523, 240, 526}
        };
        String[] rowKeys = {"苹果", "梨子", "葡萄"};
        String[] columnKeys = {"北京", "上海", "广州", "成都", "深圳"};
        CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
        createTimeXYChar("折线图", "x轴", "y轴", dataset, "lineAndShap.png");
    }

    /**
     * 生成分组的柱状图
     */
    public void makeBarGroupChart() {
        double[][] data = new double[][]{
            {672, 766, 223, 540, 126},
            {325, 521, 210, 340, 106},
            {332, 256, 523, 240, 526}
        };
        String[] rowKeys = {"苹果", "梨子", "葡萄"};
        String[] columnKeys = {"北京", "上海", "广州", "成都", "深圳"};
        CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
        createBarChart(dataset, "x坐标", "y坐标", "柱状图", "barGroup.png");
    }

    /**
     * 生成柱状图
     */
    public void makeBarChart() {
        double[][] data = new double[][]{
            {672, 766, 223, 540, 126}
        };
        String[] rowKeys = {"苹果"};
        String[] columnKeys = {"北京", "上海", "广州", "成都", "深圳"};
        CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
        createBarChart(dataset, "x坐标", "y坐标", "柱状图", "bar.png");
    }

    /**
     * 生成堆栈柱状图
     */
    public void makeStackedBarChart() {
        double[][] data = new double[][]{
            {0.21, 0.66, 0.23, 0.40, 0.26},
            {0.25, 0.21, 0.10, 0.40, 0.16}
        };
        String[] rowKeys = {"苹果", "梨子"};
        String[] columnKeys = {"北京", "上海", "广州", "成都", "深圳"};
        CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
        createStackedBarChart(dataset, "x坐标", "y坐标", "柱状图", "stsckedBar.png");
    }

    /**
     * 生成饼状图
     */
    public void makePieChart() {
        double[] data = {9, 91};
        String[] keys = {"失败率", "成功率"};

        createValidityComparePimChar(getDataPieSetByUtil(data, keys), "饼状图",
                "pie2.png", keys);
    }

    // 柱状图,折线图 数据集
    public CategoryDataset getBarData(double[][] data, String[] rowKeys,
            String[] columnKeys) {
        return DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);

    }

    // 饼状图 数据集
    public PieDataset getDataPieSetByUtil(double[] data,
            String[] datadescription) {

        if (data != null && d
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值