gpt4 book ai didi

android - MPAndroidChart折线图底部有空格,不是0开头

转载 作者:行者123 更新时间:2023-11-30 05:07:29 25 4
gpt4 key购买 nike

在我的应用程序中,我使用 MPAndroidChart 库在折线图中显示数据

当它的数据集在图表中时,底部有一些额外的空间显示,

在我的数据中,最小值是“0”,但在显示在 0-10 之间的图表中不是以 0 开头

当我点击那个地方显示 0 值的图表时,你可以在图像中显示...但是 0 在底部,为什么它不能从那个地方开始?

enter image description here

这是我的代码

private void DisplayLineChart(TotalSalesLineData data, LineChart chart, boolean b) {

// background color
chart.setBackgroundColor(Color.WHITE);


// disable description text
chart.getDescription().setEnabled(false);

// enable touch gestures
chart.setTouchEnabled(true);

// set listeners
chart.setOnChartValueSelectedListener(this);
chart.setDrawGridBackground(false);

// create marker to display box when values are selected
MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.custom_marker_view);

// Set the marker to the chart
mv.setChartView(chart);
chart.setMarker(mv);


YAxis yAxisRight = chart.getAxisRight();
yAxisRight.setEnabled(false);

YAxis leftAxis = chart.getAxisLeft();
leftAxis.setTextColor(Color.BLACK);
leftAxis.setAxisMaximum(50f);
leftAxis.setAxisMinimum(0f);

leftAxis.setDrawGridLines(true);
leftAxis.setGranularityEnabled(true);

//chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
XAxis xAxis = chart.getXAxis();
xAxis.setDrawGridLinesBehindData(false);
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setAxisMinimum(0f);
xAxis.setSpaceMin(0f);
xAxis.setSpaceMax(0f);


setData(chart, data);

// draw points over time
chart.animateX(0);
}

这里是设置数据的方法

 private void setData(LineChart chart, TotalSalesLineData range) {

ArrayList<Entry> values1 = new ArrayList<>();

for (int i = 0; i < range.getCurrentYear().size(); i++) {
// float val = (float) (Math.random() * (range / 2f)) + 50;
values1.add(new Entry(i, range.getCurrentYear().get(i)));
}

ArrayList<Entry> values2 = new ArrayList<>();

for (int i = 0; i < range.getPreviousYear().size(); i++) {
// float val = (float) (Math.random() * range) + 450;
values2.add(new Entry(i, range.getPreviousYear().get(i)));
}


LineDataSet set1, set2;

if (chart.getData() != null && chart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet) chart.getData().getDataSetByIndex(0);
set2 = (LineDataSet) chart.getData().getDataSetByIndex(1);

set1.setValues(values1);
set2.setValues(values2);

chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
// create a dataset and give it a type

set1 = new LineDataSet(values1, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));

set1.setAxisDependency(YAxis.AxisDependency.RIGHT);
set1.setColor(ColorTemplate.getHoloBlue());
set1.setCircleColor(R.color.colorAccent);
set1.setLineWidth(2f);
set1.setCircleRadius(3f);
// set1.setFillAlpha(65);
set1.setFillColor(ColorTemplate.getHoloBlue());
// set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setHighLightColor(R.color.colorAccent);
set1.setDrawCircleHole(false);
//set1.setFillFormatter(new MyFillFormatter(0f));
//set1.setDrawHorizontalHighlightIndicator(false);
//set1.setVisible(false);
//set1.setCircleHoleColor(Color.WHITE);

// create a dataset and give it a type
set2 = new LineDataSet(values2, String.valueOf(CommonMethods.getPreviousYear()));
set2.setAxisDependency(YAxis.AxisDependency.RIGHT);
set2.setColor(Color.GRAY);
set2.setCircleColor(Color.BLACK);
set2.setLineWidth(2f);
set2.setCircleRadius(3f);
// set2.setFillAlpha(65);
set2.setFillColor(Color.RED);
set2.setDrawCircleHole(false);
set2.setHighLightColor(Color.rgb(244, 117, 117));
//set2.setFillFormatter(new MyFillFormatter(900f));

// create a data object with the data sets
LineData data = new LineData(set1, set2);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(0f);
// set data
chart.setData(data);
}
}

这是我的回复

{
"error": false,
"data": {
"currentYear": [
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"previousYear": [
0,
0,
0,
0,
48970,
0,
21854.81,
242827.07,
29070.48,
1196,
3588,
441.76
]
},
"message": "Successfully"
}

最佳答案

我通过设置左右轴的最小值来解决。

Kotlin 代码:

barChart.axisLeft.axisMinimum = 0f
barChart.axisRight.axisMinimum = 0f

关于android - MPAndroidChart折线图底部有空格,不是0开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54323917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com