如何从Java折线图中获取值? [英] How can I get a values from the line chart in java?

查看:45
本文介绍了如何从Java折线图中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如何获取Y = 60的X轴(水平)值?方法.getAnnotationX().getAnnotationY()不起作用,不知道为什么(cannot find method).有人可以帮我吗?

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?

推荐答案

如果60XYSeries中某个点的坐标,则只需搜索getItems()返回的List<XYDataItem>并找到对应的横坐标.因为不是,所以您需要搜索包围点-( 0.25, 50)(0.5, 80).然后,可以使用Regression.getOLSRegression()方法找到连接两个点的直线的斜率和截距.给定这些值,您可以求解相应的横坐标.另外,您可以重新排列线性方程式的两点形式以查找所需的点.在此处显示了使用Regression.getOLSRegression()的完整示例.

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.

这篇关于如何从Java折线图中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