【问题标题】:MPAndroidChart, set different color to bar in a bar chart based on y axis valuesMPAndroidChart,根据y轴值在条形图中设置不同的颜色
【发布时间】:2015-07-05 11:59:03
【问题描述】:

我正在使用 MPAndroid 图表绘制条形图。现在我所有的条都具有相同的颜色,但我希望基于 Y 轴值的条具有不同的颜色,例如值 >100。颜色 = 红色,如下图所示。那可能吗?有人请帮助我。

问候。

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以重写 BarDataSet 类来实现这一点

    public class MyBarDataSet extends BarDataSet {
    
    
        public MyBarDataSet(List<BarEntry> yVals, String label) {
            super(yVals, label);
        }
    
        @Override
        public int getColor(int index) {
            if(getEntryForXIndex(index).getVal() < 95) // less than 95 green
                return mColors.get(0);
            else if(getEntryForXIndex(index).getVal() < 100) // less than 100 orange
                return mColors.get(1);
            else // greater or equal than 100 red
                return mColors.get(2);
        }
    
    }
    

    你需要像这样定义你的颜色:

    MyBarDataSet set = new MyBarDataSet(yVals, "");
    set.setColors(new int[]{ContextCompat.getColor(context, R.color.green), 
                    ContextCompat.getColor(context, R.color.orange), 
                    ContextCompat.getColor(context, R.color.red)});
    ArrayList<BarDataSet> dataSets = new ArrayList<>();
    dataSets.add(set);
    
    
    BarData data = new BarData(xVals, dataSets);
    

    【讨论】:

    • 确保在数据集上,您不调用 set.setColor(..)。这将使 getColor 永远不会被调用。
    • @m4n3k4s 这是条形图最需要的解决方案,其中条形颜色取决于 Y 值。谢谢 + 10。
    • BarData 数据 = 新 BarData(xVals, set);?因为 dataSets 没有分配。
    • 我正在使用这个,getApplicationContext().getResources().getColor(android.R.color.holo_orange_dark),但这似乎已被弃用...是否有另一行代码我可以用来用。。。来代替?我只是想获得不同的颜色,如黄色,但使用我附加的代码行不起作用。
    • 嗨@neo,您应该使用 ContextCompat.getColor(context, R.color.color_name)。同时我更新了答案
    【解决方案2】:

    您可以从this link 找到有关在 MPAndroidChart 中设置颜色的文档

        LineDataSet setComp1 = new LineDataSet(valsComp1, "Company 1");
      // sets colors for the dataset, resolution of the resource name to a "real" color is done internally
      setComp1.setColors(new int[] { R.color.red1, R.color.red2, R.color.red3, R.color.red4 }, Context);
    
      LineDataSet setComp2 = new LineDataSet(valsComp2, "Company 2");
      setComp2.setColors(new int[] { R.color.green1, R.color.green2, R.color.green3, R.color.green4 }, Context);
    

    【讨论】:

    • 很高兴听到这个消息:)
    • @AlexChengalan 我们可以根据 MPAndroidChart 中的条目(y 轴)值设置条形颜色吗?如果没有,请推荐支持相同的库。
    • @Alex Chengalan 我们可以根据 MPAndroidChart 中的条目(y 轴)值设置条形颜色吗?如果没有,请推荐支持相同的库。
    • 如何根据数据值设置颜色。例如,如果数据小于 50,我会给出绿色,如果数据更多,我会给出红色
    【解决方案3】:

    赞许@m4n3k4s 的回答。只是想对这些行添加一些说明,因为我花了一段时间才弄清楚:

    set.setColors(new int[]{ContextCompat.getColor(context, R.color.green), 
                ContextCompat.getColor(context, R.color.orange), 
                ContextCompat.getColor(context, R.color.red)});
    

    在找到this thread 之前,我不知道context 是什么或如何使用它。我将上面的行替换为:

    barDataSet.setColors(
                    ContextCompat.getColor(barchart.getContext(), R.color.green),
                    ContextCompat.getColor(barchart.getContext(), R.color.yellow),
                    ContextCompat.getColor(barchart.getContext(), R.color.red)
            );
    

    假设BarChartView 的子类,而getContext()View 类的方法。

    所以我的完整代码如下所示,其中 KpBarDataSet 是我的覆盖 BarDataSet 的类,而 DateKp 是自定义类。

    BarChart barchart = (BarChart) findViewById(R.id.barchart);
    
        List<BarEntry> entries = new ArrayList<BarEntry>();
        List<String> dateLabels = new ArrayList<>();
    
        int i = 0;
        for (DateKp day : data) {
            // turn your data into Entry objects
            entries.add(new BarEntry(i, day.getValueY()));
            dateLabels.add(day.getLabel());
            i++;
        }
    
        KpBarDataSet barDataSet = new KpBarDataSet(entries, null);
    
        barDataSet.setColors(
                ContextCompat.getColor(barchart.getContext(), R.color.green),
                ContextCompat.getColor(barchart.getContext(), R.color.yellow),
                ContextCompat.getColor(barchart.getContext(), R.color.red)
        );
    

    最后,R.color.greenR.color.red 等将不存在,除非您在 /res/values/colors.xml 中定义它们。

    希望这会有所帮助。

    【讨论】:

      【解决方案4】:

      我已经为 3 种颜色做了这个,而且效果很好

      if(floatArray.get(i) >= 0.0 && floatArray.get(i) <= max1)
                  {
                      barColorArray1[i] = Color.rgb(0, 128, 0);           
                  }
                  else if(floatArray.get(i) > max1 && floatArray.get(i) <= max2)
                  {
                      barColorArray1[i] = Color.rgb(247, 207, 19);                
                  }
                  else if(floatArray.get(i) > max2 )
                  {
                      barColorArray1[i] = Color.rgb(199, 0, 15);              
                  }
      

      在此我创建了一个具有整数值颜色的数组。 最后在你的BarDataSet 中传递这个barDataSet1.setColors(barColorArray1);

      【讨论】:

      • 这是应用自定义条件的最简单方法。
      【解决方案5】:

      这里是 MPAndroidChart:v3.0.2 的类

      类 MyBarDataSet 扩展 BarDataSet {
          public MyBarDataSet(List<BarEntry> yVals, String label) {
              super(yVals, label);
          }
      
          @Override
          public int getColor(int index) {
              if(getEntryForIndex(index).getY() < 140)
                  return mColors.get(0);
              else if(getEntryForIndex(index).getY() > 145)
                  return mColors.get(1);
              else
                  return mColors.get(2);
          }
      
      }
      

      【讨论】:

      • getEntryForIndex 是从哪里来的??
      • 从这里“扩展 BarDataSet”
      【解决方案6】:

      更新

      表格m4n3k4回答

      public class MyBarDataSet extends BarDataSet {
      
      
          public MyBarDataSet(List<BarEntry> yVals, String label) {
              super(yVals, label);
          }
      
           @Override
          public int getColor(int index) {
              if(getEntryForIndex(index).getY() ==40) // less than 95 green
                  return mColors.get(0);
              else if(getEntryForIndex(index).getY() ==30) // less than 100 orange
                  return mColors.get(1);
              else // greater or equal than 100 red
                  return mColors.get(2);
          }
      }
      

      在Java代码中

       set1.setColors(ContextCompat.getColor(StepCountsActivity.this, R.color.purple),
                      ContextCompat.getColor(StepCountsActivity.this, R.color.light_purple),
                      ContextCompat.getColor(StepCountsActivity.this, R.color.blue));
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-07
        • 2018-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-23
        • 2019-05-06
        • 1970-01-01
        相关资源
        最近更新 更多