动态标签不居中对齐条

时间:2018-03-23 04:26:48

标签: android bar-chart mpandroidchart

我正在开发一个带图表的应用。我在应用程序中使用了MPAndroidChart,但是当我将动态标签设置为X轴时,标签不会定位每个条形图的中心。

我尝试了来自不同网站和StackOverflow的许多答案但没有成功。也许答案与我的问题没有直接关系。

以下是java代码:

List<BarEntry> entries = new ArrayList<>();
    List<Integer> barColors = new ArrayList<>();
    final ArrayList<String> xLabel = new ArrayList<>();
    String[] labels;

    labels = xLabel.toArray(new String[xLabel.size()]);

     BarDataSet set = new BarDataSet(entries, "");
            set.setColors(barColors);
            set.setValueTextSize(15);
            BarData data = new BarData(set);
            data.setValueFormatter(new PercentFormatter());
            chart.setDrawBarShadow(false);
            chart.setDrawValueAboveBar(true);
            chart.setData(data);
            chart.setDrawGridBackground(false);
            chart.setFitBars(true);
            chart.invalidate();
            chart.setTouchEnabled(false);
            chart.setDragEnabled(false);
            chart.setDrawBorders(false);
            chart.getLegend().setEnabled(false);   // Hide the legend


            chart.getAxisLeft().setDrawAxisLine(false);
            chart.getAxisRight().setDrawAxisLine(false);
            chart.getXAxis().setDrawGridLines(false);
            Description description = new Description();
            description.setText("");
            chart.setDescription(description);
            chart.getRendererXAxis().getPaintAxisLabels().setTextAlign(Paint.Align.LEFT);

            YAxis yAxisRight = chart.getAxisRight();
            yAxisRight.setEnabled(false);

            YAxis yAxisLeft = chart.getAxisLeft();
            yAxisLeft.setEnabled(false);

            XAxis xAxis = chart.getXAxis();
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setAxisMinimum(data.getXMin() - .5f);
            xAxis.setAxisMaximum(data.getXMax() + .5f);
            xAxis.setCenterAxisLabels(false);
            xAxis.setLabelCount(xLabel.size());
            xAxis.setDrawGridLines(false);
            xAxis.setLabelRotationAngle(-45f);
            xAxis.setValueFormatter(new LabelFormatter(labels));

            YAxis leftAxis = chart.getAxisLeft();
            leftAxis.setEnabled(false);
            YAxis rightAxis = chart.getAxisRight();
            rightAxis.setEnabled(false);
            rightAxis.setValueFormatter(new PercentFormatter());
            Legend legend = chart.getLegend();
            legend.setEnabled(false);

以下是我得到的结果。

enter image description here

我想要标签&#34; WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW应该放在每个酒吧的中心。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_barchart);

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

    mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
    mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);

    mChart = (BarChart) findViewById(R.id.chart1);
    mChart.setOnChartValueSelectedListener(this);

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

    mChart.getDescription().setEnabled(false);

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

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

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

    IAxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(mChart);

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

    IAxisValueFormatter custom = new MyAxisValueFormatter();

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

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

    Legend l = mChart.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);
    // l.setExtra(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
    // "def", "ghj", "ikl", "mno" });
    // l.setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
    // "def", "ghj", "ikl", "mno" });

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

    setData(12, 50);

    // setting data
    mSeekBarY.setProgress(50);
    mSeekBarX.setProgress(12);

    mSeekBarY.setOnSeekBarChangeListener(this);
    mSeekBarX.setOnSeekBarChangeListener(this);

    // mChart.setDrawLegend(false);
}