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轴的刻度,数据作图时,纵横坐标轴刻度范围及刻度值的取法,很大程度上取决于数据的分布.对某一组数据,我们很容易就能知道如何选取这些值才能使图画得漂亮.但 ...
随机推荐
- C++ Iterator迭代器介绍及Iterator迭代器用法代码举例
C++ Iterator迭代器介绍 迭代器可被用来访问一个容器类的所包函的全部元素,其行为像一个指针.举一个例子,你可用一个迭代器来实现对vector容器中所含元素的遍历.有这么几种迭代器如下: 迭代 ...
- Session id实现通过Cookie来传输方法及代码参考
1. Web中的Session指的就是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网站所花费的时间.因此从上述的定义中我们可以看到,Session实际上是一个特定的 ...
- swift项目-模仿团购(主界面的搭建,以及首页的一些细节)
以前学习oc的时候写的一个团购的项目,现在学习swift,拿来用swift写一遍,也是连猜带蒙的,一点一点的往上凑. 今天主要是把主要的架子搭起来了. 主要有:UITabBarController,U ...
- boost::xml——基本操作以及中文乱码解决方案
下面是本人使用boost库的xml部分的基础操作,并且解决对于大家使用boost库读写中文xml内容出现的乱码问题. 1.实现boost库xml基本操作2.解决boost对xml中中文乱码问题3.实现 ...
- Qt移植 Window --Linux
1.把源代码复制到Linux目录,使用qmake命令,注意在shell中直接使用qmake命令注意设置PATH环境变量 2. 在目录中会生成Makeflie文件 3. make即可 /usr/bin/ ...
- 解决flash挡住层的问题
让div在flash上面 设置flash为透明: 插件代码换成如下: <object type="application/x-shockwave-flash" data=&q ...
- .NET基础之自定义泛型
在.NET中泛型使用非常频繁,在控制台应用程序中,默认的引入了System.Collection.Generics名称空间,其中就提供了我们经常使用的泛型:List<T>和Dictiona ...
- PHP - PDO 之 mysql 基础操作
<?php /* pdo 学习 */ $dsn = 'mysql:host=localhost;dbname=cswl';//构建连接dsn $db = new pdo($dsn,'root', ...
- C#...何时需要重写ToString()方法?
一般类型,都是继承自System.Object类,默认情况下,object类的ToString方法会返回当前类的类型的字符串表达形式.但也有例外!! DateTime,它就重写ToString方法,D ...
- 进程(Process)和线程(Thread)的关系和区别
Definition定义-------------Process进程是应用程序的一次运行活动:从操作系统核 心角度来说,进程是操作系统分配和调度系统内存资源.cpu时间片等资源的基本单位,为正在运行的 ...