Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datalabels are not displayed when many charts are added to the combined chart #5493

Open
Hyjeon830 opened this issue Nov 8, 2024 · 0 comments

Comments

@Hyjeon830
Copy link

Hyjeon830 commented Nov 8, 2024

I added StackedBarChart and LineChart in CombinedChart, but even if setDrawValues is true for all data, the DataLabel values are not displayed.
What is the solution?

Screenshot_20241108_175251

I attach the source code below.

var barEntries = ArrayList()
var linePlanEntries = ArrayList()
var lineTotalEntries = ArrayList()
var lineRateEntries = ArrayList()
var xAxises = ArrayList()
var maxTotalPrice = 0f

    var i = 0f;
    dataMap!!.toSortedMap().forEach { (key, value) ->
        var strKey = key

        if(key.length >= 8) {
            strKey = key.substring(5)
        }
        else if(key.length >= 6) {
            strKey = key.substring(2)
        }

        xAxises.add(strKey)

        barEntries.add(BarEntry(i, floatArrayOf(value.second.dReadyAmount.toFloat() / 10000f, value.second.dCncAmount.toFloat() / 10000f,
            value.second.dCo2Amount.toFloat() / 10000f, value.second.dUvAmount.toFloat() / 10000f)))

        val totalPrice = value.second.dReadyAmount.toFloat() + value.second.dCncAmount.toFloat() + value.second.dCo2Amount.toFloat() + value.second.dUvAmount.toFloat()

        if(maxTotalPrice < totalPrice / 10000f)
        {
            maxTotalPrice = totalPrice / 10000f
        }

        linePlanEntries.add(Entry(i, value.first.toFloat() / 10000f))
        lineTotalEntries.add(Entry(i, totalPrice / 10000f))
        lineRateEntries.add(Entry(i, totalPrice / value.first.toFloat() * 100))

        i++
    }

    val combinedData = CombinedData()
    val barData = BarData()
    val lineData = LineData()

    val barDataSet = BarDataSet(barEntries, "")

    barDataSet.colors =  ColorTemplate.MATERIAL_COLORS.asList()
    barDataSet.valueTextSize = 5f
    barDataSet.stackLabels = arrayOf("A", "B", "C", "D")
    barDataSet.setDrawValues(true)
    barData.addDataSet(barDataSet)
    barData.barWidth = 0.7f

    val linePlanSet = LineDataSet(linePlanEntries, "TOP_A")
    linePlanSet.color = Color.parseColor("#FF0000")
    linePlanSet.setCircleColor(Color.parseColor("#FF0000"))
    linePlanSet.valueTextSize = 5f
    linePlanSet.setDrawValues(true)

    lineData.addDataSet(linePlanSet)

    val lineTotalSet = LineDataSet(lineTotalEntries, "TOP_B")
    lineTotalSet.color = Color.parseColor("#002060")
    lineTotalSet.setCircleColor(Color.parseColor("#002060"))
    lineTotalSet.valueTextSize = 5f
    lineTotalSet.setDrawValues(true)

    lineData.addDataSet(lineTotalSet)

    val lineRateSet = LineDataSet(lineRateEntries, "TOP_C")
    lineRateSet.color = Color.parseColor("#C00000")
    lineRateSet.setCircleColor(Color.parseColor("#C00000"))
    lineRateSet.valueTextSize = 5f
    lineRateSet.setDrawValues(true)
    lineRateSet.axisDependency = YAxis.AxisDependency.RIGHT
    // 값 표시 포맷터 설정
    lineRateSet.valueFormatter = object : ValueFormatter() {
        override fun getFormattedValue(value: Float): String {
            return DecimalFormat("#.##").format(value) + "%"
        }
    }

    lineData.addDataSet(lineRateSet)

    combinedData.setData(barData)
    combinedData.setData(lineData)

    binding.combinedChart.data = combinedData

    binding.combinedChart.setTouchEnabled(false)
    binding.combinedChart.setPinchZoom(false)
    binding.combinedChart.isDragEnabled = false
    binding.combinedChart.isDoubleTapToZoomEnabled = false
    binding.combinedChart.getDescription().setEnabled(false);
    binding.combinedChart.setBackgroundColor(Color.WHITE);
    binding.combinedChart.setDrawGridBackground(false);
    binding.combinedChart.setDrawBarShadow(false);
    binding.combinedChart.setHighlightFullBarEnabled(false);
    binding.combinedChart.setScaleEnabled(false)
    binding.combinedChart.isScrollContainer = false
    binding.combinedChart.setDrawValueAboveBar(false)
    binding.combinedChart.setHighlightFullBarEnabled(false)
    binding.combinedChart.setMinOffset(0f)

    val rightAxis: YAxis = binding.combinedChart.getAxisRight()
    rightAxis.setLabelCount(lineRateEntries.size)
    rightAxis.setDrawGridLines(true)
    rightAxis.granularity = 10f
    rightAxis.axisMinimum = 0f // this replaces setStartAtZero(true)
    rightAxis.axisMaximum = 140f

    val leftAxis: YAxis = binding.combinedChart.getAxisLeft()
    rightAxis.setLabelCount(barEntries.size)
    leftAxis.setDrawGridLines(false)
    leftAxis.axisMinimum = 0f // this replaces setStartAtZero(true)
    leftAxis.axisMaximum = maxTotalPrice * 1.6f

    val xAxis: XAxis = binding.combinedChart.getXAxis()
    xAxis.position = XAxisPosition.BOTTOM
    xAxis.setLabelCount(barEntries.size)
    xAxis.setDrawGridLines(false)
    xAxis.textSize = 7f
    xAxis.axisMinimum = -0.5f
    xAxis.granularity = 1f
    xAxis.spaceMax = 0.5f
    xAxis.setValueFormatter(IndexAxisValueFormatter(xAxises))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant