我使用图表库hellocharts-android来显示我的数据。

如何在给定的示例代码中从服务器向火提供价值?

从默认的示例代码:

private void generateDefaultData() {
    int numSubcolumns = 1;
    int numColumns = 8;
    // Column can have many subcolumns, here by default I use 1 subcolumn in each of 8 columns.
    List<Column> columns = new ArrayList<Column>();
    List<SubcolumnValue> values;
    for (int i = 0; i < numColumns; ++i) {

        values = new ArrayList<SubcolumnValue>();
        for (int j = 0; j < numSubcolumns; ++j) {
            values.add(new SubcolumnValue((float) Math.random() * 50f + 5, ChartUtils.pickColor()));
        }

        Column column = new Column(values);
        column.setHasLabels(hasLabels);
        column.setHasLabelsOnlyForSelected(hasLabelForSelected);
        columns.add(column);
    }

    data = new ColumnChartData(columns);

    if (hasAxes) {
        Axis axisX = new Axis();
        Axis axisY = new Axis().setHasLines(true);
        if (hasAxesNames) {
            axisX.setName("Axis X");
            axisY.setName("Axis Y");
        }
        data.setAxisXBottom(axisX);
        data.setAxisYLeft(axisY);
    } else {
        data.setAxisXBottom(null);
        data.setAxisYLeft(null);
    }

    chart.setColumnChartData(data);

}


我适合我的代码和结果:

private void generateDefaultData() {

    ApiInterface apiService = ApiClient.getVisitor().create(ApiInterface.class);
    Call<NewVisitorResponse> call = apiService.getVisitor();

    call.enqueue(new Callback<NewVisitorResponse>() {
        @Override
        public void onResponse(Call<NewVisitorResponse> call, Response<NewVisitorResponse> response) {

            listVisitor = response.body().getVisitor(); // Already done with all data I need from server

            int numSubcolumns = 1;
            int numColumns = 3;

            List<Column> columns = new ArrayList<Column>();
            List<SubcolumnValue> values;

            for (int i = 0; i < listVisitor.size(); ++i) {

                values = new ArrayList<SubcolumnValue>();
                for (int j = 0; j < numSubcolumns; ++j) {
                    values.add(new SubcolumnValue(Float.valueOf(listVisitor.get(i).getValue()), ChartUtils.pickColor()));
                }

                Column column = new Column(values);
                column.setHasLabels(hasLabels);
                column.setHasLabelsOnlyForSelected(hasLabelForSelected);
                columns.add(column);
            }

            data = new ColumnChartData(columns);

            if (hasAxes) {
                Axis axisX = new Axis();
                Axis axisY = new Axis().setHasLines(true);
                if (hasAxesNames) {
                    axisX.setName("Date");
                    axisY.setName("Value");
                }
                data.setAxisXBottom(axisX);
                data.setAxisYLeft(axisY);
            } else {
                data.setAxisXBottom(null);
                data.setAxisYLeft(null);
            }

            chart.setColumnChartData(data);
        }

        @Override
        public void onFailure(Call<NewVisitorResponse> call, Throwable t) {
            Snackbar.make(getView(), "CONNECTION ERROR. PLEASE TRY AGAIN", Snackbar.LENGTH_LONG).setAction("Action", null).show();
        }
    });

    hidePb();
}


我已经做过,成功的是:



显示的日期数与屏幕上显示的日期数一致
我必须显示从服务器上的日期起的总数据。您可以在Float.valueOf(listVisitor.get(i).getValue())部分中看到



我的问题是


如何更改要由日期数据填充的图表上的标签
从循环?数据应填写listVisitor.get(i).getDate()


java - 如何修改图表库类以从服务器获取数据-LMLPHP


我要显示的是替换现有值0 2 4 6 8 10 12,
成为我服务器listVisitor.get(i).getDate()中的数据


我假设必须替换/修改包含的librariesColumnSubcolumnValue类。我创建了两个新类,它们的内容与原始库相同,但始终失败。在这种情况下也许我可以提供帮助,该怎么办?

我附上这两节课

Column.java

package lecho.lib.hellocharts.model;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.formatter.ColumnChartValueFormatter;
import lecho.lib.hellocharts.formatter.SimpleColumnChartValueFormatter;
import lecho.lib.hellocharts.view.Chart;

/**
 * Single column for ColumnChart. One column can be divided into multiple sub-columns(ColumnValues) especially for
 * stacked ColumnChart.
 * Note: you can set X value for columns or sub-columns, columns are by default indexed from 0 to numOfColumns-1 and
 * column index is used as column X value, so first column has X value 0, second clumn has X value 1 etc.
 * If you want to display AxisValue for given column you should initialize AxisValue with X value of that column.
 */
public class Column {
    private boolean hasLabels = false;
    private boolean hasLabelsOnlyForSelected = false;
    private ColumnChartValueFormatter formatter = new SimpleColumnChartValueFormatter();
    // TODO: consider Collections.emptyList()
    private List<SubcolumnValue> values = new ArrayList<SubcolumnValue>();

    public Column() {

    }

    public Column(List<SubcolumnValue> values) {
        setValues(values);
    }

    public Column(Column column) {
        this.hasLabels = column.hasLabels;
        this.hasLabelsOnlyForSelected = column.hasLabelsOnlyForSelected;
        this.formatter = column.formatter;

        for (SubcolumnValue columnValue : column.values) {
            this.values.add(new SubcolumnValue(columnValue));
        }
    }

    public void update(float scale) {
        for (SubcolumnValue value : values) {
            value.update(scale);
        }

    }

