Java Code Examples for com.github.mikephil.charting.components.XAxis#setLabelCount()

The following examples show how to use com.github.mikephil.charting.components.XAxis#setLabelCount() . 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: PFAChart.java    From privacy-friendly-shopping-list with Apache License 2.0 6 votes vote down vote up
private void setXlabels(List<String> labelList)
{
    int valuesSelectedItemPos = cache.getValuesSpinner().getSelectedItemPosition();
    String[] labels = new String[ labelList.size() ];
    labelList.toArray(labels);

    PFAXAxisLabels xFormatter = new PFAXAxisLabels(labels);
    chart.setMarkerView(new PFAMarkerView(context, xFormatter, valuesSelectedItemPos, cache.getNumberScale()));

    XAxis xAxis = chart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setGranularity(1f);
    xAxis.setLabelCount(5);
    xAxis.setValueFormatter(xFormatter);
}
 
Example 2
Source File: GraphUtils.java    From your-local-weather with GNU General Public License v3.0 4 votes vote down vote up
public static void setupXAxis(XAxis x,
                              List<DetailedWeatherForecast> weatherForecastList,
                              int textColorId,
                              Float textSize,
                              AppPreference.GraphGridColors gridColor,
                              Locale locale) {
    x.removeAllLimitLines();
    Map<Integer, Long> hourIndexes = new HashMap<>();

    int lastDayOflimitLine = 0;
    for (int i = 0; i < weatherForecastList.size(); i++) {
        hourIndexes.put(i, weatherForecastList.get(i).getDateTime());
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(weatherForecastList.get(i).getDateTime() * 1000);
        if (cal.get(Calendar.DAY_OF_YEAR) != lastDayOflimitLine) {
            Calendar calOfPreviousRecord = Calendar.getInstance();
            int previousRecordHour = 24;
            if (i > 0) {
                calOfPreviousRecord.setTimeInMillis(weatherForecastList.get(i - 1).getDateTime() * 1000);
                previousRecordHour = calOfPreviousRecord.get(Calendar.HOUR_OF_DAY);
            }
            int currentHour = cal.get(Calendar.HOUR_OF_DAY);
            float timeSpan = (24 - previousRecordHour) + currentHour;
            float dayLine = currentHour / timeSpan;
            float midnight = i - dayLine;
            float hour6 = midnight + (6 / timeSpan);
            float hour12 = midnight + (12 / timeSpan);
            float hour18 = midnight + (18 / timeSpan);
            LimitLine limitLine = new LimitLine(midnight);
            limitLine.setLineColor(gridColor.getMainGridColor());
            limitLine.setLineWidth(0.5f);
            x.addLimitLine(limitLine);
            /*LimitLine limitLine6 = new LimitLine(hour6, "");
            limitLine6.setLineColor(Color.LTGRAY);
            limitLine6.setLineWidth(0.5f);
            x.addLimitLine(limitLine6);*/
            LimitLine limitLine12 = new LimitLine(hour12);
            limitLine12.setLineColor(gridColor.getSecondaryGridColor());
            limitLine12.setLineWidth(0.5f);
            x.addLimitLine(limitLine12);
            /*LimitLine limitLine18 = new LimitLine(hour18, "");
            limitLine18.setLineColor(Color.LTGRAY);
            limitLine18.setLineWidth(0.5f);
            x.addLimitLine(limitLine18);*/
            lastDayOflimitLine = cal.get(Calendar.DAY_OF_YEAR);
        }
    }

    x.setEnabled(true);
    x.setPosition(XAxis.XAxisPosition.BOTTOM);
    x.setDrawGridLines(false);
    x.setLabelCount(25, true);
    x.setTextColor(textColorId);
    x.setValueFormatter(new XAxisValueFormatter(hourIndexes, locale));
    x.setDrawLimitLinesBehindData(true);

    if (textSize != null) {
        x.setTextSize(textSize);
    }
}
 
