JFreeChart 绘制折线图,如何在折线上显示坐标点数据

小边 发布于 2015/01/24 19:13
阅读 5K+
收藏 0
package com.code.jtlAnalyzeExcel;

import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;

import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;


public class LineCharts extends ApplicationFrame {
    private static final long serialVersionUID = 1L;

    public LineCharts(String s , ExcelObject excel) {
       super(s);
       setContentPane(createDemoLine(excel));
    }

    

    // 生成显示图表的面板
    public static JPanel createDemoLine(ExcelObject excel) {
       JFreeChart jfreechart = createChart(createDataset(excel));
       return new ChartPanel(jfreechart);
    }

    // 生成图表主对象JFreeChart
    public static JFreeChart createChart(DefaultCategoryDataset linedataset) {
       // 定义图表对象
       JFreeChart chart = ChartFactory.createLineChart("Load Test", //折线图名称
         "Thread", // 横坐标名称
         "Value", // 纵坐标名称
         linedataset, // 数据
         PlotOrientation.VERTICAL, // 水平显示图像
         true, // include legend
         false, // tooltips
         false // urls
         );
        // chart.setBackgroundPaint(Color.red);
        
       CategoryPlot plot = chart.getCategoryPlot();
      // plot.setDomainGridlinePaint(Color.red);
       plot.setDomainGridlinesVisible(true);
       // 5,设置水平网格线颜色
      // plot.setRangeGridlinePaint(Color.blue);
       // 6,设置是否显示水平网格线
       plot.setRangeGridlinesVisible(true);
       plot.setRangeGridlinesVisible(true); //是否显示格子线
       //plot.setBackgroundAlpha(f); //设置背景透明度
      
       NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
       
       rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
       rangeAxis.setAutoRangeIncludesZero(true);
       rangeAxis.setUpperMargin(0.20);
       rangeAxis.setLabelAngle(Math.PI / 2.0);
       rangeAxis.setAutoRange(false);
       FileOutputStream fos_jpg=null;
       try{
        fos_jpg=new FileOutputStream("D:\\ok_bing.jpg");
        /*
         * 第二个参数如果为100,会报异常:
         * java.lang.IllegalArgumentException: The 'quality' must be in the range 0.0f to 1.0f
         * 限制quality必须小于等于1,把100改成 0.1f。
         */
       // ChartUtilities.writeChartAsJPEG(fos_jpg, 0.99f, chart, 600, 300, null);
        ChartUtilities.writeChartAsJPEG(fos_jpg, chart, 900, 400);
        
       }catch(Exception e){
        System.out.println("[e]"+e);
       }finally{
        try{
         fos_jpg.close();
        }catch(Exception e){
         
        }
       }
       return chart;
    }

    // 生成数据
    public static DefaultCategoryDataset createDataset(ExcelObject excel) {
       DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
       for (int i=0; i <excel.getThreadNum(); i++) {
           linedataset.addValue(excel.getThroughput()[i], "throughput", excel.getThread()[i]);
           linedataset.addValue(excel.getAverage()[i], "average", excel.getThread()[i]);
           linedataset.addValue(excel.getError()[i], "error", excel.getThread()[i]);
       }

       return linedataset;
    }

}

以下是问题补充:

@小边:如何设置才能在折线上显示x抽对应的Y抽数据 (2015/01/24 19:28)
加载中
0
小边
小边
没人能帮忙解决下嘛?
0
扁-哥
扁-哥

引用来自“小边”的评论

没人能帮忙解决下嘛?

pls see this:http://my.oschina.net/abian/blog/278448

0
ZhengFC
ZhengFC
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot
.getRenderer();
renderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
返回顶部
顶部