Extjs 4 chart自定义坐标轴刻度
Sencha出品的ExtJs是一个非常优秀的前端框架,尤其是具有里程碑意义的4.0的发布。4.0采用MVC架构和全新的class系统,并且提供了非常丰富的组件。但是,尽管ExtJS如此强大,仍有不尽人意的地方。比如,chart里坐标轴的刻度通常是均匀分布的,ExtJS的实现也是通过坐标轴的最大值和最小值以及其他参数配置均匀的计算刻度。但是,在工作过程中碰到需要自定义刻度的情况,如下图所示

水平轴的刻度是5,10,20这样的不均匀值,但是ExtJS不支持这样的功能(至少我翻遍了文档也没找到)。最初想到的办法是隐藏不需要的label,可这样写:
{
type: 'Numeric',
position: 'bottom',
fields: ['x'],
title: xAxisTitle,
minimum: xRange.xMinimum,
maximum: xRange.xMaximum,
majorTickSteps: 5,
minorTickSteps: 10,
label: {
renderer: function(val) {
//lables: [5, 10, 20]
if (labels.indexOf(val) < 0) {
return '';
}
return val;
}
},
grid: true
}
但是这样写有两个问题,一是需要显示的label值不一定会出现在renderer中,二是即便label没显示,垂直的网格线(通过配置代码中红色部分的属性可以调节网格线密度)还是会出现,这可能并不是我们想要的。通过查阅源码发现,在坐标轴内部实现中,是通过坐标轴范围和刻度个数来计算的,并且网格线跟坐标刻度是一致的。在源码文件ext\src\chart\axis\Axis.js 480行:
// Build the array of steps out of the fixed-value 'step'.
steps = new Array;
for (val = +me.from; val < +me.to; val += step) {
steps.push(val);
}
steps.push(+me.to);
之后渲染label和网格线都用到steps这个数组。所以,可以在这个地方做点手脚,把steps变量强行改成我们需要的数组,让ExtJS按照我们的意图去做。显然,不能直接去修改Ext的源码文件。这里有两种办法,一是用Ext.override方法去重写Ext.chart.axis.Axis的drawAxis方法,二是在Axis的配置参数里提供该方法的自定义实现。前者会影响到之后的所有用到坐标轴的chart,后者只会影响当前的chart. 代码改动如下:
Ext.syncRequire('Ext.chart.axis.Axis');
Ext.override(Ext.chart.axis.Axis, {
/**
* Renders the axis into the screen and updates its position.
*/
drawAxis: function(init) {
var me = this,
i,
x = me.x,
y = me.y,
dashSize = me.dashSize,
length = me.length,
position = me.position,
verticalAxis = (position == 'left' || position == 'right'),
inflections = [],
calcLabels = (me.isNumericAxis),
stepCalcs = me.applyData(),
step = stepCalcs.step,
steps = stepCalcs.steps,
stepsArray = Ext.isArray(steps),
from = stepCalcs.from,
to = stepCalcs.to,
// If we have a single item, to - from will be 0.
axisRange = (to - from) || 1,
trueLength,
currentX,
currentY,
path,
subDashesX = me.minorTickSteps || 0,
subDashesY = me.minorTickSteps || 0,
dashesX = Math.max(subDashesX + 1, 0),
dashesY = Math.max(subDashesY + 1, 0),
dashDirection = (position == 'left' || position == 'top' ? -1 : 1),
dashLength = dashSize * dashDirection,
series = me.chart.series.items,
firstSeries = series[0],
gutters = firstSeries ? firstSeries.nullGutters : me.nullGutters,
padding,
subDashes,
subDashValue,
delta = 0,
stepCount = 0,
tick, axes, ln, val, begin, end;
me.from = from;
me.to = to;
// If there is nothing to show, then leave.
if (me.hidden || (from > to)) {
return;
}
// If no steps are specified (for instance if the store is empty), then leave.
if ((stepsArray && (steps.length == 0)) || (!stepsArray && isNaN(step))) {
return;
}
if (stepsArray) {
// Clean the array of steps:
// First remove the steps that are out of bounds.
steps = Ext.Array.filter(steps, function(elem, index, array) {
return (+elem > +me.from && +elem < +me.to);
}, this);
// Then add bounds on each side.
steps = Ext.Array.union([me.from], steps, [me.to]);
} else {
// Build the array of steps out of the fixed-value 'step'.
steps = new Array;
70 if (me.fixedSteps) {
71 steps = me.fixedSteps;
72 } else {
for (val = +me.from; val < +me.to; val += step) {
74 steps.push(val);
75 }
76 steps.push(+me.to);
77 }
}
...//此处省略其他原有代码
}
});
代码中红色部分if语句块里就是偷梁换柱的地方。使用的时候,在axis的配置代码里加上fixedSteps属性就行了。
{
type: 'Numeric',
position: 'bottom',
fields: ['x'],
title: xAxisTitle,
minimum: xRange.xMinimum,
maximum: xRange.xMaximum,
fixedSteps: [5, 10,20],
grid: true
}
至此,雕虫小技大功告成。欢迎拍砖。
Extjs 4 chart自定义坐标轴刻度的更多相关文章
- extjs 4 chart 时间轴格式的处理
var dayStore = Ext.create('Ext.data.JsonStore', { fields: [{ name: 'name', type: 'date', dateFormat: ...
- Android 自定义带刻度的seekbar
自定义带刻度的seekbar 1.布局 <span style="font-family:SimHei;font-size:18px;"><com.imibaby ...
- R语言修改标题、坐标轴刻度、坐标轴名称的大小(cex.axis、cex.lab、cex.main函数)
修改标题.坐标轴刻度.坐标轴名称的大小,用到了cex.axis.cex.lab.cex.main函数,其中,cex.axis表示修改坐标轴刻度字体大小,cex.lab表示修改坐标轴名称字体大小,cex ...
- Python之坐标轴刻度细化、坐标轴设置、标题图例添加
学习python中matplotlib绘图设置坐标轴刻度.文本 http://www.jb51.net/article/134638.htm Python绘图 https://www.cnblogs. ...
- 用Python设置matplotlib.plot的坐标轴刻度间隔以及刻度范围
一.用默认设置绘制折线图 import matplotlib.pyplot as plt x_values=list(range(11)) #x轴的数字是0到10这11个整数 y_values=[x* ...
- 63.ExtJs事件(自定义事件、on、eventManager)示例
转自:https://blog.csdn.net/leadergg/article/details/5927614?utm_source=blogxgwz5 ExtJs事件(自定义事件.on.even ...
- 带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变
带你体验Android自定义圆形刻度罗盘 仪表盘 实现指针动态改变 转 https://blog.csdn.net/qq_30993595/article/details/78915115 近期有 ...
- 坐标轴刻度取值算法-基于魔数数组-源于echarts的y轴刻度计算需求
本文链接:https://blog.csdn.net/qq_26909801/article/details/96966372数值型坐标轴刻度计算算法前言算法描述上代码代码运行效果结语前言因实习的公司 ...
- [原创][开源] SunnyUI.Net 开发日志:UIBarChart 坐标轴刻度取值算法
_ 在开发UIBarChart的过程中,需要绘制Y轴的刻度,数据作图时,纵横坐标轴刻度范围及刻度值的取法,很大程度上取决于数据的分布.对某一组数据,我们很容易就能知道如何选取这些值才能使图画得漂亮.但 ...
随机推荐
- asp网站发布步骤总结
1.在VS2012中打开索要发布的网站,初始页可重命名为index.html或default.apx. 2.点击 生成>生成“网站”,然后“发布网站”. 3.进行发布设置: (1 配置文件 ( ...
- CSS 文字溢出时的自动隐藏
http://www.111cn.net/cssdiv/css/34050.htm 语法:overflow : visible | auto | hidden | scroll visible::不剪 ...
- oracle的to_number、to_char、to_date用法
TO_DATE 是把字符串转换为数据库中得日期类型转换函数TO_CHAR 是把日期或数字转换为字符串TO_NUMBER 将字符转化为数字 TO_DATE使用TO_DATE函数将字符转换为日期TO_DA ...
- iOS开发——时间格式类
目前只实现了三个类方法, 第一个获取当前时间,以字符创的形式返回,例如"201606161532" 第二个以当前时间与给定时间的时间差(秒) 第三个以当前时间与给定时间的时间差(分 ...
- Sql Server 维护计划 备份覆盖
之前在设置服务器Sql Server 维护计划 备份的sql server 数据库,都是累加的,后来也没有仔细看过,后台回过头来考虑到服务器的存储空间,只好做sql server 数据 ...
- [Bootstrap]全局样式(四)
按钮 1.基本类.btn {display/padding/margin-bottom/font-size/border-radius/border} 作用于< a:role:button &g ...
- DEDECMS中,channelartlist标签
当前频道的下级栏目的内容列表 dede:channelartlist 标签: {dede:channelartlist row=6} <dl> <dt><a href=' ...
- 《SELinux安全上下文的管理(含图)》RedHat6.3——步骤详细、条理清晰
1.为什么浏览器只识别/var/www/html下的文件? 2.为什么不识别别的目录下的index.html文件呢? 3.这里牵扯到身份证,先安装软件包. 4.打开selinux 5.建立一个新的目录 ...
- Python Generators vs Iterators
http://stackoverflow.com/questions/2776829/difference-between-python-generators-vs-iterators iterato ...
- 用CSS3写的小案例-图片缩放隐藏内容显示
思路分析 (1).搭建界面 (2).鼠标移到图片的时候,放大显示 (3).鼠标移入到当前的li标签里面找到后面的div让其显示出来 <!DOCTYPE html> <html lan ...