简体   繁体   中英

Dynamically adding row to a timeline of Google Charts

I'm trying to add rows based on a specific data received. For now, I've stored that sample data in a constant. My Timeline component is below. YSD is year start date, msD is month start date, dsD is day start date. The same thing for yEd where e is just so for yed, it is year end date. I tried mapping but I'm not getting the data in the correct format.

class Timeline extends Component {
   render() {

     const timelineData = [
        {"type": "FTF", "name": "ABC", "ysD" : "2020", "msD": "0", "dsD": "13", 
        "yeD" : "2020", "meD": "0", "deD": "29"},
        {"type": "Video Detail", "name": "BCD", "ysD" : "2020", "msD": "1", "dsD": "15", 
        "yeD" : "2020", "meD": "1", "deD": "20"}
    ]

    return(
         <div class="row" style={{ marginBottom: 30, zoom: "75%" }}>
            {
                timelineData.map((data, idx) => (
                    <Chart
                        key={idx}
                        width={'100%'}
                        height={'200px'}
                        chartType="Timeline"
                        loader={<div>Loading Chart</div>}
                        data={[
                            [
                                { type: 'string', id: 'Position'},
                                { type: 'string', id: 'Name' },
                                { type: 'date', id: 'Start' },
                                { type: 'date', id: 'End' },
                            ],
                            [
                                data.type,
                                data.name,
                                new Date(data.ysD, data.msD, data.dsD ),
                                new Date(data.yeD, data.meD, data.deD )
                            ]
                        ]}
                        options={{
                            timeline: {
                                colorByRowLabel: true,
                            },
                        }}
                        rootProps={{ 'data-testid': '3' }} />
                ))
            }
       </div>
        )

Here is a screenshot of how it is looking like.

在此处输入图像描述

And here is how I want it to look like:

在此处输入图像描述

Is there a different approach wherein I can dynamically add rows from the data I receive in the correct way?

Found a way to display as I wanted it by writing the Chart component in a different way, where I made a clear distinction between rows and columns. Here is the code:

class Timeline extends Component {

render() {

    const timelineData = [
        {
            "type": "FTF", "name": "ABC", "ysD": "2020", "msD": "0", "dsD": "13",
            "yeD": "2020", "meD": "0", "deD": "29"
        },
        {
            "type": "Video Detail", "name": "BCD", "ysD": "2020", "msD": "1", "dsD": "15",
            "yeD": "2020", "meD": "1", "deD": "20"
        }
    ]

    return (
        <div class="row" style={{ marginBottom: 30, zoom: "75%" }}>
            <Chart
                width={'100%'}
                height={'200px'}
                chartType="Timeline"
                loader={<div>Loading Chart</div>}
                columns={
                    [
                        { type: 'string', id: 'Position' },
                        { type: 'string', id: 'Name' },
                        { type: 'date', id: 'Start' },
                        { type: 'date', id: 'End' },
                    ]
                }
                rows={
                    timelineData.map((data, idx) => (
                        [
                            data.type,
                            data.name,
                            new Date(data.ysD, data.msD, data.dsD),
                            new Date(data.yeD, data.meD, data.deD)
                        ]
                    ))
                }
                options={{
                    timeline: {
                        colorByRowLabel: true,
                        // singleColor: "white"
                    },
                    // backgroundColor: "F3F0EF"
                }}
                rootProps={{ 'data-testid': '3' }}
            />
        </div>
    )
}
}

As you can see, I'm passing rows and columns separately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM