我正在尝试使用amCharts实时绘制事件(标记)。
我在codepen中找到了一个示例,它展示了如何插入这些事件,但它们是静态的。
//https://codepen.io/anon/pen/boWJMQ
var chartData = [];
generateChartData();
function generateChartData() {
var firstDate = new Date( 2012, 0, 1 );
firstDate.setDate( firstDate.getDate() - 500 );
firstDate.setHours( 0, 0, 0, 0 );
for ( var i = 0; i < 500; i++ ) {
var newDate = new Date( firstDate );
newDate.setDate( newDate.getDate() + i );
var a = Math.round( Math.random() * ( 40 + i ) ) + 100 + i;
var b = Math.round( Math.random() * 100000000 );
chartData.push( {
"date": newDate,
"value": a,
"volume": b
} );
}
}
var chart = AmCharts.makeChart( "chartdiv", {
"type": "stock",
"theme": "light",
"dataSets": [ {
"color": "#b0de09",
"fieldMappings": [ {
"fromField": "value",
"toField": "value"
}, {
"fromField": "volume",
"toField": "volume"
} ],
"dataProvider": chartData,
"categoryField": "date",
// EVENTS
"stockEvents": [ {
"date": new Date( 2010, 8, 19 ),
"type": "sign",
"backgroundColor": "#85CDE6",
"graph": "g1",
"text": "S",
"description": "This is description of an event"
}, {
"date": new Date( 2010, 10, 19 ),
"type": "flag",
"backgroundColor": "#FFFFFF",
"backgroundAlpha": 0.5,
"graph": "g1",
"text": "F",
"description": "Some longer\ntext can also\n be added"
}, {
"date": new Date( 2010, 11, 10 ),
"showOnAxis": true,
"backgroundColor": "#85CDE6",
"type": "pin",
"text": "X",
"graph": "g1",
"description": "This is description of an event"
}, {
"date": new Date( 2010, 11, 26 ),
"showOnAxis": true,
"backgroundColor": "#85CDE6",
"type": "pin",
"text": "Z",
"graph": "g1",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 0, 3 ),
"type": "sign",
"backgroundColor": "#85CDE6",
"graph": "g1",
"text": "U",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 1, 6 ),
"type": "sign",
"graph": "g1",
"text": "D",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 3, 5 ),
"type": "sign",
"graph": "g1",
"text": "L",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 3, 5 ),
"type": "sign",
"graph": "g1",
"text": "R",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 5, 15 ),
"type": "arrowUp",
"backgroundColor": "#00CC00",
"graph": "g1",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 6, 25 ),
"type": "arrowDown",
"backgroundColor": "#CC0000",
"graph": "g1",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 8, 1 ),
"type": "text",
"graph": "g1",
"text": "Longer text can\nalso be displayed",
"description": "This is description of an event"
} ]
} ],
"panels": [ {
"title": "Value",
"stockGraphs": [ {
"id": "g1",
"valueField": "value"
} ],
"stockLegend": {
"valueTextRegular": " ",
"markerType": "none"
}
} ],
"chartScrollbarSettings": {
"graph": "g1"
},
"chartCursorSettings": {
"valueBalloonsEnabled": true,
"graphBulletSize": 1,
"valueLineBalloonEnabled": true,
"valueLineEnabled": true,
"valueLineAlpha": 0.5
},
"periodSelector": {
"periods": [ {
"period": "DD",
"count": 10,
"label": "10 days"
}, {
"period": "MM",
"count": 1,
"label": "1 month"
}, {
"period": "YYYY",
"count": 1,
"label": "1 year"
}, {
"period": "YTD",
"label": "YTD"
}, {
"period": "MAX",
"label": "MAX"
} ]
},
"panelsSettings": {
"usePrefixes": true
},
"export": {
"enabled": true
}
} );
以下是我要做的事情:
//https://codepen.io/anon/pen/wrdZgj
var chartData = [];
var chart;
generateChartData();
function generateChartData() {
var firstDate = new Date( 2012, 0, 1 );
firstDate.setDate( firstDate.getDate() - 500 );
firstDate.setHours( 0, 0, 0, 0 );
for ( var i = 0; i < 500; i++ ) {
var newDate = new Date( firstDate );
newDate.setDate( newDate.getDate() + i );
var a = Math.round( Math.random() * ( 40 + i ) ) + 100 + i;
var b = Math.round( Math.random() * 100000000 );
chartData.push( {
"date": newDate,
"value": a,
"volume": b
} );
}
}
chart = AmCharts.makeChart( "chartdiv", {
"type": "stock",
"theme": "light",
"dataSets": [ {
"color": "#b0de09",
"fieldMappings": [ {
"fromField": "value",
"toField": "value"
}, {
"fromField": "volume",
"toField": "volume"
} ],
"dataProvider": chartData,
"categoryField": "date"
} ],
"panels": [ {
"title": "Value",
"stockGraphs": [ {
"id": "g1",
"valueField": "value"
} ],
"stockLegend": {
"valueTextRegular": " ",
"markerType": "none"
}
} ],
"chartScrollbarSettings": {
"graph": "g1"
},
"chartCursorSettings": {
"valueBalloonsEnabled": true,
"graphBulletSize": 1,
"valueLineBalloonEnabled": true,
"valueLineEnabled": true,
"valueLineAlpha": 0.5
},
"periodSelector": {
"periods": [ {
"period": "DD",
"count": 10,
"label": "10 days"
}, {
"period": "MM",
"count": 1,
"label": "1 month"
}, {
"period": "YYYY",
"count": 1,
"label": "1 year"
}, {
"period": "YTD",
"label": "YTD"
}, {
"period": "MAX",
"label": "MAX"
} ]
},
"panelsSettings": {
"usePrefixes": true
},
"export": {
"enabled": true
}
} );
function insert_event(){
chart.dataSets.stockEvents= [ {
"date": new Date( 2010, 8, 19 ),
"type": "sign",
"backgroundColor": "#85CDE6",
"graph": "g1",
"text": "S",
"description": "This is description of an event"
}, {
"date": new Date( 2010, 10, 19 ),
"type": "flag",
"backgroundColor": "#FFFFFF",
"backgroundAlpha": 0.5,
"graph": "g1",
"text": "F",
"description": "Some longer\ntext can also\n be added"
}, {
"date": new Date( 2010, 11, 10 ),
"showOnAxis": true,
"backgroundColor": "#85CDE6",
"type": "pin",
"text": "X",
"graph": "g1",
"description": "This is description of an event"
}, {
"date": new Date( 2010, 11, 26 ),
"showOnAxis": true,
"backgroundColor": "#85CDE6",
"type": "pin",
"text": "Z",
"graph": "g1",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 0, 3 ),
"type": "sign",
"backgroundColor": "#85CDE6",
"graph": "g1",
"text": "U",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 1, 6 ),
"type": "sign",
"graph": "g1",
"text": "D",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 3, 5 ),
"type": "sign",
"graph": "g1",
"text": "L",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 3, 5 ),
"type": "sign",
"graph": "g1",
"text": "R",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 5, 15 ),
"type": "arrowUp",
"backgroundColor": "#00CC00",
"graph": "g1",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 6, 25 ),
"type": "arrowDown",
"backgroundColor": "#CC0000",
"graph": "g1",
"description": "This is description of an event"
}, {
"date": new Date( 2011, 8, 1 ),
"type": "text",
"graph": "g1",
"text": "Longer text can\nalso be displayed",
"description": "This is description of an event"
} ]
chart.validateNow();
}
insert_event()
创建图表对象,然后调用函数“insert_event()”并插入要插入的事件。
有办法吗?
谢谢!
答案 0 :(得分:0)
您未正确设置stockEvents数组。请注意两个示例之间的区别,即如何将stockEvents数组分配给dataSets。在第一个代码段中,它在第一个元素的dataSets array 中设置,但是,在第二个代码段中,您只是将dataSets视为对象,而不是数组。您需要做的就是添加下标来修复分配,即chart.dataSets[0].stockEvents = [/*your array */];