gpt4 book ai didi

android - MPAndroidChart 中限制线的自定义 View

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:08:46 24 4
gpt4 key购买 nike

是否可以用自定义布局替换 LimitLine?所以它看起来像这样:

enter image description here

我看到的解决方案很少:

  1. 也许库中有这样的自定义方法,有吗?
  2. 获取存储值的 TextView 的坐标,并在该位置添加自定义布局。但是我怎样才能到达这个 TextView

也许有人遇到过这个问题。请分享您的经验。


编辑:最新的部分解决方案

经过长期搜索解决方案后,我想到了通过限制线的坐标以编程方式添加自定义 View 。

屏幕总体布局如下:

enter image description here

坐标的计算非常简单。 X 可从图表坐标和 Y 中获知:

Y = ((max - lastValue) * height) / ((max + min) + y)

所以至此我基本上知道了我需要的位置。尽管由于父 ScrollView,我不确定它是否正确。

下一步是在这些坐标处添加自定义布局 (x, y)

enter image description here

新的问题来了。我试图将 View 添加到顶部 RelativeLayout。它已添加,但不会与 ScrollView 一起移动。因此,它需要准确地在图表上添加该 View 。看看我是如何尝试实现这一目标的:

private void addCustomLayoutOnLimitLine(final double lastValue) {

mChart.post(new Runnable() { //check location when view is created
public void run() {
int[] chartLocationOnScreen = new int[2];
mChart.getLocationOnScreen(chartLocationOnScreen);

int x = chartLocationOnScreen[0];
int y = chartLocationOnScreen[1];

int width = mChart.getWidth();
int height = mChart.getHeight();


double max = mChart.getYMax();
double min = mChart.getYMin();

int limitXPoint = x + width;
int limitYPoint = (int) ((((max - lastValue) * height) / (max + min))+ y);



LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout rlValue = (RelativeLayout) inflater.inflate(R.layout.item_chart_value, null);
TextView tvValue = (TextView) rlValue.findViewById(R.id.tv_value);
tvValue.setText(String.valueOf(lastValue));

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
params.leftMargin = limitXPoint - 100;
params.topMargin = limitYPoint;
mChart.addView(rlValue, params); //this doesn't seem to be working
rlValue.bringToFront();
}
});
}

也许我应该到达 Chart 的父布局并在那里扩展我的自定义布局。但是如何


编辑 2:在图表上添加了自定义 View ,但由于 ScrollView 无法找到合适的位置

现在的情况是这样的:

enter image description here

也许我计算有误。但至少那个 View 用新值改变了它的位置,尽管它从来没有达到正确的协调。

private void addCustomLayoutOnLimitLine() {
if (mChart == null){
return;
}

mChart.post(new Runnable() { //check location when view is created
public void run() {
int[] chartLocationOnScreen = new int[2];
mChart.getLocationOnScreen(chartLocationOnScreen);

int xChart = chartLocationOnScreen[0];
int yChart = chartLocationOnScreen[1];

int chartWidth = mChart.getWidth();
int chartHeight = mChart.getHeight();

int rootWidth = rlSvContent.getWidth();
int rootHeight = rlSvContent.getHeight(); //this is height of ScrollView

int infoWidth = llInfoWrapper.getWidth(); //width of info panel ABOVE chart
int infoHeight = llInfoWrapper.getHeight();

double lastValue = mSingleAsset.getGraph().get(mSingleAsset.getGraph().size() - 1).getValue();
double maxValue = mChart.getYMax();
double minValue = mChart.getYMin();

int limitXPoint = (rootWidth - chartWidth) / 2 + chartWidth;
int limitYPoint = (int) ((maxValue - lastValue) * chartHeight/(maxValue - minValue)) + yChart;

tvCustomValue.setText(SingleAsset.round((float) lastValue, 2).toString()); //display last value on custom view

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.leftMargin = limitXPoint - xChart - 50; //move custom view. xChart = right margin value and 50 is taken to count values bar to the right of chart
params.topMargin = limitYPoint;
rlCustomValue.setLayoutParams(params);
rlCustomValue.bringToFront();
rlCustomValue.invalidate();
}
});
}

最佳答案

<ScrollView>

<LinearLayout/>

<FrameLayout>

<Chart/>
<TextView/>

<FrameLauyout>

</ScrollView>

使用 ViewPortHandler 获取图表的偏移量

    float offsetTop = mChart.getViewPortHandler().offsetTop();
float offsetLeft = mChart.getViewPortHandler().offsetLeft();
float offsetRight = mChart.getViewPortHandler().offsetRight();
float chartHeight = mChart.getViewPortHandler().contentHeight();

关于android - MPAndroidChart 中限制线的自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36083802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com