【问题标题】:Creating charts using MPAndroidChart使用 MPAndroidChart 创建图表
【发布时间】:2017-07-25 05:03:05
【问题描述】:

我正在尝试创建如下所示的折线图:

  • 我要创建的图表

但我能做的最好的就是这样:

  • 我的图表

我正在使用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();
        }

    }

【问题讨论】:

  • 但是问题是什么...??
  • 试试这个库检查实现github.com/ddanny/achartengine
  • @FrantišekJeřábek 请检查图片。我想创建一个像我展示的那样的图表。我一直在努力,似乎无法完成。
  • @hareeshJ 我也想要一个 iOS 通用库。
  • 您想在每个轴上添加标签??我不知道淡入淡出的东西,但我看到的最大区别是轴标签

标签: android mpandroidchart


【解决方案1】:

如果您想要自定义格式的轴标签,您应该创建自定义轴格式器

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 中了解更多信息

【讨论】:

    猜你喜欢
    • 2022-10-26
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多