Example 3
Source File: BarChartActivity.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_barchart);

    setTitle("BarChartActivity");

    tvX = findViewById(R.id.tvXMax);
    tvY = findViewById(R.id.tvYMax);

    seekBarX = findViewById(R.id.seekBar1);
    seekBarY = findViewById(R.id.seekBar2);

    seekBarY.setOnSeekBarChangeListener(this);
    seekBarX.setOnSeekBarChangeListener(this);

    chart = findViewById(R.id.chart1);
    chart.setOnChartValueSelectedListener(this);

    chart.setDrawBarShadow(false);
    chart.setDrawValueAboveBar(true);

    chart.getDescription().setEnabled(false);

    // if more than 60 entries are displayed in the chart, no values will be
    // drawn
    chart.setMaxVisibleValueCount(60);

    // scaling can now only be done on x- and y-axis separately
    chart.setPinchZoom(false);

    chart.setDrawGridBackground(false);
    // chart.setDrawYLabels(false);

    ValueFormatter xAxisFormatter = new DayAxisValueFormatter(chart);

    XAxis xAxis = chart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTTOM);
    xAxis.setTypeface(tfLight);
    xAxis.setDrawGridLines(false);
    xAxis.setGranularity(1f); // only intervals of 1 day
    xAxis.setLabelCount(7);
    xAxis.setValueFormatter(xAxisFormatter);

    ValueFormatter custom = new MyValueFormatter("$");

    YAxis leftAxis = chart.getAxisLeft();
    leftAxis.setTypeface(tfLight);
    leftAxis.setLabelCount(8, false);
    leftAxis.setValueFormatter(custom);
    leftAxis.setPosition(YAxisLabelPosition.OUTSIDE_CHART);
    leftAxis.setSpaceTop(15f);
    leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

    YAxis rightAxis = chart.getAxisRight();
    rightAxis.setDrawGridLines(false);
    rightAxis.setTypeface(tfLight);
    rightAxis.setLabelCount(8, false);
    rightAxis.setValueFormatter(custom);
    rightAxis.setSpaceTop(15f);
    rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

    Legend l = chart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(false);
    l.setForm(LegendForm.SQUARE);
    l.setFormSize(9f);
    l.setTextSize(11f);
    l.setXEntrySpace(4f);

    XYMarkerView mv = new XYMarkerView(this, xAxisFormatter);
    mv.setChartView(chart); // For bounds control
    chart.setMarker(mv); // Set the marker to the chart

    // setting data
    seekBarY.setProgress(50);
    seekBarX.setProgress(12);

    // chart.setDrawLegend(false);
}
 
Example 4
Source File: StackedBarActivityNegative.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_age_distribution);

    setTitle("StackedBarActivityNegative");

    chart = findViewById(R.id.chart1);
    chart.setOnChartValueSelectedListener(this);
    chart.setDrawGridBackground(false);
    chart.getDescription().setEnabled(false);

    // scaling can now only be done on x- and y-axis separately
    chart.setPinchZoom(false);

    chart.setDrawBarShadow(false);
    chart.setDrawValueAboveBar(true);
    chart.setHighlightFullBarEnabled(false);

    chart.getAxisLeft().setEnabled(false);
    chart.getAxisRight().setAxisMaximum(25f);
    chart.getAxisRight().setAxisMinimum(-25f);
    chart.getAxisRight().setDrawGridLines(false);
    chart.getAxisRight().setDrawZeroLine(true);
    chart.getAxisRight().setLabelCount(7, false);
    chart.getAxisRight().setValueFormatter(new CustomFormatter());
    chart.getAxisRight().setTextSize(9f);

    XAxis xAxis = chart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTH_SIDED);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(false);
    xAxis.setTextSize(9f);
    xAxis.setAxisMinimum(0f);
    xAxis.setAxisMaximum(110f);
    xAxis.setCenterAxisLabels(true);
    xAxis.setLabelCount(12);
    xAxis.setGranularity(10f);
    xAxis.setValueFormatter(new ValueFormatter() {

        private final DecimalFormat format = new DecimalFormat("###");

        @Override
        public String getFormattedValue(float value) {
            return format.format(value) + "-" + format.format(value + 10);
        }
    });

    Legend l = chart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(false);
    l.setFormSize(8f);
    l.setFormToTextSpace(4f);
    l.setXEntrySpace(6f);

    // IMPORTANT: When using negative values in stacked bars, always make sure the negative values are in the array first
    ArrayList<BarEntry> values = new ArrayList<>();
    values.add(new BarEntry(5, new float[]{ -10, 10 }));
    values.add(new BarEntry(15, new float[]{ -12, 13 }));
    values.add(new BarEntry(25, new float[]{ -15, 15 }));
    values.add(new BarEntry(35, new float[]{ -17, 17 }));
    values.add(new BarEntry(45, new float[]{ -19, 20 }));
    values.add(new BarEntry(45, new float[]{ -19, 20 }, getResources().getDrawable(R.drawable.star)));
    values.add(new BarEntry(55, new float[]{ -19, 19 }));
    values.add(new BarEntry(65, new float[]{ -16, 16 }));
    values.add(new BarEntry(75, new float[]{ -13, 14 }));
    values.add(new BarEntry(85, new float[]{ -10, 11 }));
    values.add(new BarEntry(95, new float[]{ -5, 6 }));
    values.add(new BarEntry(105, new float[]{ -1, 2 }));

    BarDataSet set = new BarDataSet(values, "Age Distribution");
    set.setDrawIcons(false);
    set.setValueFormatter(new CustomFormatter());
    set.setValueTextSize(7f);
    set.setAxisDependency(YAxis.AxisDependency.RIGHT);
    set.setColors(Color.rgb(67,67,72), Color.rgb(124,181,236));
    set.setStackLabels(new String[]{
            "Men", "Women"
    });

    BarData data = new BarData(set);
    data.setBarWidth(8.5f);
    chart.setData(data);
    chart.invalidate();
}
 
