【发布时间】:2019-08-27 06:06:07
【问题描述】:
我对 Java 有点陌生。
现在我正在尝试使用 JFreeChart 绘制一个 XYPlot,其中域轴(X 轴)和范围轴(Y 轴)包含相同的范围。但不幸的是,刻度单位不同!出于这个原因,我尝试使用 NumberAxis 根据范围轴的范围和刻度单位来设置域轴上的范围和刻度单位。但差异仍然存在。我仍然无法找出为什么会这样。
我附上了这个绘图的代码。我还附上了问题的屏幕截图和我真正想要的!请指导我在这里做错了什么......
XYSeriesCollection lineChartDataAD = new XYSeriesCollection();
XYSeries seriesAD = new XYSeries("Real Surface Heights", false, true);
for (int m = 0; m < pt.delayedy.length - 1; m++) {
seriesAD.add((double)pt.delayedy[m], (double)pt.delayedy[m+1]);
}
lineChartDataAD.addSeries(seriesAD);
if (jRadioButton10.isSelected()) { //as it is checkbox2.getState() == true
pt.xaxisAD = pt.yvar+" ("+pt.xvar+") ["+pt.yunit+"]";
pt.yaxisADsupport = String.format("%f", (pt.xspace*pt.delay));
//pt.yaxisAD = pt.yvar +" (i+"+pt.yaxisADsupport+")";
pt.yaxisAD = pt.yvar+" ("+pt.xvar+"+d) ["+pt.yunit+"]";
jLabel44.setText("(Delay (d) = "+pt.yaxisADsupport+" "+pt.xunit+")");
}
else if (jRadioButton11.isSelected()) { //as time series checkbox1.getState() == true
//pt.yaxisAD = pt.yvar +" (i+"+pt.delay+")";
pt.xaxisAD = pt.yvar+" (i) ["+pt.yunit+"]";
pt.yaxisAD = pt.yvar +" (i+d) ["+pt.yunit+"]";
jLabel44.setText("(Delay (d) = "+pt.delay+")");
}
JFreeChart lineChartAD = ChartFactory.createXYLineChart("", pt.xaxisAD, pt.yaxisAD, (XYDataset) lineChartDataAD, PlotOrientation.VERTICAL, false, false, false);
XYPlot plotAD = lineChartAD.getXYPlot();
NumberAxis D = (NumberAxis) plotAD.getDomainAxis();
NumberAxis R = (NumberAxis) plotAD.getRangeAxis();
D.setRange(R.getRange());
D.setTickUnit(R.getTickUnit());
XYLineAndShapeRenderer rendererAD = new XYLineAndShapeRenderer();
rendererAD.setSeriesPaint(0, Color.BLACK);
double sizeAD = 0;
double deltaAD = sizeAD / 2.0;
Shape shapeAD = new Rectangle2D.Double(-deltaAD, -deltaAD, sizeAD, sizeAD);
rendererAD.setSeriesShape(0, shapeAD);
rendererAD.setSeriesStroke(0, new BasicStroke(1.0f));
Font F1AD = new Font ("Times New Roman", Font.PLAIN, 14);
plotAD.getDomainAxis().setLabelFont(F1AD);
plotAD.getRangeAxis().setLabelFont(F1AD);
plotAD.setOutlinePaint(Color.BLACK);
plotAD.setOutlineStroke(new BasicStroke(0.5f));
plotAD.setRenderer(rendererAD);
plotAD.setBackgroundPaint(Color.WHITE);
plotAD.setRangeGridlinesVisible(true);
plotAD.setRangeGridlinePaint(Color.GRAY);
plotAD.setDomainGridlinesVisible(true);
plotAD.setDomainGridlinePaint(Color.GRAY);
ChartPanel linePanelAD = new ChartPanel(lineChartAD, true, true, false, false, true); //Properties, save, print, zoom in pop-up menu, and tooltip
linePanelAD.setMouseZoomable(false);
panelChartRMA4D.removeAll();
panelChartRMA4D.add(linePanelAD, BorderLayout.CENTER);
panelChartRMA4D.setVisible(true);
panelChartRMA4D.setBorder(new LineBorder (Color.BLACK));
panelChartRMA4D.validate();
问题截图 ||刻度单位不同
所需结果的屏幕截图 ||两个轴上的刻度单位相同
【问题讨论】:
-
请edit 在您的问题中包含一个minimal reproducible example,以展示您描述的问题;这个example 可能会提供一些见解。
-
@trashgod,我已经编辑了出现这个问题的代码。使用 NumberAxis 我一直在尝试从范围轴获取刻度单位并将其设置在域轴上但不能!
-
我无法重现您说明的问题。另请参阅此相关example。
-
@trashgod 感谢您的回复。案例是这只是整个代码的一小部分。 XY 数据点是从文本文件中获取的,以绘制该文件(返回图或延迟图)。这可能是您无法复制它的原因。我研究了你的例子。在这种情况下,您已经展示了如何根据一些输入设置刻度单位!但是我的问题略有不同!我的域和范围数据范围都是 [-2.09440..., 1.71831...]。我只想从范围轴获取刻度单位并将其设置在域轴上,以便它们看起来与第二张图片中显示的相同。
标签: java jfreechart