簡體   English   中英

減少Android Graphview庫中的條形圖寬度

[英]Reducing Bar graph width in Android Graphview library

我正在使用GraphView庫創建如圖所示的條形圖,我需要減小條形的寬度並增加條形之間的空間。 注意-紅色條表示當前正在獲取,而我實際需要的顯示為黑色。 在此處輸入圖片說明

以下是上圖的代碼段:

    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);

目前尚無內置方法可以執行此操作。 但是如果需要,您可以輕松修改代碼。 您可以在此處添加間距的點是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM