效果图
描述
- 坐标轴文字颜色
- 坐标轴线宽度、颜色
- 面积区域颜色
- 图例样式
- 去除网格线
数据处理
const data = [{
month: 'Jan',
Tokyo: 7,
London: 3.9,
Beijing: 8.8,
},
{
month: 'Feb',
Tokyo: 13,
London: 4.2,
Beijing: 2.5,
},
{
month: 'Mar',
Tokyo: 16.5,
London: 5.7,
Beijing: 12.5,
},
{
month: 'Apr',
Tokyo: 14.5,
London: 8.5,
Beijing: 25,
},
{
month: 'May',
Tokyo: 10,
London: 11.9,
Beijing: 11,
},
{
month: 'Jun',
Tokyo: 7.5,
London: 15.2,
Beijing: 5,
}];
data = [{
month: "Jan",
city: "Tokyo",
value: 7
},
{
month: "Jan",
city: "London",
value: 3.9
},
{
month: "Feb",
city: "Tokyo",
value: 13
},
{
month: "Feb",
city: "London",
value: 4.2
},...];
- 使用
Bizchart
的dataset
来进行转换,如果已经是目标格式可以不用转换
const dv = new ds.View();
const transformData = dv.source(data)
.transform({
type: 'fold',
fields: ['Tokyo', 'London'],
key: 'city',
value: 'value',
})
- 设置每个city对应的颜色和文字,用于图线的颜色,图例/tooltip的颜色、文字。
const fieldMap = {
London: {
theme: '#3a8fff',
legendText: '伦敦温度',
},
Tokyo: {
theme: '#6fdfe1',
legendText: '东京温度',
},
Beijing: {
theme: '#ff6200',
legendText: '北京温度',
}
};
实现
- 横坐标纵坐标颜色、字体、角度,配置Axis属性label
<Axis label={{
rotate: 120,
style: {
fill: '#6fdfe1',
fontSize: 10,
}
}} />
<Axis line={{
style: {
lineWidth: 2,
stroke: '#6fdfe1',
fill: '#6fdfe1',
}
}}/>
<LineAdvance
area
position="month*value"
color={['city', (city) => fieldMap[city].theme]}
style={{
lineWidth: '1',
}}
/>
<Tooltip
shared
showCrosshairs
domStyles={{
'g2-tooltip': {
backgroundColor: 'rgba(0, 0, 0, 0.78)',
color: '#fff',
fontSize: 10,
},
'g2-tooltip-title': {
textAlign: 'left',
},
'g2-tooltip-list': {
textAlign: 'left',
},
'g2-tooltip-list-item': {
lineHeight: 0.6,
},
}}
/>
<LineAdvance
tooltip={[`city*value*month`, (city, value, title) => ({
title,
name: fieldMap[city].legendText,
value,
})]}
/>
<Legend
position="top-right"
itemSpacing={8}
itemName={{
style: {
textAlign: 'start',
fill: '#fff',
fontSize: 10,
},
formatter: (city) => fieldMap[city].legendText,
}}
marker={(city) => ({
symbol: 'circle',
spacing: 3,
style: {
lineWidth: 0,
r: 2.4,
fill: fieldMap[city].theme,
},
})}
/>