我需要在JFreechart中的Timeseries上获得A钻石形状,但我无法做到。有人可以指导一下代码应该添加到下面的代码中以实现Diamond形状点以及如何更改线条的颜色吗?
(该程序使用rs和stmt以及从DB派生的其他东西,并在其他地方定义。该程序现在正常工作,唯一的问题是它看起来非常无聊。)
TimeSeries s1 = new TimeSeries("Technology", Day.class);
TimeSeries s2 = new TimeSeries("Entertainment", Day.class);
TimeSeries s3 = new TimeSeries("Soap", Day.class);
TimeSeries s4 = new TimeSeries("Music", Day.class);
TimeSeries s5 = new TimeSeries("Native", Day.class);
TimeSeries s6 = new TimeSeries("Speciality", Day.class);
TimeSeries s7 = new TimeSeries("Science", Day.class);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date plotdate;
if (!(combo_individualid.getModel().getSize() == 0)) {
String sql = ""
+ "SELECT * "
+ "FROM `customerbasetag` "
+ "WHERE `individual_idindividual` =? ";
try {
stmt = conn.prepareStatement(sql);
stmt.setString(1, combo_individualid.getSelectedItem().toString());
rs = stmt.executeQuery();
while (rs.next()) {
try {
plotdate = sdf.parse(rs.getString("session_date"));
s1.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("technology"))));
s2.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("entertainment"))));
s3.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("soap"))));
s4.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("music"))));
s5.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("native"))));
s6.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("speciality"))));
s7.add(new Day(plotdate), new Integer(Integer.parseInt(rs.getString("science"))));
} catch (ParseException ex) {
JOptionPane.showMessageDialog(null,
"Parse Exception" + ex.getMessage());
}
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,
"Error During Session Select" + ex.getMessage());
}
/*NOTE: Chart plotting here*/
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
dataset.addSeries(s2);
dataset.addSeries(s3);
dataset.addSeries(s4);
dataset.addSeries(s5);
dataset.addSeries(s6);
dataset.addSeries(s7);
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"TS Chart", "Date", "Value", dataset, true, true, false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
ChartFrame f = new ChartFrame("Individual Choice Evaluation", chart);
f.setVisible(true);
f.setSize(800, 600);
f.setLocationRelativeTo(null);
} else {
JOptionPane.showMessageDialog(null, "Please Select an Individual");
}
我已经更新了代码,但它仍然无法正常工作,并且我不断恢复旧图表。这是代码。
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
dataset.addSeries(s2);
dataset.addSeries(s3);
dataset.addSeries(s4);
dataset.addSeries(s5);
dataset.addSeries(s6);
dataset.addSeries(s7);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Time Series Chart for Individual id: "+combo_individualid.getSelectedItem().toString() , "Date", "Value", dataset, true, true, false);
Shape theShape = ShapeUtilities.createDiamond(1);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYItemRenderer renderer = plot.getRenderer();
renderer.setSeriesShape(0, theShape);
renderer.setSeriesShape(1, theShape);
renderer.setSeriesShape(2, theShape);
renderer.setSeriesShape(3, theShape);
renderer.setSeriesShape(4, theShape);
renderer.setSeriesShape(5, theShape);
renderer.setSeriesShape(6, theShape);
ChartFrame f = new ChartFrame("Individual Choice Evaluation", chart);
f.setVisible(true);
f.setSize(800, 600);
f.setLocationRelativeTo(null);
答案 0 :(得分:9)
ShapeUtilities.createDiamond()
可以创造钻石形状;您可以按照其他Shape
的{{3}}中所示应用它。
附录:默认情况下,createTimeSeriesChart()
会创建一个XYLineAndShapeRenderer
,您可以修改,如下所示和example。
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
r.setSeriesShape(0, ShapeUtilities.createDiamond(5));
r.setSeriesShapesVisible(0, true);
here:
import java.awt.Dimension;
import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.*;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.util.ShapeUtilities;
/** @see https://stackoverflow.com/a/14822991/230513 */
public class Test {
private static final int N = 16;
private static final Random random = new Random();
private static XYDataset createDataset() {
final TimeSeries series = new TimeSeries("Data");
Day current = new Day();
for (int i = 0; i < N; i++) {
series.add(current, Math.abs(random.nextGaussian()));
current = (Day) current.next();
}
return new TimeSeriesCollection(series);
}
private static JFreeChart createChart(final XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Test", "Day", "Value", dataset, false, false, false);
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
r.setSeriesShape(0, ShapeUtilities.createDiamond(5));
r.setSeriesShapesVisible(0, true);
return chart;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
XYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(630, 480);
}
};
f.add(chartPanel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}