XAxis上带有MPAndroidChart的Trucanted标签

时间:2018-06-24 12:10:01

标签: android mpandroidchart

我试图绘制一个以数据和时间戳为横轴的图表,所以我将它们旋转了45度,以避免重叠。

一切正常,除了第一次加载该应用程序时,这些标签会被屏幕剪切。

Erroneous labels 但是,在旋转设备并强制重新创建主要活动后,它会进行调整并正确显示。

Correct labels

我正在使用加载器将输入数据提供给图表。

以下是MainActivity的摘录:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d(LOG_TAG, "onCreate: ");
    setContentView(R.layout.activity_main);

    tempTextView = (TextView) findViewById(R.id.temperature_text_view);
    timestampTextView = (TextView) findViewById(R.id.timestamp_text_view);
    deviceNameTextView = (TextView) findViewById(R.id.name_text_view);
    tempHistchart = (LineChart) findViewById(R.id.chart);
    dataSet = null;
    lineData = null;

    // Get a reference to the LoaderManager, in order to interact with loaders.
    LoaderManager loaderManager = getLoaderManager();

    // Initialize the loader. Pass in the int ID constant defined above and pass in null for
    // the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
    // because this activity implements the LoaderCallbacks interface).
    loaderManager.initLoader(TEMPERATURE_LOADER_ID, null, this);

}


@Override
public void onLoadFinished(Loader<TemperatureData> loader, TemperatureData data) {

    if (data != null) {

        Log.d(LOG_TAG, "onLoadFinished: " + data);
        tempTextView.setText(data.getTemperature() + "°C");
        timestampTextView.setText(data.getCurrentTime());
        deviceNameTextView.setText(data.getDeviceName());

        //plot 24h temperature history on chart
        dataSet = new LineDataSet(data.getTemperature24hEntryList(), ""); // add entries to dataset

        // create a data object with the datasets
        lineData = new LineData(dataSet);
        tempHistchart.setData(lineData);
        formatLineChart();
        tempHistchart.invalidate();
    }
}


private void formatLineChart() {

    if (tempHistchart.getData() == null
            || tempHistchart == null
            || lineData == null
            || dataSet == null)
        return;

    lineData.setValueTextColor(Color.WHITE);
    lineData.setValueTextSize(9f);
    lineData.setHighlightEnabled(true);

    dataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
    dataSet.setColor(Color.BLACK);
   // dataSet.setValueTextColor(ColorTemplate.getHoloBlue());
    dataSet.setLineWidth(2.5f);
    dataSet.setDrawCircles(false);
    dataSet.setDrawValues(false);
    dataSet.setFillAlpha(65);
    dataSet.setFillColor(android.R.color.darker_gray);
    //dataSet.setHighLightColor(Color.rgb(244, 217, 217));
    dataSet.setHighlightEnabled(false);
    dataSet.setDrawCircleHole(false);

    // no description text
    tempHistchart.getDescription().setEnabled(false);
    //no zooming allowed
    tempHistchart.setScaleEnabled(false);
    // add animation
    tempHistchart.animateX(800);

    // tempHistchart.setMarker(markerView(this.getApplicationContext()));
    List<ILineDataSet> sets = tempHistchart.getData().getDataSets();
    for (ILineDataSet iSet : sets) {
        LineDataSet set = (LineDataSet) iSet;
        set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
    }

    XAxis xAxis = tempHistchart.getXAxis();
    xAxis.setTextColor(Color.BLACK);
    xAxis.setAxisLineColor(Color.BLACK);
    xAxis.setDrawGridLines(true);
    xAxis.setDrawAxisLine(false);
    xAxis.setGridColor(Color.DKGRAY);
    xAxis.enableGridDashedLine(10, 10, 0);
    xAxis.setValueFormatter(new IAxisValueFormatter() {

        private SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM HH:mm");

        @Override
        public String getFormattedValue(float value, AxisBase axis) {

            return dateFormat.format(new Date((long) value));
        }
    });
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setLabelRotationAngle(-45f);
    xAxis.setLabelCount(7,true);
    //xAxis.setYOffset(-16f);
    xAxis.setCenterAxisLabels(false);

    float minValue = dataSet.getYMin();
    float maxValue = dataSet.getYMax();
    //Initial values for Y axis
    int yMax = (int) maxValue + 1;
    int yMin = (int) minValue - 1;

    YAxis leftAxis = tempHistchart.getAxisLeft();
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    //  leftAxis.setTypeface(Typeface.SANS_SERIF);
    leftAxis.setTextColor(Color.BLACK);
    leftAxis.setDrawGridLines(true);
    leftAxis.setDrawAxisLine(false);
    leftAxis.setGridColor(Color.DKGRAY);
    leftAxis.setAxisLineColor(Color.BLACK);
    leftAxis.setGranularity(1);
    leftAxis.setGranularityEnabled(true);
    leftAxis.setAxisMinimum(yMin);
    leftAxis.setAxisMaximum(yMax);
    leftAxis.setXOffset(10f);
    leftAxis.enableGridDashedLine(10, 10, 0);
    YAxis rightAxis = tempHistchart.getAxisRight();
    rightAxis.setEnabled(false);

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

}

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_color_blue_green"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name_text_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center|bottom"
            android:textColor="@android:color/darker_gray"
            android:textSize="32sp"
            tools:text="Home" />

        <TextView
            android:id="@+id/temperature_text_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Loading..."
            android:textColor="@android:color/white"
            android:textSize="64sp" />

        <TextView
            android:id="@+id/timestamp_text_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center|top"
            android:textColor="@android:color/darker_gray"
            android:textSize="16sp"
            tools:text="2018-04-28 22:58:00" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_marginBottom="0dp">
    enter code here
        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/chart"
            android:layout_width="match_parent"
            android:layout_height=""/>
    </LinearLayout>
</LinearLayout>

您知道我在做什么错吗?

谢谢!

0 个答案:

没有答案