mirror of
https://github.com/dzeiocom/charts.git
synced 2025-04-22 18:52:08 +00:00
feat: Add Grouped Charts support (#17)
This commit is contained in:
parent
e3d82026db
commit
b1b209035c
21
library/src/main/java/com/dzeio/charts/ChartType.kt
Normal file
21
library/src/main/java/com/dzeio/charts/ChartType.kt
Normal file
@ -0,0 +1,21 @@
|
||||
package com.dzeio.charts
|
||||
|
||||
enum class ChartType {
|
||||
|
||||
/**
|
||||
* Basic Chart where items go over the other
|
||||
*/
|
||||
BASIC,
|
||||
|
||||
/**
|
||||
* each series are next to each other nicely
|
||||
*/
|
||||
GROUPED,
|
||||
|
||||
/**
|
||||
* WILL NOT DO ANYTHING CURRENTLY
|
||||
*
|
||||
* Each series are stacked over the other one
|
||||
*/
|
||||
STACKED
|
||||
}
|
@ -21,6 +21,8 @@ class ChartView @JvmOverloads constructor(context: Context?, attrs: AttributeSet
|
||||
const val TAG = "Charts/ChartView"
|
||||
}
|
||||
|
||||
override var type: ChartType = ChartType.BASIC
|
||||
|
||||
override var debug: Boolean = false
|
||||
|
||||
override val xAxis = XAxis(this)
|
||||
|
@ -6,6 +6,11 @@ import com.dzeio.charts.series.SerieInterface
|
||||
|
||||
interface ChartViewInterface {
|
||||
|
||||
/**
|
||||
* Chart Type
|
||||
*/
|
||||
var type: ChartType
|
||||
|
||||
/**
|
||||
* Make the whole view in debug mode
|
||||
*
|
||||
|
@ -5,6 +5,7 @@ import android.graphics.Color
|
||||
import android.graphics.Paint
|
||||
import android.graphics.Rect
|
||||
import android.graphics.RectF
|
||||
import com.dzeio.charts.ChartType
|
||||
import com.dzeio.charts.ChartViewInterface
|
||||
import com.dzeio.charts.Entry
|
||||
import kotlin.math.roundToInt
|
||||
@ -51,11 +52,13 @@ class XAxis(
|
||||
}
|
||||
|
||||
override fun getPositionOnRect(entry: Entry, drawableSpace: RectF): Double {
|
||||
return translatePositionToRect(entry.x, drawableSpace)
|
||||
val result = drawableSpace.width() * (entry.x - x) / getDataWidth()
|
||||
if (view.type == ChartType.GROUPED) {
|
||||
val serie = view.series.find { it.entries.contains(entry) }
|
||||
val index = view.series.indexOf(serie)
|
||||
return result + getEntryWidth(drawableSpace) * index + spacing / 2 * index
|
||||
}
|
||||
|
||||
fun translatePositionToRect(value: Double, drawableSpace: RectF): Double {
|
||||
return drawableSpace.width() * (value - x) / getDataWidth()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun getXMax(): Double {
|
||||
@ -123,8 +126,15 @@ class XAxis(
|
||||
}
|
||||
}
|
||||
|
||||
return (drawableSpace.width() * smallest / getDataWidth() - spacing)
|
||||
val result = (drawableSpace.width() * smallest / getDataWidth() - spacing)
|
||||
.coerceIn(1.0, drawableSpace.width().toDouble())
|
||||
|
||||
// handle grouped series
|
||||
if (view.type == ChartType.GROUPED) {
|
||||
return result / view.series.size - spacing / 2 * (view.series.size - 1)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
override fun getDataWidth(): Double {
|
||||
|
@ -6,6 +6,7 @@ import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.dzeio.charts.ChartType
|
||||
import com.dzeio.charts.ChartView
|
||||
import com.dzeio.charts.Entry
|
||||
import com.dzeio.charts.series.BarSerie
|
||||
@ -30,21 +31,27 @@ class MainFragment : Fragment() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
binding.chart1.apply {
|
||||
binding.chartGrouped.apply {
|
||||
// setup the Serie
|
||||
val serie = BarSerie(this)
|
||||
val serie1 = BarSerie(this)
|
||||
val serie2 = BarSerie(this)
|
||||
|
||||
// transform the chart into a grouped chart
|
||||
type = ChartType.GROUPED
|
||||
|
||||
// utils function to use Material3 auto colors
|
||||
materielTheme(this, requireView())
|
||||
serie2.barPaint.color = Color.RED
|
||||
|
||||
// give the serie it's entries
|
||||
serie.entries = generateRandomDataset(10)
|
||||
serie1.entries = generateRandomDataset(10)
|
||||
serie2.entries = generateRandomDataset(10)
|
||||
|
||||
// refresh the Chart
|
||||
refresh()
|
||||
}
|
||||
|
||||
binding.chart2.apply {
|
||||
binding.chartLine.apply {
|
||||
// setup the Serie
|
||||
val serie = LineSerie(this)
|
||||
|
||||
@ -58,7 +65,21 @@ class MainFragment : Fragment() {
|
||||
refresh()
|
||||
}
|
||||
|
||||
binding.chart3.apply {
|
||||
binding.chartBar.apply {
|
||||
// setup the Serie
|
||||
val serie = BarSerie(this)
|
||||
|
||||
// utils function to use Material3 auto colors
|
||||
materielTheme(this, requireView())
|
||||
|
||||
// give the serie its entries
|
||||
serie.entries = generateRandomDataset(10)
|
||||
|
||||
// refresh the Chart
|
||||
refresh()
|
||||
}
|
||||
|
||||
binding.chartCustomization.apply {
|
||||
// setup the Series
|
||||
val serie1 = BarSerie(this)
|
||||
val serie2 = LineSerie(this)
|
||||
|
6
sample/src/main/res/drawable/shape_divider.xml
Normal file
6
sample/src/main/res/drawable/shape_divider.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<size
|
||||
android:height="16dp"
|
||||
android:width="0dp" />
|
||||
</shape>
|
@ -6,35 +6,39 @@
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ui.MainFragment">
|
||||
|
||||
<!-- <ScrollView-->
|
||||
<!-- android:layout_width="match_parent"-->
|
||||
<!-- android:layout_height="match_parent">-->
|
||||
<ScrollView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:showDividers="middle"
|
||||
android:divider="@drawable/shape_divider"
|
||||
android:padding="16dp">
|
||||
|
||||
<com.dzeio.charts.ChartView
|
||||
android:id="@+id/chart1"
|
||||
android:id="@+id/chart_grouped"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="200dp" />
|
||||
|
||||
<com.dzeio.charts.ChartView
|
||||
android:id="@+id/chart3"
|
||||
android:layout_marginVertical="16dp"
|
||||
android:id="@+id/chart_customization"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="200dp" />
|
||||
|
||||
<com.dzeio.charts.ChartView
|
||||
android:id="@+id/chart2"
|
||||
android:id="@+id/chart_bar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="200dp" />
|
||||
|
||||
<com.dzeio.charts.ChartView
|
||||
android:id="@+id/chart_line"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="200dp" />
|
||||
</LinearLayout>
|
||||
|
||||
<!-- </ScrollView>-->
|
||||
|
||||
|
||||
</ScrollView>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
x
Reference in New Issue
Block a user