如何使用两个数组绘制 XYLineChart 图:一个用于 x 和 y 坐标
How to plot XYLineChart graph using two arrays: one for x and y coordinates
如何使用带有两个数组的 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;
我可以像上面的代码那样做吗?
您可以使用
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))
会给出交替的 -•-•
类型的行)
如何使用带有两个数组的 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;
我可以像上面的代码那样做吗?
您可以使用
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))
会给出交替的 -•-•
类型的行)