JFree中AreaChart图的分类轴,如何显示象数据轴一样的刻度线

发布网友 发布时间:2022-04-24 12:25

我来回答

4个回答

热心网友 时间:2023-10-12 19:20

import java.awt.*;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.*;
import org.jfree.util.UnitType;

public class AreaChartDemo1 extends ApplicationFrame
{

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

private static CategoryDataset createDataset()
{
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(1.0D, "Series 1", "Type 1");
defaultcategorydataset.addValue(4D, "Series 1", "Type 2");
defaultcategorydataset.addValue(3D, "Series 1", "Type 3");
defaultcategorydataset.addValue(5D, "Series 1", "Type 4");
defaultcategorydataset.addValue(5D, "Series 1", "Type 5");
defaultcategorydataset.addValue(7D, "Series 1", "Type 6");
defaultcategorydataset.addValue(7D, "Series 1", "Type 7");
defaultcategorydataset.addValue(8D, "Series 1", "Type 8");
defaultcategorydataset.addValue(5D, "Series 2", "Type 1");
defaultcategorydataset.addValue(7D, "Series 2", "Type 2");
defaultcategorydataset.addValue(6D, "Series 2", "Type 3");
defaultcategorydataset.addValue(8D, "Series 2", "Type 4");
defaultcategorydataset.addValue(4D, "Series 2", "Type 5");
defaultcategorydataset.addValue(4D, "Series 2", "Type 6");
defaultcategorydataset.addValue(2D, "Series 2", "Type 7");
defaultcategorydataset.addValue(1.0D, "Series 2", "Type 8");
defaultcategorydataset.addValue(4D, "Series 3", "Type 1");
defaultcategorydataset.addValue(3D, "Series 3", "Type 2");
defaultcategorydataset.addValue(2D, "Series 3", "Type 3");
defaultcategorydataset.addValue(3D, "Series 3", "Type 4");
defaultcategorydataset.addValue(6D, "Series 3", "Type 5");
defaultcategorydataset.addValue(3D, "Series 3", "Type 6");
defaultcategorydataset.addValue(4D, "Series 3", "Type 7");
defaultcategorydataset.addValue(3D, "Series 3", "Type 8");
return defaultcategorydataset;
}

private static JFreeChart createChart(CategoryDataset categorydataset)
{
JFreeChart jfreechart = ChartFactory.createAreaChart("Area Chart", "Category", "Value", categorydataset, PlotOrientation.VERTICAL, true, true, false);
jfreechart.setBackgroundPaint(Color.white);
TextTitle texttitle = new TextTitle("An area chart demonstration. We use this subtitle as an example of what happens" +
" when you get a really long title or subtitle."
);
texttitle.setFont(new Font("SansSerif", 0, 12));
texttitle.setPosition(RectangleEdge.TOP);
texttitle.setPadding(new RectangleInsets(UnitType.RELATIVE, 0.050000000000000003D, 0.050000000000000003D, 0.050000000000000003D, 0.050000000000000003D));
texttitle.setVerticalAlignment(VerticalAlignment.BOTTOM);
jfreechart.addSubtitle(texttitle);
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
categoryplot.setForegroundAlpha(0.5F);
categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
categoryplot.setBackgroundPaint(Color.lightGray);
categoryplot.setDomainGridlinesVisible(true);
categoryplot.setDomainGridlinePaint(Color.white);
categoryplot.setRangeGridlinesVisible(true);
categoryplot.setRangeGridlinePaint(Color.white);
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
categoryaxis.setLowerMargin(0.0D);
categoryaxis.setUpperMargin(0.0D);
categoryaxis.addCategoryLabelToolTip("Type 1", "The first type.");
categoryaxis.addCategoryLabelToolTip("Type 2", "The second type.");
categoryaxis.addCategoryLabelToolTip("Type 3", "The third type.");
NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setLabelAngle(0.0D);
return jfreechart;
}

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

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

//////////////////////////////////////////////////

import java.awt.Dimension;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class XYAreaChartDemo2 extends ApplicationFrame
{

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

private static XYDataset createDataset()
{
TimeSeries timeseries = new TimeSeries("Random 1");
double d = 0.0D;
Day day = new Day();
for(int i = 0; i < 200; i++)
{
d = (d + Math.random()) - 0.5D;
timeseries.add(day, d);
day = (Day)day.next();
}

TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries);
return timeseriescollection;
}

private static JFreeChart createChart(XYDataset xydataset)
{
JFreeChart jfreechart = ChartFactory.createXYAreaChart("XY Area Chart Demo 2", "Time", "Value", xydataset, PlotOrientation.VERTICAL, true, true, false);
XYPlot xyplot = (XYPlot)jfreechart.getPlot();
DateAxis dateaxis = new DateAxis("Time");
dateaxis.setLowerMargin(0.0D);
dateaxis.setUpperMargin(0.0D);
xyplot.setDomainAxis(dateaxis);
xyplot.setForegroundAlpha(0.5F);
XYItemRenderer xyitemrenderer = xyplot.getRenderer();
xyitemrenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0}: ({1}, {2})", new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("#,##0.00")));
return jfreechart;
}

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

public static void main(String args[])
{
XYAreaChartDemo2 xyareachartdemo2 = new XYAreaChartDemo2("XY Area Chart Demo 2");
xyareachartdemo2.pack();
RefineryUtilities.centerFrameOnScreen(xyareachartdemo2);
xyareachartdemo2.setVisible(true);
}
}

热心网友 时间:2023-10-12 19:21

不知道楼主是不是x,y都要刻度,不然可以把垂直方向改为水平方向
设置PlotOrientation.HORIZONTAL
___________________________________________________________________
恩,这个需求比较奇怪啊
你留个邮箱,我有公司做得jfreechart的详细的一个demo,保存java文件,编译运行生成pictrue,每个函数都有详细解释,发给你看看吧,看看有没有你需要的y轴为汉字的

热心网友 时间:2023-10-12 19:21

你可以试着做一个带刻度的背景图片

热心网友 时间:2023-10-12 19:22

我的邮件是xiate@foxmail.com
楼上的大哥也给我一份吧,谢谢啦

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com