我正在使用jfreechart绘制折线图,我需要从上到下绘制线条 ....含义:0,0坐标将位于左上角。 当我通过更改'PlotOrientation'使用'CategoryPlot'时,我能够做到这一点,但是左边的标签变成了重叠......但是,我更喜欢'xyplot'
更改'setInverted'或'setDomainAxisLocation'没有帮助
任何帮助将不胜感激
更新: 我正在重新发布我的代码以获得更好的清晰度(图表绘图部分)
在第一个代码中: 我得到了所需的输出 如此显示 http://i58.tinypic.com/r1fr01.jpg 只需传递'PlotOrientation.HORIZONTAL' 在这部分中,我正在使用'CategoryPlot'
import java.awt.*;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.*;
public class PCategory extends ApplicationFrame
{
public PCategory(String s)
{
super(s);
JPanel jpanel = createDemoPanel();
jpanel.setPreferredSize(new Dimension(300, 200));
setContentPane(jpanel);
}
private static CategoryDataset createDataset()
{
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(0,"Pixels", Integer.toString(0));
defaultcategorydataset.addValue(2,"Pixels", Integer.toString(1));
defaultcategorydataset.addValue(5,"Pixels", Integer.toString(2));
defaultcategorydataset.addValue(10,"Pixels", Integer.toString(3));
defaultcategorydataset.addValue(7,"Pixels", Integer.toString(4));
defaultcategorydataset.addValue(8,"Pixels", Integer.toString(5));
return defaultcategorydataset;
}
private static JFreeChart createChart(CategoryDataset categorydataset)
{
JFreeChart jfreechart = ChartFactory.createLineChart(null, null, null, categorydataset, PlotOrientation.HORIZONTAL, false, true, false);
jfreechart.addSubtitle(new TextTitle("Title"));
CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
categoryplot.setRangePannable(true);
categoryplot.setRangeGridlinesVisible(false);
ChartUtilities.applyCurrentTheme(jfreechart);
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot.getRenderer();
lineandshaperenderer.setBaseShapesVisible(true);
lineandshaperenderer.setDrawOutlines(true);
lineandshaperenderer.setUseFillPaint(true);
lineandshaperenderer.setBaseFillPaint(Color.white);
lineandshaperenderer.setSeriesStroke(0, new BasicStroke(3F));
lineandshaperenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));
lineandshaperenderer.setSeriesShape(0, new java.awt.geom.Ellipse2D.Double(-5D, -5D, 10D, 10D));
return jfreechart;
}
public static JPanel createDemoPanel()
{
JFreeChart jfreechart = createChart(createDataset());
ChartPanel chartpanel = new ChartPanel(jfreechart);
chartpanel.setMouseWheelEnabled(true);
return chartpanel;
}
public static void main(String args[])
{
PCategory PCategory = new PCategory("PCategory.java");
PCategory.pack();
RefineryUtilities.centerFrameOnScreen(PCategory);
PCategory.setVisible(true);
}
}
在第二个代码中: 即使我使用相同的数据并通过,我也得不到我需要的东西 'PlotOrientation.HORIZONTAL' 如此显示 http://i57.tinypic.com/11hq9tk.jpg 我评论了解决问题的方法
import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.*;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class PXYPlot extends ApplicationFrame {
public PXYPlot(String s) {
super(s);
JPanel jpanel = createPanel();
jpanel.setPreferredSize(new Dimension(300, 200));
setContentPane(jpanel);
}
private static XYDataset createDataset() {
XYSeries xyseries = new XYSeries("Pixels", true);
xyseries.add(0, 0);
xyseries.add(2, 1);
xyseries.add(5, 2);
xyseries.add(10, 3);
xyseries.add(7, 4);
xyseries.add(8, 5);
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
xyseriescollection.addSeries(xyseries);
return xyseriescollection;
}
private static JFreeChart createChart(XYDataset xydataset)
{
JFreeChart Mychart = ChartFactory.createXYLineChart(null, null, null, xydataset, PlotOrientation.HORIZONTAL, true, true, false);
XYPlot xyplot = (XYPlot) Mychart.getPlot();
xyplot.setDomainPannable(true);
xyplot.setRangePannable(true);
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot.getRenderer();
xylineandshaperenderer.setBaseShapesVisible(true);
xylineandshaperenderer.setBaseShapesFilled(true);
/* ----------- this part is my attempt to solve the problem -----
NumberAxis numberaxis = (NumberAxis)xyplot.getRangeAxis();
numberaxis.setInverted(true);
xyplot.setDomainAxisLocation(AxisLocation.TOP_OR_LEFT);
//------------I like XYplot because of the part -----------------
NumberAxis xAxis = new NumberAxis();
xAxis.setTickUnit(new NumberTickUnit(1));
XYPlot plot = (XYPlot) Mychart.getPlot();
plot.setDomainAxis(xAxis);
//-----------------------------
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
*/
return Mychart;
}
public static JPanel createPanel() {
JFreeChart jfreechart = createChart(createDataset());
ChartPanel chartpanel = new ChartPanel(jfreechart);
chartpanel.setMouseWheelEnabled(true);
return chartpanel;
}
public static void main(String args[]) {
PXYPlot PXYPlot = new PXYPlot("PXYPlot.java");
PXYPlot.pack();
RefineryUtilities.centerFrameOnScreen(PXYPlot);
PXYPlot.setVisible(true);
}
}
我需要'XYPlot',因为我有大量的数据,我认为'CategoryPlot'不是为它而设计的,除了我有重叠的问题。此外,为了满足我的需要,它需要覆盖次数
答案 0 :(得分:4)
首先,按如下方式创建数据集,然后不要更改它......无论您想如何呈现数据,数据都是相同的:
private XYDataset createDataset() {
XYSeries s1 = new XYSeries("S1");
s1.add(0, 0);
s1.add(1, 2);
s1.add(2, 5);
s1.add(3, 10);
s1.add(4, 7);
s1.add(5, 8);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
现在,合理标准的JFreeChart创建将如下所示:
......这是代码:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
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.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class StackOverflow25993430 extends JFrame {
public StackOverflow25993430(String title) {
super(title);
JFreeChart chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
panel.setPreferredSize(new Dimension(500, 300));
setContentPane(panel);
}
private XYDataset createDataset() {
XYSeries s1 = new XYSeries("S1");
s1.add(0, 0);
s1.add(1, 2);
s1.add(2, 5);
s1.add(3, 10);
s1.add(4, 7);
s1.add(5, 8);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
private JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createXYLineChart(
"StackOverflow25993430", "X", "Y", dataset);
chart.removeLegend();
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesShape(0, new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0));
renderer.setSeriesFillPaint(0, Color.WHITE);
renderer.setUseFillPaint(true);
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
return chart;
}
public static void main(String[] args) {
JFrame app = new StackOverflow25993430("JFreeChart");
app.pack();
app.setVisible(true);
}
}
很少需要改变以获得你想要的情节。首先,应该改变绘图方向,然后应该反转x轴,第三,你必须提供y轴应该在顶部的提示。结果如下:
...以及生成它的代码(***标记了添加的5行):
import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation; // ***
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation; // ***
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class StackOverflow25993430b extends JFrame {
public StackOverflow25993430b(String title) {
super(title);
JFreeChart chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
panel.setPreferredSize(new Dimension(500, 300));
setContentPane(panel);
}
private XYDataset createDataset() {
XYSeries s1 = new XYSeries("S1");
s1.add(0, 0);
s1.add(1, 2);
s1.add(2, 5);
s1.add(3, 10);
s1.add(4, 7);
s1.add(5, 8);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(s1);
return dataset;
}
private JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createXYLineChart(
"StackOverflow25993430b", "X", "Y", dataset);
chart.removeLegend();
XYPlot plot = (XYPlot) chart.getPlot();
plot.setOrientation(PlotOrientation.HORIZONTAL); // ***
plot.setRangeAxisLocation(AxisLocation.TOP_OR_LEFT); // ***
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesShape(0, new Ellipse2D.Double(-4.0, -4.0, 8.0, 8.0));
renderer.setSeriesFillPaint(0, Color.WHITE);
renderer.setUseFillPaint(true);
NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
xAxis.setInverted(true); // ***
return chart;
}
public static void main(String[] args) {
JFrame app = new StackOverflow25993430b("JFreeChart");
app.pack();
app.setVisible(true);
}
}