How can I see mpandroid barchart x-axis label completely? - android

I draw barchart in my Android project, but I can't see x-axis label completely about g, j....etc.
These letters are not shown completely. The letters cut off.
I chaged many things like margin, padding.... but I can not change x-axis label.
My code is below.
binding.contributorsChart.apply {
setDrawBarShadow(false)
setTouchEnabled(false)
setPinchZoom(false)
setDrawGridBackground(false)
description.isEnabled = false
legend.isEnabled = false
axisRight.isEnabled = false
axisLeft.apply {
axisMinimum = 0f
granularity = 10f
setDrawLabels(true)
setDrawGridLines(true)
setDrawAxisLine(true)
axisLineColor = ContextCompat.getColor(context, R.color.black)
gridColor = ContextCompat.getColor(context, R.color.black)
textColor = ContextCompat.getColor(context, R.color.black)
textSize = 13f
}
xAxis.apply {
yOffset = 5f
isEnabled = true
position = XAxis.XAxisPosition.BOTTOM
granularity = 1f
setDrawAxisLine(true)
setDrawGridLines(false)
textColor = ContextCompat.getColor(context, R.color.black)
textSize = 12f
valueFormatter = MyXAxisFormatter(contributors)
}
animateY(500) // 밑에서부터 올라오는 애니매이션 적용
}
data.apply {
setValueTextSize(12f)
barWidth = 0.3f
}
binding.contributorsChart.data = data
binding.contributorsChart.invalidate()
binding.contributorsChart.visibility = View.VISIBLE
I changed margin, padding, ... etc.

Related

How can change bar view in bar chart in MPAndroidChart?

I need change bars view in barchart in MPAndroidChart like this... that's how it should be Is it possible to set custom bar drawable, or just smooth the corners, or something like this?
Now I have this
My code:
fun barChart(
activity: Activity,
chart: BarChart,
entries: ArrayList<BarEntry>,
columnsNames: ArrayList<String>,
colors: ArrayList<Int>,
) {
val colorsTemplate = intArrayOf(
Color.rgb(74, 160, 150), Color.rgb(74, 160, 150), Color.rgb(74, 160, 150),
Color.rgb(74, 160, 150), Color.rgb(74, 160, 150)
)
val colors: ArrayList<Int> = ArrayList()
for (color in colorsTemplate) {
colors.add(color)
}
val dataSet = BarDataSet(entries, "")
val decimalFormat = DecimalFormat("0.##")
dataSet.setValueFormatter(object : ValueFormatter() {
override fun getFormattedValue(value: Float): String {
return decimalFormat.format(value)
}
})
dataSet.colors = colors
val data = BarData(dataSet)
data.barWidth = 0.5f
data.setDrawValues(true)
chart.setData(data)
chart.setAutoScaleMinMaxEnabled(true)
val xAxis = chart.getXAxis()
xAxis.valueFormatter = IndexAxisValueFormatter(columnsNames)
xAxis.position = (XAxis.XAxisPosition.BOTTOM)
xAxis.setDrawGridLines(false)
xAxis.setDrawAxisLine(false)
xAxis.granularity = 1f
xAxis.labelCount = columnsNames!!.size
xAxis.labelRotationAngle = 360f
chart.legend.isEnabled = false
chart.description.isEnabled = false
chart.axisRight.isEnabled = false
chart.axisLeft.labelCount = 3
chart.axisLeft.axisMinimum = 0f
chart.animateY(1000)
chart.invalidate()
}

How can I make a progress bar around rounded rectangular shape in android

I want to make a progress bar similar to this one with jetpack compose by canvas, and I've made some Shape, but I'm having trouble implementing the progress section.
I add a shape in drawWithContent of Box
Image
val path = Path()
path.moveTo(x = startOffset.x, y = startOffset.y)
path.addRoundRect(
RoundRect(
left = 0F,
top = 0F,
right = this.size.width,
bottom = this.size.height,
cornerRadius = CornerRadius(x = 16.dp.toPx(), y = 16.dp.toPx())
)
)
clipPath(
path = path,
clipOp = ClipOp.Intersect
) {
drawPath(
path = path,
style = Stroke(5.dp.toPx(), 16.dp.toPx(), cap = StrokeCap.Round),
brush = SolidColor(Color.Red),
)
}
You can try something like that
#Composable
private fun Q74121342() {
val density = LocalDensity.current
val strokeWidth = remember { with(density) { 2.dp.toPx() } } // Convert to px for much needed stroke width
val rotateTransition = rememberInfiniteTransition()
val rotateAnimateValue = rotateTransition.animateValue(
initialValue = 0f,
targetValue = 360f,
typeConverter = Float.VectorConverter,
animationSpec = infiniteRepeatable(
animation = tween(
durationMillis = 1000,
easing = LinearEasing
),
repeatMode = RepeatMode.Restart
)
) // Creating infinite animation. You can replace with finitive one and indicate current progress outside of composable
Image(
modifier = Modifier
.size(96.dp)
.padding(2.dp)
.drawBehind {
rotate(
degrees = rotateAnimateValue.value
) {
drawArc(
color = Color.Red,
startAngle = 0f,
sweepAngle = 180f,
useCenter = false,
style = Stroke(
width = strokeWidth,
cap = StrokeCap.Round,
join = StrokeJoin.Round,
)
) // Can be replaced with path but don't forget change image shape
}
} // Applying our custom paint
.padding(2.dp)
.clip(CircleShape), // Crop original image to much circle shape
painter = painterResource(R.drawable.img_profile),
contentDescription = null,
contentScale = ContentScale.Crop
)
}
You can see the demo on gist

