mirror of
https://github.com/dzeiocom/charts.git
synced 2025-04-22 18:52:08 +00:00
fix: Horizontal Lines not displayed (#46)
This commit is contained in:
parent
cbd5f57f8d
commit
542e3afd12
@ -148,17 +148,16 @@ class YAxis(
|
||||
|
||||
val min = getYMin()
|
||||
val max = getYMax() - min
|
||||
val bottom = space.bottom
|
||||
var maxWidth = 0f
|
||||
|
||||
val increment = space.height() / (labelCount - 1)
|
||||
val valueIncrement = max / (labelCount - 1)
|
||||
for (index in 0 until labelCount) {
|
||||
val value = min + (valueIncrement * index)
|
||||
val text = onValueFormat(min + (valueIncrement * index))
|
||||
textLabel.getTextBounds(text, 0, text.length, rect)
|
||||
maxWidth = maxWidth.coerceAtLeast(rect.width().toFloat())
|
||||
|
||||
val posY = bottom - index * increment
|
||||
val posY = getPositionOnRect(value, space)
|
||||
|
||||
canvas.drawText(
|
||||
text,
|
||||
@ -166,12 +165,13 @@ class YAxis(
|
||||
(posY + rect.height() / 2).coerceAtLeast(rect.height().toFloat()),
|
||||
textLabel
|
||||
)
|
||||
// canvas.drawDottedLine(0f, posY, canvas.width.toFloat(), posY, 40f, linePaint)
|
||||
canvas.drawLine(space.left, posY, space.right - maxWidth - 32f, posY, linePaint)
|
||||
}
|
||||
|
||||
for ((y, settings) in lines) {
|
||||
val pos = ((1 - y - min / (getYMax() - min)) * space.height() + space.top)
|
||||
val pos = getPositionOnRect(y, space)
|
||||
val text = onValueFormat(y)
|
||||
textLabel.getTextBounds(text, 0, text.length, rect)
|
||||
if (settings.dotted) {
|
||||
canvas.drawDottedLine(
|
||||
0f,
|
||||
@ -190,6 +190,12 @@ class YAxis(
|
||||
settings.paint ?: linePaint
|
||||
)
|
||||
}
|
||||
canvas.drawText(
|
||||
text,
|
||||
space.width() - rect.width().toFloat(),
|
||||
(pos + rect.height() / 2).coerceAtLeast(rect.height().toFloat()),
|
||||
textLabel
|
||||
)
|
||||
}
|
||||
|
||||
return maxWidth + 32f
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.dzeio.chartstest.ui
|
||||
|
||||
import android.graphics.Color
|
||||
import android.graphics.Paint
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
@ -9,6 +10,7 @@ import androidx.fragment.app.Fragment
|
||||
import com.dzeio.charts.ChartType
|
||||
import com.dzeio.charts.ChartView
|
||||
import com.dzeio.charts.Entry
|
||||
import com.dzeio.charts.axis.Line
|
||||
import com.dzeio.charts.series.BarSerie
|
||||
import com.dzeio.charts.series.LineSerie
|
||||
import com.dzeio.chartstest.databinding.FragmentMainBinding
|
||||
@ -21,7 +23,8 @@ class MainFragment : Fragment() {
|
||||
private val binding get() = _binding!!
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
_binding = FragmentMainBinding.inflate(inflater, container, false)
|
||||
@ -144,15 +147,21 @@ class MainFragment : Fragment() {
|
||||
labelCount = 11
|
||||
|
||||
// change how labels are displayed
|
||||
onValueFormat = { "${it.roundToInt()}g"}
|
||||
onValueFormat = { "${it.roundToInt()}g" }
|
||||
|
||||
// change labels colors
|
||||
textLabel.color = Color.WHITE
|
||||
|
||||
|
||||
// change line color
|
||||
linePaint.color = Color.WHITE
|
||||
|
||||
// Add horizontal Lines
|
||||
val paint: Paint = Paint(yAxis.linePaint).apply {
|
||||
strokeWidth = 8f
|
||||
}
|
||||
addLine(10f, Line(true, paint))
|
||||
addLine(-10f, Line(true, paint))
|
||||
|
||||
// change the min/max high
|
||||
setYMin(-20f)
|
||||
setYMax(20f)
|
||||
@ -192,10 +201,12 @@ class MainFragment : Fragment() {
|
||||
val dataset: ArrayList<Entry> = arrayListOf()
|
||||
|
||||
for (i in 0 until size) {
|
||||
dataset.add(Entry(
|
||||
i.toDouble(),
|
||||
Random.nextInt(min, max).toFloat()
|
||||
))
|
||||
dataset.add(
|
||||
Entry(
|
||||
i.toDouble(),
|
||||
Random.nextInt(min, max).toFloat()
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
return dataset
|
||||
@ -205,7 +216,6 @@ class MainFragment : Fragment() {
|
||||
* Apply Material3 theme to a [ChartView]
|
||||
*/
|
||||
private fun materielTheme(chart: ChartView, view: View) {
|
||||
|
||||
chart.apply {
|
||||
yAxis.apply {
|
||||
textLabel.color = MaterialColors.getColor(
|
||||
|
Loading…
x
Reference in New Issue
Block a user