Skip to content

Commit

Permalink
折线图smooth支持
Browse files Browse the repository at this point in the history
  • Loading branch information
kener committed Sep 24, 2013
1 parent e9e80f2 commit f66756d
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 73 deletions.
38 changes: 2 additions & 36 deletions doc/example/k.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,11 @@
res += '<br/>' + params[0][0];
res += '<br/> 开盘 : ' + params[0][2][0] + ' 最高 : ' + params[0][2][3];
res += '<br/> 收盘 : ' + params[0][2][1] + ' 最低 : ' + params[0][2][2];
res += '<br/>' + params[1][0];
res += ' : ' + params[1][2];
return res;
}
},
legend: {
data:['上证指数','成交金额(万)']
data:['上证指数']
},
toolbox: {
show : true,
Expand Down Expand Up @@ -133,19 +131,13 @@
splitNumber: 9,
boundaryGap: [0.05, 0.05],
splitArea : {show : true}
},
{
type : 'value',
scale:true,
splitNumber: 9,
boundaryGap: [0.05, 0.05],
splitArea : {show : true}
}
],
series : [
{
name:'上证指数',
type:'k',
barMaxWidth: 20,
itemStyle: {
normal: {
color: 'red', // 阳线填充颜色
Expand Down Expand Up @@ -265,32 +257,6 @@
[2300, 2291.3, 2288.26, 2308.38],
[2320.26, 2302.6, 2287.3, 2362.94]
]
},
{
name:'成交金额(万)',
type:'line',
yAxisIndex: 1,
symbol: 'none',
data:[
7971452.5, 8105835, 7526158.5, 7114268,
9237718, 9164861, 10070399, 10354677, 11431155,
12035250, 9992016, 9202153, 11105205, 11529046,
11302521, 12884353, 11124881, 10225527, 6665826.5,
7566047, 7919148, 8128014, 9116529, 8139084.5,
7904092, 8162560.5, 7831570, 5987761, 6525151,
8448324, 7897721, 8221751.5, 8224500, 9538301,
6188812.5, 5998655.5, 6695584.5, 5816217.5, 5619688,
6213270, 7109021.5, 7313504, 7366616.5, 6880135.5,
7759666.5, 6699753.5, 7335819, 10759351, 8570842,
9181132, 8541862, 8595132, 10478607, 11397426,
7551207, 8148617.5, 10157810, 6921859, 7768895.5,
10063709, 7391994.5, 8843860, 12608589, 12536480,
11672120, 14304651, 11153111, 11591827, 8826293,
10591719, 7764234, 8757982, 12662783, 9722230,
10633095, 10365702, 9896523, 10933507, 10458354,
12709611, 15253370, 12555496, 11620504, 12485603,
12491697, 11691637, 8026738.5, 13560434
]
}
]
};
Expand Down
27 changes: 0 additions & 27 deletions doc/example/k1.html
Original file line number Diff line number Diff line change
Expand Up @@ -266,33 +266,6 @@
[2242.26,2210.9,2205.07,2250.63],
[2190.1,2148.35,2126.22,2190.1]
]
},
{
name:'成交金额(万)2',
type:'line',
yAxisIndex: 1,
smooth:0.5,
symbol: 'none',
data:[
13560434, 8026738.5, 11691637, 12491697, 12485603,
11620504, 12555496, 15253370, 12709611, 10458354,
10933507, 9896523, 10365702, 10633095, 9722230,
12662783, 8757982, 7764234, 10591719, 8826293,
11591827, 11153111, 14304651, 11672120, 12536480,
12608589, 8843860, 7391994.5, 10063709, 7768895.5,
6921859, 10157810, 8148617.5, 7551207, 11397426,
10478607, 8595132, 8541862, 9181132, 8570842,
10759351, 7335819, 6699753.5, 7759666.5, 6880135.5,
7366616.5, 7313504, 7109021.5, 6213270, 5619688,
5816217.5, 6695584.5, 5998655.5, 6188812.5, 9538301,
8224500, 8221751.5, 7897721, 8448324, 6525151,
5987761, 7831570, 8162560.5, 7904092, 8139084.5,
9116529, 8128014, 7919148, 7566047, 6665826.5,
10225527, 11124881, 12884353, 11302521, 11529046,
11105205, 9202153, 9992016, 12035250, 11431155,
10354677, 10070399, 9164861, 9237718, 7114268,
7526158.5, 8105835, 7971452.5
]
}
]
};
Expand Down
2 changes: 1 addition & 1 deletion doc/example/line.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
name:'联盟广告',
type:'line',
stack: '总量',
smooth: 0.5,
smooth: true,
symbol: 'emptyCircle', // 系列级个性化拐点图形
symbolSize: 8,
data:[
Expand Down
3 changes: 3 additions & 0 deletions doc/example/line3.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,21 @@
{
name:'成交',
type:'line',
smooth:true,
itemStyle: {normal: {areaStyle: {type: 'default'}}},
data:[10, 12, 21, 54, 260, 830, 710]
},
{
name:'预购',
type:'line',
smooth:true,
itemStyle: {normal: {areaStyle: {type: 'default'}}},
data:[30, 182, 434, 791, 390, 30, 10]
},
{
name:'意向',
type:'line',
smooth:true,
itemStyle: {normal: {areaStyle: {type: 'default'}}},
data:[1320, 1132, 601, 234, 120, 90, 20]
}
Expand Down
2 changes: 1 addition & 1 deletion doc/example/line5.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
{
name:'高度(km)与气温(°C)变化关系',
type:'line',
smooth:0.5,
smooth:true,
data:[15, -50, -56.5, -46.5, -22.1, -2.5, -27.7, -55.7, -76.5]
}
]
Expand Down
28 changes: 25 additions & 3 deletions src/chart/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ define(function(require) {
|| defaultColor,
lineWidth : lineWidth,
lineType : lineType,
smooth : serie.smooth,
smooth : _getSmooth(serie.smooth),
//smoothX : orient == 'vertical' ? 0 : serie.smooth,
//smoothY : orient == 'vertical' ? serie.smooth : 0,
shadowColor : self.deepQuery(
Expand Down Expand Up @@ -581,7 +581,7 @@ define(function(require) {

if (isFill) {
self.shapeList.push({
shape : 'polygon',
shape : 'halfSmoothPolygon',
zlevel : _zlevelBase,
style : {
pointList : singlePL.concat([
Expand All @@ -595,6 +595,7 @@ define(function(require) {
]
]),
brushType : 'fill',
smooth : _getSmooth(serie.smooth),
color : fillNormalColor
? fillNormalColor
: zrColor.alpha(defaultColor,0.5)
Expand All @@ -609,6 +610,24 @@ define(function(require) {
}
}
}

function _getSmooth(isSmooth/*, pointList, orient*/) {
if (isSmooth) {
/* 不科学啊,发现0.3通用了
var delta;
if (orient == 'horizontal') {
delta = Math.abs(pointList[0][0] - pointList[1][0]);
}
else {
delta = Math.abs(pointList[0][1] - pointList[1][1]);
}
*/
return 0.3
}
else {
return 0;
}
}

/**
* 生成空数据所需的可计算提示图形
Expand Down Expand Up @@ -926,7 +945,10 @@ define(function(require) {
});
}
}


// 动态扩展zrender shape:halfSmoothPolygon
require('../util/shape/halfSmoothPolygon');

// 图表注册
require('../chart').define('line', Line);

Expand Down
12 changes: 7 additions & 5 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,24 +399,26 @@ define(function() {
// color: 各异,
}
},
smooth : 0,
//smooth : false,
//symbol: null, // 拐点图形类型,非标准参数
symbolSize: 4, // 可计算特性参数,空数据拖拽提示图形大小
symbolSize: 2, // 可计算特性参数,空数据拖拽提示图形大小
showAllSymbol: false // 标志图形默认只有主轴显示(随主轴标签间隔隐藏策略)
},

// K线图默认参数
k: {
xAxisIndex: 0,
yAxisIndex: 0,
// barWidth : null // 默认自适应
// barMaxWidth : null // 默认自适应
itemStyle: {
normal: {
color: '#fff', // 阳线填充颜色
color0: '#00aa11', // 阴线填充颜色
color: '#fff', // 阳线填充颜色
color0: '#00aa11', // 阴线填充颜色
lineStyle: {
width: 1,
color: '#ff3200', // 阳线边框颜色
color0: '#00aa11' // 阴线边框颜色
color0: '#00aa11' // 阴线边框颜色
}
},
emphasis: {
Expand Down
126 changes: 126 additions & 0 deletions src/util/shape/halfSmoothPolygon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* zrender
*
* @author Kener (@Kener-林峰, linzhifeng@baidu.com)
*
* shape类:支持半平滑的polygon,折线面积图使用
* 可配图形属性:
{
// 基础属性
shape : 'halfSmoothPolygon', // 必须,shape类标识,需要显式指定
id : {string}, // 必须,图形唯一标识,可通过zrender实例方法newShapeId生成
zlevel : {number}, // 默认为0,z层level,决定绘画在哪层canvas中
invisible : {boolean}, // 默认为false,是否可见
// 样式属性,默认状态样式样式属性
style : {
pointList : {Array}, // 必须,多边形各个顶角坐标
brushType : {string}, // 默认为fill,绘画方式
// fill(填充) | stroke(描边) | both(填充+描边)
color : {color}, // 默认为'#000',填充颜色,支持rgba
strokeColor : {color}, // 默认为'#000',描边颜色(轮廓),支持rgba
lineWidth : {number}, // 默认为1,线条宽度,描边下有效
opacity : {number}, // 默认为1,透明度设置,如果color为rgba,则最终透明度效果叠加
shadowBlur : {number}, // 默认为0,阴影模糊度,大于0有效
shadowColor : {color}, // 默认为'#000',阴影色彩,支持rgba
shadowOffsetX : {number}, // 默认为0,阴影横向偏移,正值往右,负值往左
shadowOffsetY : {number}, // 默认为0,阴影纵向偏移,正值往下,负值往上
text : {string}, // 默认为null,附加文本
textFont : {string}, // 默认为null,附加文本样式,eg:'bold 18px verdana'
textPosition : {string}, // 默认为top,附加文本位置。
// inside | left | right | top | bottom
textAlign : {string}, // 默认根据textPosition自动设置,附加文本水平对齐。
// start | end | left | right | center
textBaseline : {string}, // 默认根据textPosition自动设置,附加文本垂直对齐。
// top | bottom | middle |
// alphabetic | hanging | ideographic
textColor : {color}, // 默认根据textPosition自动设置,默认策略如下,附加文本颜色
// 'inside' ? '#fff' : color
},
// 样式属性,高亮样式属性,当不存在highlightStyle时使用基于默认样式扩展显示
highlightStyle : {
// 同style
}
// 交互属性,详见shape.Base
// 事件属性,详见shape.Base
}
例子:
{
shape : 'halfSmoothPolygon',
id : '123456',
zlevel : 1,
style : {
pointList : [[10, 10], [300, 20], [298, 400], [50, 450]]
color : '#eee',
text : 'Baidu'
},
myName : 'kener', // 可自带任何有效自定义属性
clickable : true,
onClick : function(eventPacket) {
alert(eventPacket.target.myName);
}
}
*/
define(
function(require) {
function HalfSmoothPolygon() {
this.type = 'halfSmoothPolygon';
}

HalfSmoothPolygon.prototype = {
/**
* 创建多边形路径
* @param {Context2D} ctx Canvas 2D上下文
* @param {Object} style 样式
*/
buildPath : function(ctx, style) {
var pointList = style.pointList;
if (pointList.length < 2) {
// 少于2个点就不画了~
return;
}
if (style.smooth) {
var controlPoints = this.smoothBezier(
pointList.slice(0, -2), style.smooth
);

ctx.moveTo(pointList[0][0], pointList[0][1]);
var cp1;
var cp2;
var p;
var l = pointList.length;
for (var i = 0; i < l - 3; i++) {
cp1 = controlPoints[i * 2];
cp2 = controlPoints[i * 2 + 1];
p = pointList[i + 1];
ctx.bezierCurveTo(
cp1[0], cp1[1], cp2[0], cp2[1], p[0], p[1]
);
}
ctx.lineTo(pointList[l - 2][0], pointList[l - 2][1]);
ctx.lineTo(pointList[l - 1][0], pointList[l - 1][1]);
ctx.lineTo(pointList[0][0], pointList[0][1]);
}
else {
require('zrender/shape').get('polygon').buildPath(
ctx, style
);
}
return;
}
};

require('zrender/shape/base').derive(HalfSmoothPolygon);
require('zrender/shape').define(
'halfSmoothPolygon', new HalfSmoothPolygon()
);

return HalfSmoothPolygon;
}
);

0 comments on commit f66756d

Please sign in to comment.