    public void finish() {
        for (SubcolumnValue value : values) {
            value.finish();
        }
    }

    public List<SubcolumnValue> getValues() {
        return values;
    }

    public Column setValues(List<SubcolumnValue> values) {
        if (null == values) {
            this.values = new ArrayList<SubcolumnValue>();
        } else {
            this.values = values;
        }
        return this;
    }

    public boolean hasLabels() {
        return hasLabels;
    }

    public Column setHasLabels(boolean hasLabels) {
        this.hasLabels = hasLabels;
        if (hasLabels) {
            this.hasLabelsOnlyForSelected = false;
        }
        return this;
    }

    /**
     * @see #setHasLabelsOnlyForSelected(boolean)
     */
    public boolean hasLabelsOnlyForSelected() {
        return hasLabelsOnlyForSelected;
    }

    /**
     * Set true if you want to show value labels only for selected value, works best when chart has
     * isValueSelectionEnabled set to true {@link Chart#setValueSelectionEnabled(boolean)}.
     */
    public Column setHasLabelsOnlyForSelected(boolean hasLabelsOnlyForSelected) {
        this.hasLabelsOnlyForSelected = hasLabelsOnlyForSelected;
        if (hasLabelsOnlyForSelected) {
            this.hasLabels = false;
        }
        return this;
    }

    public ColumnChartValueFormatter getFormatter() {
        return formatter;
    }

    public Column setFormatter(ColumnChartValueFormatter formatter) {
        if (null != formatter) {
            this.formatter = formatter;
        }
        return this;
    }
}


SubcolumnValue.java

package lecho.lib.hellocharts.model;

import java.util.Arrays;

import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.Chart;

/**
 * Single sub-column value for ColumnChart.
 */
public class SubcolumnValue {

    private float value;
    private float originValue;
    private float diff;
    private int color = ChartUtils.DEFAULT_COLOR;
    private int darkenColor = ChartUtils.DEFAULT_DARKEN_COLOR;
    private char[] label;

    public SubcolumnValue() {
        setValue(0);
    }

    public SubcolumnValue(float value) {
        // point and targetPoint have to be different objects
        setValue(value);
    }

    public SubcolumnValue(float value, int color) {
        // point and targetPoint have to be different objects
        setValue(value);
        setColor(color);
    }

    public SubcolumnValue(SubcolumnValue columnValue) {
        setValue(columnValue.value);
        setColor(columnValue.color);
        this.label = columnValue.label;
    }

    public void update(float scale) {
        value = originValue + diff * scale;
    }

    public void finish() {
        setValue(originValue + diff);
    }

    public float getValue() {
        return value;
    }

    public SubcolumnValue setValue(float value) {
        this.value = value;
        this.originValue = value;
        this.diff = 0;
        return this;
    }

    /**
     * Set target value that should be reached when data animation finish then call {@link Chart#startDataAnimation()}
     *
     * @param target
     * @return
     */
    public SubcolumnValue setTarget(float target) {
        setValue(value);
        this.diff = target - originValue;
        return this;
    }

    public int getColor() {
        return color;
    }

    public SubcolumnValue setColor(int color) {
        this.color = color;
        this.darkenColor = ChartUtils.darkenColor(color);
        return this;
    }

    public int getDarkenColor() {
        return darkenColor;
    }

    @Deprecated
    public char[] getLabel() {
        return label;
    }

    public SubcolumnValue setLabel(String label) {
        this.label = label.toCharArray();
        return this;
    }

    public char[] getLabelAsChars() {
        return label;
    }

    @Deprecated
    public SubcolumnValue setLabel(char[] label) {
        this.label = label;
        return this;
    }

    @Override
    public String toString() {
        return "ColumnValue [value=" + value + "]";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        SubcolumnValue that = (SubcolumnValue) o;

        if (color != that.color) return false;
        if (darkenColor != that.darkenColor) return false;
        if (Float.compare(that.diff, diff) != 0) return false;
        if (Float.compare(that.originValue, originValue) != 0) return false;
        if (Float.compare(that.value, value) != 0) return false;
        if (!Arrays.equals(label, that.label)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (value != +0.0f ? Float.floatToIntBits(value) : 0);
        result = 31 * result + (originValue != +0.0f ? Float.floatToIntBits(originValue) : 0);
        result = 31 * result + (diff != +0.0f ? Float.floatToIntBits(diff) : 0);
        result = 31 * result + color;
        result = 31 * result + darkenColor;
        result = 31 * result + (label != null ? Arrays.hashCode(label) : 0);
        return result;
    }
}

最佳答案

我不确定listVisitors具有什么类型的数据,但是似乎您需要List的另一个AxisValue

如果您阅读the documentation ...


如果要显示给定列的AxisValue,则应使用该列的X值初始化AxisValue


因此,...添加axisValues并用axisX = new Axis(axisValues);设置它们

List<Column> columns = new ArrayList<Column>();
List<AxisValue> axisValues = new ArrayList<AxisValue>();

for (int i = 0; i < listVisitor.size(); ++i) {
    String label = listVisitor.get(i).getDate().toString();
    axisValues.add(new AxisValue(i).setLabel(label);

    values = new ArrayList<SubcolumnValue>();

    Column column = new Column(values);
    columns.add(column);

    // ...
 }

data = new ColumnChartData(columns);

if (hasAxes) {
    Axis axisX = new Axis(axisValues);
    // ...
    columnData.setAxisXBottom(axisX);
}

关于java - 如何修改图表库类以从服务器获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38390631/

10-10 00:53
查看更多