Example 5
Source File: BarChartPositiveNegative.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_barchart_noseekbar);

    setTitle("BarChartPositiveNegative");

    chart = findViewById(R.id.chart1);
    chart.setBackgroundColor(Color.WHITE);
    chart.setExtraTopOffset(-30f);
    chart.setExtraBottomOffset(10f);
    chart.setExtraLeftOffset(70f);
    chart.setExtraRightOffset(70f);

    chart.setDrawBarShadow(false);
    chart.setDrawValueAboveBar(true);

    chart.getDescription().setEnabled(false);

    // scaling can now only be done on x- and y-axis separately
    chart.setPinchZoom(false);

    chart.setDrawGridBackground(false);

    XAxis xAxis = chart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTTOM);
    xAxis.setTypeface(tfRegular);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(false);
    xAxis.setTextColor(Color.LTGRAY);
    xAxis.setTextSize(13f);
    xAxis.setLabelCount(5);
    xAxis.setCenterAxisLabels(true);
    xAxis.setGranularity(1f);

    YAxis left = chart.getAxisLeft();
    left.setDrawLabels(false);
    left.setSpaceTop(25f);
    left.setSpaceBottom(25f);
    left.setDrawAxisLine(false);
    left.setDrawGridLines(false);
    left.setDrawZeroLine(true); // draw a zero line
    left.setZeroLineColor(Color.GRAY);
    left.setZeroLineWidth(0.7f);
    chart.getAxisRight().setEnabled(false);
    chart.getLegend().setEnabled(false);

    // THIS IS THE ORIGINAL DATA YOU WANT TO PLOT
    final List<Data> data = new ArrayList<>();
    data.add(new Data(0f, -224.1f, "12-29"));
    data.add(new Data(1f, 238.5f, "12-30"));
    data.add(new Data(2f, 1280.1f, "12-31"));
    data.add(new Data(3f, -442.3f, "01-01"));
    data.add(new Data(4f, -2280.1f, "01-02"));

    xAxis.setValueFormatter(new ValueFormatter() {
        @Override
        public String getFormattedValue(float value) {
            return data.get(Math.min(Math.max((int) value, 0), data.size()-1)).xAxisValue;
        }
    });

    setData(data);
}
 
