Visualização personalizada da linha de limite no MPAndroidChart
É possível substituirLimitLine
com layout personalizado? Portanto, é algo parecido com isto:
Vejo algumas soluções para isso:
Talvez haja métodos dentro da biblioteca para personalizações como essa, existem?Obter coordenadas deTextView
onde o valor é armazenado e adicione um layout personalizado nesse local. Mas como posso chegar a issoTextView
?Talvez alguém tenha enfrentado esse problema. Por favor, compartilhe sua experiência.
EDIT: última solução parcial
Após uma longa pesquisa da solução, comecei a adicionar programações personalizadas programaticamente por coordenadas da Linha de limite.
Layout geral da tela da seguinte maneira:
As coordenadas são calculadas de maneira bastante direta.X
é conhecido pelas coordenadas do gráfico eY
:
Y = ((max - lastValue) * altura) / ((max + min) + y)
Então, neste ponto, eu basicamente sei a posição que preciso. Embora eu não tenha certeza se está certo devido aos paisScrollView
.
O próximo passo é adicionar layout personalizado nessas coordenadas(x, y)
E aqui vem o novoproblema. Eu tentei adicionar a visualização ao topoRelativeLayout
. É adicionado, mas não se move junto comScrollView
. Portanto, é necessário adicionar essa visualização exatamente no gráfico. Veja como eu tentei conseguir isso:
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();
}
});
}
Talvez eu deva acessar o layout pai do Chart e inflar meu layout personalizado lá. Masquão?
EDIT 2: adicionada visualização personalizada sobre o gráfico, mas não é possível encontrar o local correto devido à visualização de rolagem
Agora a situação é assim:
Talvez eu esteja em algum lugar errado nos cálculos. Mas pelo menos esse View muda de posição com o novo valor, embora nunca atinja as coordenadas corretas.
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();
}
});
}