四、Charts

2018-11-05  本文已影响11人  ngugg

21. DataSet subclasses (specific DataSet styling)

This wiki entry focuses on the subclasses of the DataSet class. All other subclasses of DataSetnot mentioned here do not provide any specific enhancements.
- 此wiki条目侧重于DataSet类的子类。 此处提到的DataSet的所有其他子类都不提供任何特定的增强功能。

Line-, Bar-, Scatter-, Bubble- & CandleDataSet (below mentioned methods can be used for any of the mentioned DataSet classes)
- 下面提到的方法可以用于任何提到的DataSet类)

Line-, Bar-, Scatter-, Candle- & RadarDataSet

Line- & RadarDataSet (methods only for LineDataSet and RadarDataSet)

Below mentioned methods are only applicable for the specifically mentioned DataSet subclass.

LineDataSet (class LineDataSet)

BarDataSet (class BarDataSet)

ScatterDataSet (class ScatterDataSet)

CandleDataSet (class CandleDataSet)

BubbleDataSet (class BubbleDataSet)

Information concerning CandleDataSet colors: The usual setColors(...), setColor(...), ... methods can still be used for coloring the chart all at once. If specific colors (for body, shadow, ...) are needed, use the above mentioned methods.

PieDataSet (class PieDataSet)

22. The ViewPortHandler

The ViewPortHandler class is responsible for handling the view-port of the chart. This means it's responsible for what is visible in the chart-view, it's current state in terms of translation and zoom / scale level, the size of the chart and it's drawing area and current offsets. The ViewPortHandler allows to directly access all the above mentioned properties and modify them.

Unlike modifying the view-port via the Chart class, as described here, modifying the ViewPortHandler directly is not always a completely safe way of changing what is visible and should be performed with care by a person familiar with the API. Incorrect use can lead to unexpected behaviour. However, the ViewPortHandler offers more advanced methods of view-port modification.

Getting an instance

An instance of the ViewPortHandler can be acquired the following way only:

ViewPortHandler handler = chart.getViewPortHandler();

Scale & Translation

Chart dimensions & content

More methods can be found in the JavaDocs or via studying the API.

23. Customizing the Fill-Line-Position (FillFormatter)

The FillFormatter interface allows to customize where the filled line of a LineDataSet should end. All that needs to be done is create a new class and implement the FillFormatter interface. Use the

public float getFillLinePosition(LineDataSet dataSet, LineDataProvider provider)

method of the interface for implementing a custom logic that calculates the ending point of the fill line for the individual LineDataSet.

Creating a class the implements the interface:

public class MyCustomFillFormatter implements FillFormatter {

    @Override
    public float getFillLinePosition(LineDataSet dataSet, LineDataProvider dataProvider) {

        float myDesiredFillPosition = ...;
        // put your logic here...

        return myDesiredFillPosition;
    }
}

And then set the custom-formatter to your LineDataSet:

lineDataSet.setFillFormatter(new MyCustomFillFormatter());

Here is the default implementation (logic) of the DefaultFillFormatter.

24. Proguard

In case you are using Proguard, you will need to whitelist MPAndroidChart, which requires to add the following line to your Proguard configuration file:

-keep class com.github.mikephil.charting.** { *; }

If you don't do this, animations might not work.

If you are having issues with the Realm.io classes, add the following to your config file:

-dontwarn io.realm.**

Additional information on Proguard.

25. Realm.io mobile database

Please refer to the official MPAndroidChart-Realm repository to - plot data directly from Realm database with MPAndroidChart.

A detailed guide can be found here.

26. Xamarin

27. Creating your own (custom) DataSets

Since v2.2.0, MPAndroidChart allows you to easily create your own custom DataSet objects and use them in the charts.

What you need to do

Example

Creating a custom BarDataSet to be used in a BarChart.

public class CustomBarDataSet extends BaseDataSet<BarEntry> implements IBarDataSet {
    // implement all by the extended class and interface required methods
}

After creating the CustomBarDataSet and implementing all methods required by the interface it can be used in any BarChart just like a normal BarDataSet.

28. Miscellaneous (more useful stuff)

Chart content
Useful getter methods
Some more methods (of the Chart class)
上一篇下一篇

猜你喜欢

热点阅读