Java Code Examples for com.github.mikephil.charting.components.Legend#setCustom()

The following examples show how to use com.github.mikephil.charting.components.Legend#setCustom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KlineFragment.java    From shinny-futures-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * date: 2019/7/31
 * author: chenli
 * description: 刷新均线图标值
 */
private void refreshTopChartLegend(int index) {
    List<LegendEntry> legendEntries = new ArrayList<>();
    for (int i = 0; i < mas.size(); i++) {
        int para = mas.get(i);
        Entry entry = mLineData.getDataSetByIndex(i).getEntryForXValue(index, 0.0f);
        String data = ":";
        if (entry != null) data = data + MathUtils.round(entry.getY()+"", 2);
        LegendEntry ma = new LegendEntry("MA" + para + data, Legend.LegendForm.NONE,
                NaN, NaN, null, mColorMas[i]);
        legendEntries.add(ma);
    }
    Legend legend = mTopChartViewBase.getLegend();
    legend.setCustom(legendEntries);
    legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
}
 
Example 2
Source File: BaseChartFragment.java    From shinny-futures-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * date: 2019/7/31
 * author: chenli
 * description: 刷新中部图标值
 */
protected void refreshChartLegend(int index){
    Map<String, KlineEntity.DataEntity> dataEntities = mKlineEntity.getData();
    int middleIndex = index;
    if (!CURRENT_DAY_FRAGMENT.equals(mFragmentType)) middleIndex = middleIndex + mBaseIndex;
    KlineEntity.DataEntity dataEntity = dataEntities.get(String.valueOf(middleIndex));
    if (dataEntity != null){
        List<LegendEntry> legendEntriesMiddle = new ArrayList<>();
        legendEntriesMiddle.add(new LegendEntry("OI:"+dataEntity.getClose_oi(), Legend.LegendForm.NONE,
                NaN, NaN, null, mOIColor));
        legendEntriesMiddle.add(new LegendEntry("VOL:"+dataEntity.getVolume(), Legend.LegendForm.NONE,
                NaN, NaN, null, mIncreasingColor));
        Legend legendMiddle = mMiddleChartViewBase.getLegend();
        legendMiddle.setCustom(legendEntriesMiddle);
        legendMiddle.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legendMiddle.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    }

    Float emasF = mEMAs.get(index);
    Float emalF = mEMAl.get(index);
    if (emasF != null && emalF != null){
        float emas = mEMAs.get(index);
        float emal = mEMAl.get(index);
        String dif = MathUtils.round((emas - emal) + "", 2);
        String dea = MathUtils.round(mDEA.get(index) + "", 2);
        List<LegendEntry> legendEntriesBottom = new ArrayList<>();
        legendEntriesBottom.add(new LegendEntry("MACD(12,26,9)" , Legend.LegendForm.NONE,
                NaN, NaN, null, mDeaColor));
        legendEntriesBottom.add(new LegendEntry("DIFF:"+dif, Legend.LegendForm.NONE,
                NaN, NaN, null, mDiffColor));
        legendEntriesBottom.add(new LegendEntry("DEA:"+dea, Legend.LegendForm.NONE,
                NaN, NaN, null, mDeaColor));
        Legend legendBottom = mBottomChartViewBase.getLegend();
        legendBottom.setCustom(legendEntriesBottom);
        legendBottom.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legendBottom.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    }

}
 
Example 3
Source File: ChartBaseManager.java    From react-native-mp-android-chart with MIT License 4 votes vote down vote up
/**
 * More details about legend customization: https://github.com/PhilJay/MPAndroidChart/wiki/Legend
 */
@ReactProp(name = "legend")
public void setLegend(T chart, ReadableMap propMap) {
    Legend legend = chart.getLegend();

    if (BridgeUtils.validate(propMap, ReadableType.Boolean, "enabled")) {
        legend.setEnabled(propMap.getBoolean("enabled"));
    }

    // Styling
    if (BridgeUtils.validate(propMap, ReadableType.String, "textColor")) {
        legend.setTextColor(Color.parseColor(propMap.getString("textColor")));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "textSize")) {
        legend.setTextSize((float) propMap.getDouble("textSize"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.String, "fontFamily") ||
            BridgeUtils.validate(propMap, ReadableType.Number, "fontStyle")) {
        legend.setTypeface(BridgeUtils.parseTypeface(chart.getContext(), propMap, "fontStyle", "fontFamily"));
    }

    // Wrapping / clipping avoidance
    if (BridgeUtils.validate(propMap, ReadableType.Boolean, "wordWrapEnabled")) {
        legend.setWordWrapEnabled(propMap.getBoolean("wordWrapEnabled"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "maxSizePercent")) {
        legend.setMaxSizePercent((float) propMap.getDouble("maxSizePercent"));
    }

    // Customizing
    if (BridgeUtils.validate(propMap, ReadableType.String, "position")) {
        legend.setPosition(LegendPosition.valueOf(propMap.getString("position").toUpperCase()));
    }
    if (BridgeUtils.validate(propMap, ReadableType.String, "form")) {
        legend.setForm(LegendForm.valueOf(propMap.getString("form").toUpperCase()));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "formSize")) {
        legend.setFormSize((float) propMap.getDouble("formSize"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "xEntrySpace")) {
        legend.setXEntrySpace((float) propMap.getDouble("xEntrySpace"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "yEntrySpace")) {
        legend.setYEntrySpace((float) propMap.getDouble("yEntrySpace"));
    }
    if (BridgeUtils.validate(propMap, ReadableType.Number, "formToTextSpace")) {
        legend.setFormToTextSpace((float) propMap.getDouble("formToTextSpace"));
    }

    // Custom labels & colors
    if (BridgeUtils.validate(propMap, ReadableType.Map, "custom")) {
        ReadableMap customMap = propMap.getMap("custom");
        if (BridgeUtils.validate(customMap, ReadableType.Array, "colors") &&
                BridgeUtils.validate(customMap, ReadableType.Array, "labels")) {

            ReadableArray colorsArray = customMap.getArray("colors");
            ReadableArray labelsArray = customMap.getArray("labels");

            if (colorsArray.size() == labelsArray.size()) {
                // TODO null label should start a group
                // TODO -2 color should avoid drawing a form
                String[] labels = BridgeUtils.convertToStringArray(labelsArray);
                String[] colors = BridgeUtils.convertToStringArray(colorsArray);

                int[] colorsParsed = new int[colors.length];
                for (int i = 0; i < colors.length; i++) {
                    colorsParsed[i] = Color.parseColor(colors[i]);
                }

                legend.setCustom(colorsParsed, labels);
            }
        }
    }

    // TODO resetCustom function
    // TODO extra

    chart.invalidate();     // TODO is this necessary? Looks like enabled is not refreshing without it
}