2

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

Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83
  • Lots going on there. Does playing around with the data or enabling/disabling different bits of formatting narrow done where the problem is at all? e.g. is it always columns 0 and 2 that show, or always the green ones? The contents of 'MainActivity.weekLabelsArray' might also be relevant. – Tbadams Nov 26 '19 at 17:32
  • 1
    @Tbadams, I can assure you that the data is completely fine and no not just for the green ones. There seems to be something with the graphview library itself because based on what I understand, the labels are not tied to specific locations. Once you zoom in and out or shift the viewport, these labels get distorted randomly based on what I found. I experimented with different data and each time the labels appear differently. The only solution so far is to never shunt the viewport and rather keep it as it is. – Traveling Salesman Nov 26 '19 at 20:53
  • where is layout xml? I mean your view – Akhil Surapuram Nov 27 '19 at 17:24
  • is GraphView, bar data seres are of android class. I couldn't find any of those classes in android. what library are you using? – Akhil Surapuram Nov 27 '19 at 17:41
  • @TravelingSalesman It sounds like you've done due diligence to make sure the problem isn't on your end; if so, your options are mainly 1) file a bug with the library maintainer (looks like https://github.com/jjoe64/GraphView/issues ?) and hope they fix it or 2) try and fix or bypass the problem yourself. If the issue is a bug in the library there's not much that can be done without wading into its source. – Tbadams Dec 02 '19 at 16:58

0 Answers0