将y值用作浮点数时,BarChart上的X条目不会显示任何数据

时间:2016-08-14 20:33:41

标签: android mpandroidchart

我试图在我的代码中添加一个BarChart,当我启动我的应用时没有任何内容。

我的代码的目的是:当用户输入开始日期(StartDate),结束日期(EndDate)和产品名称时,图表应显示该期间的展示价格演变。

我已尝试过以下代码,但图表上没有任何内容。

请帮助找到问题,或向我提出另一个代码。

public class ResultProductStatisticsActivity extends AppCompatActivity {


private String mCategory;
private String mName;
private long mStartDate;
private long mEndDate;
private int mNumber = 0;
private ProgressBar mProgressBar;
private ArrayList<ShopDetail> mDetail = new ArrayList<ShopDetail>();

private ArrayList<Float> mPriceList = new ArrayList<Float>();
protected Typeface mTfLight;
private  Typeface mTypeFaceLight;



private TextView mMontextViewtest;


private BarChart mChart;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.result_product_statistics_activity);




    mChart = (BarChart) findViewById(R.id.m_barchart);
   // mChart.setOnChartValueSelectedListener(this);

    mChart.setDrawBarShadow(false);
    mChart.setDrawValueAboveBar(true);

    mChart.setDescription("");

    // if more than 60 entries are displayed in the chart, no values will be
    // drawn
    mChart.setMaxVisibleValueCount(60);

    // scaling can now only be done on x- and y-axis separately
    mChart.setPinchZoom(false);

    mChart.setDrawGridBackground(false);
    // mChart.setDrawYLabels(false);

    AxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(mChart);

    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setTypeface(mTfLight);
    xAxis.setDrawGridLines(false);
    xAxis.setGranularity(1f); // only intervals of 1 day
    xAxis.setLabelCount(7);
    xAxis.setValueFormatter(xAxisFormatter);

    AxisValueFormatter custom = new MyAxisValueFormatter();

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setTypeface(mTfLight);
    leftAxis.setLabelCount(8, false);
    leftAxis.setValueFormatter(custom);
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    leftAxis.setSpaceTop(15f);
    leftAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)

    YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setDrawGridLines(false);
    rightAxis.setTypeface(mTfLight);
    rightAxis.setLabelCount(8, false);
    rightAxis.setValueFormatter(custom);
    rightAxis.setSpaceTop(15f);
    rightAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)

    Legend l = mChart.getLegend();
    l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
    l.setForm(Legend.LegendForm.SQUARE);
    l.setFormSize(9f);
    l.setTextSize(11f);
    l.setXEntrySpace(4f);
    // l.setExtra(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
    // "def", "ghj", "ikl", "mno" });
    // l.setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
    // "def", "ghj", "ikl", "mno" });
    init();

    mChart.setMarkerView(new XYMarkerView(this, xAxisFormatter));
}




private void init(){
    mCategory = Myrecord.getSearchShopCategory(this);
    mName = Myrecord.getSearchShopName(this);
    mStartDate = Myrecord.getSearchStartTime(this);
    mEndDate = Myrecord.getSearchEndTime(this);


    if(mStartDate == -1){
        mStartDate = Long.MIN_VALUE;
    }
    if(mEndDate == -1){
        mEndDate = Long.MAX_VALUE;
    }



    Thread thread = new Thread(){
        @Override
        public void run() {

            MyDB db = new MyDB(ResultProductStatisticsActivity.this);
            ArrayList<MyDetail> detailList = db.getMyDetailByAll(mCategory, mName, -1, 0, -1);

            for(int i = 0; i < detailList.size(); i ++){
                MyDetail detail = detailList.get(i);
                long date = detail.date;

                if(date >= mStartDate && date <= mEndDate){

                    mPriceList.add(db.getPriceGoodsByOwnName("ALEVONS")+0f);
                }
            }

            ResultProductStatisticsActivity.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                     setData(mPriceList);
                }
            });
        }

    };
    thread.start();
}


private void setData(ArrayList<Float> mPriceList) {

    mChart.getXAxis().setAxisMinValue(mStartDate);
    mChart.getXAxis().setAxisMaxValue(mEndDate);

    ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();

    for (int i = 0; i < mPriceList.size(); i++) {
        yVals1.add(new BarEntry(i , mPriceList.get(i)));
    }

    BarDataSet set1;

    if (mChart.getData() != null &&
            mChart.getData().getDataSetCount() > 0) {
        set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
        set1.setValues(yVals1);
        mChart.getData().notifyDataChanged();
        mChart.notifyDataSetChanged();
    } else {
        set1 = new BarDataSet(yVals1, "Evolution du prix produit");
        set1.setColors(ColorTemplate.MATERIAL_COLORS);

        ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
        dataSets.add(set1);

        BarData data = new BarData(dataSets);
        data.setValueTextSize(10f);
        data.setValueTypeface(mTfLight);
        data.setBarWidth(0.9f);

        mChart.setData(data);
    }
}

}

2 个答案:

答案 0 :(得分:1)

一个疯狂的猜测,没有提供任何更多细节:你将x轴的最小值和最大值设置为开始和结束日期。我猜这是在unix时间编码的,所以你有14706144001471219200之类的东西。

现在查看您的数据,它将以0开头,并上升到您的价目表(mPriceList.size())中的价格数量。因此,对于10个价格,我们的值为09。但是,由于您的x轴从1470614400开始,因此不会显示任何数据(尽管轴,网格线等应该是可见的)。

因此,为了修复您的代码(取决于您的价目表的结构,此示例假定每小时一个价格),您需要将setData中的代码更改为:

for (int i = 0; i < mPriceList.size(); i++) {
    yVals1.add(new BarEntry(mStartDate + i * 3600, mPriceList.get(i)));
}

如果您有与价格值相关的日期列表,您也可以这样做:

for (int i = 0; i < mPriceList.size(); i++) {
    yVals1.add(new BarEntry(mDateList.get(i), mPriceList.get(i)));
}

答案 1 :(得分:0)

我们回复(@ TR4Android),

我已经更新了我的代码,我可以获得图表:

        float start = 0f;

    mChart.getXAxis().setAxisMinValue(start);
    mChart.getXAxis().setAxisMaxValue(mPriceList.size());
    for (int i = 0; i < mPriceList.size(); i++) {
        yVals1.add(new BarEntry(i , mPriceList.get(i)));
    }

但是我对开始日期结束日期有点问题,每个项目都已购买。我已将每个日期放在数组列表中,但我不知道如何将它放在xAxis上。数组列表是:

private ArrayList<Long> mDateList = new ArrayList<Long>();

mDateList值的一个示例是2016/08/13