横坐标值该如何设置出来,现在我做出来了是显示了个字段的了,就五个格想要显示时间的横坐标轴,求助给一段设置出时间横坐标轴代码,谢谢

解决方案 »

  1.   

    用JFreeChart生成折线图。,X轴是时间轴 
    在下是我的一些代码设置
    package com.jznhweb.common;import java.awt.Color;
    import java.awt.Font;import org.jfree.chart.ChartFactory;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.CategoryAxis;
    import org.jfree.chart.axis.CategoryLabelPositions;
    import org.jfree.chart.axis.DateAxis;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.axis.NumberTickUnit;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.renderer.category.LineAndShapeRenderer;
    import org.jfree.chart.title.LegendTitle;
    import org.jfree.chart.title.TextTitle;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.general.DatasetUtilities;public class JfreeChatTool {
    public static JFreeChart createBarChart() {
    JFreeChart chart = null;
    try {
      String title = "温度对应折线图"; 
            // 获得数据集 
            CategoryDataset dataset =  getDataSet2();
            chart = ChartFactory.createLineChart(title, // 图表标题 
                    "温度类型",                    // 目录轴的显示标签 
                    "温度",                    // 数值轴的显示标签 
                    dataset,                   // 数据集 
                    PlotOrientation.VERTICAL,  // 图表方向:水平、垂直 
                    true,                      // 是否显示图例 
                    true,                      // 是否生成工具(提示) 
                    false                      // 是否生成URL链接 
                    ); 
            chart.setTextAntiAlias(false); 
            // 设置背景色 
            chart.setBackgroundPaint(Color.BLUE); 
            // 设置图标题的字体 
            Font font = new Font("宋体", Font.BOLD, 20); 
            
            TextTitle textTitle = new TextTitle(title); 
            
            textTitle.setFont(font); 
            
            chart.setTitle(textTitle); 
            // 设置X轴Y轴的字体 
            Font labelFont = new Font("宋体", Font.BOLD, 16); 
            chart.setBackgroundPaint(Color.WHITE); 
            // 设置图例字体 
            LegendTitle legend = chart.getLegend(0); 
            legend.setItemFont(new Font("宋体", Font.TRUETYPE_FONT, 14)); 
            // 获得plot 
            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.setd----------------------------------------------------------
            // 设置轴和面板之间的距离 
            CategoryAxis domainAxis = categoryplot.getDomainAxis(); 
            // 设置横轴标签标题字体 
            domainAxis.setLabelFont(labelFont); 
            // 设置横轴数值标签字体 
            domainAxis.setTickLabelFont(new Font("宋体", Font.TRUETYPE_FONT, 14)); 
            // 横轴上的 
            domainAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD); 
            // 设置距离图片左端距离 
            domainAxis.setLowerMargin(0.0);
            // 设置距离图片右端距离 
            domainAxis.setUpperMargin(0.0);      
            NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
            numberaxis.setTickLabelPaint(Color.blue);                  numberaxis.setVisible(true);
            // 设置纵轴显示标签的字体 
            numberaxis.setLabelFont(labelFont); 
            numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
            numberaxis.setAutoRangeIncludesZero(true); 
            //设置纵坐标值的间距为10
            numberaxis.setTickUnit(new NumberTickUnit(10));
            //纵坐标值只能是0-100之间的值
            numberaxis.setRangeWithMargins(0, 100);          // DateAxis dateAxis=(DateAxis) categoryplot.getRangeAxis();         // 获得renderer 
            LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot 
                    .getRenderer(); 
            // series 点(即数据点)可见 
            lineandshaperenderer.setBaseShapesVisible(true); 
            // series 点(即数据点)间有连线可见 
            lineandshaperenderer.setBaseLinesVisible(true); 
           
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return chart;
    }
    /**
     * 创建数据集合
     * 
     * @return
     */
    public static CategoryDataset getDataSet2(){
      double[][] data = new double[][] { { 10, 14, 34, 78, 21 }, 
                    { 2, 13, 45, 68, 72 }, { 45, 11, 34, 33, 67 } }; 
            String[] rowKeys = { "室温", "进水", "回水" }; 
            String[] columnKeys = { "室温", "进水", "回水", "南京", "深圳","ddd"}; 
            CategoryDataset dataset = DatasetUtilities.createCategoryDataset( 
                    rowKeys, columnKeys, data);  return dataset;
    }
    }
      
      

  2.   

    补充下啊,如果要在数据库中取得的一些数据添加呢??数据库也有时间字段,并且我想添加到横坐标上。
    我上面的那个例 子,我试过了,
    String[] columnKeys = { "室温", "进水", "回水", "南京", "深圳","ddd"};  
    这个地方给的长过过长的时候,就会报错。别个数据库中取出来的数据是一个  例如:List<User>  User中有 ID 温度 类型 时间(dateTime)怎么放在 JFreeChart 中让显示出来呢???
    有大神在吗?