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

标签 java jfreechart

我对 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();

问题截图||刻度单位不同

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

最佳答案

我终于找到了解决这个问题的方法!而我还不知道这个问题的确切解决方案。

不要尝试使用以下代码行,

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

关于java - 从 java 中的 jfreechart 中的范围轴获取后,如何在域轴上设置刻度单位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55533639/

相关文章:

java - 当看到 javascript 警报时 driver.close 不起作用

java - 写入 PDF 文档后使用 iText 打开它们

java - Jfreechart索引越界异常

java - 如何生成仅包含 CMYK 颜色的 JFreeChart

Java Swing JFreeChart DialPlot 图例

java - 将 jfreechart 导出为 pptx 作为 EMF 图像 - 线条渲染不正确

java - Intellij IDEA 12. 返回上一个 Activity 选项卡

java.lang.IllegalArgumentException : org. hibernate.hql.internal.ast.QuerySyntaxException:用户未映射[来自用户]

java - PHP 客户端通过 Thrift 连接到 Java 服务是否需要完整的 J2EE 服务器?

java - 如何从主要函数 int java 将文本附加到 JTextArea?