I am using MP chart in my project. I am using multiple dataset bar chart. I ma facing following issues:
Below is my code:
tco_chart?.description?.isEnabled = false
tco_chart?.setPinchZoom(false)
tco_chart?.setDrawBarShadow(false)
val leftYAxis = tco_chart?.axisLeft
leftYAxis?.setDrawGridLines(false)
leftYAxis?.axisMinimum = 0f
tco_chart?.setDrawGridBackground(false)
val mv = MyMarkerView(this, R.layout.custom_marker_view)
mv.setChartView(tco_chart) // For bounds control
tco_chart.setMarker(mv)
val legend = tco_chart?.legend
legend?.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM)
legend?.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT)
legend?.setOrientation(Legend.LegendOrientation.HORIZONTAL)
legend?.setYOffset(0f)
legend?.setXOffset(10f)
legend?.setYEntrySpace(0f)
legend?.setTextSize(5f)
val axisLabels = ArrayList<String>()
axisLabels.add("Year 1")
axisLabels.add("Year 2")
axisLabels.add("Year 3")
axisLabels.add("Year 4")
axisLabels.add("Year 5")
val xAxis = tco_chart?.xAxis
xAxis?.granularity = 1f
xAxis?.position = XAxis.XAxisPosition.BOTTOM
xAxis?.setCenterAxisLabels(true)
xAxis?.setDrawLabels(true)
xAxis?.valueFormatter = IndexAxisValueFormatter(axisLabels)
val insuranceCostEntry = ArrayList<BarEntry>()
val insuranceCostDataModel = tcoResultModel.insurance_cost
if (insuranceCostDataModel != null) {
insuranceCostEntry.add(BarEntry(0f, insuranceCostDataModel.first_year.toFloat()))
insuranceCostEntry.add(BarEntry(1f, insuranceCostDataModel.second_year.toFloat()))
insuranceCostEntry.add(BarEntry(2f, insuranceCostDataModel.third_year.toFloat()))
insuranceCostEntry.add(BarEntry(3f, insuranceCostDataModel.fourth_year.toFloat()))
insuranceCostEntry.add(BarEntry(4f, insuranceCostDataModel.fifth_year.toFloat()))
}
val fuelCostEntry = ArrayList<BarEntry>()
val fuelCostDataModel = tcoResultModel.fuel_cost
if (fuelCostDataModel != null) {
fuelCostEntry.add(BarEntry(0f, fuelCostDataModel.first_year.toFloat()))
fuelCostEntry.add(BarEntry(1f, fuelCostDataModel.second_year.toFloat()))
fuelCostEntry.add(BarEntry(2f, fuelCostDataModel.third_year.toFloat()))
fuelCostEntry.add(BarEntry(3f, fuelCostDataModel.fourth_year.toFloat()))
fuelCostEntry.add(BarEntry(4f, fuelCostDataModel.fifth_year.toFloat()))
}
val serviceCostEntry = ArrayList<BarEntry>()
val serviceCostDataModel = tcoResultModel.service_cost
if (serviceCostDataModel != null) {
serviceCostEntry.add(BarEntry(0f, serviceCostDataModel.first_year.toFloat()))
serviceCostEntry.add(BarEntry(1f, serviceCostDataModel.second_year.toFloat()))
serviceCostEntry.add(BarEntry(2f, serviceCostDataModel.third_year.toFloat()))
serviceCostEntry.add(BarEntry(3f, serviceCostDataModel.fourth_year.toFloat()))
serviceCostEntry.add(BarEntry(4f, serviceCostDataModel.fifth_year.toFloat()))
}
val tyreChangeCostEntry = ArrayList<BarEntry>()
val tyreChangeCostDataModel = tcoResultModel.tyre_change_cost
if (tyreChangeCostDataModel != null) {
tyreChangeCostEntry.add(BarEntry(0f, tyreChangeCostDataModel.first_year.toFloat()))
tyreChangeCostEntry.add(BarEntry(1f, tyreChangeCostDataModel.second_year.toFloat()))
tyreChangeCostEntry.add(BarEntry(2f, tyreChangeCostDataModel.third_year.toFloat()))
tyreChangeCostEntry.add(BarEntry(3f, tyreChangeCostDataModel.fourth_year.toFloat()))
tyreChangeCostEntry.add(BarEntry(4f, tyreChangeCostDataModel.fifth_year.toFloat()))
}
val depreciationCostEntry = ArrayList<BarEntry>()
val depriciationCostDataModel = tcoResultModel.depreciation_cost
if (depriciationCostDataModel != null) {
depreciationCostEntry.add(BarEntry(0f, depriciationCostDataModel.first_year.toFloat()))
depreciationCostEntry.add(BarEntry(1f, depriciationCostDataModel.second_year.toFloat()))
depreciationCostEntry.add(BarEntry(2f, depriciationCostDataModel.third_year.toFloat()))
depreciationCostEntry.add(BarEntry(3f, depriciationCostDataModel.fourth_year.toFloat()))
depreciationCostEntry.add(BarEntry(4f, depriciationCostDataModel.fifth_year.toFloat()))
}
val year1Set: BarDataSet
val year2Set: BarDataSet
val year3Set: BarDataSet
val year4Set: BarDataSet
val year5Set: BarDataSet
var data = tco_chart?.data
if (data != null) {
if (data.dataSetCount > 0) {
year1Set = data.getDataSetByIndex(0) as BarDataSet
year1Set.values = insuranceCostEntry
year2Set = data.getDataSetByIndex(1) as BarDataSet
year2Set.values = fuelCostEntry
year3Set = data.getDataSetByIndex(2) as BarDataSet
year3Set.values = tyreChangeCostEntry
year4Set = data.getDataSetByIndex(3) as BarDataSet
year4Set.values = serviceCostEntry
year5Set = data.getDataSetByIndex(4) as BarDataSet
year5Set.values = depreciationCostEntry
data.notifyDataChanged()
tco_chart?.notifyDataSetChanged()
}
} else {
year1Set = BarDataSet(insuranceCostEntry, "Insurance Cost")
year1Set.color = Color.rgb(232, 128, 44)
year2Set = BarDataSet(fuelCostEntry, "Fuel Cost")
year2Set.color = Color.rgb(232, 199, 44)
year3Set = BarDataSet(tyreChangeCostEntry, "Tyre Change Cost")
year3Set.color = Color.rgb(10, 149, 221)
year4Set = BarDataSet(serviceCostEntry, "Service Cost")
year4Set.color = Color.rgb(47, 194, 144)
year5Set = BarDataSet(depreciationCostEntry, "Depriciation Cost")
year5Set.color = Color.rgb(105, 123, 192)
data = BarData(year1Set, year2Set, year3Set, year4Set, year5Set)
data.setValueFormatter(IndexAxisValueFormatter())
tco_chart?.data = data
}
val rightYAxis = tco_chart?.axisRight
rightYAxis?.isEnabled = false
tco_chart?.getBarData()?.setBarWidth(0.1f)
tco_chart?.xAxis?.axisMaximum = (0 + data.getGroupWidth(0.4f, 0.02f) * 5)
tco_chart?.groupBars(0f, 0.4f, 0.02f)
tco_chart?.invalidate()
(if the legend has already been calculated, you will need to call notifyDataSetChanged()to let the changes take effect) Example Legendl= chart.getLegend(); l.setFormSize(10f); // set the size of the legend forms/shapesl.setForm(LegendForm. CIRCLE); // set what type of form/shape should be usedl.setPosition(LegendPosition.
There are various methods that allow you to control the axis range. setStartAtZero (boolean enabled): If this is enabled, this axis will always have it's minimum value at zero (0), no matter which kind of data the chart displays. setAxisMaxValue (float max): Set a custom maximum value for this axis.
Please look for the given answer MPAndroidChart - Legend labels are being cut off. I have already provided the answer according to your problem. Look for given code which will definitely help you. You will have to implement customized legends with their legends colours and labels by following the steps below:
When you have a look at the Legend class you will notice that it has member variables for colors and labels. You can retrieve those arrays ( getColors (), getLegendLabels ()) and use them to be displayed in the ListView. Show activity on this post. Please look for the given answer MPAndroidChart - Legend labels are being cut off.
I had the same issue, you can fix it with use some additional padding for chart. Use this:
chart.setExtraOffsets(5f,5f,5f,15f)
Hope it helps.
You Give Offset to Legend Like ..
val legend = chart.legend
legend.xOffset = 10f
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With