JFreeChart x y 축에 중국어 난자 발생

11355 단어 UI.netswingjfreechart
원본 코드:
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);
  }
}
 

좋은 웹페이지 즐겨찾기