2014-05-14 58 views
0

有這樣的圖表: package org.jfree.chart.demo;JFreeChart創建自定義數值軸的時間

import java.awt.Color; 
import java.awt.Dimension; 
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.NumberAxis; 
import org.jfree.chart.axis.SubCategoryAxis; 
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 SubCategoryAxisDemo1 extends ApplicationFrame { 

    private static final long serialVersionUID = 1L; 

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

    private static CategoryDataset createDataset() { 
     String series1 = "S1"; 
     String series2 = "S2"; 
     String series3 = "S3"; 
     String category1 = "Category 1"; 
     String category2 = "Category 2"; 
     String category3 = "Category 3"; 
     String category4 = "Category 4"; 
     String category5 = "Category 5"; 
     DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
     dataset.addValue(1.0D, series1, category1); 
     dataset.addValue(4D, series1, category2); 
     dataset.addValue(3D, series1, category3); 
     dataset.addValue(5D, series1, category4); 
     dataset.addValue(5D, series1, category5); 
     dataset.addValue(5D, series2, category1); 
     dataset.addValue(7D, series2, category2); 
     dataset.addValue(6D, series2, category3); 
     dataset.addValue(8D, series2, category4); 
     dataset.addValue(4D, series2, category5); 
     dataset.addValue(4D, series3, category1); 
     dataset.addValue(3D, series3, category2); 
     dataset.addValue(2D, series3, category3); 
     dataset.addValue(3D, series3, category4); 
     dataset.addValue(6D, series3, category5); 
     return dataset; 
    } 

    private static JFreeChart createChart(CategoryDataset dataset) { 
     JFreeChart chart = ChartFactory.createBarChart("SubCategoryAxis Demo 1", "Category", "Value", dataset, PlotOrientation.VERTICAL, false, true, false); 
     chart.setBackgroundPaint(Color.white); 
     CategoryPlot plot = (CategoryPlot) chart.getPlot(); 
     plot.setBackgroundPaint(Color.lightGray); 
     SubCategoryAxis subCategoryAxis = new SubCategoryAxis(null); 
     subCategoryAxis.addSubCategory("S1"); 
     subCategoryAxis.addSubCategory("S2"); 
     subCategoryAxis.addSubCategory("S3"); 
     plot.setDomainAxis(subCategoryAxis); 
     plot.setDomainGridlinePaint(Color.white); 
     plot.setDomainGridlinesVisible(true); 
     plot.setRangeGridlinePaint(Color.white); 
     NumberAxis valueAxis = (NumberAxis) plot.getRangeAxis(); 
     valueAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
     BarRenderer renderer = (BarRenderer) plot.getRenderer(); 
     renderer.setDrawBarOutline(false); 
     GradientPaint gradientpaint0 = 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)); 
     renderer.setSeriesPaint(0, gradientpaint0); 
     renderer.setSeriesPaint(1, gradientpaint1); 
     renderer.setSeriesPaint(2, gradientpaint2); 
     return chart; 
    } 

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

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

在我的情況,值表示的時間段,像10鐘頭有36分鐘後(時間以毫秒爲單位,長型)。我找不到這樣的例子。在文檔中只能說這是可能的。如何爲這種情況創建軸或Tick單位?

+0

如果設置DateAxis它將從01.01.1970開始計數。但我只需要多少小時和幾分鐘的時間。 – disable1992

回答

1

它使用01.01.1970作爲它試圖在unix time顯示零。您可以使用Axis#setLowerBound()將lowe限制設置爲更接近您的開始數據,並且NumberAxis.setNumberFormatOverride()可以控制格式化

+0

如果數據集中的y值表示一個以毫秒爲單位的時間長度,那麼可以使用[HMSNumberFormat] [1]類更改格式以顯示小時,分鐘和秒。 [1]:http://www.jfree.org/jfreechart/api/javadoc/org/jfree/chart/util/HMSNumberFormat.html –