Java Code Examples for com.github.mikephil.charting.data.LineData#addDataSet()

The following examples show how to use com.github.mikephil.charting.data.LineData#addDataSet() . 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: CombinedChartActivity.java    From StockChart-MPAndroidChart with MIT License 6 votes vote down vote up
private LineData generateLineData() {

        LineData d = new LineData();

        ArrayList<Entry> entries = new ArrayList<>();

        for (int index = 0; index < count; index++)
            entries.add(new Entry(index + 0.5f, getRandom(15, 5)));

        LineDataSet set = new LineDataSet(entries, "Line DataSet");
        set.setColor(Color.rgb(240, 238, 70));
        set.setLineWidth(2.5f);
        set.setCircleColor(Color.rgb(240, 238, 70));
        set.setCircleRadius(5f);
        set.setFillColor(Color.rgb(240, 238, 70));
        set.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        set.setDrawValues(true);
        set.setValueTextSize(10f);
        set.setValueTextColor(Color.rgb(240, 238, 70));

        set.setAxisDependency(YAxis.AxisDependency.LEFT);
        d.addDataSet(set);

        return d;
    }
 
Example 2
Source File: ChartStatistics.java    From TwistyTimer with GNU General Public License v3.0 6 votes vote down vote up
/**
     * Adds the data set for the average-of-N (AoN) times and the corresponding data set for the
     * single best average time for that value of "N". The best AoN times are not shown as a
     * progression; only one time is shown and it superimposed on its main AoN line, rendered in
     * the same color as a circle and with the value drawn on the chart.
     *
     * @param chartData The chart data to which to add the new data sets.
     * @param label     The label of the AoN line and best AoN time marker.
     * @param color     The color of the AoN line and best AoN time marker.
     */
    private void addAoNDataSets(LineData chartData, String label, int color) {
        // Main AoN data set for all AoN times for one value of "N".
        chartData.addDataSet(createDataSet(label, color));

        // Data set for the single best AoN time for this "N".
        final LineDataSet bestAoNDataSet = createDataSet(label, color);

        bestAoNDataSet.setDrawCircles(true);
        bestAoNDataSet.setCircleRadius(BEST_TIME_CIRCLE_RADIUS_DP);
        bestAoNDataSet.setCircleColor(color);

        // Drawing the value of the best AoN time for each "N" seems like it would be a good idea,
        // but the values are really hard because they appear over other chart lines and sometimes
        // over the values drawn for the best time progression. Disabling them is no great loss,
        // as the statistics table shows the same values, anyway. Just showing a circle to mark
        // the best AoN time looks well enough on its own.
        bestAoNDataSet.setDrawValues(false);
//        bestAoNDataSet.setValueTextColor(color);
//        bestAoNDataSet.setValueTextSize(BEST_TIME_VALUES_TEXT_SIZE_DP);
//        bestAoNDataSet.setValueFormatter(new TimeChartValueFormatter());

        chartData.addDataSet(bestAoNDataSet);
    }
 
Example 3
Source File: CombinedChartActivity.java    From Stayfit with Apache License 2.0 6 votes vote down vote up
private LineData generateLineData() {

        LineData d = new LineData();

        ArrayList<Entry> entries = new ArrayList<Entry>();

        for (int index = 0; index < itemcount; index++)
            entries.add(new Entry(getRandom(15, 10), index));

        LineDataSet set = new LineDataSet(entries, "Line DataSet");
        set.setColor(Color.rgb(240, 238, 70));
        set.setLineWidth(2.5f);
        set.setCircleColor(Color.rgb(240, 238, 70));
        set.setCircleRadius(5f);
        set.setFillColor(Color.rgb(240, 238, 70));
        set.setDrawCubic(true);
        set.setDrawValues(true);
        set.setValueTextSize(10f);
        set.setValueTextColor(Color.rgb(240, 238, 70));

        set.setAxisDependency(YAxis.AxisDependency.LEFT);

        d.addDataSet(set);

        return d;
    }
 
