如何从JFreeChart中的X值获取Y值 [英] How to get Y value from X value in JFreeChart

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

问题描述

我正在使用JFreeChart绘制图表.我有XYSeries,其点为(0,0),(1、2),(2、5),并且我想读取Y值,例如x = 1.5.

I'm using JFreeChart to draw chart. I have XYSeries with points (0, 0), (1, 2), (2, 5) and I want to read Y value for let's say x=1.5.

是否可以读取不在XYSeries中的点的值?我找不到类似的话题.

Is it possible to read value for points which are not in XYSeries? I couldn't find similar topic.

推荐答案

不直接支持此功能.在许多情况下,这没有任何意义:根本没有可用的x = 1.5数据.那里的值可能是1000.0或-3.141.你不知道.

This is not supported directly. It does not make sense in many cases: There simply is no data available for x=1.5. The value there could be 1000.0, or -3.141. You don't know.

但是,您最有可能正在寻找线性插值.因此,实用的方法是找到包含各个x值的区间,并对y值进行线性插值.

However, you're most likely looking for a linear interpolation. The pragmatic approach is thus to find the interval that contains the respective x-value, and interpolate the y-values linearly.

有一些技术上的警告.例如. XYSeries可能未排序,或者可能包含重复的x值,在这种情况下,给定x值没有唯一的y值.但是现在,我们可以假定数据集不具有这些属性.

There are some technical caveats. E.g. the XYSeries may be not sorted, or may contain duplicate x-values, in which case there is no unique y-value for a given x-value. But for now, we can assume that the data set does not have these properties.

以下是如何实现此示例.请注意,这不是很有效.如果您必须计算许多中间值(也就是说,如果您打算非常频繁地调用interpolate方法),那么创建一个基于树的数据结构以查找该中间值将是有益的. O(logn)中的时间间隔.

The following is an example of how this could be implemented. Note that this is not very efficient. If you have to compute many intermediate values (that is, if you intend to call the interpolate method very often), it would be beneficial to create a tree-based data structure that allows looking up the interval in O(logn).

但是,如果这不是时间紧迫的(例如,如果您仅打算在工具提示中显示该值),则可以插值如下:

However, if this is not time critical (e.g. if you only intend to show the value in a tooltip or so), you may interpolate the values like this:

import java.util.List;

import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYSeries;

public class XYInterpolation
{
    public static void main(String[] args)
    {
        XYSeries s = new XYSeries("Series");

        s.add(0,0);
        s.add(1,2);
        s.add(2,5);

        double minX = -0.5;
        double maxX = 3.0;
        int steps = 35;
        for (int i=0; i<=steps; i++)
        {
            double a = (double)i / steps;
            double x = minX + a * (maxX - minX);
            double y = interpolate(s, x);
            System.out.printf("%8.3f : %8.3f\n", x, y);
        }
    }

    private static double interpolate(XYSeries s, double x)
    {
        if (x <= s.getMinX())
        {
            return s.getY(0).doubleValue();
        }
        if (x >= s.getMaxX())
        {
            return s.getY(s.getItemCount()-1).doubleValue();
        }
        List<?> items = s.getItems();
        for (int i=0; i<items.size()-1; i++)
        {
            XYDataItem i0 = (XYDataItem) items.get(i);
            XYDataItem i1 = (XYDataItem) items.get(i+1);
            double x0 = i0.getXValue();
            double y0 = i0.getYValue();
            double x1 = i1.getXValue();
            double y1 = i1.getYValue();

            if (x >= x0 && x <= x1)
            {
                double d = x - x0;
                double a = d / (x1-x0);
                double y = y0 + a * (y1 - y0);
                return y;
            }
        }
        // Should never happen
        return 0;
    }

}

(此实现钳制处于极限.这意味着对于小于最小x值或大于最大x值的x值,最小的y值/maximum x-value将分别返回)

(This implementation clamps at the limits. This means that for x-values that are smaller than the minimum x-value or larger than the maximum x-value, the y-value of the minimum/maximum x-value will be returned, respectively)

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

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