系统维护中Morris.js折线图显示为直线,如何改为波动折线?
Here are the key adjustments to resolve the issue:
1. Ensure Numeric Y-Values
Morris.js requires y-axis values to be numbers, not strings. If your JSON response returns 'a' values as strings, convert them to numeric values before initializing the chart:
success: function(response) { // Convert 'a' values to numbers response.forEach(item => { item.a = parseFloat(item.a) || 0; // Fallback to 0 if parsing fails }); console.log(response); new Morris.Line({ // ... existing config }); }
2. Check for Unique X-Values
Duplicate xkey values (the 'y' field in your code) can cause Morris.js to aggregate data points, leading to a flat line. Verify each entry in your response has a distinct 'y' value. If duplicates exist, clean the data or adjust how you fetch it to ensure uniqueness.
3. Adjust Y-Axis Scale
Your ymax: 300000 setting might be drastically higher than the actual range of your 'a' values, making the line appear flat. Remove the fixed ymax or calculate it dynamically based on your data:
success: function(response) { // Convert values first if needed response.forEach(item => { item.a = parseFloat(item.a) || 0; }); // Calculate dynamic ymax with padding const maxValue = Math.max(...response.map(item => item.a)); const dynamicYmax = maxValue * 1.1; // Add 10% padding new Morris.Line({ element: 'morrisLine1', data: response, xkey: 'y', ykeys: ['a'], labels: ['Daily Progress'], lineColors: ['#f74f75'], lineWidth: 3, ymax: dynamicYmax, // Use calculated value instead of fixed 300000 gridTextSize: 12, hideHover: 'auto', resize: true }); }
4. Validate Data Structure
Confirm your response is an array of objects with the correct properties. Each object should have both 'y' (x-axis) and 'a' (y-axis) fields, like this:
[ {"y": "2024-05-01", "a": 15000}, {"y": "2024-05-02", "a": 22000}, {"y": "2024-05-03", "a": 18000} ]
If after making these adjustments the chart still shows a flat line, log the processed data to the console to confirm values are correctly parsed and structured before passing to Morris.Line.
内容的提问来源于stack exchange,提问作者Davi Amaral

