【问题标题】:How to remove labels on the chart when using MPAndroidChart Library使用 MPAndroidChart 库时如何删除图表上的标签
【发布时间】:2022-11-28 00:53:04
【问题描述】:

我正在尝试使用 MPAndroidChartLibrary 绘制图表并绘制图表。我在图中有两个问题:

  • 我不想在图表上添加值
  • 我不希望在擦洗时出现水平线和垂直线,而是希望在图表线上出现气泡或小圆圈(不确定这是否可能)。

请帮助我,我可以在MPAndroidChart中使用什么api来实现这个

【问题讨论】:

    标签: android mpandroidchart


    【解决方案1】:

    你在这里有几个不同的问题

    如何摆脱价值观?

    要隐藏在每个点绘制的值,您可以在数据集上调用setDrawValues(false)

    如何摆脱突出显示的线条?

    要在单击某个点时禁用水平和垂直黄线,您可以使用lineSet.isHighlightEnabled = false,但是如果您还想使用图表弹出窗口,您应该保持启用状态,但使用setDrawVerticalHighlightIndicatorsetDrawHorizontalHighlightIndicator隐藏线

    单击某个点时如何添加显示自定义视图的弹出窗口?

    要添加popup view,您需要定义一个类来扩展MarkerView,这是您的弹出视图的 XML 布局,然后将其设置在图表上。我在下面展示了一个示例,该示例显示了 TextView 中的值 - 如果您只想要一个自定义图标,您可以将其放在 XML 中。

    一个完整的例子

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    
        val lineChart = findViewById<LineChart>(R.id.line_chart)
    
        val x = listOf(1f,  2f,  3f,  4f,  5f,  6f)
        val y = listOf(10f, 20f, 30f, 40f, 50f, 60f)
        val entries = x.zip(y).map { Entry(it.first, it.second) }
    
        val lineSet = LineDataSet(entries, "test")
        lineSet.circleRadius = 10f
    
        // Disable drawn values
        lineSet.setDrawValues(false)
    
        // Disable highlight lines
        // lineSet.isHighlightEnabled = false // this works, but also disables the marker
    
        // instead, leave highlighting enabled but disable the lines
        lineSet.setDrawVerticalHighlightIndicator(false)
        lineSet.setDrawHorizontalHighlightIndicator(false)
        
        // Add a popup when you click a point
        lineChart.marker = ChartPopup(this)
        
        val xaxis: XAxis = lineChart.xAxis
        xaxis.setDrawGridLines(false)
        xaxis.position = XAxis.XAxisPosition.BOTTOM
        xaxis.granularity = 1f
        xaxis.isGranularityEnabled = true
        xaxis.setDrawLabels(true)
        xaxis.setDrawAxisLine(false)
        xaxis.setDrawGridLines(false)
    
        val yAxisLeft: YAxis = lineChart.axisLeft
        yAxisLeft.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART)
        yAxisLeft.setDrawGridLines(false)
        yAxisLeft.setDrawAxisLine(false)
        yAxisLeft.isEnabled = true
        yAxisLeft.textSize = 15f
    
        lineChart.axisRight.isEnabled = false
        lineChart.extraBottomOffset = 10f
        lineChart.extraLeftOffset = 10f
        lineChart.description.isEnabled = false
        lineChart.setDrawGridBackground(false)
        
        lineChart.data = LineData(lineSet)
    
        lineChart.invalidate()
    }
    

    图表弹出类和布局

    class ChartPopup(context: Context) : MarkerView(context, R.layout.chart_popup) {
    
        private var popupText: TextView? = null
    
        override fun refreshContent(e: Entry, highlight: Highlight?) {
            popupText?.text = "${e.y}"
            super.refreshContent(e, highlight)
        }
    
        private var customOffset: MPPointF? = null
        override fun getOffset(): MPPointF {
            if (customOffset == null) {
                // center the marker horizontally and vertically
                customOffset = MPPointF((-(width / 2)).toFloat(), (-height).toFloat())
            }
            return customOffset!!
        }
    
        init {
            popupText = findViewById(R.id.popup_text)
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/popup_text"
            android:padding="4dp"
            android:textSize="16sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    这是更改前后的样子,选择“30”点:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      相关资源
      最近更新 更多