Example 4
Source File: DynamicalAddingActivity.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
private void addDataSet() {

        LineData data = chart.getData();

        if (data == null) {
            chart.setData(new LineData());
        } else {
            int count = (data.getDataSetCount() + 1);
            int amount = data.getDataSetByIndex(0).getEntryCount();

            ArrayList<Entry> values = new ArrayList<>();

            for (int i = 0; i < amount; i++) {
                values.add(new Entry(i, (float) (Math.random() * 50f) + 50f * count));
            }

            LineDataSet set = new LineDataSet(values, "DataSet " + count);
            set.setLineWidth(2.5f);
            set.setCircleRadius(4.5f);

            int color = colors[count % colors.length];

            set.setColor(color);
            set.setCircleColor(color);
            set.setHighLightColor(color);
            set.setValueTextSize(10f);
            set.setValueTextColor(color);

            data.addDataSet(set);
            data.notifyDataChanged();
            chart.notifyDataSetChanged();
            chart.invalidate();
        }
    }
 
Example 5
Source File: RealtimeLineChartActivity.java    From StockChart-MPAndroidChart with MIT License 5 votes vote down vote up
private void addEntry() {

        LineData data = chart.getData();

        if (data != null) {

            ILineDataSet set = data.getDataSetByIndex(0);
            // set.addEntry(...); // can be called as well

            if (set == null) {
                set = createSet();
                data.addDataSet(set);
            }

            data.addEntry(new Entry(set.getEntryCount(), (float) (Math.random() * 40) + 30f), 0);
            data.notifyDataChanged();

            // let the chart know it's data has changed
            chart.notifyDataSetChanged();

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

            // move to the latest entry
            chart.moveViewToX(data.getEntryCount());

            // this automatically refreshes the chart (calls invalidate())
            // chart.moveViewTo(data.getXValCount()-7, 55f,
            // AxisDependency.LEFT);
        }
    }
 
Example 6
Source File: TickChart.java    From android-kline with Apache License 2.0 5 votes vote down vote up
public void refreshData(float price) {
    if (price <= 0 || price == mLastPrice) {
        return;
    }
    mLastPrice = price;
    LineData data = mChart.getData();

    if (data != null) {
        ILineDataSet setSell = data.getDataSetByIndex(DATA_SET_PRICE);
        if (setSell == null) {
            setSell = createSet(TYPE_FULL);
            data.addDataSet(setSell);
        }

        data.removeEntry(setSell.getEntryCount(), DATA_SET_PRICE);
        Entry entry = new Entry(setSell.getEntryCount(), price);
        data.addEntry(entry, DATA_SET_PRICE);

        ILineDataSet paddingSet = data.getDataSetByIndex(DATA_SET_PADDING);
        if (paddingSet == null) {
            paddingSet = createSet(TYPE_DASHED);
            data.addDataSet(paddingSet);
        }

        int count = paddingSet.getEntryCount();
        paddingSet.clear();
        for (int i = 0; i < count; i++) {
            paddingSet.addEntry(new Entry(setSell.getEntryCount() + i, price));
        }

        Highlight chartHighlighter = new Highlight(setSell.getEntryCount() + paddingSet.getEntryCount(), price, DATA_SET_PADDING);
        mChart.highlightValue(chartHighlighter);

        data.notifyDataChanged();
        mChart.notifyDataSetChanged();
        mChart.invalidate();


    }
}
 
Example 7
Source File: ChartStatistics.java    From TwistyTimer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds the main data set for all times and the data set for the progression of record best
 * times among all times. The progression of best times are marked in a different color to the
 * main line of all time using circles lined with a dashed line. This will appear to connect
 * the lowest troughs along the main line of all times.
 *
 * @param chartData The chart data to which to add the new data sets.
 * @param allLabel  The label of the all-times line.
 * @param allColor  The color of the all-times line.
 * @param bestLabel The label of the best-times line.
 * @param bestColor The color of the best-times line.
 */
private void addMainDataSets(LineData chartData, String allLabel, int allColor,
                             String bestLabel, int bestColor) {
    // Main data set for all solve times.
    final LineDataSet mainDataSet = createDataSet(allLabel, allColor);

    mainDataSet.setDrawCircles(getDrawCircle());
    mainDataSet.setCircleRadius(getCircleRadius());
    mainDataSet.setCircleColor(allColor);
    mainDataSet.setColor(getLineColor(allColor));

    chartData.addDataSet(mainDataSet);

    // Data set to show the progression of best times along the main line of all times.
    final LineDataSet bestDataSet = createDataSet(bestLabel, bestColor);

    bestDataSet.enableDashedLine(3f, 6f, 0f);

    bestDataSet.setDrawCircles(true);
    bestDataSet.setCircleRadius(BEST_TIME_CIRCLE_RADIUS_DP);
    bestDataSet.setCircleColor(bestColor);

    bestDataSet.setDrawValues(false);
    bestDataSet.setValueTextColor(bestColor);
    bestDataSet.setValueTextSize(BEST_TIME_VALUES_TEXT_SIZE_DP);
    bestDataSet.setValueFormatter(new TimeChartValueFormatter());

    chartData.addDataSet(bestDataSet);
}
 
