I'm using MPAndroidChart to draw Chart in my App. I have a problem when my Chart display data with value from negavite to positive.I reseached example of this lib by using BarChart but have not solution.
E.g. Column 1 will draw from -10 to 30. (has minValue and maxValue)
I don't know how to do that, BarChart is always draw from zero line.
I am setting like below:
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setAxisMaximum(100);
leftAxis.setAxisMinimum(-100);
float[] data = new float[]{-10f, 30f};
values.add(new BarEntry(1, data));
But have not effect. Please help me.
I want to draw chart like this image bellow:
Related
I've got the following line chart using MPAndroidChart:
Print of the first X position
Print of the last X position
I'm using the following method:
chart.setVisibleXRangeMaximum(5)
Which shows only 5 values on the viewport, the rest of them will be shown by scrolling.
I'm also using this method:
chart.setViewPortOffsets(0,25,0,40);
Which removes the left and right offset to make the chart covers whole screen and sets top and bottom offsets to have a space between the container it's placed in.
The problem is that both, first and last position got hidden into the device border. I think if there was a way to set a padding to these individual values, the problem could be fixed.
I tried to use some methods to change the space or offset of the X-axis, but neither of them worked.
I'm trying to create a chart like this where the entire filled dataset covers whole screen, but the first and last values are placed with a padding between the border of the device and the value itself.
The entire code of my chart is down bellow:
LineChart chart = view.findViewById(R.id.chartCalendar);
// Setting up the data
List<Entry> entries = new ArrayList<Entry>();
entries.add(new Entry(1,1200));
.....
// Creating dataset
LineDataSet dataSet = new LineDataSet(entries, "Label"); // add entries to dataset
// Dataset styling
dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
dataSet.setDrawFilled(true);
dataSet.setFillColor(ContextCompat.getColor(this.getActivity(), R.color.whiteBlue));
dataSet.setColor(ContextCompat.getColor(this.getActivity(), R.color.darkBlue));
dataSet.setLineWidth(2); // Line border thickness
dataSet.setCircleColor(ContextCompat.getColor(this.getActivity(), R.color.darkBlue)); // Border circle values color
// Setting up LineChart
LineData lineData = new LineData(dataSet);
// Styling chart
chart.setData(lineData);
chart.getAxisLeft().setDrawGridLines(false);
chart.getAxisRight().setDrawGridLines(false);
chart.getXAxis().setDrawGridLines(false);
chart.getXAxis().setAxisMaximum(31);
chart.setVisibleXRangeMaximum(5); // Show maximum 10 before scrolling
chart.getLegend().setEnabled(false);
chart.setDrawBorders(false);// Hide the description
chart.getXAxis().setTextColor(ContextCompat.getColor(this.getActivity(), R.color.white)); // Change X-axis label color
YAxis yAxis = chart.getAxisLeft();
yAxis.setTextColor(ContextCompat.getColor(this.getActivity(), R.color.white)); // change Y-axis label color
yAxis.setAxisLineColor(ContextCompat.getColor(this.getActivity(), R.color.darkBlue));
yAxis.setTextColor(ContextCompat.getColor(this.getActivity(), R.color.white));
// remove axis
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setEnabled(false);
YAxis rightAxis = chart.getAxisRight();
rightAxis.setEnabled(false);
// X axis to bottom
chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
chart.getLineData().setValueTextColor(ContextCompat.getColor(this.getActivity(), R.color.white)); // Change value color
chart.getXAxis().setGranularity(1);
chart.setViewPortOffsets(0,25,0,40);
chart.getDescription().setEnabled(false);
// Refresh graph
chart.animateX(500);
Do you guys have any idea of how to fix it? Thanks!
To avoid the first and last values from being clipped, use your_chart.xAxis.setAvoidFirstLastClipping(true)
I am using MPAndroidChart library version 2.2.4. My requirement is I want to set three marker lines in a BarChart with values "Minimum", "Average" and "Maximum" like in the image below, but I can't find any solution for this.
In MPAndroidChart 3.x.x what you are asking for is called a LimitLine
There is an example of how to consume it in the sample project:
LimitLine ll1 = new LimitLine(150f, "Upper Limit");
ll1.setLineWidth(4f);
ll1.enableDashedLine(10f, 10f, 0f);
ll1.setLabelPosition(LimitLabelPosition.RIGHT_TOP);
ll1.setTextSize(10f);
ll1.setTypeface(tf);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.removeAllLimitLines(); // reset all limit lines to avoid overlapping lines
leftAxis.addLimitLine(ll1);
If you require a customized limit line, you will have to look at the instructions in this question here
In my application I want to show float data (from JSON) to a y-axis in a pie chart. It is taking the values but converting into percentage values. It not showing my data, it is showing percentage values. How do I show floats?
Comment these lines:
mChart.setUsePercentValues(false);
mChart.highlightValues(null);
dataSet.setValueFormatter(new PercentFormatter());
I'm trying to make a line chart using MPAndroidChart that has a Y Axis with a min value of 0 and a max of 255, but I haven't been able to get the max value to show up when viewing the chart. It always shows the top value as something like 240 or 250. I've tried a couple variations of setting the label count but that hasn't helped either. Here is what I'm doing right now.
YAxis yAxis = lineChart.getAxisLeft();
yAxis.setAxisMaxValue(255);
yAxis.setAxisMinValue(0);
yAxis.setLabelCount(5);
I'm setting these before I add the data to the chart.
I´m using MPAndroidChart 2.0.9 version. I would like to set labels on y-axis from 0 to 100 and display always this range, but i can´t find the chart.setYRange() method.
The range can be customized via the YAxis class.
Here is the documentation: https://github.com/PhilJay/MPAndroidChart/wiki/YAxis-%28YLabels%29
YAxis y = chart.getAxisLeft();
y.setAxisMaxValue(100);
y.setAxisMinValue(0);
You can try to write this:
yAxis.setLabelCount(x)
This code should give you labels in steps of x, assuming you set a min and max value.
Example of my code:
YAxis y = mChart.getAxisLeft();
y.setAxisMaxValue(100);
y.setAxisMinValue(0);
y.setLabelCount(6);
This will give me labels from 0 to 100 in 6 steps, so: 0-20-40-60-80-100.