24
回答
jfreechart曲线图
开发十年,就只剩下这套Java开发体系了   

jfreechart曲线图怎么在在数据点下面显示数据?

举报
南希
发帖于8年前 24回/6K+阅
共有24个答案 最后回答: 5年前

我做过饼图,柱形图,并且有所有的jfreechart实例的源代码。可以PM我

--- 共有 1 条评论 ---
唯一大家别加我了,谢谢,忙不过来,抱歉啊 7年前 回复

package demo;

import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.Rectangle2D.Double;
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.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class LineChartDemo5 extends ApplicationFrame
{
  public LineChartDemo5(String paramString)
  {
    super(paramString);
    JPanel localJPanel = createDemoPanel();
    localJPanel.setPreferredSize(new Dimension(500, 270));
    setContentPane(localJPanel);
  }

  private static CategoryDataset createDataset()
  {
    String str1 = "First";
    String str2 = "Second";
    String str3 = "Third";
    String str4 = "Type 1";
    String str5 = "Type 2";
    String str6 = "Type 3";
    String str7 = "Type 4";
    String str8 = "Type 5";
    String str9 = "Type 6";
    String str10 = "Type 7";
    String str11 = "Type 8";
    DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
    localDefaultCategoryDataset.addValue(1.0D, str1, str4);
    localDefaultCategoryDataset.addValue(4.0D, str1, str5);
    localDefaultCategoryDataset.addValue(3.0D, str1, str6);
    localDefaultCategoryDataset.addValue(5.0D, str1, str7);
    localDefaultCategoryDataset.addValue(5.0D, str1, str8);
    localDefaultCategoryDataset.addValue(7.0D, str1, str9);
    localDefaultCategoryDataset.addValue(7.0D, str1, str10);
    localDefaultCategoryDataset.addValue(8.0D, str1, str11);
    localDefaultCategoryDataset.addValue(5.0D, str2, str4);
    localDefaultCategoryDataset.addValue(7.0D, str2, str5);
    localDefaultCategoryDataset.addValue(6.0D, str2, str6);
    localDefaultCategoryDataset.addValue(8.0D, str2, str7);
    localDefaultCategoryDataset.addValue(4.0D, str2, str8);
    localDefaultCategoryDataset.addValue(4.0D, str2, str9);
    localDefaultCategoryDataset.addValue(2.0D, str2, str10);
    localDefaultCategoryDataset.addValue(1.0D, str2, str11);
    localDefaultCategoryDataset.addValue(4.0D, str3, str4);
    localDefaultCategoryDataset.addValue(3.0D, str3, str5);
    localDefaultCategoryDataset.addValue(2.0D, str3, str6);
    localDefaultCategoryDataset.addValue(3.0D, str3, str7);
    localDefaultCategoryDataset.addValue(6.0D, str3, str8);
    localDefaultCategoryDataset.addValue(3.0D, str3, str9);
    localDefaultCategoryDataset.addValue(4.0D, str3, str10);
    localDefaultCategoryDataset.addValue(3.0D, str3, str11);
    return localDefaultCategoryDataset;
  }

  private static JFreeChart createChart(CategoryDataset paramCategoryDataset)
  {
    JFreeChart localJFreeChart = ChartFactory.createLineChart("Line Chart Demo 5", "Type", "Value", paramCategoryDataset, PlotOrientation.VERTICAL, true, true, false);
    Shape[] arrayOfShape = new Shape[3];
    int[] arrayOfInt1 = { -3, 3, -3 };
    int[] arrayOfInt2 = { -3, 0, 3 };
    arrayOfShape[0] = new Polygon(arrayOfInt1, arrayOfInt2, 3);
    arrayOfShape[1] = new Rectangle2D.Double(-2.0D, -3.0D, 3.0D, 6.0D);
    arrayOfInt1 = new int[] { -3, 3, 3 };
    arrayOfInt2 = new int[] { 0, -3, 3 };
    arrayOfShape[2] = new Polygon(arrayOfInt1, arrayOfInt2, 3);
    DefaultDrawingSupplier localDefaultDrawingSupplier = new DefaultDrawingSupplier(DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE, DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE, DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE, DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE, arrayOfShape);
    CategoryPlot localCategoryPlot = (CategoryPlot)localJFreeChart.getPlot();
    localCategoryPlot.setOrientation(PlotOrientation.HORIZONTAL);
    localCategoryPlot.setDrawingSupplier(localDefaultDrawingSupplier);
    localCategoryPlot.getRenderer().setSeriesStroke(0, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] { 10.0F, 6.0F }, 0.0F));
    localCategoryPlot.getRenderer().setSeriesStroke(1, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] { 6.0F, 6.0F }, 0.0F));
    localCategoryPlot.getRenderer().setSeriesStroke(2, new BasicStroke(2.0F, 1, 1, 1.0F, new float[] { 2.0F, 6.0F }, 0.0F));
    LineAndShapeRenderer localLineAndShapeRenderer = (LineAndShapeRenderer)localCategoryPlot.getRenderer();
    localLineAndShapeRenderer.setBaseShapesVisible(true);
    localLineAndShapeRenderer.setBaseItemLabelsVisible(true);
    localLineAndShapeRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    NumberAxis localNumberAxis = (NumberAxis)localCategoryPlot.getRangeAxis();
    localNumberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    localNumberAxis.setAutoRangeIncludesZero(false);
    localNumberAxis.setUpperMargin(0.12D);
    return localJFreeChart;
  }

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

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

这是1.0.13版本中唯一一个带有数据显示的折线图实例的源码

    LineAndShapeRenderer localLineAndShapeRenderer = (LineAndShapeRenderer)localCategoryPlot.getRenderer();
    localLineAndShapeRenderer.setBaseShapesVisible(true);
    localLineAndShapeRenderer.setBaseItemLabelsVisible(true);

第三句起的作用

引用来自#7楼“唯一”的帖子

    LineAndShapeRenderer localLineAndShapeRenderer = (LineAndShapeRenderer)localCategoryPlot.getRenderer();
    localLineAndShapeRenderer.setBaseShapesVisible(true);
    localLineAndShapeRenderer.setBaseItemLabelsVisible(true);

第三句起的作用

 我已经实现在数据点上显示数据值,我想问的是:数据值能不能显示在数据点下面?

如上图所示

当某个数据点显示在最高位置时,此数据点的数据值看不到,我想要达到的效果就是:能看到该数据点的数据!

引用来自#9楼“唯一”的帖子

localLineAndShapeRenderer.setItemLabelAnchorOffset(-10D);

加一句这个

加上上面那一句之后,会导致显示在靠下方的数据值看不到。如下图所示:

顶部