首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

jfreechart输出图片时有乱码的有关问题

2012-04-11 
jfreechart输出图片时有乱码的问题Java codepackage com.walkman.jfreechartimport java.awt.Colorimpor

jfreechart输出图片时有乱码的问题

Java code
package com.walkman.jfreechart;import java.awt.Color;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.labels.StandardPieSectionLabelGenerator;import org.jfree.chart.plot.PiePlot3D;import org.jfree.data.general.DefaultPieDataset;public class PieChartPicture {    public static void main(String[] args)    {        // 准备数据        DefaultPieDataset dataset = new DefaultPieDataset();        dataset.setValue("关键", 2);        dataset.setValue("严重", 10);        dataset.setValue("平均", 50);        dataset.setValue("较轻", 20);        // 使用ChartFactory创建图表对象        JFreeChart chart = ChartFactory.createPieChart3D("某项目某月份缺陷严重程度分析图2",                dataset, true, true, false);        // 设置图表样式        PiePlot3D plot = (PiePlot3D) chart.getPlot();        // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(                "{0}={1}({2})", NumberFormat.getNumberInstance(),                new DecimalFormat("0.00%")));        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(                "{0}={1}({2})"));        // 设置背景色为白色        chart.setBackgroundPaint(Color.white);        // 指定图片的透明度(0.0-1.0)        plot.setForegroundAlpha(1.0f);        // 指定显示的饼图上圆形(false)还椭圆形(true)        plot.setCircular(false);        // 将生成的图表文件输出到磁盘。        FileOutputStream fos_jpg = null;        try {            fos_jpg = new FileOutputStream("D:\\某项目某月份缺陷严重程度分析图.jpg");            ChartUtilities.writeChartAsJPEG(fos_jpg, 1.0f, chart, 400, 250,                    null);            fos_jpg.close();        } catch (Exception e) {            e.printStackTrace();        }    }}


乱码问题该如何解决?

[解决办法]
Java code
import java.awt.Color;import java.awt.Font;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.StandardChartTheme;import org.jfree.chart.labels.StandardPieSectionLabelGenerator;import org.jfree.chart.plot.PiePlot3D;import org.jfree.data.general.DefaultPieDataset;public class PieChartPicture {    public static void main(String[] args)    {         Font font = new Font("宋体",Font.PLAIN,12) ;        // 准备数据        DefaultPieDataset dataset = new DefaultPieDataset();        dataset.setValue("关键", 2);        dataset.setValue("严重", 10);        dataset.setValue("平均", 50);        dataset.setValue("较轻", 20);        // 使用ChartFactory创建图表对象                //先给个主题,将字体设置好        StandardChartTheme s = new StandardChartTheme("a");        s.setRegularFont(font);        ChartFactory.setChartTheme(s);        JFreeChart chart = ChartFactory.createPieChart3D("某项目某月份缺陷严重程度分析图2",dataset, false, false, false);        //设置标题的字体        chart.getTitle().setFont(font);        // 设置图表样式        PiePlot3D plot = (PiePlot3D) chart.getPlot();        // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位        plot.setLabelGenerator(new StandardPieSectionLabelGenerator(                "{0}={1}({2})", NumberFormat.getNumberInstance(),                new DecimalFormat("0.00%")));        plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(                "{0}={1}({2})"));        // 设置背景色为白色        chart.setBackgroundPaint(Color.white);        // 指定图片的透明度(0.0-1.0)        plot.setForegroundAlpha(1.0f);        // 指定显示的饼图上圆形(false)还椭圆形(true)        plot.setCircular(false);        // 将生成的图表文件输出到磁盘。        FileOutputStream fos_jpg = null;        try {            fos_jpg = new FileOutputStream("D:\\a.jpg");            ChartUtilities.writeChartAsJPEG(fos_jpg, 1.0f, chart, 400, 250,                    null);            fos_jpg.close();        } catch (Exception e) {            e.printStackTrace();        }    }} 

热点排行