mirror of
https://github.com/dzeiocom/charts.git
synced 2025-04-23 02:52:10 +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"
|
const val TAG = "Charts/ChartView"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override var type: ChartType = ChartType.BASIC
|
||||||
|
|
||||||
override var debug: Boolean = false
|
override var debug: Boolean = false
|
||||||
|
|
||||||
override val xAxis = XAxis(this)
|
override val xAxis = XAxis(this)
|
||||||
|
@ -6,6 +6,11 @@ import com.dzeio.charts.series.SerieInterface
|
|||||||
|
|
||||||
interface ChartViewInterface {
|
interface ChartViewInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Chart Type
|
||||||
|
*/
|
||||||
|
var type: ChartType
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make the whole view in debug mode
|
* Make the whole view in debug mode
|
||||||
*
|
*
|
||||||
|
@ -5,6 +5,7 @@ import android.graphics.Color
|
|||||||
import android.graphics.Paint
|
import android.graphics.Paint
|
||||||
import android.graphics.Rect
|
import android.graphics.Rect
|
||||||
import android.graphics.RectF
|
import android.graphics.RectF
|
||||||
|
import com.dzeio.charts.ChartType
|
||||||
import com.dzeio.charts.ChartViewInterface
|
import com.dzeio.charts.ChartViewInterface
|
||||||
import com.dzeio.charts.Entry
|
import com.dzeio.charts.Entry
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
@ -51,11 +52,13 @@ class XAxis(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun getPositionOnRect(entry: Entry, drawableSpace: RectF): Double {
|
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) }
|
||||||
fun translatePositionToRect(value: Double, drawableSpace: RectF): Double {
|
val index = view.series.indexOf(serie)
|
||||||
return drawableSpace.width() * (value - x) / getDataWidth()
|
return result + getEntryWidth(drawableSpace) * index + spacing / 2 * index
|
||||||
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getXMax(): Double {
|
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())
|
.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 {
|
override fun getDataWidth(): Double {
|
||||||
|
@ -6,6 +6,7 @@ import android.view.LayoutInflater
|
|||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
|
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.series.BarSerie
|
import com.dzeio.charts.series.BarSerie
|
||||||
@ -30,21 +31,27 @@ class MainFragment : Fragment() {
|
|||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
|
||||||
binding.chart1.apply {
|
binding.chartGrouped.apply {
|
||||||
// setup the Serie
|
// 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
|
// utils function to use Material3 auto colors
|
||||||
materielTheme(this, requireView())
|
materielTheme(this, requireView())
|
||||||
|
serie2.barPaint.color = Color.RED
|
||||||
|
|
||||||
// give the serie it's entries
|
// give the serie it's entries
|
||||||
serie.entries = generateRandomDataset(10)
|
serie1.entries = generateRandomDataset(10)
|
||||||
|
serie2.entries = generateRandomDataset(10)
|
||||||
|
|
||||||
// refresh the Chart
|
// refresh the Chart
|
||||||
refresh()
|
refresh()
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.chart2.apply {
|
binding.chartLine.apply {
|
||||||
// setup the Serie
|
// setup the Serie
|
||||||
val serie = LineSerie(this)
|
val serie = LineSerie(this)
|
||||||
|
|
||||||
@ -58,7 +65,21 @@ class MainFragment : Fragment() {
|
|||||||
refresh()
|
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
|
// setup the Series
|
||||||
val serie1 = BarSerie(this)
|
val serie1 = BarSerie(this)
|
||||||
val serie2 = LineSerie(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"
|
android:layout_height="match_parent"
|
||||||
tools:context=".ui.MainFragment">
|
tools:context=".ui.MainFragment">
|
||||||
|
|
||||||
<!-- <ScrollView-->
|
<ScrollView
|
||||||
<!-- android:layout_width="match_parent"-->
|
android:layout_width="match_parent"
|
||||||
<!-- android:layout_height="match_parent">-->
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:showDividers="middle"
|
||||||
|
android:divider="@drawable/shape_divider"
|
||||||
android:padding="16dp">
|
android:padding="16dp">
|
||||||
|
|
||||||
<com.dzeio.charts.ChartView
|
<com.dzeio.charts.ChartView
|
||||||
android:id="@+id/chart1"
|
android:id="@+id/chart_grouped"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="200dp" />
|
android:layout_height="200dp" />
|
||||||
|
|
||||||
<com.dzeio.charts.ChartView
|
<com.dzeio.charts.ChartView
|
||||||
android:id="@+id/chart3"
|
android:id="@+id/chart_customization"
|
||||||
android:layout_marginVertical="16dp"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="200dp" />
|
android:layout_height="200dp" />
|
||||||
|
|
||||||
<com.dzeio.charts.ChartView
|
<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_width="match_parent"
|
||||||
android:layout_height="200dp" />
|
android:layout_height="200dp" />
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<!-- </ScrollView>-->
|
</ScrollView>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
x
Reference in New Issue
Block a user