我正在使用MPAndroidChart,我想在此CombinedChart
内显示自定义drawable,如下图所示:
如果条形值>> =目标值,比如50,那么我想在条形图中添加一个星形图像。
任何人都可以帮我定制BarChart吗?
答案 0 :(得分:10)
要在我们的条形图中获取星形图像,我们需要创建一个自定义渲染器。因为我们的条形图使用BarChartRenderer
,所以我们首先将其子类化并为我们的图像添加参数:
public class ImageBarChartRenderer extends BarChartRenderer {
private final Bitmap barImage;
public ImageBarChartRenderer(BarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler, Bitmap barImage) {
super(chart, animator, viewPortHandler);
this.barImage = barImage;
}
如果我们检查BarChartRenderer
的来源,我们可以看到它调用名为drawData
的方法,然后遍历每个数据集并调用drawDataSet
。 drawDataSet
是行动发生的地方:它正在绘制阴影和条形图。这是添加逻辑以绘制额外类似图像的合适位置,所以让我们添加对方法的调用以在那里绘制图像:
@Override
protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
super.drawDataSet(c, dataSet, index);
drawBarImages(c, dataSet, index);
}
我们现在需要一种迭代数据集并绘制星形图像的方法。作为模板的适当方法是drawValues
,所以让我们复制并更改它,以便绘制图像而不是文本。了解这一点的关键是了解BarBuffer的工作原理。 BarBuffer在j
,j + 1
,j + 2
,j + 3
保存给定条目的条形图(像素)坐标。
为了澄清,j
是左x坐标,j + 1
是最高坐标,依此类推到j + 3
的右x坐标。我们将这些变量提取到变量中以便于理解:
protected void drawBarImages(Canvas c, IBarDataSet dataSet, int index) {
BarBuffer buffer = mBarBuffers[index];
float left; //avoid allocation inside loop
float right;
float top;
float bottom;
for (int j = 0; j < buffer.buffer.length * mAnimator.getPhaseX(); j += 4) {
left = buffer.buffer[j];
right = buffer.buffer[j + 2];
top = buffer.buffer[j + 1];
bottom = buffer.buffer[j + 3];
float x = (left + right) / 2f;
if (!mViewPortHandler.isInBoundsRight(x))
break;
if (!mViewPortHandler.isInBoundsY(top)
|| !mViewPortHandler.isInBoundsLeft(x))
continue;
BarEntry entry = dataSet.getEntryForIndex(j / 4);
float val = entry.getY();
if (val > 50) {
drawStar(c, barImage, x, top);
}
}
}
以下是使用渲染器的方法:
Bitmap starBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star);
mChart.setRenderer(new ImageBarChartRenderer(mChart, mChart.getAnimator(), mChart.getViewPortHandler(), starBitmap));
渲染器的最后一步是添加逻辑以缩放位图并正确定位。以下是自定义渲染器的最终概念验证:
package com.xxmassdeveloper.mpchartexample;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import com.github.mikephil.charting.animation.ChartAnimator;
import com.github.mikephil.charting.buffer.BarBuffer;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.dataprovider.BarDataProvider;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.renderer.BarChartRenderer;
import com.github.mikephil.charting.utils.ViewPortHandler;
/**
* Created by David on 29/12/2016.
*/
public class ImageBarChartRenderer extends BarChartRenderer {
private final Bitmap barImage;
public ImageBarChartRenderer(BarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler, Bitmap barImage) {
super(chart, animator, viewPortHandler);
this.barImage = barImage;
}
@Override
public void drawData(Canvas c) {
super.drawData(c);
}
@Override
protected void drawDataSet(Canvas c, IBarDataSet dataSet, int index) {
super.drawDataSet(c, dataSet, index);
drawBarImages(c, dataSet, index);
}
protected void drawBarImages(Canvas c, IBarDataSet dataSet, int index) {
BarBuffer buffer = mBarBuffers[index];
float left; //avoid allocation inside loop
float right;
float top;
float bottom;
final Bitmap scaledBarImage = scaleBarImage(buffer);
int starWidth = scaledBarImage.getWidth();
int starOffset = starWidth / 2;
for (int j = 0; j < buffer.buffer.length * mAnimator.getPhaseX(); j += 4) {
left = buffer.buffer[j];
right = buffer.buffer[j + 2];
top = buffer.buffer[j + 1];
bottom = buffer.buffer[j + 3];
float x = (left + right) / 2f;
if (!mViewPortHandler.isInBoundsRight(x))
break;
if (!mViewPortHandler.isInBoundsY(top)
|| !mViewPortHandler.isInBoundsLeft(x))
continue;
BarEntry entry = dataSet.getEntryForIndex(j / 4);
float val = entry.getY();
if (val > 50) {
drawImage(c, scaledBarImage, x - starOffset, top);
}
}
}
private Bitmap scaleBarImage(BarBuffer buffer) {
float firstLeft = buffer.buffer[0];
float firstRight = buffer.buffer[2];
int firstWidth = (int) Math.ceil(firstRight - firstLeft);
return Bitmap.createScaledBitmap(barImage, firstWidth, firstWidth, false);
}
protected void drawImage(Canvas c, Bitmap image, float x, float y) {
if (image != null) {
c.drawBitmap(image, x, y, null);
}
}
}
这是一个屏幕截图 - 你可以看到超过50的值有明星:
答案 1 :(得分:0)
for (i in 0 until values.size) {
if (values[i].y.toInt() >= 8000) {
values[i].icon = ContextCompat.getDrawable(this, R.drawable.oval_check)
}
}