Gradient color in graph bar using MPAndroidChart - android

I would like to make a graph like this :
The problem is I don't know how to set a gradient color like this using MPAndroidChart. Maybe I should use an other library ?
Maybe it's better to use progressbar with transparent color (and gradient background) ?
This is my code :
val entries = ArrayList<BarEntry>()
entries.add(BarEntry(1.toFloat(), 43.toFloat()))
entries.add(BarEntry(2.toFloat(), 3.toFloat()))
entries.add(BarEntry(3.toFloat(), 13.toFloat()))
entries.add(BarEntry(4.toFloat(), 41.toFloat()))
entries.add(BarEntry(5.toFloat(), 22.toFloat()))
entries.add(BarEntry(6.toFloat(), 11.toFloat()))
entries.add(BarEntry(7.toFloat(), 13.toFloat()))
entries.add(BarEntry(8.toFloat(), 99.toFloat()))
entries.add(BarEntry(9.toFloat(), 67.toFloat()))
entries.add(BarEntry(10.toFloat(), 3.toFloat()))
entries.add(BarEntry(11.toFloat(), 56.toFloat()))
entries.add(BarEntry(12.toFloat(), 88.toFloat()))
val dataSet = BarDataSet(entries, "Label")
chart.data = BarData(dataSet)

This worked for me:
dataset.setGradientColor(Color.parseColor("#00FF5722"),Color.parseColor("#FFFF5722"));

You can use setGradientFill() method which will accept n number of color.
int[] colors = { getResources().getColor(R.color.menu_text),
getResources().getColor(android.R.color.white) };
float[] index = { 0, 1 };
dataset.setGradientFill(colors, index);
for more you can refer this. Hope it will help you!!

Related

Change color from one value to another based on offset

I want to change 1 color value which will be represented by offset 1.0 to another color value represented by offset 0.0.
I don't want to use ValueAnimator because animation will be made by myself (function to change color is called everytime offset changes based on scroll listener) and I don't need to really "animate" it by duration.
I tried this:
val color = ArgbEvaluator().evaluate(offset, R.color.start, R.color.end)
But color is type of Any and not color I can set as backgroundTint for example.
You're on the right track, you're just missing a cast.
val color = ArgbEvaluator().evaluate(offset, startColor, endColor) as Int
myView.setBackgroundColor(color)
ArgbEvaluator#evaluate takes an
Object: A 32-bit int value representing colors in the separate bytes of the parameter
as its second and third arguments. What you are using are the resource id of the colors you want. You will need to translate those resource ids into 32-bit colors.
val startColor = ResourcesCompat.getColor(resources, R.color.start, null)
val endColor = ResourcesCompat.getColor(resources, R.color.end, null)
val color = ArgbEvaluator().evaluate(offset, startColor, endColor)
binding.textView.setBackgroundColor(color as Int)

How to convert String to int Color value

I want to set background with gradient. This's my code:
val startColor = "0xFFAC235E"
val endColor = "0xFF640C35"
val gradient = GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT,
intArrayOf(
startColor.toInt(),
endColor.toInt()
)
)
view.background = gradient
and it through an exception:
java.lang.NumberFormatException: For input string: "0xFFAC235E"
If I replace startColor = 0xFFAC235E, the code above work fine. But that's not what I want.
I need put color as param String. Is there anyway to convert it?
Try replacing 0x with #.
For ex:
startColor.replace("0x", "#")
Generally we define colors with hex color codes. So, I think this will work for you.
Edit
You have to parse the color string to convert it into integer.
Color.parseColor(startColor.replace("0x", "#"))

setStrokeColor not working programmatically

