使用Morris.js绘制折线图报错:raphael-min.js:10 Error: <path>属性d应为数字
Hey there! Let's fix that frustrating Morris.js line chart error you're dealing with. The raphael-min.js:10 Error: <path> attribute d: Expected number, "M,0,0" almost always happens because Morris.js isn't getting valid numerical values (or properly formatted data) for your chart axes from your database.
Here's how to troubleshoot and fix this step by step:
1. Double-check your data format matches Morris.js requirements
Morris Line charts expect structured data like this (array of objects with clear x/y fields):
// Example valid data structure const validData = [ { month: 'Jan', revenue: 4500 }, { month: 'Feb', revenue: 5200 }, { month: 'Mar', revenue: 3800 } ];
Make sure the field you've set as xkey (for the x-axis) and ykeys (for y-axis series) are present in every data item, and that y-axis values are actual numbers (not strings that can't be parsed, or null/undefined).
2. Inspect the raw data from your database
Before passing data to Morris.js, log it to the console to spot invalid entries:
// Example if you're fetching data via AJAX fetch('/your-db-api-endpoint') .then(response => response.json()) .then(rawData => { console.log("Raw database data:", rawData); // Check for empty values or wrong types // Initialize chart after verifying data Morris.Line({ element: 'line-chart', xkey: 'month', ykeys: ['revenue'], labels: ['Monthly Revenue'], data: rawData }); });
Look for items where your xkey field is empty, or y-axis values are null, empty strings, or non-numeric text. These will break the chart rendering.
3. Clean and transform your data
If you find invalid entries, filter them out and ensure numeric values are properly typed:
const cleanedData = rawData // Remove items with missing x-axis values or invalid y-values .filter(item => { return item.month && !isNaN(parseFloat(item.revenue)) && isFinite(item.revenue); }) // Convert y-values to numbers if they're stored as strings in the DB .map(item => { return { month: item.month, revenue: parseFloat(item.revenue) }; }); // Use the cleaned data for your chart Morris.Line({ element: 'line-chart', xkey: 'month', ykeys: ['revenue'], labels: ['Monthly Revenue'], data: cleanedData });
4. Verify your x-axis type setting
If your x-axis uses category strings (not dates/times), make sure to set parseTime: false to prevent Morris from trying to parse non-time values into timestamps (which can cause empty axis values):
Morris.Line({ element: 'line-chart', xkey: 'productCategory', ykeys: ['sales'], labels: ['Category Sales'], data: cleanedData, parseTime: false // Critical for non-date x-axis values });
内容的提问来源于stack exchange,提问作者Tovo

