公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了如何使用JFree Chart创建仪表图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用饼图和半圆环图的组合来创建仪表图。预期的图像已附上。
有没有人可以帮我修改附件中的代码以获得预期的结果?附加样例代码,改编自here:

import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.RingPlot;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

public class RingChartTest {

    private static final String INVISIBLE = "have_a_look_on_me_if_you_can_xD";
    private static java.awt.Color whiteColorAlphaChannel = new java.awt.Color(255, 255, 255, 0);
    
    private static PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("Safari", 40);
        dataset.setValue("Safari1", 50);
        dataset.setValue("Safari2", 90);
        dataset.setValue(INVISIBLE, 180);
        return dataset;
    }

    private static JFreeChart createChart(PieDataset dataset) {
        JFreeChart chart = ChartFactory.createRingChart("Overall Performance", dataset, false, false, false);
        
        RingPlot plot = (RingPlot) chart.getPlot();
        
        plot.setStartAngle(180);
        plot.setCircular(true);
        plot.setSimpleLabels(true);
        plot.setSectionDepth(0.2);
        plot.setBackgroundPaint(Color.WHITE);
        plot.setSeparatorsVisible(false);
        Color invisible = new Color(0xffffff, true);
        plot.setSectionPaint(INVISIBLE, whiteColorAlphaChannel); // 180° alpha invisible
        plot.setSectionOutlinePaint(INVISIBLE, whiteColorAlphaChannel); // 180° alpha invisible
        plot.setShadowPaint(null);
        plot.setLabelGenerator(null);
         plot.setSectionOutlinesVisible(false);
        return chart;
    }

    public JPanel createDemoPanel() {
        JFreeChart jfreechart = createChart(createDataset());
        ChartPanel chartPanel = new ChartPanel(jfreechart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(500, 400);
            }
        };
        chartPanel.setLayout(new OverlayLayout(chartPanel));
        JLabel label = new JLabel("BrowserShare");
        label.setFont(label.getFont().deriveFont(48.0f));
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setVerticalAlignment(JLabel.CENTER);
        label.setAlignmentX(0.5f);
        label.setAlignmentY(0.75f);
        label.setOpaque(false);
        label.setBackground(Color.LIGHT_GRAY);
        chartPanel.add(label);
        return chartPanel;
    }

    public static void main(String args[]) {
        PieDataset pieDataSet = createDataset();
        JFreeChart jFreeChart = createChart(pieDataSet);
        String filename1 = "C://Users//136965//Desktop//gauge_nut.jpg";

        try {
            ChartUtils.saveChartAsJPEG(new File(filename1), jFreeChart, 500, 400);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

由上述代码创建的图形为

我尝试使用JFreeChart库创建的图表如下:

推荐答案

还可以考虑使用org.jfree.chart.plot.dial。下面的示例使用一个DialPlot和一个StandardDialScale。StandardDialRange的三个实例提供颜色。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.dial.ArcDialFrame;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.StandardDialRange;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.data.general.DefaultValueDataset;

/**
 * @see https://stackoverflow.com/a/70648615/230513
 * @see https://stackoverflow.com/a/10353270/230513
 */
public class DialTest {

    private static final Color LT_GRAY = new Color(0xe0e0e0);

    private void display() {
        DefaultValueDataset data = new DefaultValueDataset(70);
        DialPlot plot = new DialPlot(data);
        plot.setView(0, -0.25, 1, 1);
        ArcDialFrame arcDialFrame = new ArcDialFrame();
        arcDialFrame.setInnerRadius(0.42);
        arcDialFrame.setOuterRadius(0.95);
        arcDialFrame.setForegroundPaint(Color.darkGray);
        plot.setDialFrame(arcDialFrame);
        StandardDialScale scale = new StandardDialScale(0, 100, 180, -180, 10, 0);
        scale.setTickRadius(0.95);
        scale.setTickLabelOffset(0.15);
        scale.setMajorTickIncrement(10);
        plot.addScale(0, scale);
        DialPointer.Pin pin = new DialPointer.Pin();
        pin.setPaint(Color.black);
        pin.setRadius(0.8);
        plot.addLayer(pin);
        plot.addLayer(new StandardDialRange(0, 40, Color.red));
        plot.addLayer(new StandardDialRange(40, 60, Color.yellow));
        plot.addLayer(new StandardDialRange(60, 100, Color.green));

        JFreeChart chart = new JFreeChart("Overall Performance", plot);
        chart.setBackgroundPaint(LT_GRAY);

        JFrame f = new JFrame("Meter Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 300);
            }
        });
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new DialTest()::display);
    }
}

这篇关于如何使用JFree Chart创建仪表图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:Chart JFree 仪表 创建 如何使用
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定