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