MPAndroidChart是有办法对不同的酒吧设置不同的颜色? [英] MPAndroidChart is there a way to set different colors for different bars?
问题描述
我在我的应用程序中使用MPAndroidChart,我想知道如果有一种方法来设置动态移动条形图中的第一棒,这样的颜色diferent:
将条动态添加,而整个堆栈被移动到左侧为数据不断到来,有没有办法设置第一个栏的颜色是不同的颜色?
预先感谢您。
编辑:
这里是我的code添加新条目到图表,它发生每500米利斯dynamicaly左右。
私人无效addBarEntry(浮点值){
BarData数据= mDownloadChart.getData(); 如果(数据!= NULL){ BarDataSet套= data.getDataSetByIndex(0);
// set.addEntry(...); //可以称为以及 如果(设置== NULL){
设定= createBarSet();
data.addDataSet(套);
} //首先添加一个新的x值
data.addXValue(set.getEntryCount()+); //随机选择一个数据集
// INT randomDataSetIndex =(int)的(的Math.random()* data.getDataSetCount()); data.addEntry(新BarEntry(值,set.getEntryCount()),0); //让图表知道它的数据已经改变
mDownloadChart.notifyDataSetChanged(); SLog.d(TAG,下载值:+值); mDownloadChart.setVisibleXRange(10);
mDownloadChart.moveViewToX(mDownloadChart.getHighestVisibleXIndex() - 5); //重新绘制图表
mDownloadChart.invalidate();
}
}
由于@Philipp Jahoda我得到它的工作,只需添加这块code在你的addEntry方法:
INT [] =色彩新INT [set.getEntryCount()];
的for(int i = 0; I< colors.length;我++){
颜色[I] = Color.parseColor(你的十六进制颜色换全项);
}
颜色[colors.length-1] = Color.parseColor(你的十六进制颜色换最后条目); set.setColors(颜色);
对了,还有,它在的文档。
基本上你可以设置图表中的每个酒吧单独的颜色。这是此刻有点不方便,因为在你的情况下,你必须设置每种颜色为红色,而最后一种颜色绿色。
我在努力改进这一点。
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.
EDIT: 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();
}
}
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);
Yes, there is, it's in the documentation.
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".
I'm working on improving that.
这篇关于MPAndroidChart是有办法对不同的酒吧设置不同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!