Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

javascript - Morris line chart is not working properly

I created this simple line chart using morris.js. But it is not showing data properly. I don't know why? Please check it at js fiddle.

Js fiddle: link enter image description here

new Morris.Line({
    element: 'multi-line-mp',

    data: [
        {
            day: 'Jan 1',
            sales: '0',
            purchases: '1'
        },
        {
            day: 'Jan 2',
            sales: '14',
            purchases: '3'
        },
        {
            day: 'Jan 3',
            sales: '45',
            purchases: '0'
        },
        {
            day: 'Jan 4',
            sales: '47',
            purchases: '32'
        },
        {
            day: 'Jan 5',
            sales: '90',
            purchases: '10'
        }        
    ],

    xkey: 'day',
    ykeys: ['Sales', 'Purchases'],
    labels: ['Sales', 'Purchases'],
    resize: true
});
question from:https://stackoverflow.com/questions/65847853/morris-line-chart-is-not-working-properly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The date format of your xkey (day) is not good, this is rather a string than a real date.

You have 2 choices:

  1. change the format of the xkey to be a real date (like 2021-01-01 instead of Jan 1)
  2. set the option parseTime to false, so that it won't try to format the string to be a date

In case you change the format of xkey, you still can change the way the date is displayed thanks to the function dateFormat (see the documentation for this here: http://morrisjs.github.io/morris.js/lines.html)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...