【发布时间】:2014-08-11 06:10:35
【问题描述】:
我在 netbeans swing 中使用 jfreechart 创建了 xyline 图表。
我有两个文件,一个用于连续获取 x 和 y 坐标的值,另一个用于显示它们现在我正在从显示文件的构造函数创建图表,它使用硬编码值显示图表,但是当我通过我传入的图表没有相应更新。
请在下面找到显示文件的代码:
public Display_Unit_Mod() {
JPanel jPanel1 = createChartPanel();
}
public JPanel createChartPanel() {
String chartTitle = "";
String xAxisLabel = "x_value";
String yAxisLabel = "y_value";
XYDataset dataset = createDataset();
JFreeChart chart = ChartFactory.createXYLineChart(chartTitle,
xAxisLabel, yAxisLabel, dataset);
customizeChart(chart);
return new ChartPanel(chart);
}
public XYDataset createDataset() {
XYSeriesCollection dataset = new XYSeriesCollection();
XYSeries series1 = new XYSeries("");
System.out.println(String.valueOf(test.x_value));
System.out.println(String.valueOf(test.y_value));
series1.add(test.x_value,test.y_value); ->test is the object of other file and using it I am taking values from there
//series1.add(7.0,8.0);
//series1.add(3.0,4.0); -> displays values and prints chart
//series1.add(1.0,2.0);
dataset.addSeries(series1);
return dataset;
}
private void customizeChart(JFreeChart chart) {
XYPlot plot = chart.getXYPlot();
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
// sets paint color for each series
renderer.setSeriesPaint(0, Color.RED);
// sets thickness for series (using strokes)
renderer.setSeriesStroke(0, new BasicStroke(4.0f));
renderer.setSeriesStroke(1, new BasicStroke(3.0f));
renderer.setSeriesStroke(2, new BasicStroke(2.0f));
// sets paint color for plot outlines
plot.setOutlinePaint(Color.BLUE);
plot.setOutlineStroke(new BasicStroke(2.0f));
// sets renderer for lines
plot.setRenderer(renderer);
// sets plot background
plot.setBackgroundPaint(Color.DARK_GRAY);
// sets paint color for the grid lines
plot.setRangeGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.BLACK);
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.BLACK);
}
我可以看到来自 system.out.println 的值,但没有反映在 UI 上。
请帮我更新一下。
【问题讨论】:
-
在图表上做一个updateUI()?也许您的 X 或 Y 范围没有捕获数据?
-
当我得到 x 和 y 的值时,我尝试了 updateUI() 和 repaint(),但还没有运气。
-
如果注释掉cutomizeChart代码,你看到了什么?
-
它只是在外观和感觉上有所不同,显示点没有运气。
-
也许你的问题不考虑那个方法,因为它对答案没有影响
标签: java swing jfreechart