- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如何获取散点图中像素点对应的x轴值(xIndex)?
我一直在查看库的源代码,我认为下面的方法之一(我从库的源代码复制/粘贴 here )可以提供帮助。 getValuesByTouchPoint
似乎是正确的方法,但我不知道如何使用它。
/**
* Transformer class that contains all matrices and is responsible for
* transforming values into pixels on the screen and backwards.
*
* @author Philipp Jahoda
*/
public class Transformer {
/**
* matrix to map the values to the screen pixels
*/
protected Matrix mMatrixValueToPx = new Matrix();
/**
* matrix for handling the different offsets of the chart
*/
protected Matrix mMatrixOffset = new Matrix();
protected ViewPortHandler mViewPortHandler;
public Transformer(ViewPortHandler viewPortHandler) {
this.mViewPortHandler = viewPortHandler;
}
/**
* Prepares the matrix that transforms values to pixels. Calculates the
* scale factors from the charts size and offsets.
*
* @param xChartMin
* @param deltaX
* @param deltaY
* @param yChartMin
*/
public void prepareMatrixValuePx(float xChartMin, float deltaX, float deltaY, float yChartMin) {
float scaleX = (float) ((mViewPortHandler.contentWidth()) / deltaX);
float scaleY = (float) ((mViewPortHandler.contentHeight()) / deltaY);
if (Float.isInfinite(scaleX)) {
scaleX = 0;
}
if (Float.isInfinite(scaleY)) {
scaleY = 0;
}
// setup all matrices
mMatrixValueToPx.reset();
mMatrixValueToPx.postTranslate(-xChartMin, -yChartMin);
mMatrixValueToPx.postScale(scaleX, -scaleY);
}
/**
* Prepares the matrix that contains all offsets.
*
* @param inverted
*/
public void prepareMatrixOffset(boolean inverted) {
mMatrixOffset.reset();
// offset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom);
if (!inverted)
mMatrixOffset.postTranslate(mViewPortHandler.offsetLeft(),
mViewPortHandler.getChartHeight() - mViewPortHandler.offsetBottom());
else {
mMatrixOffset
.setTranslate(mViewPortHandler.offsetLeft(), -mViewPortHandler.offsetTop());
mMatrixOffset.postScale(1.0f, -1.0f);
}
}
protected float[] valuePointsForGenerateTransformedValuesScatter = new float[1];
/**
* Transforms an List of Entry into a float array containing the x and
* y values transformed with all matrices for the SCATTERCHART.
*
* @param data
* @return
*/
public float[] generateTransformedValuesScatter(IScatterDataSet data, float phaseX,
float phaseY, int from, int to) {
final int count = (int) ((to - from) * phaseX + 1) * 2;
if (valuePointsForGenerateTransformedValuesScatter.length != count) {
valuePointsForGenerateTransformedValuesScatter = new float[count];
}
float[] valuePoints = valuePointsForGenerateTransformedValuesScatter;
for (int j = 0; j < count; j += 2) {
Entry e = data.getEntryForIndex(j / 2 + from);
if (e != null) {
valuePoints[j] = e.getX();
valuePoints[j + 1] = e.getY() * phaseY;
} else {
valuePoints[j] = 0;
valuePoints[j + 1] = 0;
}
}
getValueToPixelMatrix().mapPoints(valuePoints);
return valuePoints;
}
protected float[] valuePointsForGenerateTransformedValuesBubble = new float[1];
/**
* Transforms an List of Entry into a float array containing the x and
* y values transformed with all matrices for the BUBBLECHART.
*
* @param data
* @return
*/
public float[] generateTransformedValuesBubble(IBubbleDataSet data, float phaseY, int from, int to) {
final int count = (to - from + 1) * 2; // (int) Math.ceil((to - from) * phaseX) * 2;
if (valuePointsForGenerateTransformedValuesBubble.length != count) {
valuePointsForGenerateTransformedValuesBubble = new float[count];
}
float[] valuePoints = valuePointsForGenerateTransformedValuesBubble;
for (int j = 0; j < count; j += 2) {
Entry e = data.getEntryForIndex(j / 2 + from);
if (e != null) {
valuePoints[j] = e.getX();
valuePoints[j + 1] = e.getY() * phaseY;
} else {
valuePoints[j] = 0;
valuePoints[j + 1] = 0;
}
}
getValueToPixelMatrix().mapPoints(valuePoints);
return valuePoints;
}
protected float[] valuePointsForGenerateTransformedValuesLine = new float[1];
/**
* Transforms an List of Entry into a float array containing the x and
* y values transformed with all matrices for the LINECHART.
*
* @param data
* @return
*/
public float[] generateTransformedValuesLine(ILineDataSet data,
float phaseX, float phaseY, int from, int to) {
final int count = (int) ((to - from) * phaseX + 1) * 2;
if (valuePointsForGenerateTransformedValuesLine.length != count) {
valuePointsForGenerateTransformedValuesLine = new float[count];
}
float[] valuePoints = valuePointsForGenerateTransformedValuesLine;
for (int j = 0; j < count; j += 2) {
Entry e = data.getEntryForIndex(j / 2 + from);
if (e != null) {
valuePoints[j] = e.getX();
valuePoints[j + 1] = e.getY() * phaseY;
} else {
valuePoints[j] = 0;
valuePoints[j + 1] = 0;
}
}
getValueToPixelMatrix().mapPoints(valuePoints);
return valuePoints;
}
protected float[] valuePointsForGenerateTransformedValuesCandle = new float[1];
/**
* Transforms an List of Entry into a float array containing the x and
* y values transformed with all matrices for the CANDLESTICKCHART.
*
* @param data
* @return
*/
public float[] generateTransformedValuesCandle(ICandleDataSet data,
float phaseX, float phaseY, int from, int to) {
final int count = (int) ((to - from) * phaseX + 1) * 2;
if (valuePointsForGenerateTransformedValuesCandle.length != count) {
valuePointsForGenerateTransformedValuesCandle = new float[count];
}
float[] valuePoints = valuePointsForGenerateTransformedValuesCandle;
for (int j = 0; j < count; j += 2) {
CandleEntry e = data.getEntryForIndex(j / 2 + from);
if (e != null) {
valuePoints[j] = e.getX();
valuePoints[j + 1] = e.getHigh() * phaseY;
} else {
valuePoints[j] = 0;
valuePoints[j + 1] = 0;
}
}
getValueToPixelMatrix().mapPoints(valuePoints);
return valuePoints;
}
/**
* transform a path with all the given matrices VERY IMPORTANT: keep order
* to value-touch-offset
*
* @param path
*/
public void pathValueToPixel(Path path) {
path.transform(mMatrixValueToPx);
path.transform(mViewPortHandler.getMatrixTouch());
path.transform(mMatrixOffset);
}
/**
* Transforms multiple paths will all matrices.
*
* @param paths
*/
public void pathValuesToPixel(List<Path> paths) {
for (int i = 0; i < paths.size(); i++) {
pathValueToPixel(paths.get(i));
}
}
/**
* Transform an array of points with all matrices. VERY IMPORTANT: Keep
* matrix order "value-touch-offset" when transforming.
*
* @param pts
*/
public void pointValuesToPixel(float[] pts) {
mMatrixValueToPx.mapPoints(pts);
mViewPortHandler.getMatrixTouch().mapPoints(pts);
mMatrixOffset.mapPoints(pts);
}
/**
* Transform a rectangle with all matrices.
*
* @param r
*/
public void rectValueToPixel(RectF r) {
mMatrixValueToPx.mapRect(r);
mViewPortHandler.getMatrixTouch().mapRect(r);
mMatrixOffset.mapRect(r);
}
/**
* Transform a rectangle with all matrices with potential animation phases.
*
* @param r
* @param phaseY
*/
public void rectToPixelPhase(RectF r, float phaseY) {
// multiply the height of the rect with the phase
r.top *= phaseY;
r.bottom *= phaseY;
mMatrixValueToPx.mapRect(r);
mViewPortHandler.getMatrixTouch().mapRect(r);
mMatrixOffset.mapRect(r);
}
public void rectToPixelPhaseHorizontal(RectF r, float phaseY) {
// multiply the height of the rect with the phase
r.left *= phaseY;
r.right *= phaseY;
mMatrixValueToPx.mapRect(r);
mViewPortHandler.getMatrixTouch().mapRect(r);
mMatrixOffset.mapRect(r);
}
/**
* Transform a rectangle with all matrices with potential animation phases.
*
* @param r
*/
public void rectValueToPixelHorizontal(RectF r) {
mMatrixValueToPx.mapRect(r);
mViewPortHandler.getMatrixTouch().mapRect(r);
mMatrixOffset.mapRect(r);
}
/**
* Transform a rectangle with all matrices with potential animation phases.
*
* @param r
* @param phaseY
*/
public void rectValueToPixelHorizontal(RectF r, float phaseY) {
// multiply the height of the rect with the phase
r.left *= phaseY;
r.right *= phaseY;
mMatrixValueToPx.mapRect(r);
mViewPortHandler.getMatrixTouch().mapRect(r);
mMatrixOffset.mapRect(r);
}
/**
* transforms multiple rects with all matrices
*
* @param rects
*/
public void rectValuesToPixel(List<RectF> rects) {
Matrix m = getValueToPixelMatrix();
for (int i = 0; i < rects.size(); i++)
m.mapRect(rects.get(i));
}
protected Matrix mPixelToValueMatrixBuffer = new Matrix();
/**
* Transforms the given array of touch positions (pixels) (x, y, x, y, ...)
* into values on the chart.
*
* @param pixels
*/
public void pixelsToValue(float[] pixels) {
Matrix tmp = mPixelToValueMatrixBuffer;
tmp.reset();
// invert all matrixes to convert back to the original value
mMatrixOffset.invert(tmp);
tmp.mapPoints(pixels);
mViewPortHandler.getMatrixTouch().invert(tmp);
tmp.mapPoints(pixels);
mMatrixValueToPx.invert(tmp);
tmp.mapPoints(pixels);
}
/**
* buffer for performance
*/
float[] ptsBuffer = new float[2];
/**
* Returns a recyclable MPPointD instance.
* returns the x and y values in the chart at the given touch point
* (encapsulated in a MPPointD). This method transforms pixel coordinates to
* coordinates / values in the chart. This is the opposite method to
* getPixelForValues(...).
*
* @param x
* @param y
* @return
*/
public MPPointD getValuesByTouchPoint(float x, float y) {
MPPointD result = MPPointD.getInstance(0, 0);
getValuesByTouchPoint(x, y, result);
return result;
}
public void getValuesByTouchPoint(float x, float y, MPPointD outputPoint) {
ptsBuffer[0] = x;
ptsBuffer[1] = y;
pixelsToValue(ptsBuffer);
outputPoint.x = ptsBuffer[0];
outputPoint.y = ptsBuffer[1];
}
/**
* Returns a recyclable MPPointD instance.
* Returns the x and y coordinates (pixels) for a given x and y value in the chart.
*
* @param x
* @param y
* @return
*/
public MPPointD getPixelForValues(float x, float y) {
ptsBuffer[0] = x;
ptsBuffer[1] = y;
pointValuesToPixel(ptsBuffer);
double xPx = ptsBuffer[0];
double yPx = ptsBuffer[1];
return MPPointD.getInstance(xPx, yPx);
}
public Matrix getValueMatrix() {
return mMatrixValueToPx;
}
public Matrix getOffsetMatrix() {
return mMatrixOffset;
}
private Matrix mMBuffer1 = new Matrix();
public Matrix getValueToPixelMatrix() {
mMBuffer1.set(mMatrixValueToPx);
mMBuffer1.postConcat(mViewPortHandler.mMatrixTouch);
mMBuffer1.postConcat(mMatrixOffset);
return mMBuffer1;
}
private Matrix mMBuffer2 = new Matrix();
public Matrix getPixelToValueMatrix() {
getValueToPixelMatrix().invert(mMBuffer2);
return mMBuffer2;
}
}
最佳答案
使用 MPAndroidChart 3.0.1
我认为你想要的方法是:
public MPPointD getValuesByTouchPoint(float x, float y);
这是来自 javadoc 的 fragment :
/**
* Returns a recyclable MPPointD instance.
* returns the x and y values in the chart at the given touch point
* (encapsulated in a MPPointD). This method transforms pixel coordinates to
* coordinates / values in the chart. This is the opposite method to
* getPixelsForValues(...).
要在您的应用中使用它,请执行以下操作:
MPPointD point = mChart.getTransformer(AxisDependency.LEFT).getValuesByTouchPoint(x,y);
double xValue = point.x;
double yValue = point.y;
传递给方法 (float x, float y)
的参数是您要在图表中转换为 x 和 y 值的像素坐标。
关于android - MPAndroidChart:如何获取像素点对应的图表 x 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41766419/
我想检查我的应用程序或系统中是否存在库。在 Java 中,我通常执行 System.loadlibrary,但是有谁知道 C 中类似的相应调用吗? 最佳答案 是dlopen打开一个库,dlsym 从加
我在 typescript 中输入以下内容 export type Excludable = T & { isExcluded?: boolean } 其中过滤值: export type Filte
我最近在我的应用程序中添加了一种方法,可以自动格式化 TextView ,从“50000”到“50,000”,效果绝对完美。现在我遇到的问题是,在我的应用程序中,有多个按钮功能可以从该 TextVie
SELECT * FROM conversations WHERE chatMembers LIKE '%1%'AND chatMembers LIKE '%10%' 对话表 id | chatMem
我正在编写一个需要将 Java Date() 值保存到 MySQL 数据库的 RESTful Web 服务,但是,我不确定 MySQL 中可以保存 Java Date() 的数据类型是什么,或者我是否
同样,在任何 Red Hat 或 JBoss 站点上都没有关于此的信息,所以我在这里问... 我不确定是 13 还是 14。 最佳答案 Mapping the Community versions w
同样,在任何 Red Hat 或 JBoss 站点上都没有关于此的信息,所以我在这里问... 我不确定是 13 还是 14。 最佳答案 Mapping the Community versions w
我曾尝试使用 swift 开发一款利用 iPhone 的 3D 触摸硬件的游戏。然而,当我将我的应用程序提交到 App Store 时,它被拒绝了,因为该游戏无法在 iPad 上玩。 我的问题是,
Qt 的有序关联容器对应项 std::map是QMap , std::set是QSet , 对于无序关联容器 std::unordered_map是QHash . 我应该用什么来代替std::unor
JavaScript 方法 String.fromCharCode() 在以下意义上与 Python 的 unichar() 等效: print unichr(213) # prints Õ on t
正如谷歌在 "Discontinuing support for JSON-RPC and Global HTTP Batch Endpoints" 中提到的那样,Google API 客户端库已重新
我正在使用 MapLayer 和 MapOverlay 在 map 中创建自己的路径/折线,GPS 捕获的所有点都存储在一个结构中,以便我可以访问它们。随时。 现在,我希望路径在用户操作 map (缩
我们使用 Adobe Flash Builder 创建由 Flex 提供支持的交互式 Web 应用程序。现在我们正在寻找替代方案,让我们在 UI 设计和迎合 HTML5 的编码方面拥有同样的开发便
我想知道Android/Java 中类似C#/C++ 中的GetTickCount 方法的相应方法吗? 最佳答案 Android 为 SystemClock.uptimeMillis() .请注意,u
我用 Vue + Phaser 开始了新项目,但是当我尝试加载 Assets 时,this.game.load.image 中的“load”和“add”返回“undefined”。我尝试从 JS 文件
我是一名优秀的程序员,十分优秀!