【问题标题】:mpandroidchart bar chart, how to know the x value from y value?mpandroidchart 条形图,如何从 y 值知道 x 值?
【发布时间】:2022-01-22 23:03:38
【问题描述】:

我的问题是如何知道或定义 y 轴或 x 轴的另一个(耦合)值。 我的代码:

  formatterValueY = new ValueFormatter() {
            @Override
            public String getAxisLabel(float valueY, AxisBase axis) {
                //how to find the valueX of valueY ??
                // need it to return string 
                //note: I know the search solution(data loop 
               //search),
                // it is useless if there is two equal values y1=y2
      
                //this is example of what I wanna achieve,
                // this is simple example,
                
               float x = findTheRealXOfY(valueY);
               //or
               //float x = findTheRealXOfY(valueY,axis);

               if(x %2==0)
               {
                    return "Pair:"+valueY;
                 }
               else{
                  return "inPair:"+valueY;
                }

                
            }
        }


        YAxis yAxis = myBarChar.getYAxis();
        yAxis.setValueFormatter(formatterValueY);

所以如果有办法使用 valueY 和轴找到 Y 的真实对值。

这是我想要的一个例子

【问题讨论】:

  • 你想用这个实现什么?也许如果你添加了一个更具体的例子来说明你为什么想要这个人可以建议方法。通常,图表上的 y 轴标签与 x 值无关。
  • @TylerV 完成了,看看这个简单的例子。
  • 你一开始没有回答为什么要这样做。
  • 一个给定的 y 值可能有多个 x 值(或者根本没有,轴标签与数据点无关)。为什么要将它放在 y 轴标签中呢?也许添加一张您希望图表看起来像什么以及为什么需要它的简单图片?如果您想在单个点上添加标签,有一种更好的方法可以做到这一点,而不是使用 y 轴标签。
  • @MehdiS 你只是在告诉“你想做什么”,而不是“为什么”你想做什么。您提出的解决方案可能是也可能不是解决实际问题的最佳方法。这就是为什么人们会问“为什么”你想这样做。可能还有其他方法可以满足您的实际需求。见What is the XY problem?

标签: android mpandroidchart


【解决方案1】:

example of what I needed,这是最好的解决方案,没有过度编码

这是代码:

BarData data = new BarData(colorBarSet);

data.setValueFormatter(new ValueFormatter() {

                @Override
                public String getBarLabel(BarEntry barEntry) {
                    Log.e("TAG", "this is the X value: " + barEntry.getX());
                    Log.e("TAG", "this is the Y value: " + barEntry.getY());

                // this will return "Girl" for red color with x value=1, 
                // value of x is the index of the color in bar chart
                return getMajeureVoteOfColor(barEntry.getX);
                }
            });

【讨论】:

    【解决方案2】:

    根据您想要这些标签的位置,您有几个选择。如果要沿图表底部设置标签,可以使用 x 轴上的IndexAxisValueFormatter(labels)。如果要在条形顶部添加标签,可以在BarDataSet 上设置ValueFormatter。这是一个示例,它根据条形高度在下轴上设置“L”或“M”,在条形标签本身上设置“Less”或“More” - 也取决于条形高度。

    设置x轴标签,相关命令为

    xaxis.valueFormatter = IndexAxisValueFormatter(labels)
    

    对于条形标签

    barDataSet.valueFormatter = object : ValueFormatter() {
        override fun getBarLabel(barEntry: BarEntry?): String {
            var label = ""
            barEntry?.let { e ->
                // here you can access both x and y
                label = if( e.y > 25 ) {
                    "More"
                } else {
                    "Less"
                }
            }
            return label
        }
    }
    

    如果你想要条形上方的标签,你可以使用

    barChart.setDrawValueAboveBar(true)
    

    完整示例

    val barChart = findViewById<BarChart>(R.id.bar_chart)
    
    // the values on your bar chart - wherever they may come from
    val yVals = listOf(10f, 20f, 30f, 40f, 50f, 60f)
    
    val valueSet1 = ArrayList<BarEntry>()
    val labels = ArrayList<String>()
    
    // You can create your data sets and labels at the same time - 
    // then you have access to the x and y values and can 
    // determine what labels to set
    for (i in yVals.indices) {
        val yVal = yVals[i]
        val entry = BarEntry(i.toFloat(), yVal)
        valueSet1.add(entry)
    
        if( yVal > 20) {
            labels.add("M")
        }
        else {
            labels.add("L")
        }
    }
    
    val dataSets: MutableList<IBarDataSet> = ArrayList()
    val barDataSet = BarDataSet(valueSet1, " ")
    
    barChart.setDrawBarShadow(false)
    barChart.setDrawValueAboveBar(false)
    barChart.description.isEnabled = false
    barChart.setDrawGridBackground(false)
    
    
    val xaxis: XAxis = barChart.xAxis
    xaxis.setDrawGridLines(false)
    xaxis.position = XAxis.XAxisPosition.BOTTOM
    xaxis.granularity = 1f
    xaxis.isGranularityEnabled = true
    xaxis.setDrawLabels(true)
    xaxis.setDrawAxisLine(false)
    xaxis.valueFormatter = IndexAxisValueFormatter(labels)
    xaxis.textSize = 15f
    
    val yAxisLeft: YAxis = barChart.axisLeft
    yAxisLeft.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART)
    yAxisLeft.setDrawGridLines(true)
    yAxisLeft.setDrawAxisLine(true)
    yAxisLeft.isEnabled = true
    yAxisLeft.textSize = 15f
    
    barChart.axisRight.isEnabled = false
    barChart.extraBottomOffset = 10f
    barChart.extraLeftOffset = 10f
    
    val legend: Legend = barChart.legend
    legend.isEnabled = false
    
    
    barDataSet.color = Color.CYAN
    barDataSet.valueTextSize = 15f
    
    // You can also set a value formatter on the bar data set itself (and enable
    // setDrawValues). In this you get a BarEntry which has both the x position
    // and bar height.
    barDataSet.setDrawValues(true)
    barDataSet.valueFormatter = object : ValueFormatter() {
        override fun getBarLabel(barEntry: BarEntry?): String {
            var label = ""
            barEntry?.let { e ->
                // here you can access both x and y
                // values with e.x and e.y
                label = if( e.y > 25 ) {
                    "More"
                } else {
                    "Less"
                }
            }
            return label
        }
    }
    dataSets.add(barDataSet)
    
    val data = BarData(dataSets)
    barChart.data = data
    barChart.invalidate()
    

    【讨论】:

    • 哦,非常感谢,但已经找到了:D,但非常感谢。你在 Kotlin 中的解决方案我在 Java 中。错误是我使用“getAxisLabel”而不是“getBarLabel”,tnx dude
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    相关资源
    最近更新 更多