如上圖value軸上刻度線是錯亂的。且圖標內顯示圖形也無規矩可言。
貼上代碼:
var data = [
{
“type”: “有源手術器械”,
“date”: 8,
“currNum”: Math.round(Math.random()(15)),
“preNum”: Math.round(Math.random()(15))
},
{
“type”: “有源手術器械”,
“date”: 9,
“currNum”: Math.round(Math.random()(15)),
“preNum”: Math.round(Math.random()(15))
},
{
"type": "醫用成像器械",
"date": 8,
"currNum": Math.round(Math.random()*(15)),
"preNum": Math.round(Math.random()*(15))
},
{
"type": "醫用成像器械",
"date": 9,
"currNum": Math.round(Math.random()*(15)),
"preNum": Math.round(Math.random()*(15))
},
{
"type": "醫用診察和監護器械",
"date": 8,
"currNum": Math.round(Math.random()*(15)),
"preNum": Math.round(Math.random()*(15))
},
{
"type": "醫用診察和監護器械",
"date": 9,
"currNum": Math.round(Math.random()*(15)),
"preNum": Math.round(Math.random()*(15))
},
{
"type": "呼吸、麻醉和急救器械",
"date": 8,
"currNum": Math.round(Math.random()*(15)),
"preNum": Math.round(Math.random()*(15))
},
{
"type": "呼吸、麻醉和急救器械",
"date": 9,
"currNum": Math.round(Math.random()*(15)),
"preNum": Math.round(Math.random()*(15))
},
{
"type": "醫療器械消毒滅菌器械",
"date": 8,
"currNum": Math.round(Math.random()*(15)),
"preNum": Math.round(Math.random()*(15))
},
{
"type": "醫療器械消毒滅菌器械",
"date": 9,
"currNum": Math.round(Math.random()*(15)),
"preNum": Math.round(Math.random()*(15))
},
]
function RenderChart() {
var chart = new G2.Chart({
container: 'chart',
forceFit: true,
height: 500,
background: {
fill: "#F8F9FA"
}
});
var dv = new DataSet().createView().source(data);
dv.transform({
type: 'map',
callback(row) {
***//此處進行數據加工,根據兩列值計算出新的列 percent ,通過控制檯輸出可以看到這列值類型爲字符串,並非數值類型。***
row.percent = (row.preNum == 0) ? 0 : ((row.currNum - row.preNum) * 100.0 / row.preNum).toFixed(2);
row.date = row.date + "月";
return row;
}
})
console.log(data);
console.log(dv);
chart.source(dv);
dv.transform({
type: 'partition',
groupBy: ['type'], // 以type字段進行分組
orderBy: ['date'], // 以date字段進行排序
});
chart.scale('percent', {
alias: '佔比(%)',
tickCount: 10
});
chart.axis('percent', {
label: {
textStyle: {
fill: '#aaaaaa'
}
},
title: {
offset: 50
}
});
//chart.legend({
// position: 'bottom-center'
//});
chart.interval()
.position('date*percent')
.color('type')
.opacity(1)
.label("percent")
.adjust([{
type: 'dodge',
marginRatio: 1 / 32
}]);
chart.render();
問題就出在這裏,當進行數據處理時,數值、浮點類型最終得出的是字符串類型,故此圖表會對數值渲染失敗,解決辦法就是對處理結果值進行轉換即可。