`

JFreeChart x y轴 出现中文 乱码

阅读更多

原始代码:

package net.java2000.jfreechat.demo.bar;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
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.labels.StandardCategorySeriesLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class BarChartDemo1 extends ApplicationFrame {

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

  private static CategoryDataset createDataset() {
    String s = "每日PV";
    String s1 = "每日IP数";
    String s2 = "注册用户数";
    String s3 = "A网站";
    String s4 = "B网站";
    String s5 = "C网站";
    String s6 = "D网站";
    String s7 = "E网站";
    DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
    defaultcategorydataset.addValue(1.0D, s, s3);
    defaultcategorydataset.addValue(4D, s, s4);
    defaultcategorydataset.addValue(3D, s, s5);
    defaultcategorydataset.addValue(5D, s, s6);
    defaultcategorydataset.addValue(5D, s, s7);
    defaultcategorydataset.addValue(5D, s1, s3);
    defaultcategorydataset.addValue(7D, s1, s4);
    defaultcategorydataset.addValue(6D, s1, s5);
    defaultcategorydataset.addValue(8D, s1, s6);
    defaultcategorydataset.addValue(4D, s1, s7);
    defaultcategorydataset.addValue(4D, s2, s3);
    defaultcategorydataset.addValue(3D, s2, s4);
    defaultcategorydataset.addValue(2D, s2, s5);
    defaultcategorydataset.addValue(3D, s2, s6);
    defaultcategorydataset.addValue(6D, s2, s7);
    return defaultcategorydataset;
  }

  private static JFreeChart createChart(CategoryDataset categorydataset) {
    JFreeChart jfreechart = ChartFactory.createBarChart("Bar Chart Demo 1", "网站", "数值",
        categorydataset, PlotOrientation.VERTICAL, true, true, false);
    CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
    categoryplot.setDomainGridlinesVisible(true);
    categoryplot.setRangeCrosshairVisible(true);
    categoryplot.setRangeCrosshairPaint(Color.blue);
    NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
    numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
    barrenderer.setBaseItemLabelFont(new Font("宋体", Font.PLAIN, 12));
    barrenderer.setSeriesItemLabelFont(1, new Font("宋体", Font.PLAIN, 12));

    
    barrenderer.setDrawBarOutline(false);
    GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, new Color(
        0, 0, 64));
    GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F,
        new Color(0, 64, 0));
    GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(
        64, 0, 0));
    barrenderer.setSeriesPaint(0, gradientpaint);
    barrenderer.setSeriesPaint(1, gradientpaint1);
    barrenderer.setSeriesPaint(2, gradientpaint2);
    barrenderer.setLegendItemToolTipGenerator(new StandardCategorySeriesLabelGenerator(
        "Tooltip: {0}"));
    CategoryAxis categoryaxis = categoryplot.getDomainAxis();
    categoryaxis.setCategoryLabelPositions(CategoryLabelPositions
        .createUpRotationLabelPositions(0.52359877559829882D));
    return jfreechart;
  }

  public static JPanel createDemoPanel() {
    JFreeChart jfreechart = createChart(createDataset());
    return new ChartPanel(jfreechart);
  }

  public static void main(String args[]) {
    BarChartDemo1 barchartdemo1 = new BarChartDemo1("黑体JFreeChart: BarChartDemo1.java");
    barchartdemo1.pack();
    RefineryUtilities.centerFrameOnScreen(barchartdemo1);
    barchartdemo1.setVisible(true);
  }
}

 增加如下的代码,解决汉字乱码问题

    CategoryAxis domainAxis = categoryplot.getDomainAxis();

    /*------设置X轴坐标上的文字-----------*/
    domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));

    /*------设置X轴的标题文字------------*/
    domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));

    /*------设置Y轴坐标上的文字-----------*/
    numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
    
    /*------设置Y轴的标题文字------------*/
    numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));

    /*------这句代码解决了底部汉字乱码的问题-----------*/
    jfreechart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));

 完整代码

 

package net.java2000.jfreechat.demo.bar;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
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.labels.StandardCategorySeriesLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class BarChartDemo1 extends ApplicationFrame {

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

  private static CategoryDataset createDataset() {
    String s = "每日PV";
    String s1 = "每日IP数";
    String s2 = "注册用户数";
    String s3 = "A网站";
    String s4 = "B网站";
    String s5 = "C网站";
    String s6 = "D网站";
    String s7 = "E网站";
    DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
    defaultcategorydataset.addValue(1.0D, s, s3);
    defaultcategorydataset.addValue(4D, s, s4);
    defaultcategorydataset.addValue(3D, s, s5);
    defaultcategorydataset.addValue(5D, s, s6);
    defaultcategorydataset.addValue(5D, s, s7);
    defaultcategorydataset.addValue(5D, s1, s3);
    defaultcategorydataset.addValue(7D, s1, s4);
    defaultcategorydataset.addValue(6D, s1, s5);
    defaultcategorydataset.addValue(8D, s1, s6);
    defaultcategorydataset.addValue(4D, s1, s7);
    defaultcategorydataset.addValue(4D, s2, s3);
    defaultcategorydataset.addValue(3D, s2, s4);
    defaultcategorydataset.addValue(2D, s2, s5);
    defaultcategorydataset.addValue(3D, s2, s6);
    defaultcategorydataset.addValue(6D, s2, s7);
    return defaultcategorydataset;
  }

  private static JFreeChart createChart(CategoryDataset categorydataset) {
    JFreeChart jfreechart = ChartFactory.createBarChart("Bar Chart Demo 1", "网站", "数值",
        categorydataset, PlotOrientation.VERTICAL, true, true, false);
    CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
    categoryplot.setDomainGridlinesVisible(true);
    categoryplot.setRangeCrosshairVisible(true);
    categoryplot.setRangeCrosshairPaint(Color.blue);
    NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
    numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer();
    barrenderer.setBaseItemLabelFont(new Font("宋体", Font.PLAIN, 12));
    barrenderer.setSeriesItemLabelFont(1, new Font("宋体", Font.PLAIN, 12));

    CategoryAxis domainAxis = categoryplot.getDomainAxis();

    /*------设置X轴坐标上的文字-----------*/
    domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));

    /*------设置X轴的标题文字------------*/
    domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));

    /*------设置Y轴坐标上的文字-----------*/
    numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));

    /*------设置Y轴的标题文字------------*/
    numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));

    /*------这句代码解决了底部汉字乱码的问题-----------*/
    jfreechart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
    
    barrenderer.setDrawBarOutline(false);
    GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, new Color(
        0, 0, 64));
    GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F,
        new Color(0, 64, 0));
    GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(
        64, 0, 0));
    barrenderer.setSeriesPaint(0, gradientpaint);
    barrenderer.setSeriesPaint(1, gradientpaint1);
    barrenderer.setSeriesPaint(2, gradientpaint2);
    barrenderer.setLegendItemToolTipGenerator(new StandardCategorySeriesLabelGenerator(
        "Tooltip: {0}"));
    CategoryAxis categoryaxis = categoryplot.getDomainAxis();
    categoryaxis.setCategoryLabelPositions(CategoryLabelPositions
        .createUpRotationLabelPositions(0.52359877559829882D));
    return jfreechart;
  }

  public static JPanel createDemoPanel() {
    JFreeChart jfreechart = createChart(createDataset());
    return new ChartPanel(jfreechart);
  }

  public static void main(String args[]) {
    BarChartDemo1 barchartdemo1 = new BarChartDemo1("黑体JFreeChart: BarChartDemo1.java");
    barchartdemo1.pack();
    RefineryUtilities.centerFrameOnScreen(barchartdemo1);
    barchartdemo1.setVisible(true);
  }
}
 

 

分享到:
评论

相关推荐

    jfreechart乱码问题

    这个问题主要是由于jFreeChart默认使用的字体不支持中文字符,导致在图表中显示的中文无法正常识别,进而出现乱码现象。本文将详细介绍jFreeChart中解决中文乱码问题的方法。 #### 二、jFreeChart概述 jFreeChart...

    解决jfreechart中文乱码

    对于X轴和Y轴的标签,可以通过以下方式设置: ```java CategoryAxis categoryAxis = plot.getDomainAxis(); categoryAxis.setTickLabelFont(new Font("Arial Unicode MS", Font.PLAIN, 12)); NumberAxis rangeAxis...

    JFreeChart中文乱码问题解决

    ### JFreeChart中文乱码问题解决 在使用Java进行数据可视化的过程中,经常需要用到JFreeChart这一强大的图表绘制库。然而,在处理中文字符时,开发者往往会遇到一个常见的问题——中文乱码。本文将深入探讨如何解决...

    zysong.ttf jfreechart 解决中文乱码 问题

    JFreeChart chart = ChartFactory.createBarChart("我的中文图表", "X轴", "Y轴", dataset); chart.getTitle().setFont(zysongFont); ``` 4. **输出图表**:最后,使用`ChartUtilities.writeChartAsPNG()`或`...

    Jfreechart

    3. **图的底部说明**:如图的X轴和Y轴标签、图例、工具提示等可能出现乱码。 **二、解决方案** 解决 JFreeChart 乱码的关键在于正确设置字体。以下是一些关键步骤: 1. 首先,定义一个合适的字体,例如 `Font ...

    jfreechart中文版

    "Y轴标签", // Y轴标签 dataset); // 数据集 // 设置图表属性 ChartUtilities.writeChartAsPNG(new File("chart.png"), chart, 500, 300); ``` 以上代码演示了如何使用JFreeChart创建一个简单的柱状图,并将其...

    JFreeChart乱码解决.doc

    5. **处理轴标签**:除了标题,还需要确保X轴和Y轴的标签也使用了正确的字体。这可以通过获取并设置轴的`TickLabelFont`属性实现。 6. **使用ServletUtilities**:如果你是在服务器环境中生成图表并将其嵌入到网页...

    JFreeChart

    "值", // Y轴标签 dataset // 数据集 ); ``` 3. **图表定制**:你可以通过设置`ChartTheme`来改变图表的默认样式,或者直接修改`Chart`、`Plot`、`Axis`等组件的属性来实现个性化定制。 ### **JFreeChart中文...

    JFreeChart Demo

    在这个过程中,可以设置X轴和Y轴的标签,以及是否启用网格线。 4. **设置绘图区**:在`CategoryPlot`中,我们可以定制条形图的外观,如颜色、宽度、间距等。还可以设置`Renderer`,以控制每个数据系列的绘制方式。 ...

    JFreeChart 各种图形的 web工程

    通过`XYPlot`类,我们可以配置X轴和Y轴的数据源,然后使用`LineAndShapeRenderer`来控制折线的样式和标记。 **2. 饼形图** 饼形图用于展示各部分占总体的比例关系。JFreeChart的`PieChart`类支持创建饼形图,可以...

    利用JFreechart 做折线图

    "数值", // Y轴标签 createDataset(), // 数据集 PlotOrientation.VERTICAL, // 图表方向 true, // 显示图例 true, // 生成工具提示 false); // 生成URL链接 ``` #### 四、设置中文显示及字体 为了解决中文...

    JFC/JFreechart

    - `XXXAxis` 类处理图表的X轴和Y轴。`CategoryAxis` 和 `ValueAxis` 是主要的轴类,它们可以进一步定制,如设置刻度、标签、范围等。 - `XXXRender` 类负责图表元素的渲染方式。比如,`LineAndShapeRenderer` 控制...

Global site tag (gtag.js) - Google Analytics