mycharts
MyCharts 是一个既可以在浏览器里绘制,也可以在服务器渲染(SSR)的 SVG 图表库。
Run Demo:
yarn dev
Run Build:
yarn build
CSS Import:
@import 'mycharts/dist/client/theme.css';
使用 mycharts 生成图表的示例:
import chart from 'mycharts';
import chart from 'mycharts/dist/client/mycharts.esm';
const data = {
type
...
}
const config = {
id: 'chart_' + (new Date()).getTime(),
insertSSRData: true,
debug: false,
errorMessage: '暂无数据',
box: {
height: 170,
width: 360,
padding: 15,
left: 'auto',
bottom: 18,
right: 0,
top: 12,
},
parent,
insertType,
animation: {
duration: 200,
delay: 0,
}
interaction: {
custom,
callback,
tooltip: {
delay: 2000,
insertType: 'append',
selected: 2,
trigger: false,
moveTrigger: true,
preventEmpty: false,
hide: false,
align: 'auto',
content: undefined,
},
legend: {
show: true,
selected: [],
canUnselectAll: false,
callback,
clickable
},
source: {
key: 'name',
'北京': { popular: 3000 }
}
style: {
colorize,
textColor,
textSize,
textTranslate
}
}
}
let chartObj = chart.pie(data[, config]);
chartObj.render([parent, insertType])
chart.hydrate(nodeOrId[, { data, config }])
Data API:
提示:启动 demo 查看控制台看到的 API 最为完整。
Radar Chart:
chart(data[, config])
{
"indicator": [{
"name": "销售\n4300",
"max": 6500
},
{
"name": "管理\n2500",
"max": 16000
},
{
"name": "其他\n2500",
"max": 16000
}
],
"style" : {
"curve": 1,
"axisCurve": 1
},
"series": [{
"name": "预算 vs 开销(Budget vs spending)",
"data": [{
"value": [
4300,
10000,
2000
],
"name": "预算分配(Allocated Budget)"
},
{
"value": [
5000,
14000,
100
],
"name": "实际开销(Actual Spending)",
"lineStyle": {
"normal": {
"color": "#10df67"
},
"area": {
"opacity": 0
}
}
}
]
}]
}
Pie Chart:
chart.pie(data[, config])
{
"value": [46.6, 38.9, 14.6],
"color": ['#FF574D', '#3E8FF1', '#2BC7FF'],
"name": ['type 1', 'type 2', 'type 3'],
"unit": '%',
"hideLabel": false,
"hideCenterLabel": false,
"labelStyle": {
"showIcon": true,
"splitText": true
"color": ['#FF574D', '#3E8FF1', '#2BC7FF'],
"lineScale": 1.5,
"size": 2
},
"centerLabelStyle": {
"insertType": "prepend",
"content": "aa\nbb",
"color": "#fff",
},
"labelFormat": function(d) {
return d + '%';
},
"elastic": true,
"ringRange": [80, 90],
"selectedRange": [75, 95]
"innerShadow": 10,
"stroke": 1,
}
Bar Chart:
chart.bar(data[, config])
{
series: [{
data: [{
xValue: '20',
yValue: 7.3
},
{
xValue: '20~24',
yValue: 31.7,
color: "#FCB205"
}
],
style: {
borderRadius: 3,
fill: "linear-gradient",
opacity: 1,
gradient: {
colors: ["#188df0", "#83bff6"]
}
}
}],
style: {
position: 'stack',
},
xAxis: {
type: 'category',
barWidth: "100",
data: ['20', '20~24', '25~29', '30~34', '35~39', '40'],
style: {
rotate: 45,
color: 'red' | ['red', 'blue'],
fontSize: 10,
},
format: () => {
}
},
yAxis: {
type: 'linear',
data: [40, 0],
unit: '%'
hide: true
}
}
Scatter Chart:
chart.scatter(data[, config])
{
xAxis: {
type: 'time',
splitNumber: 4,
data: [20170524,
20170525,
...
]
},
yAxis: {
type: 'points',
data: [123, 345, ...]
},
series: [{
"color": {
"normalDots": "rgba(25, 100, 150, 0.5)",
"markedDots": "rgba(120, 36, 50, 0.5)"
},
"data": [{
yValue: 3160,
tooltip: '2017-06-22 3160',
xValue: 20170524
},
{
yValue: 7556,
tooltip: '2017-06-23 7556',
xValue: 20170525
}
]
}, ]
}
Line Chart:
chart.line(data[, config])
{
xAxis: {
type: 'time',
cutIndex: 2,
data: [20170524,
20170525,
...
]
},
yAxis: {
type: 'points',
persistence: false,
domain: [10000, 10000],
data: [123, 345, ...],
animation : { duration: 300 },
markLine: {
value: 125,
color: 'red',
name: 'xxxx',
},
max: {
type: 0,
color: "#f00",
seriesIndex: 1
},
format: function(d) {
return d * 0.001 + 'K';
}
},
series: [{
name: '收益',
color: '#010101',
dashed: true,
data: [{
yValue: 3160,
tooltip: '2017-06-22 3160',
xValue: 20170524
},
{
yValue: 7556,
tooltip: '2017-06-23;名称;7556;7556',
xValue: 20170525
}
]
},
{
data: [{
yValue: 60000,
tooltip: {
"title": "2017-08-29 new",
"name": "爱奇艺",
"value": "864",
"desc": "864 new"
},
xValue: 20170524
},
{
yValue: 80000,
tooltip: '2017-06-23 8',
xValue: 20170525
}
]
}
]
}
Area Chart:
chart.area(data[, config])
{
xAxis: {
type: 'time',
data: [20170524,
20170525,
...
]
},
yAxis: {
type: 'points',
data: [123, 345, ...]
},
style: {
position: 'stack',
},
series: [{
"color": "#DF514E",
"dashed": true,
"style": {
"fill": "linear-gradient",
"opacity": 0.2,
"gradient": {
"start": "#f3a33a",
"stop": "#fcedd8",
"colors": ["#f00"]
},
"smooth": false,
},
data: [{
"label": "这里的内容会和tooltip拼接起来用'\n'连接",
"labelStyle": {
"color": "#10df67"
},
"xValue": "2017-08-20T00:00:00.000Z",
"yValue": 1474,
"tooltip": "2017-08-20 周日 1474场次",
"text": "测试"
},
{
"label": "",
"xValue": "2017-08-29T00:00:00.000Z",
"yValue": 871,
"tooltip": "2017-08-29 周二 871场次",
"text": "上映",
"textStyle": {
"color": "#2598B6",
"size": "12px",
"padding": "8px"
}
}
]
},
{
data: [{
yValue: 60000,
tooltip: '2017-06-22 6',
xValue: 20170524
},
{
yValue: 80000,
tooltip: '2017-06-23 8',
xValue: 20170525
}
]
}, {
"type": "line",
"dashed": true,
"color": "#2598B6",
"showSelected": false,
"data": [{
"xValue": "2017-08-30T00:00:00.000Z",
"yValue": 7.2
}, {
"xValue": "2017-08-31T00:00:00.000Z",
"yValue": 4.6,
"tooltip": {
"title": "2017-08-29 new",
"name": "爱奇艺",
"value": "864",
"desc": "864 new"
},
}, {
"xValue": "2017-09-01T00:00:00.000Z",
"yValue": 1
}]
}
]
}
Map:
chart.map(data[, config])
data 是绘图的关键,现兼容 d3 和 AntV 两种 GeoJSON。中国地图的简单版(适用于移动端,数据格式为 d3 标准)和复杂版(适用于 PC 端,数据格式为 AntV 标准)数据都在项目的src/browser/map
目录下,可以直接导入使用。
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"id": "65",
"size": "550",
"name": "新疆",
"cp": [84.9023, 42.148],
"childNum": 18
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[96.416, 42.7588],
[96.416, 42.7148],
...
]
]
}
}]
}
注意事项:
- axis 有四种类型: 'category', 'time', 'linear' and 'points', 和 d3 定义一致
- 如果要使用'time'类型, 所对应的 x 轴信息必须是 Date 类型
- y 轴暂时只有'linear', 'points'两种类型
TODO:
- 文档完善,EN version
- 双坐标混搭(例:折线+bar)
- stack Area
- 隐藏坐标
- hydrate 体积优化*
- 图表连续性(断点不显示,现可由入参过滤实现)*
- 横纵转换*
- 图标类型切换*