我使用https://www.amcharts.com/docs/v3/在我的页面上创建图表。数据来自api。
在我将数据生成到 amchart 后,它给了我错误消息Uncaught TypeError: Cannot read property 'write' of undefined
以及生成数据的代码:
从 api 获取数据
generateChart() {
const { priceItem } = this.state
let dataProvider = []
priceItem.forEach(item => {
dataProvider.push({
"date": moment(new Date(item.date)).format('DD-MM-YYYY'),
"value": item.amount
})
})
var result = simpleConfig(dataProvider)
this.setState({ config: result })
}
我在另一个文件中创建配置:
export const simpleConfig = (dataProvider) => {
dataProvider.map(data => {
console.log(data)
let config = {
"type": "stock",
"theme": "light",
"dataDateFormat": "DD-MM-YYYY",
"dataSets": [{
"title": "first data set",
"fieldMappings": [{
"fromField": "value",
"toField": "value"
}, {
"fromField": "volume",
"toField": "volume"
}],
"dataProvider": data,
"categoryField": "date"
}],
"panels": [{
"showCategoryAxis": false,
"title": "value",
"percentHeight": 70,
"stockGraphs": [{
"id": "g1",
"valueField": "value",
"comparable": false,
"compareField": "value",
"balloonText": "[[title]]:<b>[[value]]</b>",
"compareGraphBalloonText": "[[title]]:<b>[[value]]</b>"
}],
"stockLegend": {
"periodValueTextComparing": "[[percents.value.close]]%",
"periodValueTextRegular": "[[value.close]]"
}
}],
"chartCursorSettings": {
"valueBalloonsEnabled": true,
"fullWidth": true,
"cursorAlpha": 0.1,
"valueLineBalloonEnabled": true,
"valueLineEnabled": true,
"valueLineAlpha": 0.5
},
"export": {
"enabled": false
},
"responsive": {
"enabled": true
}
}
return config;
})
}
然后用于显示图表:
<AmCharts.React
style={{
width: "100%",
height: "500px"
}}
options={config}
/>
有什么我做错了吗?