使用 MPAndroidChart 创建图表
Creating charts using MPAndroidChart
我正在尝试创建如下所示的折线图:
- 我要创建的图表
但我能做的最好的事情是:
- 我的图表
我正在使用 MPAndroidChart。我想在 YAxis 上固定 3 个标签。
并且 XAxis 应该根据我从 API 调用中得到的内容进行标记。
这是我目前的代码:
public void drawLineChart(LineChart mChart, Context context) {
this.mChart = mChart;
this.context = context;
mChart.getDescription().setEnabled(false);
mChart.setTouchEnabled(true); // enable touch gestures
mChart.setDragEnabled(true); // enable scaling and dragging
mChart.setScaleEnabled(true);
mChart.getXAxis().setEnabled(false);
mChart.getAxisLeft().setEnabled(true);
mChart.getAxisRight().setEnabled(false);
mChart.getAxisLeft().setDrawLabels(false);
mChart.getAxisRight().setDrawLabels(false);
mChart.getXAxis().setDrawLabels(false);
mChart.getLegend().setEnabled(false);
mChart.setPinchZoom(true); // if disabled, scaling can be done on x- and y-axis separately
YAxis yAxis = mChart.getAxisLeft();
yAxis.setDrawZeroLine(false);
YAxis rightAxis = mChart.getAxisRight();
mChart.setViewPortOffsets(0f, 0f, 0f, 0f);
if (NetworkCheck.Instance().isOnline(context)) {
graphPrice = NetworkAccessor.Instance().getGraphPrice(context, "day");
setData(context); // add data
} else {
NetworkCheck.Instance().showNetworkAlert(context);
}
mChart.animateX(2500);
Legend l = mChart.getLegend();
l.setForm(Legend.LegendForm.LINE); // modify the legend
//return displayPrice;
}
/*------------------------------------------------------------------------------------------------*/
private void setData(Context context) {
this.context = context;
String currentPrice;
float width = 2;
HashMap<Integer, String> dataList = new HashMap<>();
ArrayList<Entry> values = new ArrayList<Entry>();
for (int i = 0; i < graphPrice.size(); i++) {
int val = graphPrice.get(i).getPrice();
values.add(new Entry(i, val));
}
for (int i = 0; i < graphPrice.size(); i++) {
dataList.put(graphPrice.get(i).getPrice(), graphPrice.get(i).getTime());
}
// CustomMarkerView mv = new CustomMarkerView(context, R.layout.custom_marker_view, dataList, price); // create a custom MarkerView
// mv.setChartView(mChart); // For bounds control
// mChart.setMarker(mv); // Set the marker to the chart
LineDataSet set1;
if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet) mChart.getData().getDataSetByIndex(0);
set1.setValues(values);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
set1 = new LineDataSet(values, ""); // create a dataset and give it a type
set1.setColor(Color.WHITE);
set1.setDrawCircles(false);
set1.setLineWidth(width);
set1.setDrawIcons(false);
set1.setDrawValues(false);
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(set1); // add the datasets
LineData data = new LineData(dataSets); // create a data object with the datasets
mChart.setData(data); // set data
mChart.invalidate();
}
}
如果您想要具有自定义格式的轴标签,您应该创建自定义轴格式化程序
public class MyAxisValueFormatter implements IAxisValueFormatter {
public MyAxisValueFormatter() {
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return value + " $";
}
/** this is only needed if numbers are returned, else return 0 */
@Override
public int getDecimalDigits() { return 1; }
}
然后您需要像这样将格式化程序应用于轴:
mChart.getXAxis().setValueFormatter(new MyAxisValueFormatter());
这应该会为您提供末尾带有 $ 的值
您可以在 github wiki page
中了解更多相关信息
我正在尝试创建如下所示的折线图:
- 我要创建的图表
但我能做的最好的事情是:
- 我的图表
我正在使用 MPAndroidChart。我想在 YAxis 上固定 3 个标签。
并且 XAxis 应该根据我从 API 调用中得到的内容进行标记。
这是我目前的代码:
public void drawLineChart(LineChart mChart, Context context) {
this.mChart = mChart;
this.context = context;
mChart.getDescription().setEnabled(false);
mChart.setTouchEnabled(true); // enable touch gestures
mChart.setDragEnabled(true); // enable scaling and dragging
mChart.setScaleEnabled(true);
mChart.getXAxis().setEnabled(false);
mChart.getAxisLeft().setEnabled(true);
mChart.getAxisRight().setEnabled(false);
mChart.getAxisLeft().setDrawLabels(false);
mChart.getAxisRight().setDrawLabels(false);
mChart.getXAxis().setDrawLabels(false);
mChart.getLegend().setEnabled(false);
mChart.setPinchZoom(true); // if disabled, scaling can be done on x- and y-axis separately
YAxis yAxis = mChart.getAxisLeft();
yAxis.setDrawZeroLine(false);
YAxis rightAxis = mChart.getAxisRight();
mChart.setViewPortOffsets(0f, 0f, 0f, 0f);
if (NetworkCheck.Instance().isOnline(context)) {
graphPrice = NetworkAccessor.Instance().getGraphPrice(context, "day");
setData(context); // add data
} else {
NetworkCheck.Instance().showNetworkAlert(context);
}
mChart.animateX(2500);
Legend l = mChart.getLegend();
l.setForm(Legend.LegendForm.LINE); // modify the legend
//return displayPrice;
}
/*------------------------------------------------------------------------------------------------*/
private void setData(Context context) {
this.context = context;
String currentPrice;
float width = 2;
HashMap<Integer, String> dataList = new HashMap<>();
ArrayList<Entry> values = new ArrayList<Entry>();
for (int i = 0; i < graphPrice.size(); i++) {
int val = graphPrice.get(i).getPrice();
values.add(new Entry(i, val));
}
for (int i = 0; i < graphPrice.size(); i++) {
dataList.put(graphPrice.get(i).getPrice(), graphPrice.get(i).getTime());
}
// CustomMarkerView mv = new CustomMarkerView(context, R.layout.custom_marker_view, dataList, price); // create a custom MarkerView
// mv.setChartView(mChart); // For bounds control
// mChart.setMarker(mv); // Set the marker to the chart
LineDataSet set1;
if (mChart.getData() != null && mChart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet) mChart.getData().getDataSetByIndex(0);
set1.setValues(values);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
set1 = new LineDataSet(values, ""); // create a dataset and give it a type
set1.setColor(Color.WHITE);
set1.setDrawCircles(false);
set1.setLineWidth(width);
set1.setDrawIcons(false);
set1.setDrawValues(false);
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(set1); // add the datasets
LineData data = new LineData(dataSets); // create a data object with the datasets
mChart.setData(data); // set data
mChart.invalidate();
}
}
如果您想要具有自定义格式的轴标签,您应该创建自定义轴格式化程序
public class MyAxisValueFormatter implements IAxisValueFormatter {
public MyAxisValueFormatter() {
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return value + " $";
}
/** this is only needed if numbers are returned, else return 0 */
@Override
public int getDecimalDigits() { return 1; }
}
然后您需要像这样将格式化程序应用于轴:
mChart.getXAxis().setValueFormatter(new MyAxisValueFormatter());
这应该会为您提供末尾带有 $ 的值
您可以在 github wiki page
中了解更多相关信息