我正在开发一个使用MPAndroidChart的应用。它显示特定月份的某些值。数据是这样的:
1430418600000, 130.279999\n1433097000000, 125.43\n1435689000000, 121.300003\n1438540200000, 112.760002\n1441045800000, 110.300003\n1443637800000, 119.50\n1446402600000, 118.300003\n1448908200000, 105.260002\n1451845800000, 97.339996\n1454265000000, 96.690002\n1456770600000, 108.989998\n1459449000000, 93.739998\n1462127400000, 99.860001\n1464719400000, 95.599998\n1467311400000, 104.209999\n1469989800000, 106.099998\n1472668200000, 113.050003\n1475433000000, 113.540001\n1477938600000, 110.519997\n1480530600000, 115.82\n1483381800000, 121.349998\n1485887400000, 136.990005\n1488306600000, 143.660004\n1491157800000, 143.649994
现在Y-Axis
显示了提供的最大和最小点数,但x轴只显示了6个点。
图表初始化代码:
Cursor cursor = getContentResolver().query(Contract.Quote.makeUriForStock(quote_symbol),
Contract.Quote.QUOTE_COLUMNS.toArray(new String[]{}),
null, null, Contract.Quote.COLUMN_SYMBOL);
if(cursor!=null)
{
cursor.moveToNext();
}
chart = (LineChart) findViewById(R.id.chart);
// no description text
chart.getDescription().setEnabled(false);
// enable touch gestures
chart.setTouchEnabled(true);
chart.setDragDecelerationFrictionCoef(0.9f);
// enable scaling and dragging
chart.setDragEnabled(true);
chart.setScaleEnabled(false);
chart.setDrawGridBackground(false);
chart.setHighlightPerDragEnabled(true);
// set an alternative background color
chart.setBackgroundColor(Color.WHITE);
chart.setViewPortOffsets(0f, 0f, 0f, 0f);
chart.setScaleXEnabled(true);
;
// set data
chart.setData(prepareLineData(cursor));
// do not forget to refresh the chart
chart.invalidate();
chart.animateX(750);
准备图表数据:
private LineData prepareLineData(Cursor cursor)
{
String historyData=cursor.getString(cursor.getColumnIndex(Contract.Quote.COLUMN_HISTORY));
String maxValueString=cursor.getString(cursor.getColumnIndex(Contract.Quote.COLUMN_HISTORY_MAX_VALUE));
String maxDateString=cursor.getString(cursor.getColumnIndex(Contract.Quote.COLUMN_HISTORY_MAX_DATE));
String minValueString=cursor.getString(cursor.getColumnIndex(Contract.Quote.COLUMN_HISTORY_MIN_VALUE));
String minDateString=cursor.getString(cursor.getColumnIndex(Contract.Quote.COLUMN_HISTORY_MIN_DATE));
String[] historyDataArray=historyData.split("\n");
ArrayList<Entry> e1 = new ArrayList<>();
for (int i = 0; i < historyDataArray.length; i++) {
String[] historyDataValues=historyDataArray[i].split(",");
e1.add(new Entry(Float.parseFloat(historyDataValues[0]), Float.parseFloat(historyDataValues[1])));
}
Typeface mTf = Typeface.createFromAsset(this.getAssets(), "OpenSans-Regular.ttf");
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
xAxis.setTypeface(mTf);
xAxis.setTextSize(10f);
xAxis.setTextColor(Color.WHITE);
xAxis.setDrawAxisLine(false);
xAxis.setDrawGridLines(true);
xAxis.setTextColor(Color.rgb(255, 192, 56));
xAxis.setCenterAxisLabels(true);
xAxis.setAxisMaximum(Float.parseFloat(maxDateString));
xAxis.setAxisMinimum(Float.parseFloat(minDateString));
xAxis.setAvoidFirstLastClipping(true);
xAxis.setValueFormatter(new IAxisValueFormatter() {
private SimpleDateFormat mFormat = new SimpleDateFormat("MMM,yy");
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mFormat.format(new Date((long) value));
}
});
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
leftAxis.setTypeface(mTf);
leftAxis.setTextColor(ColorTemplate.getHoloBlue());
leftAxis.setDrawGridLines(false);
leftAxis.setGranularityEnabled(true);
leftAxis.setAxisMinimum(0f);
leftAxis.setAxisMaximum(Float.parseFloat(maxValueString));
leftAxis.setTextColor(Color.rgb(255, 192, 56));
YAxis rightAxis = chart.getAxisRight();
rightAxis.setEnabled(false);
LineDataSet d1 = new LineDataSet(e1, "Price");
d1.setLineWidth(2.5f);
d1.setAxisDependency(YAxis.AxisDependency.LEFT);
d1.setHighLightColor(Color.rgb(244, 117, 117));
d1.setDrawValues(false);
ArrayList<ILineDataSet> sets = new ArrayList<>();
sets.add(d1);
return new LineData(sets);
}
答案 0 :(得分:0)
MPAndroidChart计算要自动显示的标签。
如果要在特定位置显示标签,可以使用库的修改版本。
请参阅以下示例:
leftAxis.setShowSpecificLabelPositions(true);
leftAxis.setSpecificLabelPositions(new float[]{0, 10, 20, 50, 100, 300});
您可以在此处下载图书馆:https://github.com/philippeauriach/MPAndroidChart/tree/custom-labels