【问题标题】:How to plot XYLineChart graph using two arrays: one for x and y coordinates [duplicate]如何使用两个数组绘制 XYLineChart 图:一个用于 x 和 y 坐标 [重复]
【发布时间】:2017-10-31 11:14:02
【问题描述】:

如何使用具有两个数组的 JFreeChart 绘制折线图:一个用于 x 坐标,另一个用于 y 坐标。我有两个函数,它们给了我两个数组。我想用该数组绘制折线图有什么可能的方法可以做到这一点。

XYSeries series = new XYSeries(" "); 
for(int i=((fIndex-1)*2);i<=((tIndex*2)-1);i+=2)
{
    series.add(xTValue[i],yTValue[i]);
} 
XYSeriesCollection dataset = new XYSeriesCollection(); 
dataset.addSeries(series); 
return dataset;

我可以像上面的代码那样做吗?

【问题讨论】:

  • 可能是散点图?
  • 不,我想绘制 XYLineChart 请建议我
  • 如果这不是重复的,请edit 你的问题包括一个minimal reproducible example 来显示你修改的方法。

标签: arrays jfreechart


【解决方案1】:

你可以使用

XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series = new DefaultXYSeries();
series.addSeries(" ", new double[][]{xTValue,yTValue});
dataset.addSeries(series);
JFreeChart chart = ChartFactory.createXYLineChart("My chart title", 
        "my x-axis label", "my y-axis label", dataset);

或者,如果您不特别需要 XYLineChart,那么您可以通过子类化 FastScatterPlot 来做到这一点(我知道您想要一个线图,但这是一种简单的方法 - 您覆盖 render() 方法以画线!),类似于以下内容:

public class LinePlot extends FastScatterPlot {
    private float[] x, y;

    public LinePlot(float[] x, float[] y){
        super();
        this.x=x;
        this.y=y;
    }

    @Override
    public void render(final Graphics2D g2, final Rectangle2D dataArea,
        final PlotRenderingInfo info, final CrosshairState crosshairState) {

        // Get the axes
        ValueAxis xAxis = getDomainAxis();
        ValueAxis yAxis = getRangeAxis();

        // Move to the first datapoint
        Path2D line = new Path2D.Double();
        line.moveTo(xAxis.valueToJava2D(x[0], dataArea, RectangleEdge.BOTTOM),
                yAxis.valueToJava2D(y[0], dataArea, RectangleEdge.LEFT));

        for (int i = 1; i<x.length; i++){
            //Draw line to next datapoint
            line.lineTo(aAxis.valueToJava2D(x[i], dataArea, RectangleEdge.BOTTOM),
                    yAxis.valueToJava2D(y[i], dataArea, RectangleEdge.LEFT));
        }
        g2.draw(line);
    }
}

这是一个相当简单的实现 - 例如,不检查 x 和 y 是否具有相同的点数,也没有添加颜色等(例如 g2.setPaint(myPaintColour) 或线型(例如 g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 10.0f, new float[] { 7, 3, 1, 3 }, 0.0f)) 将给出交替的 @987654328 @-type 行)

【讨论】:

  • 我想绘制 XYLineChart。但不是散点图。我在 X 和 Y 中有相同数量的点
  • 在这种情况下使用DefaultXYSeries 如下XYSeries series = new Default XYSeries(); series.addSeries(" ", new double[][]{xTValue,yTValue});
  • 我是 JFreeChart 新手,请给我推荐一个使用 DefalutXYSeries 的示例
  • 好的,在这种情况下,使用XYSeriesCollection dataset = new XYSeriesCollection(); dataset.addSeries(series); JFreeChart chart = ChartFactory.createXYLineChart("My chart title", "my x-axis label", "my y-axis label", dataset);
  • 谢谢 .. 使用 DefaultXYSeries 我们可以绘制 XYLine 图表
猜你喜欢
  • 1970-01-01
  • 2016-10-27
  • 2021-03-01
  • 2020-09-23
  • 2020-03-20
  • 2020-07-25
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
相关资源
最近更新 更多