如何在X aixs上获得时间戳如实时标签(16:24:00)实时线图MPAndroid图表

时间:2017-02-06 09:04:40

标签: android mpandroidchart

  private void setUpChartView() {

    mChart.getDescription().setEnabled(false);

    mChart.setNoDataText("Please wait, while fetching chart data...");

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

    // enable scaling and dragging
    mChart.setDragEnabled(true);
    mChart.setScaleEnabled(true);
    mChart.setDrawGridBackground(false);

    // if disabled, scaling can be done on x- and y-axis separately
    mChart.setPinchZoom(true);

    // set an alternative background color
    mChart.setBackgroundColor(Color.WHITE);

    // get the legend (only possible after setting data)
    Legend l = mChart.getLegend();

    // modify the legend ...
    l.setForm(Legend.LegendForm.SQUARE);

    l.setEnabled(true);

    XAxis xl = mChart.getXAxis();
    xl.setTextColor(ContextCompat.getColor(mActivity, R.color.chart_axis));
    xl.setDrawGridLines(false);
    xl.setAvoidFirstLastClipping(true);
    xl.setEnabled(true);
    xl.setPosition(XAxis.XAxisPosition.BOTTOM);
   /* xl.setValueFormatter(new IAxisValueFormatter() {
        @Override public String getFormattedValue(float value, AxisBase axis) {
            Date time = new java.util.Date((long) value * 1000);
            SimpleDateFormat destinationFormat = new SimpleDateFormat("HH:mm:ss");

            return destinationFormat.format(time);
        }
    });*/

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setTextColor(ContextCompat.getColor(mActivity,R.color.chart_axis));
    leftAxis.setDrawGridLines(true);

    YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setEnabled(false);

}

=============================================== =============================

private void addEntry(ArrayList<ChartBean> values, Float base) {

    LineData data = mChart.getData();

    if (data == null) {
        for (int i = 0; i < values.size(); i++) {
            Entry newEntry = new Entry(base, values.get(i).getValue(), values.get(i).getLabel());

            ArrayList<Entry> newEntryList = new ArrayList<Entry>();
            newEntryList.add(newEntry);

            int color;

            if(i == 1){
                color = R.color.chart_line3;
            }else if ((i == 2)){
                color = R.color.chart_line2;
            } else if ((i == 3)) {
                color = R.color.blue;
            } else if ((i == 4)) {
                color = R.color.bg_purple;
            }else {
                color = R.color.chart_line1;
            }

            LineDataSet newSet = createSet(newEntryList, R.color.bg_green,color);
            if (data == null) {
                data = new LineData(newSet);
            }else {

                data.addDataSet(newSet);
            }
        }
        data.setValueTextColor(Color.GREEN);
        data.setValueTextSize(9f);
    }else {
        for (int i = 0; i < values.size(); i++) {
            Entry newEntry = new Entry(base, values.get(i).getValue(), values.get(i).getLabel());

            LineDataSet newSet = (LineDataSet) mChart.getData().getDataSetByIndex(i);

            List<Entry> newEntryList = newSet.getValues();
            newEntryList.add(newEntry);
            newSet.setValues(newEntryList);
        }

    }

    // set data
    mChart.setData(data);
    mChart.getData().notifyDataChanged();
    mChart.notifyDataSetChanged();

    // limit the number of visible entries
    mChart.setVisibleXRangeMaximum(10);
    // mChart.setVisibleYRange(30, AxisDependency.LEFT);

    // move to the latest entry
    mChart.moveViewToX(base);

    // this automatically refreshes the chart (calls invalidate())
    // mChart.moveViewTo(data.getXValCount()-7, 55f,
    // AxisDependency.LEFT)
}

=============================================== ================================================== =============

 private LineDataSet createSet(ArrayList<Entry> entryList , int circleColor , int lineColor) {

    LineDataSet set = new LineDataSet(entryList, ((entryList.get(0).getData()!= null) ? entryList.get(0).getData().toString() : ""));
    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    set.setColor(ContextCompat.getColor(mActivity,lineColor));
    set.setCircleColor(ContextCompat.getColor(mActivity,circleColor));
    set.setLineWidth(1f);
    set.setCircleRadius(3f);
    set.setFillAlpha(65);
    set.setFillColor(ContextCompat.getColor(mActivity,R.color.chart_axis));
    set.setHighLightColor(Color.rgb(244, 117, 117));
    set.setValueTextColor(Color.GREEN);
    set.setValueTextSize(9f);
    set.setDrawValues(false);
    return set;
}

=============================================== ================================

if (Utils.isJSONValid(message.toString())) 
{
   JSONObject jsonObject = new JSONObject(message.toString());

   if(jsonObject.optString("did").equals(deviceId)) 
{
      if (jsonObject.get("data") instanceof JSONArray) {
            JSONArray data = (JSONArray) jsonObject.get("data");
                  for (int i = 0; i < data.length(); i++) {
                          JSONObject item = data.getJSONObject(i);
              if (item.get(liveDataTitle) instanceof Double) {
                    float x = (float) item.getDouble(liveDataTitle);
                   ArrayList<ChartBean> values = new ArrayList<ChartBean>();
                       values.add(new ChartBean(liveDataTitle, x));
                                    addEntry(values, count);
           } else if (item.get(liveDataTitle) instanceof Boolean) {
                Boolean x = (Boolean) item.getBoolean(liveDataTitle);
                 ArrayList<ChartBean> values = new ArrayList<ChartBean>();
               values.add(new ChartBean(liveDataTitle, (x ? 1f : 0f)));
                 addEntry(values, count);
       } else if(item.get(liveDataTitle) instanceof  JSONObject){
       JSONObject newEntry = (JSONObject) item.get(liveDataTitle);
       ArrayList<ChartBean> values = new ArrayList<ChartBean>();
       Iterator iterator = newEntry.keys();
       while (iterator.hasNext()) {
         String key = (String) iterator.next();
         float x = (float) newEntry.getDouble(key);
             values.add(new ChartBean(key,x));
                }
       addEntry(values, count);
        }
           count ++;
             }
          }
       }
   }

0 个答案:

没有答案