3
我有一組元素,它們屬於一個系列,使用這些元素計算了質心。問題是當我用「ScatterPlot」顯示它們時,需要用「One Color」顯示「Array Elements」,並在「Different Color」中顯示這些點的質心。將不同的顏色設置爲系列中的特定行-JFreeChart
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.DefaultXYDataset;
import org.jfree.data.xy.XYDataset;
public class Scatteradd extends JFrame {
int i, x = 0, n1 = 0;
public Scatteradd(String title, final double[][] samples) {
super(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final DefaultXYDataset dataset = new DefaultXYDataset();
dataset.addSeries("Series0", createSeries(0, samples));
//dataset.addSeries("Series1", createSeries(1,trainingset3));
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart, false);
chartPanel.setPreferredSize(new Dimension(640, 480));
this.add(chartPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JButton addButton = new JButton("Add Series");
buttonPanel.add(addButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int n = dataset.getSeriesCount();
System.out.println("N-SIZE" + n);
dataset.addSeries("Series" + n, createSeries(n, samples));
System.exit(1);
}
});
JButton remButton = new JButton("Remove Series");
buttonPanel.add(remButton);
remButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int n = dataset.getSeriesCount() - 1;
dataset.removeSeries("Series" + n);
}
});
this.add(buttonPanel, BorderLayout.SOUTH);
}
/**
* Create a series
* @param samples
*
* @ return the series
*/
private double[][] createSeries(int mean, double[][] samples) {
double[][] series = new double[2][samples.length + 1];
System.out.println("SSSKSKSValue" + series.length);
double p = 0, q = 0;
for (i = 0; i < samples.length; i++) {
series[0][i] = samples[i][0];
p = p + samples[i][0];
series[1][i] = samples[i][1];
q = q + samples[i][1];
//System.out.println("Series Values"+series[0][i]+","+series[1][i]);
}
series[0][samples.length] = p/samples.length;//Centroid Calculation
series[1][samples.length] = q/samples.length;//Centroid Calculation
//Printing All Points in Series Array and the Last Row is the Centroid Values
//which I want display in different Color on Scatter Plot
for (int v = 0; v < series[0].length; v++) {
System.out.println("Series Values" + series[0][v] + "," + series[1][v]);
}
return series;
}
private JFreeChart createChart(XYDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createScatterPlot(
"Scatter Plot Demo", "X", "Y", dataset,
PlotOrientation.VERTICAL, true, true, false);
// set chart background
chart.setBackgroundPaint(Color.white);
// set a few custom plot features
XYPlot plot = (XYPlot) chart.getPlot();
Shape[] cross = DefaultDrawingSupplier.createStandardSeriesShapes();
plot.setBackgroundPaint(new Color(0xffffe0));
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.lightGray);
XYItemRenderer renderer = (XYItemRenderer) plot.getRenderer();
renderer.setSeriesShape(0, cross[0]);
plot.setRenderer(renderer);
return chart;
}
/** Main method **/
public static void main(String[] args) {
double[][] trainingset3 = {
{0.428053, 0.409742,},
{0.415487, 0.401414,},
{0.404834, 0.400493,},};
Scatteradd demo = new Scatteradd("JFreeChartDemo", trainingset3);
demo.pack();
demo.setLocationRelativeTo(null);
demo.setVisible(true);
}
}
SSSKSKSValue2 Series Values0.428053,0.409742 Series Values0.415487,0.401414 Series Values0.404834,0.400493 Series Values0.4161246666666667,0.403883 //Centroids of above 3 Rows
有沒有表現出特定的行與不同的顏色,提供了有關這將是巨大的和有益的任何例子一系列方法。 感謝
@ trashgod.I已經通過你上面提到的例子不見了,上面我的重心是成對的值有參數的行和列,但在我的例子(0.4161246,0.403883)。其中意味着排是3但列是0和1 ...我怎樣才能指定一列單列行..謝謝 – Sam
對不起,請注意更新有關渲染。我沒有關注數據集。你可以發佈[sscce](http://sscce.org/)嗎? – trashgod
@trashgod.I添加代碼請通過那..如果你可以幫我..: – Sam