Example 8
Source File: DynamicalAddingActivity.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
private void addEntry() {

        LineData data = mChart.getData();
        
        if(data != null) {

            ILineDataSet set = data.getDataSetByIndex(0);
            // set.addEntry(...); // can be called as well

            if (set == null) {
                set = createSet();
                data.addDataSet(set);
            }

            // add a new x-value first
            data.addXValue(set.getEntryCount() + "");
            
            // choose a random dataSet
            int randomDataSetIndex = (int) (Math.random() * data.getDataSetCount());
            
            data.addEntry(new Entry((float) (Math.random() * 10) + 50f, set.getEntryCount()), randomDataSetIndex);

            // let the chart know it's data has changed
            mChart.notifyDataSetChanged();
            
            mChart.setVisibleXRangeMaximum(6);
            mChart.setVisibleYRangeMaximum(15, AxisDependency.LEFT);
//            
//            // this automatically refreshes the chart (calls invalidate())
            mChart.moveViewTo(data.getXValCount()-7, 50f, AxisDependency.LEFT);
        }
    }
 
Example 9
Source File: DynamicalAddingActivity.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
private void addDataSet() {

        LineData data = mChart.getData();
        
        if(data != null) {

            int count = (data.getDataSetCount() + 1);

            // create 10 y-vals
            ArrayList<Entry> yVals = new ArrayList<Entry>();
            
            if(data.getXValCount() == 0) {
                // add 10 x-entries
                for (int i = 0; i < 10; i++) {
                    data.addXValue("" + (i+1));
                }
            }

            for (int i = 0; i < data.getXValCount(); i++) {
                yVals.add(new Entry((float) (Math.random() * 50f) + 50f * count, i));
            }

            LineDataSet set = new LineDataSet(yVals, "DataSet " + count);
            set.setLineWidth(2.5f);
            set.setCircleRadius(4.5f);

            int color = mColors[count % mColors.length];

            set.setColor(color);
            set.setCircleColor(color);
            set.setHighLightColor(color);
            set.setValueTextSize(10f);
            set.setValueTextColor(color);

            data.addDataSet(set);
            mChart.notifyDataSetChanged();
            mChart.invalidate();   
        }
    }
 
Example 10
Source File: RealtimeLineChartActivity.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
private void addEntry() {

        LineData data = mChart.getData();

        if (data != null) {

            ILineDataSet set = data.getDataSetByIndex(0);
            // set.addEntry(...); // can be called as well

            if (set == null) {
                set = createSet();
                data.addDataSet(set);
            }

            // add a new x-value first
            data.addXValue(mMonths[data.getXValCount() % 12] + " "
                    + (year + data.getXValCount() / 12));
            data.addEntry(new Entry((float) (Math.random() * 40) + 30f, set.getEntryCount()), 0);


            // let the chart know it's data has changed
            mChart.notifyDataSetChanged();

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

            // move to the latest entry
            mChart.moveViewToX(data.getXValCount() - 121);

            // this automatically refreshes the chart (calls invalidate())
            // mChart.moveViewTo(data.getXValCount()-7, 55f,
            // AxisDependency.LEFT);
        }
    }
 
Example 11
Source File: PowerChart.java    From AndroidApp with GNU Affero General Public License v3.0 5 votes vote down vote up
private LineData createData() {
    LineDataSet powerDataset = createDataSet();

    LineData ld = new LineData();
    ld.addDataSet(powerDataset);
    powerChart.setData(ld);
    return  ld;
}
 
