G2-chart 刻度和圖形顯示錯亂問題。

對於摻雜正負值數據,且是通過data-set轉換過的數據,生成的數值類型是字符。
如上圖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();

可以看到錯亂時,數值列的類型是字符,而非浮點
問題就出在這裏,當進行數據處理時,數值、浮點類型最終得出的是字符串類型,故此圖表會對數值渲染失敗,解決辦法就是對處理結果值進行轉換即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章