Skip to content

Commit 23c081b

Browse files
authored
Merge pull request #104 from AkYML/feature/bubbleChart
Feature/bubble chart
2 parents da9603a + ac4480a commit 23c081b

File tree

15 files changed

+1114
-12
lines changed

15 files changed

+1114
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package co.yml.charts.common.components.accessibility
2+
3+
import androidx.compose.foundation.background
4+
import androidx.compose.foundation.clickable
5+
import androidx.compose.foundation.layout.Box
6+
import androidx.compose.foundation.layout.Column
7+
import androidx.compose.foundation.layout.Row
8+
import androidx.compose.foundation.layout.Spacer
9+
import androidx.compose.foundation.layout.padding
10+
import androidx.compose.foundation.layout.size
11+
import androidx.compose.foundation.layout.width
12+
import androidx.compose.material3.Text
13+
import androidx.compose.runtime.Composable
14+
import androidx.compose.ui.Alignment
15+
import androidx.compose.ui.Modifier
16+
import androidx.compose.ui.graphics.Color
17+
import androidx.compose.ui.semantics.semantics
18+
import androidx.compose.ui.unit.dp
19+
import androidx.compose.ui.unit.sp
20+
21+
/**
22+
* Composable to display each Bubble point data for given bubble chart.
23+
* @param axisLabelDescription: Axis label description.
24+
* @param pointDescription: Details of each point on the line.
25+
* @param bubbleColor: Color of each bubble.
26+
*/
27+
@Composable
28+
fun BubblePointInfo(
29+
axisLabelDescription: String,
30+
pointDescription: String,
31+
bubbleColor: Color
32+
) {
33+
// Merge elements below for accessibility purposes
34+
Row(modifier = Modifier
35+
.padding(start = 10.dp, end = 10.dp)
36+
.clickable { }
37+
.semantics(mergeDescendants = true) {}, verticalAlignment = Alignment.CenterVertically
38+
) {
39+
Text(axisLabelDescription, fontSize = 12.sp)
40+
Spacer(modifier = Modifier.width(5.dp))
41+
Column(
42+
modifier = Modifier
43+
.weight(1f)
44+
.padding(5.dp)
45+
) {
46+
Row(verticalAlignment = Alignment.CenterVertically) {
47+
Box(
48+
modifier = Modifier
49+
.padding(5.dp)
50+
.background(bubbleColor)
51+
.size(20.dp)
52+
)
53+
Text(pointDescription, fontSize = 12.sp)
54+
}
55+
}
56+
}
57+
}

YChartsLib/src/main/java/co/yml/charts/common/utils/DataUtils.kt

+74
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import co.yml.charts.common.model.Point
1111
import co.yml.charts.ui.barchart.models.BarChartType
1212
import co.yml.charts.ui.barchart.models.BarData
1313
import co.yml.charts.ui.barchart.models.GroupBar
14+
import co.yml.charts.ui.bubblechart.model.Bubble
15+
import co.yml.charts.ui.bubblechart.model.BubbleStyle
1416
import co.yml.charts.ui.piechart.models.PieChartData
1517
import kotlin.math.sin
1618
import kotlin.random.Random
@@ -35,6 +37,77 @@ object DataUtils {
3537
return list
3638
}
3739

40+
/**
41+
* Returns list of points
42+
* @param listSize: Size of total number of points needed.
43+
* @param start: X values to start from. ex: 50 to 100
44+
* @param maxRange: Max range of Y values
45+
*/
46+
fun getRandomPoints(listSize: Int, start: Int = 0, maxRange: Int): List<Point> {
47+
val list = arrayListOf<Point>()
48+
for (index in 0 until listSize) {
49+
list.add(
50+
Point(
51+
index.toFloat(),
52+
(start until maxRange).random().toFloat()
53+
)
54+
)
55+
}
56+
return list
57+
}
58+
59+
/**
60+
* Returns list of points
61+
* @param listSize: Size of total number of points needed.
62+
* @param start: X values to start from. ex: 50 to 100
63+
* @param maxRange: Max range of Y values
64+
*/
65+
fun getBubbleChartDataWithGradientStyle(
66+
points: List<Point>,
67+
minDensity: Float = 10F,
68+
maxDensity: Float = 100F
69+
): List<Bubble> {
70+
val list = arrayListOf<Bubble>()
71+
points.forEachIndexed { index, point ->
72+
val bubbleColor = if (index % 2 == 0) Color.Red else Color.Blue
73+
list.add(
74+
Bubble(
75+
center = point,
76+
density = (minDensity.toInt() until maxDensity.toInt()).random().toFloat(),
77+
bubbleStyle = BubbleStyle(gradientColors = listOf(bubbleColor, Color.White), useGradience = true)
78+
)
79+
)
80+
81+
}
82+
return list
83+
}
84+
85+
/**
86+
* Returns list of points
87+
* @param listSize: Size of total number of points needed.
88+
* @param start: X values to start from. ex: 50 to 100
89+
* @param maxRange: Max range of Y values
90+
*/
91+
fun getBubbleChartDataWithSolidStyle(
92+
points: List<Point>,
93+
minDensity: Float = 10F,
94+
maxDensity: Float = 100F
95+
): List<Bubble> {
96+
val list = arrayListOf<Bubble>()
97+
points.forEachIndexed { index, point ->
98+
val bubbleColor = if (index % 2 == 0) Color.Red else Color.Blue
99+
list.add(
100+
Bubble(
101+
center = point,
102+
density = (minDensity.toInt() until maxDensity.toInt()).random().toFloat(),
103+
bubbleStyle = BubbleStyle(solidColor = bubbleColor, useGradience = false)
104+
)
105+
)
106+
107+
}
108+
return list
109+
}
110+
38111
/**
39112
* Return the sample bar chart data
40113
* @param duration : Duration of the wave in seconds
@@ -71,6 +144,7 @@ object DataUtils {
71144
"%.2f".format(Random.nextDouble(1.0, maxRange.toDouble())).toFloat()
72145
)
73146
}
147+
74148
BarChartType.HORIZONTAL -> {
75149
Point(
76150
"%.2f".format(Random.nextDouble(1.0, maxRange.toDouble())).toFloat(),

0 commit comments

Comments
 (0)