【问题标题】:MPAndroidChart is there a way to set different colors for different bars?MPAndroidChart 有没有办法为不同的条设置不同的颜色?
【发布时间】:2015-04-26 15:59:34
【问题描述】:

我在我的应用程序中使用 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);

【问题讨论】:

    标签: android view graph charts mpandroidchart


    【解决方案1】:

    是的,有,在documentation

    基本上,您可以为图表中的每个条形设置单独的颜色。目前有点不方便,因为在您的情况下,您必须将每种颜色设置为“红色”,将最后一种颜色设置为“绿色”。

    我正在努力改进它。

    【讨论】:

    • 非常感谢!我已经编辑了我的问题以显示解决方案。
    • @Philipp Jahoda for (int i = 0; i
    • @Phillipp Jahoda 你能解释更多关于在 MPAndroidChart 中为单个条设置单独的颜色吗?
    猜你喜欢
    • 2018-04-07
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 2011-07-31
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多