【问题标题】:Reducing Bar graph width in Android Graphview library减少 Android Graphview 库中的条形图宽度
【发布时间】:2014-08-26 12:15:32
【问题描述】:

我正在使用 GraphView 库来创建如图所示的条形图,我需要减小条形的宽度并增加条形之间的空间。 注意-红色条是我目前得到的,而我真正需要的是黑色。

下面是上图的sn-p代码:

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

【问题讨论】:

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


    【解决方案1】:

    目前没有内置方法来执行此操作。但如果您愿意,您可以轻松修改代码。可以添加间距的地方在这里:

    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

    【讨论】:

    • GraphView 4.0 支持这个开箱即用
    • 例如。 barGraphSeries.setSpacing(15);
    • 有没有办法减少单个条形图塔(系列)的宽度。我知道setspacing 减少了图塔(系列)之间的宽度。
    猜你喜欢
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多