I'm trying to set my color for the outline of my button, but I don't get it to work
I'm using material button and when I use
button.setStrokeColorResource(Color.parseColor(#e4dcd4))
is not working and tells me this
Expected a color resource id (R.color.) but received an RGB integer
I tried almost everything I could found about in stack, but I can't get it to set this strokeColor programmatically
Edit
Almost all setColors use #ColorInt , but this strokeColor uses #ColorRes, which is not working for me, also there is setStrokeColor
public void setStrokeColor(#Nullable ColorStateList strokeColor) {
if (isUsingOriginalBackground()) {
materialButtonHelper.setStrokeColor(strokeColor);
}
}
But I can't get it to work either.
It worked like this
val colorInt = Color.parseColor("#e4dcd4")
val csl = ColorStateList.valueOf(colorInt)
my_button.strokeColor = csl
You might try this
button.setStrokeColor(ContextCompat.getColor(this, R.color.your_color_xml));
Other way you can do is
ShapeDrawable gradientDrawable = (ShapeDrawable)button.getBackground();
gradientDrawable.setStroke(2, your_color);
Also as #Gabriele said you can get an int as a color as :
//From RGB
int colorRGB = Color.rgb(255,0,0);
//From HEX String
int colorHEX = Color.parseColor("#FF11AA");
You have to set the width of the stroke because the default value is 0.
<Button
app:strokeWidth="2dp"
../>
button.strokeColor = ColorStateList.valueOf(Color.parseColor("#e4dcd4"))
or
// if color define in color.xml
button.strokeColor = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.yourColorCOde))
// if you have different state and you want to set programmatically then do as :-
var states = arrayOf(
intArrayOf(R.attr.state_enabled),
intArrayOf(-R.attr.state_enabled),
intArrayOf(-R.attr.state_checked),
intArrayOf(R.attr.state_pressed)
)
// Color list define respect of state
var colors = intArrayOf(
Color.BLACK,
Color.RED,
Color.GREEN,
Color.BLUE
)
// Set stroke color
button.strokeColor = ColorStateList(states, colors)

How to custom a chart like image below using MP Android Chart library?

I am using MPAndroidChart, this is the chart I need to create:
Hi everyone. Please help me. I'm trying to customize this chart but I can't make it come be better. have some problems:
How to add new axis at bottom chart?
How to hide horizontal lines?
List<String> xAxisValues; // YOUR DATA HERE
...
IAxisValueFormatter xAxisFormatter = new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
return xAxisValues.get((int) value);
}
};
...
XAxis xAxis = chart.getXAxis();
xAxis.setTextColor(Color.BLUE));
xAxis.setValueFormatter(xAxisFormatter);
I beleive the above line will match your need, below stuff will be useful may be,
LineDataSet set1 = new LineDataSet(values, title);
set1.setColor(Color.RED); // Your line color
set1.addColor(Color.GRAY); // Your Blue
set1.setCircleColor(Color.BLUE);
set1.setLineWidth(3f); // Increase here for line width
set1.setCircleRadius(10f);
set1.setDrawCircleHole(true);
set1.setValueTextSize(9f);
set1.setDrawFilled(true);
set1.setFormLineWidth(10f);
set1.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
set1.setFormSize(15.f);
Fine tune according to your need in above code see comments
The below two lines are important for the smooth line and filled color
set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
set1.setFillColor(Color.BLUE); // Change your color
For the horizontal issue, please refere to this page
Here, I think it will be :
YourObjettoDrawVariable.getAxisRight().setDrawLabels(false);
But, it is always better to show your code for ccontext purpose.
I hope it's help.

Unable to set color for BarChart in MPAndroidChart

I want to set a specific color for a bar in BarChart from MPAndroidChart. I do everything according to a documentation, but the color isn't changing.
Here's my code:
barChart = (BarChart) findViewById(R.id.bar_chart);
List<BarEntry> entries = new ArrayList<BarEntry>();
entries.add(new BarEntry(1.0f, 10.0f)); //tmp values
BarDataSet dataSet = new BarDataSet(entries, "bars");
dataSet.setColor(R.color.red); //color from resourses
BarData barData = new BarData(dataSet);
barChart.setData(barData);
barChart.invalidate();
The funny thing is that before I tried to change the color of the bar, the bar was blue, after I tried to change its color, it became grey (no matter, what color it must be). I don't understand why doesn't the color change.
I also tried to override getColor method in the BarDataSet class, but result is the same -- bar is grey.
Change this line,
dataSet.setColor(R.color.red); //resource id of a color
to,
dataSet.setColor(getResources().getColor(R.color.red)); //resolved color
When you call setColor you need to pass in an integer that represents an RGB triple. R.color.red is not an RGB triple but instead an integer that represents a resource in R.java.
See this question for more about the difference between a resource id and a resolved color.
BarDataSet dataSet = new BarDataSet(entries, "bars");
dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
If you want to set color, you can create and array of color. Then set that array to Bardataset.
Example is given Below.
int[] colors = {Color.rgb(153, 193, 12), Color.rgb(179, 130, 76)};
Bardataset.setColors(colors);
I think you should write Your code in this sequence.
Take a look below :
BarDataSet dataSet = new BarDataSet(entries, "bars");
dataSet.setColor(Color.parseColor("#104E78"));
BarData barData = new BarData(dataSet);
Try it.
We can change color this way.
bardataset.setColors(new int[]{getResources().getColor(R.color.red)});

Categories

Resources