在Java的jfreechart中从范围轴获取刻度单位后,如何在域轴上设置刻度单位?

时间:2019-04-05 10:41:55

标签: java jfreechart

我对Java有点陌生。

现在,我正在尝试使用JFreeChart绘制XYPlot,其中域轴(X轴)和范围轴(Y轴)包含相同范围。但是不幸的是,刻度单位不同!因此,我尝试使用NumberAxis根据范围轴和范围轴的刻度单位来设置域轴上的范围和刻度单位。但是仍然存在差异。我仍然不知道为什么会这样。

我正在附上此绘图的代码。另外,我附上问题的屏幕快照以及我真正想要的!请指导我有关我在这里做错了什么...

lot2 <- structure(list(LOT = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), .Label = "LOT2", class = "factor"), Step = 1:9, Tool_code = structure(c(1L, 
2L, 3L, 2L, 4L, 5L, 6L, 7L, 8L), .Label = c("Machine 1", "Machine 2", 
"Machine 3", "Machine 4", "Machine 5", "Machine 6", "Machine 7", 
"Machine 8"), class = "factor"), Time_taken = c(3L, 10L, 3L, 
10L, 2L, 2L, 1L, 5L, 5L), Transfer_time = c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), START_Time = structure(1:9, .Label = c("09:04:00", 
"09:15:00", "09:26:00", "09:37:00", "09:48:00", "09:51:00", "09:54:00", 
"09:56:00", "10:02:00"), class = "factor"), end_time = structure(1:9, .Label = c("09:08:00", 
"09:26:00", "09:30:00", "09:48:00", "09:51:00", "09:54:00", "09:56:00", 
"10:02:00", "10:08:00"), class = "factor")), class = "data.frame", row.names = c(NA, 
-9L))

问题的屏幕截图||刻度单位不同

所需结果的屏幕截图||两个轴上的刻度单位相同

1 个答案:

答案 0 :(得分:0)

最后,我已经解决了这个问题!我还不知道解决这个问题的确切方法。

与其尝试使用以下代码行,

NumberAxis D = (NumberAxis) plotAD.getDomainAxis(); 
NumberAxis R = (NumberAxis) plotAD.getRangeAxis(); 
D.setRange(R.getRange()); 
D.setTickUnit(R.getTickUnit());

我尝试了以下代码行:

//getting the number axes from the plot

NumberAxis D = (NumberAxis) plotAD.getDomainAxis();
NumberAxis R = (NumberAxis) plotAD.getRangeAxis();

//creating custom tick units based on lower and upper bound

Double DT = (D.getUpperBound() - D.getLowerBound())/5;
DecimalFormat DF = new DecimalFormat("#.#");
DF.setRoundingMode(RoundingMode.FLOOR);
String DTS = DF.format(DT);
DT = Double.parseDouble(DTS);
D.setTickUnit(new NumberTickUnit(DT));
Double RT = (R.getUpperBound() - R.getLowerBound())/5;
String RTS = DF.format(RT);
RT = Double.parseDouble(RTS);
R.setTickUnit(new NumberTickUnit(RT));

它有效!!!请参阅下面的屏幕截图,这是我想要的,两个轴上的刻度单位相同...

Final Result as Expected!

下面也提供了这种绘图的完整代码(可能会增加一些代码行,但是我很高兴它可以在任何情况下都可以使用,直到我得到此问题的确切解决方案为止):

//Creating XYseries based on an array (i.e., pt.delayedy)

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

//Customizing my axis labels (required for my purpose)

if (jRadioButton10.isSelected()) {
   pt.xaxisAD = pt.yvar+" ("+pt.xvar+") ["+pt.yunit+"]";
   pt.yaxisADsupport = String.format("%f", (pt.xspace*pt.delay));            
   pt.yaxisAD = pt.yvar+" ("+pt.xvar+"+d) ["+pt.yunit+"]";      
}
else if (jRadioButton11.isSelected()) {            
   pt.xaxisAD = pt.yvar+" (i) ["+pt.yunit+"]";
   pt.yaxisAD = pt.yvar +" (i+d) ["+pt.yunit+"]";
   jLabel44.setText("(Delay (d) = "+pt.delay+")");
}

//Creating a JFreechart with my labels and series

JFreeChart lineChartAD = ChartFactory.createXYLineChart("", pt.xaxisAD, pt.yaxisAD, (XYDataset) lineChartDataAD, PlotOrientation.VERTICAL, false, false, false);

//Getting the chart plot

XYPlot plotAD  = lineChartAD.getXYPlot();

//Creating the renderer for the chart

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

//Customizing the font of the axes labels

Font F1AD = new Font ("Times New Roman", Font.PLAIN, 14);        
plotAD.getDomainAxis().setLabelFont(F1AD);
plotAD.getRangeAxis().setLabelFont(F1AD);

//The below lines are for exact same x-scaling and y-scaling in plot

NumberAxis D = (NumberAxis) plotAD.getDomainAxis();
NumberAxis R = (NumberAxis) plotAD.getRangeAxis();
D.setAutoRangeIncludesZero(false);
R.setAutoRangeIncludesZero(false);
Double DT = (D.getUpperBound() - D.getLowerBound())/5;
DecimalFormat DF = new DecimalFormat("#.#");
DF.setRoundingMode(RoundingMode.FLOOR);
String DTS = DF.format(DT);
DT = Double.parseDouble(DTS);
D.setTickUnit(new NumberTickUnit(DT));
Double RT = (R.getUpperBound() - R.getLowerBound())/5;
String RTS = DF.format(RT);
RT = Double.parseDouble(RTS);
R.setTickUnit(new NumberTickUnit(RT));

//Plot customization

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

//Creating ChartPanel

ChartPanel linePanelAD = new ChartPanel(lineChartAD, true, true, false, false, true);
linePanelAD.setMouseZoomable(false);

//Adding the ChartPanel to the JPanel 

panelChartRMA4D.removeAll();
panelChartRMA4D.add(linePanelAD, BorderLayout.CENTER);
panelChartRMA4D.setVisible(true);
panelChartRMA4D.setBorder(new LineBorder (Color.BLACK));
panelChartRMA4D.validate();