Example 12
Source File: StatisticsFragment.java    From HeartBeat with Apache License 2.0 5 votes vote down vote up
private LineData getWeekThoughtData() {
    LineData d = new LineData();

    ArrayList<Entry> entries = new ArrayList<>();

    for (int i=0; i<WEEK_COUNT; i++) {
        entries.add(
                new BarEntry(
                        new GetCountSpecDayApi(getActivity()).exec(
                                TimeUtils.calendarDaysBefore(WEEK_COUNT-i),
                                GetCountSpecDayApi.THOUGHT
                        ),i
                )
        );
    }

    LineDataSet set = new LineDataSet(entries, getString(R.string.thought_count_text));
    set.setColor(mAccentColor);
    set.setLineWidth(2.5f);
    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    set.setCircleColor(mPrimaryColorDark);
    set.setCircleSize(5f);
    set.setFillColor(mPrimaryColor);
    set.setDrawValues(true);
    set.setValueFormatter(new IntValueFormatter());
    d.addDataSet(set);
    return d;
}
 
Example 13
Source File: Utils.java    From Aria2App with GNU General Public License v3.0 4 votes vote down vote up
public static void setupChart(@NonNull LineChart chart, boolean small, @ColorRes @Nullable Integer fgColorRes) {
    chart.clear();

    int fgColor;
    Context context = chart.getContext();
    if (fgColorRes == null)
        fgColor = CommonUtils.resolveAttrAsColor(context, R.attr.colorOnSurface);
    else
        fgColor = ContextCompat.getColor(context, fgColorRes);

    chart.setDescription(null);
    chart.setDrawGridBackground(false);
    chart.setBackgroundColor(Color.alpha(0));
    chart.setTouchEnabled(false);

    Legend legend = chart.getLegend();
    legend.setTextColor(fgColor);
    legend.setEnabled(true);

    LineData data = new LineData();
    data.setValueTextColor(fgColor);
    chart.setData(data);

    YAxis ya = chart.getAxisLeft();
    ya.setAxisLineColor(fgColor);
    ya.setTextColor(fgColor);
    ya.setTextSize(small ? 8 : 9);
    ya.setAxisMinimum(0);
    ya.setDrawAxisLine(false);
    ya.setLabelCount(small ? 4 : 8, true);
    ya.setEnabled(true);
    ya.setDrawGridLines(true);
    ya.setGridColor(fgColor);
    ya.setValueFormatter(new CustomYAxisValueFormatter());

    chart.getAxisRight().setEnabled(false);
    chart.getXAxis().setEnabled(false);

    data.addDataSet(initUploadSet(context));
    data.addDataSet(initDownloadSet(context));

    chart.invalidate();
}
 
Example 14
Source File: TickChart.java    From android-kline with Apache License 2.0 4 votes vote down vote up
public void addEntry(HisData hisData) {
    hisData = DataUtils.calculateHisData(hisData, mList);
    LineData data = mChart.getData();

    if (data != null) {
        ILineDataSet setSell = data.getDataSetByIndex(DATA_SET_PRICE);
        if (setSell == null) {
            setSell = createSet(TYPE_FULL);
            data.addDataSet(setSell);
        }
        ILineDataSet aveSet = data.getDataSetByIndex(DATA_SET_AVE);
        if (aveSet == null) {
            aveSet = createSet(DATA_SET_AVE);
            data.addDataSet(aveSet);
        }

        int index = mList.indexOf(hisData);
        if (index >= 0) {
            mList.remove(hisData);
            data.removeEntry(index, DATA_SET_PRICE);
            data.removeEntry(index, DATA_SET_AVE);
        }
        mList.add(hisData);
        float price = (float) hisData.getClose();
        data.addEntry(new Entry(setSell.getEntryCount(), price), DATA_SET_PRICE);
        data.addEntry(new Entry(setSell.getEntryCount(), (float) hisData.getAvePrice()), DATA_SET_AVE);

        ILineDataSet paddingSet = data.getDataSetByIndex(DATA_SET_PADDING);
        if (paddingSet == null) {
            paddingSet = createSet(TYPE_DASHED);
            data.addDataSet(paddingSet);
        }

        int count = paddingSet.getEntryCount();

        if (count > PADDING_COUNT && index < 0) {
            count--;
        }
        paddingSet.clear();
        for (int i = 0; i < count; i++) {
            paddingSet.addEntry(new Entry(setSell.getEntryCount() + i, price));
        }

        Highlight chartHighlighter = new Highlight(setSell.getEntryCount() + paddingSet.getEntryCount(), price, DATA_SET_PADDING);
        mChart.highlightValue(chartHighlighter);

        data.notifyDataChanged();
        mChart.notifyDataSetChanged();
        mChart.invalidate();
    }
}