Example 6
Source File: TimeLineView.java    From android-kline with Apache License 2.0 4 votes vote down vote up
public void initDatas(List<HisData>... hisDatas) {
        // 设置标签数量,并让标签居中显示
        XAxis xAxis = mChartVolume.getXAxis();
        xAxis.setLabelCount(hisDatas.length + 1, true);
        xAxis.setAvoidFirstLastClipping(false);
        xAxis.setCenterAxisLabels(true);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                value += 1; // 这里不设置+1会有bug
                if (mData.isEmpty()) {
                    return "";
                }
                if (value < 0) {
                    value = 0;
                }
                if (value < mData.size()) {
                    return DateUtils.formatDate(mData.get((int) value).getDate(), mDateFormat);
                }
                return "";
            }
        });
        mData.clear();
        ArrayList<ILineDataSet> sets = new ArrayList<>();
        ArrayList<IBarDataSet> barSets = new ArrayList<>();

        for (List<HisData> hisData : hisDatas) {
            hisData = DataUtils.calculateHisData(hisData);
            ArrayList<Entry> priceEntries = new ArrayList<>(INIT_COUNT);
            ArrayList<Entry> aveEntries = new ArrayList<>(INIT_COUNT);
            ArrayList<Entry> paddingEntries = new ArrayList<>(INIT_COUNT);
            ArrayList<BarEntry> barPaddingEntries = new ArrayList<>(INIT_COUNT);
            ArrayList<BarEntry> barEntries = new ArrayList<>(INIT_COUNT);

            for (int i = 0; i < hisData.size(); i++) {
                HisData t = hisData.get(i);
                priceEntries.add(new Entry(i + mData.size(), (float) t.getClose()));
                aveEntries.add(new Entry(i + mData.size(), (float) t.getAvePrice()));
                barEntries.add(new BarEntry(i + mData.size(), (float) t.getVol(), t));
            }
            if (!hisData.isEmpty() && hisData.size() < INIT_COUNT / hisDatas.length) {
                for (int i = hisData.size(); i < INIT_COUNT / hisDatas.length; i++) {
                    paddingEntries.add(new Entry(i, (float) hisData.get(hisData.size() - 1).getClose()));
                    barPaddingEntries.add(new BarEntry(i, (float) hisData.get(hisData.size() - 1).getClose()));
                }
            }
            sets.add(setLine(NORMAL_LINE_5DAY, priceEntries));
            sets.add(setLine(AVE_LINE, aveEntries));
            sets.add(setLine(INVISIABLE_LINE, paddingEntries));
            barSets.add(setBar(barEntries, NORMAL_LINE));
            barSets.add(setBar(barPaddingEntries, INVISIABLE_LINE));
            barSets.add(setBar(barPaddingEntries, INVISIABLE_LINE));
            mData.addAll(hisData);
        }

        LineData lineData = new LineData(sets);

        CombinedData combinedData = new CombinedData();
        combinedData.setData(lineData);
        mChartPrice.setData(combinedData);
        mChartPrice.setVisibleXRange(MAX_COUNT, MIN_COUNT);
        mChartPrice.notifyDataSetChanged();
//        mChartPrice.moveViewToX(combinedData.getEntryCount());
        moveToLast(mChartVolume);


        BarData barData = new BarData(barSets);
        barData.setBarWidth(0.75f);
        CombinedData combinedData2 = new CombinedData();
        combinedData2.setData(barData);
        mChartVolume.setData(combinedData2);
        mChartVolume.setVisibleXRange(MAX_COUNT, MIN_COUNT);
        mChartVolume.notifyDataSetChanged();
        mChartVolume.moveViewToX(combinedData2.getEntryCount());

        mChartPrice.getXAxis().setAxisMaximum(combinedData.getXMax() + 0.5f);
        mChartVolume.getXAxis().setAxisMaximum(mChartVolume.getData().getXMax() + 0.5f);

        mChartPrice.zoom(MAX_COUNT * 1f / INIT_COUNT, 0, 0, 0);
        mChartVolume.zoom(MAX_COUNT * 1f / INIT_COUNT, 0, 0, 0);

        setDescription(mChartVolume, "成交量 " + getLastData().getVol());
    }
 
