You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在GraphView绘制SQLite数据:折线恒定及X轴星期标签问题求助

Fixing Your GraphView Issues: Flat Line & Incorrect X-axis Labels

Let's tackle the two problems you're running into with your SQLite data visualization in GraphView—flat line rendering and messed-up weekday X-axis labels—with concrete fixes and checks:

1. Why Your Line Is Staying Constant

The most likely culprit is that the DataPoint array returned by getChartData() has identical Y-values for every X-point. Here's how to diagnose and fix this:

First, Verify Your Data Source

  • Double-check the implementation of getChartData(): Are you correctly querying SQLite for daily mileage values (one per weekday)? If your query is pulling a total sum or the same single value every time, your line will be flat.
  • Add debug logs to print each DataPoint in the array to confirm values:
    DataPoint[] dataPoints = getChartData();
    for (DataPoint point : dataPoints) {
        Log.d("GraphData", "X: " + point.getX() + ", Y: " + point.getY());
    }
    
    If all Y-values are the same, your SQLite query logic needs adjustment (e.g., make sure you're grouping data by day and fetching individual day totals, not a grand total).

Check Y-axis Range Configuration

Your current code sets the max Y-value to db.getTotalMiles()*1.5—if this total is way larger than individual daily mileage values, the line might appear flat because it's squashed at the bottom of the graph. Try setting a more appropriate range, or let GraphView auto-scale:

// Replace manual max Y with auto-scaling
graphView.getViewport().setYAxisBoundsManual(false);
graphView.getViewport().setScalableY(true); // Allow zooming if needed

2. Fixing Weekday X-axis Labels

GraphView uses numerical X-values by default—you need to add a custom label formatter to convert those numbers (e.g., 1=Monday, 2=Tuesday...) to weekday names. Here's how to implement this:

Add a Custom Label Formatter

// Inside your OnCreate method, after initializing the series
graphView.getGridLabelRenderer().setLabelFormatter(new LabelFormatter() {
    @Override
    public String formatLabel(double value, boolean isValueX) {
        if (isValueX) {
            // Convert numerical X value to weekday abbreviation
            switch ((int) value) {
                case 1: return "Mon";
                case 2: return "Tue";
                case 3: return "Wed";
                case 4: return "Thu";
                case 5: return "Fri";
                case 6: return "Sat";
                case 7: return "Sun";
                default: return "";
            }
        } else {
            // Format Y-axis mileage values for readability
            return String.valueOf((int) value) + " mi";
        }
    }

    @Override
    public void setViewport(Viewport viewport) {}
});

// Lock X-axis bounds to match your weekday range (1-7)
graphView.getViewport().setMinX(1);
graphView.getViewport().setMaxX(7);
graphView.getViewport().setXAxisBoundsManual(true);

// Ensure all weekday labels are displayed
graphView.getGridLabelRenderer().setNumHorizontalLabels(7);

Full Revised OnCreate Snippet

Putting it all together, here's how your code might look:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_graph);

    GraphView graphView = findViewById(R.id.graph);
    DBAdapter db = new DBAdapter(this);

    // Fetch and validate data first
    DataPoint[] chartData = getChartData();
    for (DataPoint point : chartData) {
        Log.d("GraphDebug", "Day " + point.getX() + ": " + point.getY() + " miles");
    }

    LineGraphSeries<DataPoint> lineGraphSeries = new LineGraphSeries<>(chartData);
    lineGraphSeries.setDrawDataPoints(true);
    graphView.addSeries(lineGraphSeries);

    // Configure Y-axis (adjust bounds based on actual daily values)
    db.openDB();
    double totalMiles = db.getTotalMiles();
    graphView.getViewport().setMaxY(totalMiles * 1.2); // Slightly higher than max daily value
    graphView.getViewport().setYAxisBoundsManual(true);
    db.closeDB();

    // Fix X-axis weekday labels
    graphView.getGridLabelRenderer().setLabelFormatter(new LabelFormatter() {
        @Override
        public String formatLabel(double value, boolean isValueX) {
            if (isValueX) {
                String[] weekdays = {"", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
                int dayIndex = (int) value;
                return dayIndex >=1 && dayIndex <=7 ? weekdays[dayIndex] : "";
            } else {
                return String.format("%.1f mi", value);
            }
        }

        @Override
        public void setViewport(Viewport viewport) {}
    });
    graphView.getViewport().setMinX(1);
    graphView.getViewport().setMaxX(7);
    graphView.getViewport().setXAxisBoundsManual(true);
    graphView.getGridLabelRenderer().setNumHorizontalLabels(7);
}

Key Checks to Remember

  • Always validate your raw data before passing it to GraphView—flat lines almost always come from incorrect data fetching logic.
  • Make sure your SQLite query for daily mileage is grouping records by day and calculating the right aggregate (sum, count, etc.) per day.
  • Test the label formatter with your actual X-values to ensure they map correctly to weekdays.

内容的提问来源于stack exchange,提问作者M.Dixon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:03:32