在Android Graphview库中减少条形图宽度

时间:2014-08-26 12:15:32

标签: android graph charts bar-chart android-graphview

我正在使用GraphView Library创建条形图,如图所示,我需要减少条形宽度并增加条形之间的空间。 注意 - 目前我正在获得红色条,我实际需要的是黑色。 enter image description here

以下是上图的代码段:

    GraphViewSeriesStyle barStyle = new GraphViewSeriesStyle();
    barStyle.thickness = 5;
    barStyle.setValueDependentColor(new ValueDependentColor() {
        @Override
        public int get(GraphViewDataInterface data) {
            return Color.rgb(205, 0, 0);
        }
    });

    // init example series data
    GraphViewSeries scoreSeries = new GraphViewSeries(
            "HealthCare Bar Graph", barStyle, new GraphViewData[] {
                    new GraphViewData(1, rsCVD),
                    new GraphViewData(2, rsHypertension4),
                    new GraphViewData(3, rsHypertension2),
                    new GraphViewData(4, rsHypertension1) });


    GraphView graphView = new BarGraphView(this // context
            , "GRAPH_VIEW_HEADING" // heading
    );

    graphView.setHorizontalLabels(new String[] { "1", "2",
            "3", "4" });


    graphView.addSeries(scoreSeries);
    graphView.setViewPort(0, 25);
    graphView.setScalable(true);

    graphView.getGraphViewStyle().setHorizontalLabelsColor(Color.BLACK);
    graphView.getGraphViewStyle().setVerticalLabelsColor(Color.BLACK);

    graphView.getGraphViewStyle().setTextSize(16);
    graphView.getGraphViewStyle().setGridColor(Color.WHITE);
    // graphView.getGraphViewStyle().setLegendWidth(legendWidth)

    int maxValue = myScore+1;
    // search the interval between 2 vertical labels
    double interval;
    if (maxValue >= 0 && maxValue < 3) {
        interval = 0.5; // increment of 1 between each label
    } else if (maxValue >= 3 && maxValue < 55) {
        interval = 5; // increment of 5 between each label
    } else if (maxValue >= 55 && maxValue <= 110) {
        interval = 10; // increment of 10 between each label
    } else {
        interval = 20; // increment of 20 between each label
    }
    // search the top value of our graph
    int maxLabel = maxValue;
    while (maxLabel % interval != 0) {
        maxLabel++;
    }
    // set manual bounds
    graphView.setManualYAxisBounds(maxLabel, 0);
    // indicate number of vertical labels
    int numVerticalLabels = (int) ((int) maxLabel / interval + 1);
    Log.v(TAG, "numVerticalLabels: " + numVerticalLabels);
    graphView.getGraphViewStyle().setNumVerticalLabels(numVerticalLabels);

    graphView.getGraphViewStyle().setVerticalLabelsWidth(20);
    // graphView.getGraphViewStyle().setLegendWidth(20);
    graphView.getGraphViewStyle().setLegendSpacing(30);

    // LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
    m_llayForRiskGraphContainer.addView(graphView);

1 个答案:

答案 0 :(得分:0)

目前还没有构建方法来实现此目的。但如果您愿意,可以轻松修改代码。您可以在此处添加间距:

BarGraphView.java第95行:

float left = (i * colwidth) + horstart -offset;
float top = (border - y) + graphheight;
float right = ((i * colwidth) + horstart) + (colwidth - 1) -offset;
canvas.drawRect(left, top, right, graphheight + border - 1, paint);

只需向left添加几个像素,然后从right中删除一些像素,它就会显示出来。

https://github.com/jjoe64/GraphView/blob/master/src/main/java/com/jjoe64/graphview/BarGraphView.java#L97