multiple linedataset label didn't show MPAndroidChart

I'm trying to show the label of my LineDataSet, but it didn't show on my android chart. It should be 4 labels at the bottom, with 4 different colors and names. I simply the code so I can post it here
Here is my code :
val chartLineDataSet = LineDataSet(arrayData, "Phasa A")
chartLineDataSet.mode = LineDataSet.Mode.CUBIC_BEZIER
chartLineDataSet.color = Color.RED
chartLineDataSet.circleRadius = 5f
chartLineDataSet.lineWidth = 2f
chartLineDataSet.setCircleColor(Color.RED)
chartLineDataSet.valueTextColor = Color.RED
chartLineDataSet.valueTextSize = 10F
chartLineDataSet.setDrawValues(false)
chartLineDataSet.setDrawCircleHole(false)
chartLineDataSet.valueFormatter = MyValueFormatter()
val lineDatas: MutableList<ILineDataSet> = ArrayList()
lineDatas.add(chartLineDataSet)
binding.lineChart.let {
it.description.isEnabled = false
it.xAxis.position = XAxis.XAxisPosition.BOTTOM
it.data = LineData(lineDatas)
it.animateXY(100, 500)
it.xAxis.setDrawGridLines(true)
it.axisLeft.textColor = Color.BLACK
it.axisRight.textColor = Color.BLACK
it.xAxis.textColor = Color.BLACK
it.legend.textColor = Color.BLACK
it.axisRight.isEnabled = false
it.axisLeft.isEnabled = true
it.isClickable = false
it.setVisibleXRangeMaximum(7F)
}
and the result for the code above
Did I miss something ?
Nevermind, I forgot to set enabled / true the binding.chart.legend. Finally, the labels are already shown.

Circular draggable slider in jetpack compose

I need to prepare a UI with a circular slider where I can pass value or move drag the pointer to slide in the slider similar to the screenshot attached.
How can I achieve it?
You can check out this quick solution. I have 0 degrees at the right but you can reverse the quadrants to suit your needs.
#Composable
fun Content() {
var radius by remember {
mutableStateOf(0f)
}
var shapeCenter by remember {
mutableStateOf(Offset.Zero)
}
var handleCenter by remember {
mutableStateOf(Offset.Zero)
}
var angle by remember {
mutableStateOf(20.0)
}
Canvas(
modifier = Modifier
.fillMaxSize()
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
handleCenter += dragAmount
angle = getRotationAngle(handleCenter, shapeCenter)
change.consumeAllChanges()
}
}
.padding(30.dp)
) {
shapeCenter = center
radius = size.minDimension / 2
val x = (shapeCenter.x + cos(Math.toRadians(angle)) * radius).toFloat()
val y = (shapeCenter.y + sin(Math.toRadians(angle)) * radius).toFloat()
handleCenter = Offset(x, y)
drawCircle(color = Color.Black.copy(alpha = 0.10f), style = Stroke(20f), radius = radius)
drawArc(
color = Color.Yellow,
startAngle = 0f,
sweepAngle = angle.toFloat(),
useCenter = false,
style = Stroke(20f)
)
drawCircle(color = Color.Cyan, center = handleCenter, radius = 60f)
}
}
private fun getRotationAngle(currentPosition: Offset, center: Offset): Double {
val (dx, dy) = currentPosition - center
val theta = atan2(dy, dx).toDouble()
var angle = Math.toDegrees(theta)
if (angle < 0) {
angle += 360.0
}
return angle
}
Check out the solution in this code on Github.
Just replace the angle value in Canvas.

How to outline text in Jetpack Compose

I have a Jetpack Compose Text() element that I'd like to outline in black like so .
Anyone know how to do this?
I've tried using the border() modifier, but that just adds a border around the rectangular area containing the text. I've also tried overlaying two text elements, but that doesn't quite work either.
The 1.4.0-alpha01 introduced a DrawStyle parameter to TextStyle function that enables drawing outlined text.
You can use something like:
Text(
text = "Sample",
style = TextStyle.Default.copy(
fontSize = 64.sp,
drawStyle = Stroke(
miter = 10f,
width = 5f,
join = StrokeJoin.Round
)
)
)
Before 1.4.0-alpha01 you can use a Canvas and the drawIntoCanvas function.
Something like:
Canvas(
modifier = Modifier.fillMaxSize(),
onDraw = {
drawIntoCanvas {
it.nativeCanvas.drawText(
"Sample",
0f,
120.dp.toPx(),
textPaintStroke
)
it.nativeCanvas.drawText(
"Sample",
0f,
120.dp.toPx(),
textPaint
)
}
}
)
with these Paint:
val textPaintStroke = Paint().asFrameworkPaint().apply {
isAntiAlias = true
style = android.graphics.Paint.Style.STROKE
textSize = 64f
color = android.graphics.Color.BLACK
strokeWidth = 12f
strokeMiter= 10f
strokeJoin = android.graphics.Paint.Join.ROUND
}
val textPaint = Paint().asFrameworkPaint().apply {
isAntiAlias = true
style = android.graphics.Paint.Style.FILL
textSize = 64f
color = android.graphics.Color.WHITE
}

Categories

Resources