gpt4 book ai didi

java - 如何在 JFreechart 中获取点的菱形

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:57:44 25 4
gpt4 key购买 nike

我需要在 JFreechart 的时间序列上获得 A 菱形,但我做不到。有人可以指导应该在下面的代码中添加什么代码来实现菱形点以及如何更改线条的颜色吗?

(该程序使用了 rs 和 stmt 以及其他从 DB 派生并在其他地方定义的东西。该程序现在可以正常运行,唯一的问题是它看起来 super 无聊。)

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);

最佳答案

ShapeUtilities.createDiamond()可以创建菱形;您可以按此 example 所示应用它不同的 Shape

附录:默认情况下,createTimeSeriesChart() 创建一个 XYLineAndShapeRenderer,您可以按如下所示进行修改和 here .

XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer r = (XYLineAndShapeRenderer) plot.getRenderer();
r.setSeriesShape(0, ShapeUtilities.createDiamond(5));
r.setSeriesShapesVisible(0, true);

image

SSCCE :

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);
}
}

关于java - 如何在 JFreechart 中获取点的菱形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14822218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com