How can I get a values from the line chart in java?

风格不统一 提交于 2019-12-11 05:49:00

问题


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

How can I get the value on X axis (hotrizontal) in the point Y=60? Methods .getAnnotationX() and .getAnnotationY() doesn't work, don't know why (cannot find method). Can somebody help me?


回答1:


If 60 were the ordinate of a point in your XYSeries, you could simply search the List<XYDataItem> returned by getItems() and find the corresponding abscissa. Because it is not, you'll need to search for the bracketing points—( 0.25, 50) and (0.5, 80). Then you can use the Regression.getOLSRegression() method to find the slope and intercept of the straight line connecting the two points. Given these values, you can solve for the corresponding abscissa. Alternatively, you can rearrange the two-point form of a linear equation to find the desired point. A complete example that uses Regression.getOLSRegression() is shown here.



来源:https://stackoverflow.com/questions/38743024/how-can-i-get-a-values-from-the-line-chart-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!