Example 7
Source File: BaseView.java    From android-kline with Apache License 2.0 4 votes vote down vote up
protected void initBottomChart(AppCombinedChart chart) {
        chart.setScaleEnabled(true);
        chart.setDrawBorders(false);
        chart.setBorderWidth(1);
        chart.setDragEnabled(true);
        chart.setScaleYEnabled(false);
        chart.setAutoScaleMinMaxEnabled(true);
        chart.setDragDecelerationEnabled(false);
        chart.setHighlightPerDragEnabled(false);
        Legend lineChartLegend = chart.getLegend();
        lineChartLegend.setEnabled(false);


        XAxis xAxisVolume = chart.getXAxis();
        xAxisVolume.setDrawLabels(true);
        xAxisVolume.setDrawAxisLine(false);
        xAxisVolume.setDrawGridLines(false);
        xAxisVolume.setTextColor(mAxisColor);
        xAxisVolume.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxisVolume.setLabelCount(3, true);
        xAxisVolume.setAvoidFirstLastClipping(true);
        xAxisVolume.setAxisMinimum(-0.5f);

        xAxisVolume.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if (mData.isEmpty()) {
                    return "";
                }
                if (value < 0) {
                    value = 0;
                }
                if (value < mData.size()) {
                    return DateUtils.formatDate(mData.get((int) value).getDate(), mDateFormat);
                }
                return "";
            }
        });

        YAxis axisLeftVolume = chart.getAxisLeft();
        axisLeftVolume.setDrawLabels(true);
        axisLeftVolume.setDrawGridLines(false);
        axisLeftVolume.setLabelCount(3, true);
        axisLeftVolume.setDrawAxisLine(false);
        axisLeftVolume.setTextColor(mAxisColor);
        axisLeftVolume.setSpaceTop(10);
        axisLeftVolume.setSpaceBottom(0);
        axisLeftVolume.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        /*axisLeftVolume.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                String s;
                if (value > 10000) {
                    s = (int) (value / 10000) + "w";
                } else if (value > 1000) {
                    s = (int) (value / 1000) + "k";
                } else {
                    s = (int) value + "";
                }
                return String.format(Locale.getDefault(), "%1$5s", s);
            }
        });
*/
        Transformer leftYTransformer = chart.getRendererLeftYAxis().getTransformer();
        ColorContentYAxisRenderer leftColorContentYAxisRenderer = new ColorContentYAxisRenderer(chart.getViewPortHandler(), chart.getAxisLeft(), leftYTransformer);
        leftColorContentYAxisRenderer.setLabelInContent(true);
        leftColorContentYAxisRenderer.setUseDefaultLabelXOffset(false);
        chart.setRendererLeftYAxis(leftColorContentYAxisRenderer);

        //右边y
        YAxis axisRightVolume = chart.getAxisRight();
        axisRightVolume.setDrawLabels(false);
        axisRightVolume.setDrawGridLines(false);
        axisRightVolume.setDrawAxisLine(false);

    }
 
Example 8
Source File: QuotationFragment.java    From bitshares_wallet with MIT License 4 votes vote down vote up
private void initChart() {
    int colorHomeBg = getResources().getColor(android.R.color.transparent);
    int colorLine = getResources().getColor(R.color.common_divider);
    int colorText = getResources().getColor(R.color.text_grey_light);

    mChart.setDescription(null);
    mChart.setDrawGridBackground(true);
    mChart.setBackgroundColor(colorHomeBg);
    mChart.setGridBackgroundColor(colorHomeBg);
    mChart.setScaleYEnabled(false);
    mChart.setPinchZoom(true);

    mChart.setNoDataText(getString(R.string.main_activity_loading));
    mChart.setAutoScaleMinMaxEnabled(true);
    mChart.setDragEnabled(true);
    mChart.setDoubleTapToZoomEnabled(false);

    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(true);
    xAxis.setGridColor(colorLine);
    xAxis.setTextColor(colorText);
    xAxis.setLabelCount(3);
    //xAxis.setSpaceBetweenLabels(4);

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setLabelCount(4, false);
    leftAxis.setDrawGridLines(true);
    leftAxis.setDrawAxisLine(true);
    leftAxis.setGridColor(colorLine);
    leftAxis.setTextColor(colorText);

    YAxis rightAxis = mChart.getAxisRight();
    rightAxis.setTextSize(8);
    rightAxis.setLabelCount(5, false);
    rightAxis.setDrawGridLines(false);
    rightAxis.setDrawAxisLine(true);
    rightAxis.setGridColor(colorLine);
    rightAxis.setTextColor(colorText);
    rightAxis.setAxisMinimum(0);
}