JFreeChart:如何在同一图表上绘制折线图和散点图 [英] JFreeChart: How to plot a line graph and a scatter on same chart

查看:163
本文介绍了JFreeChart:如何在同一图表上绘制折线图和散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两组数据

int[] x1 = {1,2,3,4,5,6,7,8,9,10};
int[] y1 = {1,2,3,5,6,8,9,10,14,11};

int[] x2 = {1,2,3,4,5,6,7,8,9,10};
int[] y2 = {0,2,3,5,0,8,9,8,14,11};

int[] z2 = {1,2,3,1,2,3,1,2,3,1};

我想将x1,y1绘制为XYLineChart,然后将x2,y2绘制为散点图,而不绘制直线.

I want to plot the x1,y1 as an XYLineChart and then plot x2,y2 as a scatter on the same plot without a line.

根据z2 (1=Color.red, 2=Color.green, 3=Color.blue)

我该怎么做?

到目前为止,我有:

JPanel panel_1 = new JPanel();
panel_1.setLayout(new BorderLayout(0, 0));
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1 = new XYSeries("series1");
for(int i=0; i<x1.length; i++){
    series1.add(x1[i],y1[i]);
}
dataset.add(series1);
JFreeChart chart = ChartFactory.createXYLineChart("Title", "x", "y", dataset, PlotOrientation.VERTICAL, false, false, false);
ChartPanel cp = new ChartPanel(chart);
panel_1.add(cp, BorderLayout.CENTER);

这将对折线图进行排序.现在,我需要为x2,y2(具有上述颜色)编写散点图,这就是我卡住的地方.

This gets the line graph sorted. I now need to code the scatter plot for x2,y2 (with colors described above) which is where im stuck.

推荐答案

createXYLineChart()方法将创建使用

The createXYLineChart() method will create a chart that uses an XYLineAndShapeRenderer. So fetch the renderer from the plot and cast it to XYLineAndShapeRenderer. Then you can call the methods setSeriesLinesVisible() and setSeriesShapesVisible() to control, for each series, whether shapes and/or lines are drawn for the data items. That way you can use a single renderer and dataset, which makes things simpler.

您需要根据另一个数据值更改颜色需要更多的工作.您应该子类化XYLineAndShapeRenderer类,并覆盖

Your requirement to change the colors depending on another data value requires a little more work. You should subclass the XYLineAndShapeRenderer class and override the getItemPaint(int, int) method. Here you can return any color you want for a data item. The default implementation looks at the series index and returns the color for the series. You need to look at the item index as well, then do a lookup in your table of z-values and decide what color to return.

这篇关于JFreeChart:如何在同一图表上绘制折线图和散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