通過jfreechart輸出儀錶盤圖到本地

來源:互聯網
上載者:User

項目又得用到jfreechart產生儀錶盤,趕緊到網上找了找代碼,拿來自己修改了一下,終於把圖片輸出到了本地.項目需求多變,什麼情況都有可能,呵呵.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.io.File;

import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DialShape;
import org.jfree.chart.plot.MeterInterval;
import org.jfree.chart.plot.MeterPlot;
import org.jfree.data.Range;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.data.general.ValueDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class MeterChartDemo1 extends ApplicationFrame ...{

    public MeterChartDemo1(String s) ...{
        super(s);
        JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }
    
    //建立儀錶圖
    private static JFreeChart createChart(ValueDataset valuedataset) ...{
        MeterPlot meterplot = new MeterPlot(valuedataset);
        meterplot.setRange(new Range(0.0D, 60D));
        meterplot.addInterval(new MeterInterval(" Normal ",
                new Range(0.0D, 35D), Color.lightGray, new BasicStroke(2.0F),
                new Color(0, 255, 0, 64)));
        meterplot.addInterval(new MeterInterval(" Warning ",
                new Range(35D, 50D), Color.lightGray, new BasicStroke(2.0F),
                new Color(255, 255, 0, 64)));
        meterplot.addInterval(new MeterInterval(" Critical ", new Range(50D,
                60D), Color.lightGray, new BasicStroke(2.0F), new Color(255, 0,
                0, 128)));
        meterplot.setNeedlePaint(Color.darkGray);
        meterplot.setDialBackgroundPaint(Color.white);
        meterplot.setDialOutlinePaint(Color.gray);
        meterplot.setDialShape(DialShape.CHORD);
        meterplot.setMeterAngle(260);
        meterplot.setTickLabelsVisible(true);
        meterplot.setTickLabelFont(new Font(" Dialog ", 1, 10));
        meterplot.setTickLabelPaint(Color.darkGray);
        meterplot.setTickSize(5D);
        meterplot.setTickPaint(Color.lightGray);
        meterplot.setValuePaint(Color.black);
        meterplot.setValueFont(new Font(" Dialog ", 1, 14));
        JFreeChart jfreechart = new JFreeChart(" Meter Chart 1 ",
                JFreeChart.DEFAULT_TITLE_FONT, meterplot, true);
        return jfreechart;
    }

    //調試用的圖形顯示以及調用輸出的方法
    public static JPanel createDemoPanel() ...{
        dataset = new DefaultValueDataset(23D);
        JFreeChart jfreechart = createChart(dataset);
        outputJPG(jfreechart);
        ChartPanel chartpanel = new ChartPanel(jfreechart);
        return chartpanel;
    }

    public static void main(String args[]) ...{
        MeterChartDemo1 meterchartdemo1 = new MeterChartDemo1(
                " Meter Chart Demo 1 ");
        meterchartdemo1.pack();
        RefineryUtilities.centerFrameOnScreen(meterchartdemo1);
        meterchartdemo1.setVisible(true);

    }
    //把圖片輸出到本地目錄
    public static void outputJPG(JFreeChart chart) ...{
        try ...{
            File fp = new File("c:/test.jpg");
            ChartUtilities.saveChartAsJPEG(fp,chart,300,300);
        } catch (Exception e) ...{
            
        }
        
    }

    private static DefaultValueDataset dataset;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.