jfreechart 自定义线段图



package lqz.view.chart;

import java.awt.*;
import javax.swing.*;

import org.jfree.chart.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

// Referenced classes of package demo:
//            SampleXYDataset2

public class ScatterPlotDemo1 extends ApplicationFrame
{

    public ScatterPlotDemo1(String s)
    {
        super(s);
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }
    public ScatterPlotDemo1()
    {
        super("asd");
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }

    String bookTitle[] = {"Python", "JAVA", "C#", "Perl", "PHP"};
    String category[] = {"第1周", "第2周", "第3周", "第4周" };
    double bookSales;
    String chartTitle = "JFreeChart实例11: 自定义线段图";

    // 创建数据集
    public CategoryDataset createDataset()
    {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (int i=0; i < bookTitle.length; i++)
        {
            for (int j=0; j < category.length; j++ )
            {
                bookSales = 1 +  (Math.random() * 100);
                dataset.addValue(bookSales, bookTitle[i], category[j]);
            }
        }
        return dataset;
    }

    public JFreeChart createChart(CategoryDataset dataset)
    {
        // 创建图表对象
        JFreeChart chart = ChartFactory.createLineChart
                (
                        chartTitle,     // 图表标题
                        "销售时间:2005年2月",  // 坐标标题
                        "销售量",                  // 坐标标题
                        dataset,                  // 定义绘制数据
                        PlotOrientation.VERTICAL, // 直方图的方向
                        true,                     // 定义图表是否包含图例
                        true,                     // 定义图表是否包含提示
                        false                     // 定义图表是否包含URL
                );
        return chart;
    }

    public static void main(String[] args) {


        // 创建一个 500X375 的图像
        int width = 500, height = 375;

        ScatterPlotDemo1 aaaa=    new ScatterPlotDemo1();
        CategoryDataset dataset = aaaa.createDataset();
        JFreeChart chart = aaaa.createChart(dataset);

// 开始自定义图表绘制的相关属性

        // 设置图表的背景颜色
        chart.setBackgroundPaint(new Color(205, 241, 197));

        // 自定义图表的标题的字体和颜色
        TextTitle title = chart.getTitle();
        title.setFont(new Font("黑体", Font.BOLD, 25));

        // 获得图表对象的引用,用于设置更多的自定义绘制属性
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        GradientPaint bg = new GradientPaint(0, 50, new Color(248, 253, 255),
                0, 250, new Color(205, 237, 252));
        plot.setBackgroundPaint(bg);
        plot.setDomainGridlinePaint(Color.BLACK);
        plot.setDomainGridlinesVisible(true);
        plot.setRangeGridlinePaint(Color.RED);

        // 设置横轴标题的字体
        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setLabelFont(new Font("黑体", Font.BOLD, 15));

        // 设置纵轴标题文字的字体及其旋转方向
        ValueAxis rangeAxis = plot.getRangeAxis();
        rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 15));
        rangeAxis.setLabelAngle(Math.PI / 2);



        // 自定义图例的显示风格
//        LegendTitle legend = (StandardLegend) chart.getLegend();
//        legend.setDisplaySeriesShapes(true);
//        legend.setShapeScaleX(1.5);
//        legend.setShapeScaleY(1.5);
//        legend.setDisplaySeriesLines(true);

        // 获取渲染对象
        LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        renderer.setBaseItemLabelsVisible(true);
        //renderer.setDrawShapes(true);
        //renderer.setShapesFilled(true);

        //设置数据显示位置
        //ItemLabelPosition p = new ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER_LEFT,TextAnchor.CENTER_LEFT, -Math.PI / 2.0 );
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        //显示折点相应数据
//        renderer.setBaseLabelGenerator(new StandardCategoryLabelGenerator());
        // 自定义线段的绘制颜色
        String bookTitle[] = {"Python", "JAVA", "C#", "Perl", "PHP"};
        Color color[] = new Color[bookTitle.length];
        color[0] = new Color(99, 99, 0);
        color[1] = new Color(255, 169, 66);
        color[2] = new Color(33, 255, 66);
        color[3] = new Color(33, 0, 255);
        color[4] = new Color(255, 0, 66);
        for (int i = 0; i < color.length; i++) {
            renderer.setSeriesPaint(i, color[i]);
        }

        // 自定义线段的绘制风格
        BasicStroke bs;
        for (int i = 0; i < bookTitle.length; i++) {
            float dashes[] = {10.0f};
            bs = new BasicStroke(2.0f, BasicStroke.CAP_ROUND,
                    BasicStroke.JOIN_ROUND, 10.f, dashes, 0.0f);
            if (i % 2 != 0)
                renderer.setSeriesStroke(i, bs);
            else
                renderer.setSeriesStroke(i, new BasicStroke(2.0f));
        }

        // 结束自定义图表绘制的相关属性

//        ChartRenderingInfo info =
//                new ChartRenderingInfo(new StandardEntityCollection());

//        // 设置图片生成格式
//        String fileName =
//                ServletUtilities.saveChartAsPNG(chart, width, height, info, session);

        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1024, 420);
        frame.setLocationRelativeTo(null);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                // 创建图形
                ChartPanel chartPanel = new ChartPanel(chart);
//                ChartPanel chartPanel = new LineChart().createChart();
                frame.getContentPane().add(chartPanel);
                frame.setVisible(true);
            }
        });
    }

    private static JFreeChart createChart(XYDataset xydataset)
    {
        JFreeChart jfreechart = ChartFactory.createScatterPlot("Scatter Plot Demo 1", "X", "Y", xydataset, PlotOrientation.VERTICAL, true, false, false);
        XYPlot xyplot = (XYPlot)jfreechart.getPlot();
        xyplot.setNoDataMessage("NO DATA");
        xyplot.setDomainZeroBaselineVisible(true);
        xyplot.setRangeZeroBaselineVisible(true);
        XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyplot.getRenderer();
        xylineandshaperenderer.setSeriesOutlinePaint(0, Color.black);
        xylineandshaperenderer.setUseOutlinePaint(true);
        NumberAxis numberaxis = (NumberAxis)xyplot.getDomainAxis();
        numberaxis.setAutoRangeIncludesZero(false);
        numberaxis.setTickMarkInsideLength(2.0F);
        numberaxis.setTickMarkOutsideLength(0.0F);
        NumberAxis numberaxis1 = (NumberAxis)xyplot.getRangeAxis();
        numberaxis1.setTickMarkInsideLength(2.0F);
        numberaxis1.setTickMarkOutsideLength(0.0F);
        return jfreechart;
    }

    public static JPanel createDemoPanel()
    {
        JFreeChart jfreechart = createChart(new SampleXYDataset2());
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        chartpanel.setVerticalAxisTrace(true);
        chartpanel.setHorizontalAxisTrace(true);
        chartpanel.setPopupMenu(null);
        chartpanel.setDomainZoomable(true);
        chartpanel.setRangeZoomable(true);
        return chartpanel;
    }
//
//    public static void main(String args[])
//    {
//        ScatterPlotDemo1 scatterplotdemo1 = new ScatterPlotDemo1("Scatter Plot Demo 1");
//        scatterplotdemo1.pack();
//        RefineryUtilities.centerFrameOnScreen(scatterplotdemo1);
//        scatterplotdemo1.setVisible(true);
//    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值