2016-08-03 94 views
1
public class createLineChartForSandSoil { 

    static JFreeChart chart; 
    public static XYSeries series; 

    public static void createLineChartForSandSoil(Document document) throws DocumentException, BadElementException, IOException { 
     Paragraph wordDegreeOfHeterogeneity = new Paragraph("Визначаємо ступінь неоднорідності піску:", smallFont); 
     document.add(wordDegreeOfHeterogeneity); 

     ChartPanel chartPanel = createChartPanel(); 
     int width = 450; 
     int height = 350; 
     XYPlot plot = chart.getXYPlot(); 
     XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(); 
     renderer.setSeriesPaint(0, Color.BLACK); 
     plot.setRenderer(renderer); 
     plot.setOutlinePaint(Color.WHITE); 
     plot.setBackgroundPaint(Color.WHITE); 
     plot.setRangeGridlinesVisible(true); 
     plot.setRangeGridlinePaint(Color.GRAY); 
     plot.setDomainGridlinesVisible(true); 
     plot.setDomainGridlinePaint(Color.GRAY); 

     File lineChart = new File("D:/LineChart.png"); 
     ChartUtilities.saveChartAsPNG(lineChart, chart, width, height); 
     Image img = Image.getInstance("D:/LineChart.png"); 
     img.scalePercent(60f); 
     document.add(img); 
    } 

    private static XYDataset createDataset() { 
     XYSeriesCollection dataset = new XYSeriesCollection(); 
     series = new XYSeries(""); 

     series.add(2.0, sumOfParticlesLess_ValueMoreThan2); 
     series.add(1.0, sumOfParticlesLess_Value1_2); 
     series.add(0.5, sumOfParticlesLess_Value05_1); 
     series.add(0.25, sumOfParticlesLess_Value025_05); 
     series.add(0.1, sumOfParticlesLess_Value01_025); 
     series.add(0.0, 0.0); 

     dataset.addSeries(series); 

     return dataset; 
    } 

    private static ChartPanel createChartPanel() { 
     String chartTitle = ""; 
     String xAxisLabel = "Діаметр частинок d, мм"; 
     String yAxisLabel = "Сума частинок, %"; 

     XYDataset dataset = createDataset(); 

     chart = ChartFactory.createXYLineChart(chartTitle, xAxisLabel, yAxisLabel, dataset, PlotOrientation.VERTICAL, false, false, false); 

     return new ChartPanel(chart); 
    } 
} 

This code creates a line chart like on image如何從java中的折線圖中獲取值?

我怎樣才能在點Y = 60獲得X軸(hotrizontal)的值?方法.getAnnotationX().getAnnotationY()不起作用,不知道爲什麼(cannot find method)。有人能幫助我嗎?

回答

1

如果60是一個點的縱座標在XYSeries,你可以簡單地通過搜索的getItems()返回List<XYDataItem>並找到相應的橫座標。因爲不是,所以您需要搜索包圍點(0.25, 50)(0.5, 80)。然後,您可以使用Regression.getOLSRegression()方法來查找連接兩個點的直線的斜率和截距。給定這些值,可以求出相應的橫座標。或者,您可以重新排列線性方程的two-point form以找到所需的點。使用Regression.getOLSRegression()的完整示例顯示here

image