首页 > 解决方案 > Bar graph X axis labels disappeared after Shunting the viewport to show the full width of bars

问题描述

In my android code, I am creating a bar graph with x axis labeled with dates (String), and y axis being double values. Everything was fine, but when I had to Shunt the viewport to show the full width of the first and last bars, the dates disappeared on the X axis. Only 2 of them are visible, and 3 are not. How can I fix the problem?

Below is my code:

//BAR Graph:
//Only possible to make bar graph for 2 weeks or more
GraphView barGraph = (GraphView) findViewById(R.id.bar_graph);

 if (MainActivity.weekLabelsArray.length <= 1)
     barGraph.setVisibility(GraphView.GONE);

else {

     ArrayList<DataPoint> barData = MainActivity.weeklyData;
     BarGraphSeries<DataPoint> barSeries = new BarGraphSeries<>();

     for (DataPoint d : barData)
         barSeries.appendData(d, true, 30);
     barGraph.addSeries(barSeries);


     // styling
     barSeries.setValueDependentColor(new ValueDependentColor<DataPoint>() {
         @Override
         public int get(DataPoint data) {

             double expense = data.getY();
             if (expense > 500)
                 return Color.rgb(255, 0, 0);
             else if (expense > 200)
                 return Color.rgb(0, 0, 255);
             else
                 return Color.rgb(0, 220, 0);

         }
     });

     barSeries.setSpacing(10);


     barSeries.setDrawValuesOnTop(true);
     barSeries.setValuesOnTopColor(Color.RED);

     //All this below just to make the x axis labeled with the dates
     barGraph.getViewport().setScrollable(true);
     barGraph.getViewport().setYAxisBoundsManual(true);
     barGraph.getViewport().setXAxisBoundsManual(true);
     StaticLabelsFormatter staticLabelsFormatterBar = new StaticLabelsFormatter(barGraph);
     staticLabelsFormatterBar.setHorizontalLabels(MainActivity.weekLabelsArray);
     barGraph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatterBar);

     //adjusting labels and other stuff
     barGraph.getGridLabelRenderer().setHorizontalLabelsAngle(100);
     barGraph.getViewport().setMinY(0);
     barGraph.getGridLabelRenderer().setTextSize(40);
     barGraph.getGridLabelRenderer().reloadStyles();
     barGraph.setTitle("Total Weekly Expenses");


     //adjusting bars width
     double xInterval=1.0;
     barGraph.getViewport().setXAxisBoundsManual(true);
     barGraph.getViewport().setMinX(barSeries.getLowestValueX() - (xInterval/2.0));
     barGraph.getViewport().setMaxX(barSeries.getHighestValueX() + (xInterval/2.0));
     barGraph.getViewport().setYAxisBoundsManual(true);
     barGraph.getViewport().setMinY(barSeries.getLowestValueY() );
     barGraph.getViewport().setMaxY(barSeries.getHighestValueY()+50);

     barGraph.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.NONE );
     barGraph.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.HORIZONTAL);

The dates disappeared after adding the following exact lines of code:

         barGraph.getViewport().setXAxisBoundsManual(true);
         barGraph.getViewport().setMinX(barSeries.getLowestValueX() - (xInterval/2.0));
         barGraph.getViewport().setMaxX(barSeries.getHighestValueX() + (xInterval/2.0));

Here's how my graph looks like with missing date labels on the x Axis:

enter image description here

标签: androidandroid-graphview

解决方案


推荐阅读