MPAndroidChart 有没有办法为不同的条设置不同的颜色?
MPAndroidChart is there a way to set different colors for different bars?
我在我的应用程序中使用 MPAndroidChart,我想知道是否有办法为动态移动条形图中的第一个条设置不同的颜色,如下所示:
随着数据的不断涌入,整个堆栈向左移动的同时动态添加了条形图,有没有办法将第一个条形图的颜色设置为不同的颜色?
提前谢谢你。
编辑和解决方案:
这是我用于向图表添加新条目的代码,它每 500 毫秒左右动态发生一次。
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 方法中添加这段代码:
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);
是的,有,它在 documentation。
基本上您可以为图表中的每个柱设置单独的颜色。目前有点不方便,因为在您的情况下,您必须将每种颜色设置为 "red",将最后一种颜色设置为 "green"。
我正在努力改进它。
我在我的应用程序中使用 MPAndroidChart,我想知道是否有办法为动态移动条形图中的第一个条设置不同的颜色,如下所示:
随着数据的不断涌入,整个堆栈向左移动的同时动态添加了条形图,有没有办法将第一个条形图的颜色设置为不同的颜色? 提前谢谢你。
编辑和解决方案: 这是我用于向图表添加新条目的代码,它每 500 毫秒左右动态发生一次。
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 方法中添加这段代码:
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);
是的,有,它在 documentation。
基本上您可以为图表中的每个柱设置单独的颜色。目前有点不方便,因为在您的情况下,您必须将每种颜色设置为 "red",将最后一种颜色设置为 "green"。
我正在努力改进它。