MPAndroidChart有没有办法为不同的酒吧设置不同的颜色? [英] MPAndroidChart is there a way to set different colors for different bars?
问题描述
我在我的应用程序中使用MPAndroidChart,我想知道是否有一种方法设置一个不同的颜色为动态移动条形图中的第一个条形图,如下所示:
I am using MPAndroidChart in my app and i was wondering if there is a way to set a diferent color for the first bar in a dynamically moving bar chart, like this:
当整个堆栈移动到左边为数据不断来临,有没有办法将第一个栏的颜色设置为不同的颜色?
提前谢谢。
The bars are added dynamically while the whole stack is being moved to the left as the data keeps coming, is there a way to set the color of the first bar to be a different color? thank you in advance.
编辑:
这里是我的代码添加一个新的条目到图表,它发生动态每500毫升所以。
here is my code for adding a new entry to the chart, it occurs dynamicaly every 500 millis or so.
private void addBarEntry(float value) {
BarData data = mDownloadChart.getData();
if(data != null) {
BarDataSet set = data.getDataSetByIndex(0);
// set.addEntry(...); // can be called as well
if (set == null) {
set = createBarSet();
data.addDataSet(set);
}
// add a new x-value first
data.addXValue(set.getEntryCount() + "");
// choose a random dataSet
//int randomDataSetIndex = (int) (Math.random() * data.getDataSetCount());
data.addEntry(new BarEntry(value, set.getEntryCount()), 0);
// let the chart know it's data has changed
mDownloadChart.notifyDataSetChanged();
SLog.d(TAG, "download value: "+value);
mDownloadChart.setVisibleXRange(10);
mDownloadChart.moveViewToX(mDownloadChart.getHighestVisibleXIndex()-5);
// redraw the chart
mDownloadChart.invalidate();
}
}
感谢@Philipp Jahoda我得到它的工作,只需在addEntry方法中添加这段代码:
Thanks to @Philipp Jahoda I got it to work, just add this piece of code in you addEntry method:
int[] colors = new int[set.getEntryCount()];
for (int i = 0; i<colors.length; i++){
colors[i]=Color.parseColor("your-hex-color-for-all-entries");
}
colors[colors.length-1] = Color.parseColor("your-hex-color-for-last-entry");
set.setColors(colors);
推荐答案
是的,它在文档。
基本上,您可以为图表中的每个条。这是一个有点不方便,因为在你的情况下,你必须设置每个颜色为红色,最后一个颜色为绿色。
Basically you can set a separate color for each bar in the chart. It's a little inconvenient at the moment because in your case you have to set each color to "red" and the last color to "green".
改善。
这篇关于MPAndroidChart有没有办法为不同的酒吧设置不同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!