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

JFreeChart生成的折线图一部分汉字没识别

2013-07-04 
JFreeChart生成的折线图部分汉字没识别今天要求做一个折线图,原来没做过,在网上找了一个demo,运行结果出来

JFreeChart生成的折线图部分汉字没识别
今天要求做一个折线图,原来没做过,在网上找了一个demo,运行结果出来以后变成JFreeChart生成的折线图一部分汉字没识别
这样的了,竖坐标变成框框了,折现标示也变成框框了,我觉得有可能是编码格式不正确的问题,但是,第一次接触这个,不知道怎么改,下面是我在网上找的代码:
package a;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
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.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;

public class CreateLinechart {
    /**
     * 图片保存的根目录
     * @param filename
     * @return
     */
    public String Savepath(){
        String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        String testpath = path.substring(0,path.lastIndexOf("WEB-INF"));
        String filepath = testpath+"images/";
        System.out.println(filepath);
        return filepath;//Tomcat的中webapps目录下项目的images文件夹
    }
    
    /**
     *  柱状图,折线图 数据集 方法
     */
    public CategoryDataset getBarData(double[][] data, String[] rowKeys,     
            String[] columnKeys) {     
        return DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data);     
    
    }
    private void isChartPathExist(String chartPath) {     
        File file = new File(chartPath);     
        if (!file.exists()) {     


            file.mkdirs();     
        // log.info("CHART_PATH="+CHART_PATH+"create.");     
        }
             
    }
 
    /**
     * 折线图样式
     * @param chartTitle
     * @param x
     * @param y
     * @param xyDataset
     * @param charName
     * @return
     */
     public JFreeChart createTimeXYChar(String chartTitle, String x, String y,     
                CategoryDataset xyDataset, String charName) {    
            JFreeChart chart = ChartFactory.createLineChart(chartTitle, x, y,     
                    xyDataset, PlotOrientation.VERTICAL, true, true, false);    
            System.out.println("---------------------------------");
            System.out.println(y);
            chart.setTextAntiAlias(false);     
            chart.setBackgroundPaint(Color.RED);     
            // 设置图标题的字体重新设置title     
            Font font = new Font("宋体", Font.BOLD, 20);     
            TextTitle title = new TextTitle(chartTitle);     
            title.setFont(font);     
            chart.setTitle(title);     
            // 设置面板字体     


            Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);    
            chart.setBackgroundPaint(Color.WHITE);    
            CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();     
            // x轴 // 分类轴网格是否可见     
            categoryplot.setDomainGridlinesVisible(true);     
            // y轴 //数据轴网格是否可见     
            categoryplot.setRangeGridlinesVisible(true);    
            categoryplot.setRangeGridlinePaint(Color.WHITE);// 虚线色彩    
            categoryplot.setDomainGridlinePaint(Color.WHITE);// 虚线色彩    
            categoryplot.setBackgroundPaint(Color.lightGray);    
            // 设置轴和面板之间的距离     
            // categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));    
            CategoryAxis domainAxis = categoryplot.getDomainAxis();    
            domainAxis.setLabelFont(labelFont);// 轴标题 
            domainAxis.setTickLabelFont(labelFont);// 轴数值    
            domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD); // 横轴上的     
            // Lable     
            // 45度倾斜     
            // 设置距离图片左端距离    
            domainAxis.setLowerMargin(0.0);     
            // 设置距离图片右端距离     


            domainAxis.setUpperMargin(0.0);             
            NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();     
            numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());     
            numberaxis.setAutoRangeIncludesZero(true);    
            // 获得renderer 注意这里是下嗍造型到lineandshaperenderer!!     
            LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();     
            //XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) categoryplot  
           // .getRenderer();//改变曲线颜色
            lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见    
            lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见 
            // 显示折点数据     
           /* lineandshaperenderer.setBaseItemLabelGenerator(new     
            StandardCategoryItemLabelGenerator());     
            lineandshaperenderer.setBaseItemLabelsVisible(true);  */       
            //图片路径
            FileOutputStream fos_jpg = null;     
            try {     
                isChartPathExist(Savepath());     
                String chartName = Savepath() + charName;     


                fos_jpg = new FileOutputStream(chartName);     
        
                // 将报表保存为JPG文件     
                ChartUtilities.writeChartAsJPEG(fos_jpg, chart, 500, 510);             
                   
            } catch (Exception e) {     
                e.printStackTrace();     
                return null;     
            } finally {     
                try {     
                    fos_jpg.close();     
                    System.out.println("create time-createTimeXYChar.");     
                } catch (Exception e) {     
                    e.printStackTrace();     
                }     
            }
            return chart;    
     }  
}
求帮忙改一下啊!!! jfreechart 图片 折线图


[解决办法]
在createTimeXYChar这个方法中加上
chart.getLegend.setItemFont(labelFont);//设置你的说明区域的字体
numberaxis.setLabelFont(labelFont);//设置你的纵坐标标题字体

热点排行